OfficialAccountReplyLogic.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\channel;
  15. use app\common\enum\OfficialAccountEnum;
  16. use app\common\enum\YesNoEnum;
  17. use app\common\logic\BaseLogic;
  18. use app\common\model\channel\OfficialAccountReply;
  19. use app\common\model\channel\OfficialAuth;
  20. use app\common\service\wechat\WeChatConfigService;
  21. use app\common\service\wechat\WeChatOaService;
  22. use think\facade\Log;
  23. /**
  24. * 微信公众号回复逻辑层
  25. * Class OfficialAccountReplyLogic
  26. * @package app\adminapi\logic\channel
  27. */
  28. class OfficialAccountReplyLogic extends BaseLogic
  29. {
  30. /**
  31. * @notes 添加回复(关注/关键词/默认)
  32. * @param $params
  33. * @return bool
  34. * @author 段誉
  35. * @date 2022/3/29 10:57
  36. */
  37. public static function add($params)
  38. {
  39. try {
  40. // 关键字回复排序值须大于0
  41. if ($params['reply_type'] == OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['sort'] < 0) {
  42. throw new \Exception('排序值须大于或等于0');
  43. }
  44. if ($params['reply_type'] != OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['status']) {
  45. // 非关键词回复只能有一条记录处于启用状态,所以将该回复类型下的已有记录置为禁用状态
  46. OfficialAccountReply::where(['reply_type' => $params['reply_type']])->update(['status' => YesNoEnum::NO]);
  47. }
  48. OfficialAccountReply::create($params);
  49. return true;
  50. } catch (\Exception $e) {
  51. self::setError($e->getMessage());
  52. return false;
  53. }
  54. }
  55. /**
  56. * @notes 查看回复详情
  57. * @param $params
  58. * @return array
  59. * @author 段誉
  60. * @date 2022/3/29 11:00
  61. */
  62. public static function detail($params)
  63. {
  64. $field = 'id,name,keyword,reply_type,matching_type,content_type,content,status,sort';
  65. $field .= ',reply_type as reply_type_desc, matching_type as matching_type_desc, content_type as content_type_desc, status as status_desc';
  66. return OfficialAccountReply::field($field)->findOrEmpty($params['id'])->toArray();
  67. }
  68. /**
  69. * @notes 编辑回复(关注/关键词/默认)
  70. * @param $params
  71. * @return bool
  72. * @author 段誉
  73. * @date 2022/3/29 11:01
  74. */
  75. public static function edit($params)
  76. {
  77. try {
  78. // 关键字回复排序值须大于0
  79. if ($params['reply_type'] == OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['sort'] < 0) {
  80. throw new \Exception('排序值须大于或等于0');
  81. }
  82. if ($params['reply_type'] != OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['status']) {
  83. // 非关键词回复只能有一条记录处于启用状态,所以将该回复类型下的已有记录置为禁用状态
  84. OfficialAccountReply::where(['reply_type' => $params['reply_type']])->update(['status' => YesNoEnum::NO]);
  85. }
  86. OfficialAccountReply::update($params);
  87. return true;
  88. } catch (\Exception $e) {
  89. self::setError($e->getMessage());
  90. return false;
  91. }
  92. }
  93. /**
  94. * @notes 删除回复(关注/关键词/默认)
  95. * @param $params
  96. * @author 段誉
  97. * @date 2022/3/29 11:01
  98. */
  99. public static function delete($params)
  100. {
  101. OfficialAccountReply::destroy($params['id']);
  102. }
  103. /**
  104. * @notes 更新排序
  105. * @param $params
  106. * @author 段誉
  107. * @date 2022/3/29 11:01
  108. */
  109. public static function sort($params)
  110. {
  111. $params['sort'] = $params['new_sort'];
  112. OfficialAccountReply::update($params);
  113. }
  114. /**
  115. * @notes 更新状态
  116. * @param $params
  117. * @author 段誉
  118. * @date 2022/3/29 11:01
  119. */
  120. public static function status($params)
  121. {
  122. $reply = OfficialAccountReply::findOrEmpty($params['id']);
  123. $reply->status = !$reply->status;
  124. $reply->save();
  125. }
  126. /**
  127. * @notes 微信公众号回调
  128. * @return \Psr\Http\Message\ResponseInterface|void
  129. * @throws \EasyWeChat\Kernel\Exceptions\BadRequestException
  130. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  131. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  132. * @throws \ReflectionException
  133. * @throws \Throwable
  134. * @author 段誉
  135. * @date 2023/2/27 14:38\
  136. */
  137. public static function index()
  138. {
  139. $server = (new WeChatOaService())->getServer();
  140. // 事件
  141. $server->addMessageListener(OfficialAccountEnum::MSG_TYPE_EVENT, function ($message, \Closure $next) {
  142. Log::write(json_encode($message,JSON_UNESCAPED_UNICODE));
  143. switch ($message['Event']) {
  144. case OfficialAccountEnum::EVENT_SUBSCRIBE: // 关注事件
  145. $wx_user = (new WeChatOaService())->getUserInfo($message['FromUserName']);
  146. if(!empty($wx_user)){
  147. $official_auth = OfficialAuth::where('openid',$wx_user['openid'])->findOrEmpty();
  148. if(!$official_auth->isEmpty()){
  149. $official_auth->subscribe = 1;
  150. $official_auth->openid = $wx_user['openid'];
  151. $official_auth->unionid = $wx_user['unionid'];
  152. }else{
  153. $official_auth = new OfficialAuth();
  154. $official_auth->subscribe = 1;
  155. $official_auth->openid = $wx_user['openid'];
  156. $official_auth->unionid = $wx_user['unionid'];
  157. }
  158. $official_auth->save();
  159. }
  160. $replyContent = OfficialAccountReply::where([
  161. 'reply_type' => OfficialAccountEnum::REPLY_TYPE_FOLLOW,
  162. 'status' => YesNoEnum::YES
  163. ])
  164. ->value('content');
  165. if ($replyContent) {
  166. return $replyContent;
  167. }
  168. break;
  169. case OfficialAccountEnum::EVENT_UNSUBSCRIBE: // 取消关注事件
  170. $wx_user = (new WeChatOaService())->getUserInfo($message['FromUserName']);
  171. if(!empty($wx_user)){
  172. $official_auth = OfficialAuth::where('openid',$wx_user['openid'])->findOrEmpty();
  173. if(!$official_auth->isEmpty()){
  174. $official_auth->subscribe = 0;
  175. $official_auth->save();
  176. }
  177. }
  178. break;
  179. }
  180. return $next($message);
  181. });
  182. // 文本
  183. $server->addMessageListener(OfficialAccountEnum::MSG_TYPE_TEXT, function ($message, \Closure $next) {
  184. $replyList = OfficialAccountReply::where([
  185. 'reply_type' => OfficialAccountEnum::REPLY_TYPE_KEYWORD,
  186. 'status' => YesNoEnum::YES
  187. ])
  188. ->order('sort asc')
  189. ->select();
  190. $replyContent = '';
  191. foreach ($replyList as $reply) {
  192. switch ($reply['matching_type']) {
  193. case OfficialAccountEnum::MATCHING_TYPE_FULL:
  194. $reply['keyword'] === $message['Content'] && $replyContent = $reply['content'];
  195. break;
  196. case OfficialAccountEnum::MATCHING_TYPE_FUZZY:
  197. stripos($message['Content'], $reply['keyword']) !== false && $replyContent = $reply['content'];
  198. break;
  199. }
  200. if ($replyContent) {
  201. break; // 得到回复文本,中止循环
  202. }
  203. }
  204. //消息回复为空的话,找默认回复
  205. if (empty($replyContent)) {
  206. $replyContent = static::getDefaultReply();
  207. }
  208. if ($replyContent) {
  209. return $replyContent;
  210. }
  211. return $next($message);
  212. });
  213. return $server->serve();
  214. }
  215. /**
  216. * @notes 默认回复信息
  217. * @return mixed
  218. * @author 段誉
  219. * @date 2023/2/27 14:36
  220. */
  221. public static function getDefaultReply()
  222. {
  223. return OfficialAccountReply::where([
  224. 'reply_type' => OfficialAccountEnum::REPLY_TYPE_DEFAULT,
  225. 'status' => YesNoEnum::YES
  226. ])
  227. ->value('content');
  228. }
  229. }