作者 郭文星
<?php
namespace app\api\controller\v1;
use app\server\Wechat as WehcatModule;
use app\common\controller\Api;
use think\Cache;
use think\Config;
use think\Db;
use think\Response;
use think\Validate;
/**
* 首页公共
*/
class Wechat extends Api
{
protected $noNeedLogin = ["*"];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
}
/*
1.前端获取jsjdk的配置
*/
public function get_jsjdk_config(){
try{
$app = WehcatModule::getInstance()->initWechat();
$APIs = ['updateAppMessageShareData','updateTimelineShareData','onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone','chooseWXPay'];
$config = $app->jssdk->buildConfig($APIs, true);
}catch(\Exception $e){
$this->error($e->getMessage());
}
$this->success('活动获取成功', $config);
}
}
... ...
<?php
namespace app\server;
use EasyWeChat\Factory;
/**
* easyWechat 插件
*/
class Wechat {
static public $app = null;
static private $appid= '';
static private $appsecret= '';
static private $config= [];
static private $inc = null;
function __construct(){
self::$appid= config("site.appid");
self::$appsecret= config("site.appsecret");
self::$config = [
'app_id' => self::$appid,
'secret' => self::$appsecret,
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
'log' => [
'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod
'channels' => [
// 测试环境
'dev' => [
'driver' => 'single',
'path' => '/tmp/easywechat.log',
'level' => 'debug',
],
// 生产环境
'prod' => [
'driver' => 'daily',
'path' => '/tmp/easywechat.log',
'level' => 'info',
],
],
],
/**
* OAuth 配置
*
* scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
* callback:OAuth授权完成后的回调页地址:需要前端提供完整的url
*/
'oauth' => [
'scopes' => ['snsapi_base'],
'callback' => '/api/oauth_callback.php',
],
];
}
static public function getInstance(){
if(self::$inc == null){
self::$inc = new self();
}
return self::$inc;
}
static public function initWechat(){
self::$app = Factory::officialAccount(self::$config);
return self::$app;
}
}
... ...
... ... @@ -24,7 +24,7 @@
"karsonzhang/fastadmin-addons": "~1.3.2",
"overtrue/pinyin": "^3.0",
"phpoffice/phpspreadsheet": "1.19",
"overtrue/wechat": "^4.6",
"overtrue/wechat": "~4.0",
"nelexa/zip": "^3.3",
"ext-json": "*",
"ext-curl": "*",
... ... @@ -33,7 +33,11 @@
"txthinking/mailer": "^2.0"
},
"config": {
"preferred-install": "dist"
"preferred-install": "dist",
"allow-plugins": {
"topthink/think-installer": true,
"easywechat-composer/easywechat-composer": true
}
},
"repositories": [
{
... ...