作者 郭文星
  1 +<?php
  2 +
  3 +namespace app\api\controller\v1;
  4 +use app\server\Wechat as WehcatModule;
  5 +use app\common\controller\Api;
  6 +
  7 +use think\Cache;
  8 +use think\Config;
  9 +use think\Db;
  10 +use think\Response;
  11 +use think\Validate;
  12 +
  13 +/**
  14 + * 首页公共
  15 + */
  16 +class Wechat extends Api
  17 +{
  18 + protected $noNeedLogin = ["*"];
  19 + protected $noNeedRight = '*';
  20 +
  21 + public function _initialize()
  22 + {
  23 + parent::_initialize();
  24 +
  25 + }
  26 +
  27 + /*
  28 + 1.前端获取jsjdk的配置
  29 + */
  30 + public function get_jsjdk_config(){
  31 + try{
  32 + $app = WehcatModule::getInstance()->initWechat();
  33 + $APIs = ['updateAppMessageShareData','updateTimelineShareData','onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone','chooseWXPay'];
  34 + $config = $app->jssdk->buildConfig($APIs, true);
  35 + }catch(\Exception $e){
  36 + $this->error($e->getMessage());
  37 + }
  38 + $this->success('活动获取成功', $config);
  39 + }
  40 +
  41 +}
  1 +<?php
  2 +
  3 +namespace app\server;
  4 +use EasyWeChat\Factory;
  5 +
  6 +
  7 +/**
  8 + * easyWechat 插件
  9 + */
  10 +class Wechat {
  11 + static public $app = null;
  12 + static private $appid= '';
  13 + static private $appsecret= '';
  14 + static private $config= [];
  15 + static private $inc = null;
  16 +
  17 + function __construct(){
  18 + self::$appid= config("site.appid");
  19 + self::$appsecret= config("site.appsecret");
  20 + self::$config = [
  21 + 'app_id' => self::$appid,
  22 + 'secret' => self::$appsecret,
  23 + // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  24 + 'response_type' => 'array',
  25 + 'log' => [
  26 + 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
  27 + 'channels' => [
  28 + // 测试环境
  29 + 'dev' => [
  30 + 'driver' => 'single',
  31 + 'path' => '/tmp/easywechat.log',
  32 + 'level' => 'debug',
  33 + ],
  34 + // 生产环境
  35 + 'prod' => [
  36 + 'driver' => 'daily',
  37 + 'path' => '/tmp/easywechat.log',
  38 + 'level' => 'info',
  39 + ],
  40 + ],
  41 + ],
  42 + /**
  43 + * OAuth 配置
  44 + *
  45 + * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
  46 + * callback:OAuth授权完成后的回调页地址:需要前端提供完整的url
  47 + */
  48 + 'oauth' => [
  49 + 'scopes' => ['snsapi_base'],
  50 + 'callback' => '/api/oauth_callback.php',
  51 + ],
  52 + ];
  53 + }
  54 +
  55 + static public function getInstance(){
  56 + if(self::$inc == null){
  57 + self::$inc = new self();
  58 + }
  59 + return self::$inc;
  60 + }
  61 +
  62 + static public function initWechat(){
  63 +
  64 + self::$app = Factory::officialAccount(self::$config);
  65 + return self::$app;
  66 + }
  67 +}
@@ -24,7 +24,7 @@ @@ -24,7 +24,7 @@
24 "karsonzhang/fastadmin-addons": "~1.3.2", 24 "karsonzhang/fastadmin-addons": "~1.3.2",
25 "overtrue/pinyin": "^3.0", 25 "overtrue/pinyin": "^3.0",
26 "phpoffice/phpspreadsheet": "1.19", 26 "phpoffice/phpspreadsheet": "1.19",
27 - "overtrue/wechat": "^4.6", 27 + "overtrue/wechat": "~4.0",
28 "nelexa/zip": "^3.3", 28 "nelexa/zip": "^3.3",
29 "ext-json": "*", 29 "ext-json": "*",
30 "ext-curl": "*", 30 "ext-curl": "*",
@@ -33,7 +33,11 @@ @@ -33,7 +33,11 @@
33 "txthinking/mailer": "^2.0" 33 "txthinking/mailer": "^2.0"
34 }, 34 },
35 "config": { 35 "config": {
36 - "preferred-install": "dist" 36 + "preferred-install": "dist",
  37 + "allow-plugins": {
  38 + "topthink/think-installer": true,
  39 + "easywechat-composer/easywechat-composer": true
  40 + }
37 }, 41 },
38 "repositories": [ 42 "repositories": [
39 { 43 {