| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop100%开源免费商用商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | 商业版本务必购买商业授权,以免引起法律纠纷
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshopTeam
- // +----------------------------------------------------------------------
- namespace app\adminapi\logic\wechat;
- use app\common\enum\OfficialAccountEnum;
- use app\common\enum\YesNoEnum;
- use app\common\logic\BaseLogic;
- use app\common\model\OfficialAccountReply;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\Messages\Text;
- /**
- * 微信公众号回复逻辑层
- * Class OfficialAccountReplyLogic
- * @package app\adminapi\logic\wechat
- */
- class OfficialAccountReplyLogic extends BaseLogic
- {
- /**
- * @notes 添加回复(关注/关键词/默认)
- * @param $params
- * @author Tab
- * @date 2021/7/29 16:41
- */
- public static function add($params)
- {
- try {
- // 关键字回复排序值须大于0
- if ($params['reply_type'] == OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['sort'] <= 0) {
- throw new \Exception('排序值须大于0');
- }
- if($params['reply_type'] != OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['status']) {
- // 非关键词回复只能有一条记录处于启用状态,所以将该回复类型下的已有记录置为禁用状态
- OfficialAccountReply::where(['reply_type' => $params['reply_type']])->update(['status' => YesNoEnum::NO]);
- }
- OfficialAccountReply::create($params);
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 查看回复详情
- * @param $params
- * @return array
- * @author Tab
- * @date 2021/7/29 17:08
- */
- public static function detail($params)
- {
- $field = 'id,name,keyword,reply_type,matching_type,content_type,content,status,sort';
- $field .= ',reply_type as reply_type_desc, matching_type as matching_type_desc, content_type as content_type_desc, status as status_desc';
- return OfficialAccountReply::field($field)->findOrEmpty($params['id'])->toArray();
- }
- /**
- * @notes 编辑回复(关注/关键词/默认)
- * @param $params
- * @author Tab
- * @date 2021/7/29 17:30
- */
- public static function edit($params)
- {
- try {
- // 关键字回复排序值须大于0
- if ($params['reply_type'] == OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['sort'] <= 0) {
- throw new \Exception('排序值须大于0');
- }
- if($params['reply_type'] != OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['status']) {
- // 非关键词回复只能有一条记录处于启用状态,所以将该回复类型下的已有记录置为禁用状态
- OfficialAccountReply::where(['reply_type' => $params['reply_type']])->update(['status' => YesNoEnum::NO]);
- }
- OfficialAccountReply::update($params);
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 删除回复(关注/关键词/默认)
- * @param $params
- * @author Tab
- * @date 2021/7/29 17:45
- */
- public static function delete($params)
- {
- OfficialAccountReply::destroy($params['id']);
- }
- /**
- * @notes 更新排序
- * @param $params
- * @author Tab
- * @date 2021/7/29 18:16
- */
- public static function sort($params)
- {
- $params['sort'] = $params['new_sort'];
- OfficialAccountReply::update($params);
- }
- /**
- * @notes 更新状态
- * @param $params
- * @author Tab
- * @date 2021/9/9 18:29
- */
- public static function status($params)
- {
- $reply = OfficialAccountReply::findOrEmpty($params['id']);
- $reply->status = !$reply->status;
- $reply->save();
- }
- /**
- * @notes 微信公众号回调
- * @throws \EasyWeChat\Kernel\Exceptions\BadRequestException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \ReflectionException
- * @author Tab
- * @date 2021/7/30 11:58
- */
- public static function index()
- {
- // 确认此次GET请求来自微信服务器,原样返回echostr参数内容,接入生效,成为开发者成功
- if(isset($_GET['echostr'])) {
- echo $_GET['echostr'];
- exit;
- }
- $officialAccountSetting = OfficialAccountSettingLogic::getConfig();
- $config = [
- 'app_id' => $officialAccountSetting['app_id'],
- 'secret' => $officialAccountSetting['app_secret'],
- 'response_type' => 'array',
- ];
- $app = Factory::officialAccount($config);
- $app->server->push(function ($message) {
- switch ($message['MsgType']) { // 消息类型
- case OfficialAccountEnum::MSG_TYPE_EVENT: // 事件
- switch ($message['Event']) {
- case OfficialAccountEnum::EVENT_SUBSCRIBE: // 关注事件
- $reply_content = OfficialAccountReply::where(['reply_type' => OfficialAccountEnum::REPLY_TYPE_FOLLOW, 'status' => YesNoEnum::YES])
- ->value('content');
- if (empty($reply_content)) {
- // 未启用关注回复 或 关注回复内容为空
- $reply_content = OfficialAccountReply::where(['reply_type' => OfficialAccountEnum::REPLY_TYPE_DEFAULT, 'status' => YesNoEnum::YES])
- ->value('content');
- }
- if ($reply_content) {
- $text = new Text($reply_content);
- return $text;
- }
- break;
- }
- break;
- case OfficialAccountEnum::MSG_TYPE_TEXT: // 文本
- $reply_list = OfficialAccountReply::where(['reply_type' => OfficialAccountEnum::REPLY_TYPE_KEYWORD, 'status' => YesNoEnum::YES])
- ->order('sort asc')
- ->select();
- $reply_content = '';
- foreach ($reply_list as $reply) {
- switch ($reply['matching_type']) {
- case OfficialAccountEnum::MATCHING_TYPE_FULL:
- $reply['keyword'] === $message['Content'] && $reply_content = $reply['content'];
- break;
- case OfficialAccountEnum::MATCHING_TYPE_FUZZY:
- stripos($message['Content'], $reply['keyword']) !== false && $reply_content = $reply['content'];
- break;
- }
- if($reply_content) {
- break; // 得到回复文本,中止循环
- }
- }
- //消息回复为空的话,找默认回复
- if (empty($reply_content)) {
- $reply_content = OfficialAccountReply::where(['reply_type' => OfficialAccountEnum::REPLY_TYPE_DEFAULT, 'status' => YesNoEnum::YES])
- ->value('content');
- }
- if ($reply_content) {
- $text = new Text($reply_content);
- return $text;
- }
- break;
- }
- });
- $response = $app->server->serve();
- $response->send();
- }
- }
|