1 Star 1 Fork 0

jiangslee / tp6-wechat

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

think-wechat

微信SDK For ThinkPHP 6.0 基于overtrue/wechat

框架要求

ThinkPHP >= 6.0

安装

composer require jiangslee/think-wechat:dev-ew6 -vvv

配置

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

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

  3. .env 文件

[WECHAT]
# 公众号
WECHAT_OFFICIAL_ACCOUNT_APPID=
WECHAT_OFFICIAL_ACCOUNT_SECRET=
WECHAT_OFFICIAL_ACCOUNT_TOKEN=
WECHAT_OFFICIAL_ACCOUNT_AES_KEY=
WECHAT_OFFICIAL_ACCOUNT_OAUTH_SCOPES=
WECHAT_OFFICIAL_ACCOUNT_OAUTH_CALLBACK=

# 微信支付
WECHAT_PAYMENT_APPID=
WECHAT_PAYMENT_MCH_ID=
WECHAT_PAYMENT_KEY= 
WECHAT_PAYMENT_V2_KEY= 
WECHAT_PAYMENT_CERT_PATH=156xxxxxx1/apiclient_cert.pem
WECHAT_PAYMENT_KEY_PATH=156xxxxxx1/apiclient_key.pem
WECHAT_PAYMENT_NOTIFY_URL=https://api.domain.com/wexin/notify

使用

接受普通消息

<?php

namespace app\index\controller;

use app\BaseController;

class Wechat extends BaseController
{

    public function index()
    {
        // 初始化微信
        $app = app('wechat.official_account');
        $server = $app->getServer();
        
        $server->addMessageListener('text', function ($message) {
            return sprintf("你对 overtrue 说:“%s”", $message->Content);
        });
        return $server->serve();
    }
}

获得SDK实例

直接用 app()

$official_account = app('wechat.official_account');
$mini = app('wechat.mini_app');
$payment = app('wechat.payment');
$work = app('wechat.work');
$open_platform = app('wechat.open_platform');
$open_work = app('wechat.open_work');

使用facade

use jiangslee\ThinkWechat\Facade;

$officialAccount = Facade::officialAccount();  // 公众号
$miniApp = Facade::miniApp(); // 小程序
$payment = Facade::payment(); // 微信支付
$openPlatform = Facade::openPlatform(); // 开放平台
$work = Facade::work(); // 企业微信
$openWork = Facade::openWork(); // 企业微信第三方服务商

以上均支持传入自定义账号:例如

$officialAccount = Facade::officialAccount('test'); // 公众号

以上均支持传入自定义账号+配置(注:这里的config和配置文件中账号的格式相同):例如

$officialAccount = Facade::officialAccount('',$config); // 公众号

Oauth登录中间件(ThinkPHP6.0+)

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

\think\facade\Route::rule('user','wechat/user')->middleware(\jiangslee\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

License

MIT

The MIT License (MIT) Copyright (c) hectorqin <hectorqin@163.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.

简介

thinkphp6.x + easywechat6.x 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/jiangslee/tp6-wechat.git
git@gitee.com:jiangslee/tp6-wechat.git
jiangslee
tp6-wechat
tp6-wechat
ew6

搜索帮助