OfficialAccountReplyLogic.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\wechat;
  20. use app\common\enum\OfficialAccountEnum;
  21. use app\common\enum\YesNoEnum;
  22. use app\common\logic\BaseLogic;
  23. use app\common\model\OfficialAccountReply;
  24. use EasyWeChat\Factory;
  25. use EasyWeChat\Kernel\Messages\Text;
  26. /**
  27. * 微信公众号回复逻辑层
  28. * Class OfficialAccountReplyLogic
  29. * @package app\adminapi\logic\wechat
  30. */
  31. class OfficialAccountReplyLogic extends BaseLogic
  32. {
  33. /**
  34. * @notes 添加回复(关注/关键词/默认)
  35. * @param $params
  36. * @author Tab
  37. * @date 2021/7/29 16:41
  38. */
  39. public static function add($params)
  40. {
  41. try {
  42. // 关键字回复排序值须大于0
  43. if ($params['reply_type'] == OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['sort'] <= 0) {
  44. throw new \Exception('排序值须大于0');
  45. }
  46. if($params['reply_type'] != OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['status']) {
  47. // 非关键词回复只能有一条记录处于启用状态,所以将该回复类型下的已有记录置为禁用状态
  48. OfficialAccountReply::where(['reply_type' => $params['reply_type']])->update(['status' => YesNoEnum::NO]);
  49. }
  50. OfficialAccountReply::create($params);
  51. return true;
  52. } catch (\Exception $e) {
  53. self::setError($e->getMessage());
  54. return false;
  55. }
  56. }
  57. /**
  58. * @notes 查看回复详情
  59. * @param $params
  60. * @return array
  61. * @author Tab
  62. * @date 2021/7/29 17:08
  63. */
  64. public static function detail($params)
  65. {
  66. $field = 'id,name,keyword,reply_type,matching_type,content_type,content,status,sort';
  67. $field .= ',reply_type as reply_type_desc, matching_type as matching_type_desc, content_type as content_type_desc, status as status_desc';
  68. return OfficialAccountReply::field($field)->findOrEmpty($params['id'])->toArray();
  69. }
  70. /**
  71. * @notes 编辑回复(关注/关键词/默认)
  72. * @param $params
  73. * @author Tab
  74. * @date 2021/7/29 17:30
  75. */
  76. public static function edit($params)
  77. {
  78. try {
  79. // 关键字回复排序值须大于0
  80. if ($params['reply_type'] == OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['sort'] <= 0) {
  81. throw new \Exception('排序值须大于0');
  82. }
  83. if($params['reply_type'] != OfficialAccountEnum::REPLY_TYPE_KEYWORD && $params['status']) {
  84. // 非关键词回复只能有一条记录处于启用状态,所以将该回复类型下的已有记录置为禁用状态
  85. OfficialAccountReply::where(['reply_type' => $params['reply_type']])->update(['status' => YesNoEnum::NO]);
  86. }
  87. OfficialAccountReply::update($params);
  88. return true;
  89. } catch (\Exception $e) {
  90. self::setError($e->getMessage());
  91. return false;
  92. }
  93. }
  94. /**
  95. * @notes 删除回复(关注/关键词/默认)
  96. * @param $params
  97. * @author Tab
  98. * @date 2021/7/29 17:45
  99. */
  100. public static function delete($params)
  101. {
  102. OfficialAccountReply::destroy($params['id']);
  103. }
  104. /**
  105. * @notes 更新排序
  106. * @param $params
  107. * @author Tab
  108. * @date 2021/7/29 18:16
  109. */
  110. public static function sort($params)
  111. {
  112. $params['sort'] = $params['new_sort'];
  113. OfficialAccountReply::update($params);
  114. }
  115. /**
  116. * @notes 更新状态
  117. * @param $params
  118. * @author Tab
  119. * @date 2021/9/9 18:29
  120. */
  121. public static function status($params)
  122. {
  123. $reply = OfficialAccountReply::findOrEmpty($params['id']);
  124. $reply->status = !$reply->status;
  125. $reply->save();
  126. }
  127. /**
  128. * @notes 微信公众号回调
  129. * @throws \EasyWeChat\Kernel\Exceptions\BadRequestException
  130. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  131. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  132. * @throws \ReflectionException
  133. * @author Tab
  134. * @date 2021/7/30 11:58
  135. */
  136. public static function index()
  137. {
  138. // 确认此次GET请求来自微信服务器,原样返回echostr参数内容,接入生效,成为开发者成功
  139. if(isset($_GET['echostr'])) {
  140. echo $_GET['echostr'];
  141. exit;
  142. }
  143. $officialAccountSetting = OfficialAccountSettingLogic::getConfig();
  144. $config = [
  145. 'app_id' => $officialAccountSetting['app_id'],
  146. 'secret' => $officialAccountSetting['app_secret'],
  147. 'response_type' => 'array',
  148. ];
  149. $app = Factory::officialAccount($config);
  150. $app->server->push(function ($message) {
  151. switch ($message['MsgType']) { // 消息类型
  152. case OfficialAccountEnum::MSG_TYPE_EVENT: // 事件
  153. switch ($message['Event']) {
  154. case OfficialAccountEnum::EVENT_SUBSCRIBE: // 关注事件
  155. $reply_content = OfficialAccountReply::where(['reply_type' => OfficialAccountEnum::REPLY_TYPE_FOLLOW, 'status' => YesNoEnum::YES])
  156. ->value('content');
  157. if (empty($reply_content)) {
  158. // 未启用关注回复 或 关注回复内容为空
  159. $reply_content = OfficialAccountReply::where(['reply_type' => OfficialAccountEnum::REPLY_TYPE_DEFAULT, 'status' => YesNoEnum::YES])
  160. ->value('content');
  161. }
  162. if ($reply_content) {
  163. $text = new Text($reply_content);
  164. return $text;
  165. }
  166. break;
  167. }
  168. break;
  169. case OfficialAccountEnum::MSG_TYPE_TEXT: // 文本
  170. $reply_list = OfficialAccountReply::where(['reply_type' => OfficialAccountEnum::REPLY_TYPE_KEYWORD, 'status' => YesNoEnum::YES])
  171. ->order('sort asc')
  172. ->select();
  173. $reply_content = '';
  174. foreach ($reply_list as $reply) {
  175. switch ($reply['matching_type']) {
  176. case OfficialAccountEnum::MATCHING_TYPE_FULL:
  177. $reply['keyword'] === $message['Content'] && $reply_content = $reply['content'];
  178. break;
  179. case OfficialAccountEnum::MATCHING_TYPE_FUZZY:
  180. stripos($message['Content'], $reply['keyword']) !== false && $reply_content = $reply['content'];
  181. break;
  182. }
  183. if($reply_content) {
  184. break; // 得到回复文本,中止循环
  185. }
  186. }
  187. //消息回复为空的话,找默认回复
  188. if (empty($reply_content)) {
  189. $reply_content = OfficialAccountReply::where(['reply_type' => OfficialAccountEnum::REPLY_TYPE_DEFAULT, 'status' => YesNoEnum::YES])
  190. ->value('content');
  191. }
  192. if ($reply_content) {
  193. $text = new Text($reply_content);
  194. return $text;
  195. }
  196. break;
  197. }
  198. });
  199. $response = $app->server->serve();
  200. $response->send();
  201. }
  202. }