# wechatopen **Repository Path**: checkpsd/wechatopen ## Basic Information - **Project Name**: wechatopen - **Description**: 适配fastadmin的微信第三方平台模块,支持多公众号/小程序授权第三方平台管理,支持多应用多商户SaaS系统的基础插件,支持微信应用多模块并行开发,可实现公众号/小程序/APP/网站应用统一用户身份。 - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2023-11-01 - **Last Updated**: 2023-11-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # wechatopen #### 介绍 适配fastadmin的微信第三方平台模块,支持多公众号/小程序授权第三方平台管理,支持多应用多商户SaaS系统的基础插件,支持微信应用多模块并行开发,可实现公众号/小程序/APP/网站应用统一用户身份。 #### 软件架构 软件架构说明 1. 采用fastadmin (ThinkPHP 5.0.*)、easywechat 4 技术选型的微信第三方平台模块,仅支持Linux环境运行 2. 第三方平台配置信息保存在 wechatopen 表。授权到第三方平台的公众号/小程序等帐号信息保存在 wechatopen_applet表,wechatopen 表的参数用来初始化 easywechat SDK,wechatopen_applet表的参数用来作为第三方平台代公众号/小程序实现业务的帐号信息 3. 使用了uctoo/think-easywechat SDK 集成ThinkPHP 5.0.* 和 easywechat 4 #### 安装教程 1. 安装fastadmin,请参考 https://www.fastadmin.net/ 相关文档 2. wechatopen 插件依赖于以下扩展,在项目根目录运行命令 composer require uctoo/think-easywechat:dev-master 安装, 命令 composer require topthink/think-queue:1.1.6 安装 3. 从https://www.fastadmin.net/ 插件市场购买 wechatopen 插件安装到 fastadmin , 开源版仅提供开发示例未包含商业版本中的应用实现 4. 在微信开放平台open.weixin.qq.com 注册认证开发者帐号,创建第三方平台,配置第三方平台参数,其中授权事件接收URL 填写为 ```php https://域名/wechatopen/authevent ,消息与事件接收URL 填写为 https://域名/wechatopen/eventmessage/index/appid/$APPID$ ``` 5. 在wechatopen插件第三方平台菜单,添加微信第三方平台配置信息,仅需填写appid、appsecret、encodingAesKey、token 4个参数 6. 在微信开放平台open.weixin.qq.com 提交测试第三方平台,通过测试后提交全网发布 7. 建议将easywechat的日志关闭,貌似和thinkphp日志不能很好同时工作,将\application\extra\wechat.php中的log level设置为error #### 使用说明 详细文档请参考 https://www.kancloud.cn/doc_uctoo/manual 1. 模块支持多公众号/小程序帐号管理, 如需明确显示当前管理的应用帐号, 需要手动在application\extra\addons.php 中的config_init后添加user_app_select监听 ```php [ 'wechatopen', ], ``` 并在application\admin\view\common\header.html 文件 帐号信息下拉框前添加以下代码 ```php {if condition="$auth->check('wechatopen/applet') eq true"} {:hook('user_app_select')} {/if} ``` 不添加以上代码并不影响插件正常使用, 只是无法直观获知当前操作的应用帐号。 2. xxxx 3. xxxx #### 开发说明 1. 建议基于TP5.0和easywechat的项目采用uctoo/think-easywechat进行重构, 重构方式非常简单, 仅需替换获取easywechat SDK实例的代码即可, 理论上其他代码无需改动,以fastadmin官方微信管理插件为例 菜单管理功能 app\admin\controller\wechat\Menu ```php app = Facade::officialAccount('',Config::load()); // 公众号 Hook::add('text_auto_reply','app\\admin\\eventhandler\\wechat\\CustomSendAutoReply'); //注册具体业务处理的模块 Hook::add('subscribemsg','app\\admin\\eventhandler\\wechat\\CustomSendAutoReply'); } /** * 微信API对接接口 */ public function api() { //..... //自动回复处理 $res = Hook::listen("text_auto_reply", $message); //实现点监听消息,将处理逻辑分发到具体业务实现模块 $res = Hook::listen("subscribemsg", $message); return $res[0]; } } ``` 在app\admin\eventhandler\wechat\CustomSendAutoReply类中具体实现功能 ```php cache(true)->order('weigh DESC,id DESC')->select(); foreach ($autoreplyList as $index => $item) { //完全匹配和正则匹配 if ($item['text'] == $message['Content'] || (in_array(mb_substr($item['text'], 0, 1), ['#', '~', '/']) && preg_match($item['text'], $message['Content'], $matches))) { $autoreply = $item; break; } } if ($autoreply) { $wechatResponse = WechatResponse::where(["eventkey" => $autoreply['eventkey'], 'status' => 'normal'])->find(); if ($wechatResponse) { $responseContent = (array)json_decode($wechatResponse['content'], true); $wechatContext = WechatContext::where(['openid' => $message['FromUserName']])->order('id', 'desc')->find(); $result = $wechatService->response($this, $message['FromUserName'], $message['Content'], $responseContent, $wechatContext, $matches); if ($result) { return $result; } } } } return $unknownMessage; } /** * 用户关注默认发消息 * @access public */ public function subscribemsg() { } } ``` 这样可以将消息响应的中控服务器逻辑代码开源,需要实现业务的模块在开源代码中注册入口,在各自商业化模块中实现具体功能。 3. 建议采用微信第三方平台方式进行微信相关功能开发,好处很多。建议系统内都以wechatapplet_id作为应用唯一标识, 其值是MD5(appid)的值。 #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)