1 Star 2 Fork 2

读心印 / think-wechat

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

thinkphp-wechat

微信SDK For ThinkPHP 基于overtrue/wechat

本分支在 naixiaoxin/think-wechat 的基础做了版本的改进

命名空间不变,包名为 zyan/think-wechat 请勿同时安装

框架要求

ThinkPHP5.1(中间件要求支持ThinkPHP5.1.6+) ThinkPHP6.x(开发中...)

安装

请选择您对应的 thinkphp 版本与 overtrue/wechat 版本

overtrue/wechat 4.x && tp5.1

composer require "zyan/think-wechat:4.0" -vvv

overtrue/wechat 5.x && tp5.1

composer require "zyan/think-wechat:5.0" -vvv

配置

  1. 修改配置文件 修改项目根目录下config/wechat.php中对应的参数

  2. 每个模块基本都支持多账号,默认为 default。

使用

接受普通消息

新建一个Controller,我这边用的是Note

<?php

namespace app\wechat\controller;


use think\Controller;

class Note extends Controller
{

    public function index()
    {
        //    先初始化微信
        $app = app('wechat.official_account');
        $app->server->push(function($message){
            return 'hello,world';
        });
        $app->server->serve()->send();
    }
}

获得SDK实例

使用facade

use Naixiaoxin\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登录中间件(ThinkPHP5.1.6+)

使用中间件情况下,config的oauth.callback可以随便写~,反正是直接获取了当前URL

\think\facade\Route::rule('user','usere')->middleware(\Naixiaoxin\ThinkWechat\Middleware\OauthMiddleware::class);

上面的路由定义了 /user 是需要微信授权的,那么在这条路由的回调 或 控制器对应的方法里, 你就可以从 session('wechat_oauth_user_default') 拿到已经授权的用户信息了。

关于ThinkPHP5.1的中间件使用方法不在叙述,详情可以查看官方文档

中间件参数说明

由于ThinkPHP中间件只支持一个参数,所以以:做分割

支持传入account账号别名以及scope类型

若不传入account,会使用default账号

若不传入scope,会使用配置文件中的oauth.scope

支持一下两种方式

default:snsapi_base
snsapi_base

HOOK

你可以监听相应的事件,并对事件发生后执行相应的操作。

  • OAuth授权 wechat_oauth
// 该事件有以下属性
$params['user']; // 同 session('wechat_oauth_user_default') 一样
$params['is_new']; // 是不是新的会话(第一次创建 session 时为 true)

更多 SDK 的具体使用请参考:https://easywechat.com

参考项目

License

MIT

FOSSA Status

The MIT License (MIT) Copyright (c) SeanWang <qiqizjl@qq.com>, 读心印 <aa24615@qq.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

thinkphp-wechat 微信SDK For ThinkPHP 基于overtrue/wechat expand collapse
PHP
MIT
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
PHP
1
https://gitee.com/flash127/think-wechat.git
git@gitee.com:flash127/think-wechat.git
flash127
think-wechat
think-wechat
5.x

Search