WechatLogic.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\basics\Logic;
  4. use app\common\model\wechat\Wechat;
  5. use app\common\model\wechat\WechatReply;
  6. use app\common\server\WeChatServer;
  7. use EasyWeChat\Kernel\Exceptions\Exception;
  8. use EasyWeChat\Kernel\Messages\Text;
  9. use EasyWeChat\Factory;
  10. class WechatLogic extends Logic
  11. {
  12. public static function index($params)
  13. {
  14. // Token验证 将微信转过来的数据原样返回
  15. if(isset($params['echostr'])) {
  16. echo $params['echostr'];
  17. exit;
  18. }
  19. // 获取公众号配置
  20. $config = WechatServer::getOaConfig();
  21. $app = Factory::officialAccount($config);
  22. $app->server->push(function ($message) {
  23. switch ($message['MsgType']) { // 消息类型
  24. case WeChat::msg_type_event: // 回复事件
  25. switch ($message['Event']) {
  26. case WeChat::msg_event_subscribe: // 关注事件
  27. $reply_content = WechatReply::where(['reply_type' => WeChat::msg_event_subscribe, 'status' => 1, 'del' => 0])
  28. ->value('content');
  29. //关注回复空的话,找默认回复
  30. if (empty($reply_content)) {
  31. $reply_content = WechatReply::where(['reply_type' => WeChat::msg_type_default, 'status' => 1, 'del' => 0])
  32. ->value('content');
  33. }
  34. if ($reply_content) {
  35. $text = new Text($reply_content);
  36. return $text;
  37. }
  38. break;
  39. case WeChat::msg_event_click: // 点击事件
  40. $reply_content = self::getKeyWordContent($message['EventKey']);
  41. if ($reply_content) {
  42. $text = new Text($reply_content);
  43. return $text;
  44. }
  45. break;
  46. }
  47. case WeChat::msg_type_text://消息类型
  48. // 获取关键字内容
  49. $reply_content = self::getKeyWordContent($message['Content']);
  50. if (empty($reply_content)) {
  51. // 获取默认内容
  52. $reply_content = self::getDefaultReplyContent();
  53. }
  54. if ($reply_content) {
  55. $text = new Text($reply_content);
  56. return $text;
  57. }
  58. break;
  59. }
  60. });
  61. $response = $app->server->serve();
  62. $response->send();
  63. }
  64. /**
  65. * 获取微信配置
  66. * @param $url
  67. * @return array|string
  68. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  69. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  70. * @throws \Psr\SimpleCache\InvalidArgumentException
  71. */
  72. public static function jsConfig($url)
  73. {
  74. $config = WeChatServer::getOaConfig();
  75. $app = Factory::officialAccount($config);
  76. $url = urldecode($url);
  77. $app->jssdk->setUrl($url);
  78. $apis = [
  79. 'onMenuShareTimeline',
  80. 'onMenuShareAppMessage',
  81. 'onMenuShareQQ',
  82. 'onMenuShareWeibo',
  83. 'onMenuShareQZone',
  84. 'openLocation',
  85. 'getLocation',
  86. 'chooseWXPay',
  87. 'updateAppMessageShareData',
  88. 'updateTimelineShareData',
  89. 'openAddress',
  90. 'requestMerchantTransfer'
  91. ];
  92. try {
  93. $data = $app->jssdk->getConfigArray($apis, $debug = false, $beta = false);
  94. return data_success('', $data);
  95. } catch (Exception $e) {
  96. return data_error('公众号配置出错' . $e->getMessage());
  97. }
  98. }
  99. /**
  100. * @notes 获取关键词内容
  101. * @param $keyword
  102. * @return mixed|string
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\DbException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. * @author 段誉
  107. * @date 2022/8/3 17:51
  108. */
  109. public static function getKeyWordContent($keyword)
  110. {
  111. $reply_list = WechatReply::where(['reply_type' => WeChat::msg_type_text, 'status' => 1, 'del' => 0])
  112. ->order('sort asc')
  113. ->select();
  114. $reply_content = '';
  115. foreach ($reply_list as $reply) {
  116. switch ($reply['matching_type']) {
  117. case 1://全匹配
  118. $reply['keyword'] === $keyword && $reply_content = $reply['content'];
  119. break;
  120. case 2://模糊匹配
  121. stripos($reply['keyword'], $keyword) !== false && $reply_content = $reply['content'];
  122. break;
  123. }
  124. if($reply_content) {
  125. break; // 得到回复文本,中止循环
  126. }
  127. }
  128. return $reply_content;
  129. }
  130. /**
  131. * @notes 获取默认回复内容
  132. * @return mixed
  133. * @author 段誉
  134. * @date 2022/8/3 17:51
  135. */
  136. public static function getDefaultReplyContent()
  137. {
  138. return WechatReply::where(['reply_type' => WeChat::msg_type_default, 'status' => 1, 'del' => 0])
  139. ->value('content');
  140. }
  141. }