trenton93 / think-wechat
EasyWechat For Thinkphp6.0
v1.0.0
2023-03-02 04:00 UTC
Requires
- php: >=7.1.0
- psr/simple-cache: ^1.0
- topthink/framework: ^6.0
- w7corp/easywechat: ^5.0
This package is auto-updated.
Last update: 2024-09-30 01:54:34 UTC
README
微信SDK For ThinkPHP 6.0 基于w7corp/easywechat
框架要求
ThinkPHP >= 6.0
安装
composer require trenton93/think-wechat
php think wechat:config
配置
-
修改配置文件,修改项目根目录下config/wechat.php中对应的参数
-
每个模块基本都支持多账号,默认为 default。
使用
接收普通消息
<?php namespace app\index\controller; use app\BaseController; class Wechat extends BaseController { public function index() { // 先初始化微信 $app = app('wechat.official_account'); $app->server->push(function($message){ return 'hello,world'; }); $app->server->serve()->send(); } }
获取SDK实例
使用facade
use trenton93\ThinkWechat\Facade; $officialAccount = Facade::officialAccount(); // 公众号 $work = Facade::work(); // 企业微信 $payment = Facade::payment(); // 微信支付 $openPlatform = Facade::openPlatform(); // 开放平台 $miniProgram = Facade::miniProgram(); // 小程序 $openWork = Facade::openWork(); // 企业微信第三方服务商 $microMerchant = Facade::microMerchant(); // 小微商户
以上均支持传入自定义账号:例如
$officialAccount = Facade::officialAccount('test'); // 公众号
以上均支持传入自定义账号+配置(注:这里的config和配置文件中账号的格式相同):例如
$officialAccount = Facade::officialAccount('',$config); // 公众号
Oauth登录中间件(ThinkPHP6.0+)
使用中间件的情况下,config的oauth.callback可以随便写~,反正直接获取了当前URL
\think\facade\Route::rule('user','wechat/user')->middleware(\trenton93\ThinkWechat\Middleware\OauthMiddleware::class);
上面的路由定义了 /user 是需要微信授权的,那么在这条路由的回调 或 控制器对应的方法里,你就可以从 session('wechat_oauth_user_default') 拿到已经授权的用户信息了。
关于ThinkPHP6.0的中间件使用方法不再叙述,详情可以查看官方文档
中间件参数说明
由于ThinkPHP中间件只支持一个参数,所以以:
做分割
支持传入account账号别名以及scope类型
若不传入account
,会使用default
账号
若不传入scope
,会使用配置文件中的oauth.scope
支持以下两种方式
default:snsapi_base
snsapi_base
更多 SDK 的具体使用请参考:https://easywechat.com
参考项目
许可协议
MIT