WechatCommonService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. *
  4. * @file:WechatCommonSevice.php
  5. * @Date: 2023/7/7
  6. * @Time: 11:44
  7. */
  8. namespace Modules\WechatPlatform\Services;
  9. use Illuminate\Support\Facades\DB;
  10. use Modules\Common\Services\BaseService;
  11. use Modules\WechatPlatform\Models\WechatOfficialUserInfo;
  12. class WechatCommonService extends BaseService
  13. {
  14. /**
  15. * 播放页面地址
  16. * name: getPlayPageUrl
  17. * @param $videoId
  18. * @param $sequence
  19. * date 2023/07/07 11:52
  20. */
  21. public static function getPlayPageUrl($videoId, $sequence)
  22. {
  23. return ['url' => "/pages/video/index?video_id={$videoId}&sequence={$sequence}"];
  24. }
  25. /**
  26. * 微信消息推送处理
  27. * name: handleMessage
  28. * @param $app
  29. * @param $wechatInfoId 公众号授权信息id
  30. * @param $appid 公众号APPID
  31. * @param $message
  32. * @return string|void
  33. * date 2023/07/10 16:04
  34. */
  35. public static function handleMessage($app, $wechatInfoId, $appid, $message)
  36. {
  37. switch ($message['MsgType']) {
  38. case 'event':
  39. return self::handleEvent($app, $wechatInfoId, $appid, $message);
  40. case 'text':
  41. // 更新用户活跃时间
  42. self::updateUserActivityTime($app,$appid, $message['FromUserName'],$wechatInfoId );
  43. return self::handleTextMessage($wechatInfoId, $message);
  44. // case 'image':
  45. // return '收到图片消息';
  46. // case 'voice':
  47. // return '收到语音消息';
  48. // case 'video':
  49. // return '收到视频消息';
  50. // case 'location':
  51. // return '收到坐标消息';
  52. // case 'link':
  53. // return '收到链接消息';
  54. // case 'file':
  55. // return '收到文件消息';
  56. default:
  57. self::updateUserActivityTime($app, $appid, $message['FromUserName'],$wechatInfoId);
  58. return '收到其它消息';
  59. }
  60. return "";
  61. }
  62. /**
  63. * 文本纤细处理
  64. * name: handleTextMessage
  65. * @param $appid
  66. * @param $content
  67. * date 2023/07/10 15:03
  68. */
  69. private static function handleTextMessage($wechatAppId, $msg)
  70. {
  71. $content = $msg['Content'];
  72. if ($content == "pk") {
  73. return $msg['FromUserName'];
  74. }
  75. $back = WechatKeywordsService::getKeywordsByWords($content, $wechatAppId);
  76. return $back ?: "";
  77. }
  78. /**
  79. * 更新用户活跃时间
  80. * name: updateUserActivityTime
  81. * @param $app
  82. * @param $appid
  83. * @param $openId
  84. * date 2023/07/10 18:30
  85. */
  86. private static function updateUserActivityTime($app, $appid, $openId, $wechatInfoId)
  87. {
  88. $info = WechatOfficialUserInfo::query()->where('mp_openid', $openId)->where('gzh_appid', $appid)->first();
  89. $data = [
  90. 'mp_openid' => $openId,
  91. 'gzh_appid' => $appid,
  92. 'gzh_id' => $wechatInfoId,
  93. 'active_at' => time(),
  94. ];
  95. if (is_empty($info)) {
  96. $data = self::getUserInfo($app,$appid,$openId,$wechatInfoId);
  97. $data['active_at'] = time();
  98. }else{
  99. if ($info->uid == 0 && !empty($info->unionid)){
  100. $data['uid'] = intval(DB::table('wechat_miniprogram_users')->where('unionid', $info->unionid)->value('id'));
  101. }
  102. }
  103. WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
  104. }
  105. /**
  106. * 关注取消关注更新用户信息
  107. * name: updateUserInfo
  108. * @param $app
  109. * @param $appid
  110. * @param $openId
  111. * @param $is_gz
  112. * date 2023/07/10 18:31
  113. */
  114. private static function updateUserInfo($app, $appid, $openId, $wechatInfoId, $is_gz)
  115. {
  116. if (!$is_gz) {
  117. $data = [
  118. 'mp_openid' => $openId,
  119. 'gzh_appid' => $appid,
  120. 'gzh_id' => $wechatInfoId,
  121. 'active_at' => time(),
  122. 'is_subscribe' => 0,
  123. 'unsubscribe_at' => get_date(),
  124. ];
  125. } else {
  126. $data = [
  127. 'mp_openid' => $openId,
  128. 'gzh_appid' => $appid,
  129. 'gzh_id' => $wechatInfoId,
  130. 'is_subscribe' => 1,
  131. 'subscribe_time' => time(),
  132. 'subscribe_time_str' => get_date(),
  133. ];
  134. }
  135. $info = WechatOfficialUserInfo::query()->where('mp_openid', $openId)->where('gzh_appid', $appid)->first();
  136. if ($is_gz == true && is_empty($info)) {
  137. $data = self::getUserInfo($app,$appid,$openId,$wechatInfoId);
  138. WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
  139. }else{
  140. if (!is_empty($info)){
  141. if ($info->uid == 0 && !empty($info->unionid)){
  142. $data['uid'] = intval(DB::table('wechat_miniprogram_users')->where('unionid', $info->unionid)->value('id'));
  143. }
  144. WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
  145. }
  146. }
  147. }
  148. /**3
  149. * 构建用户数据
  150. * name: getUserInfo
  151. * @param $app
  152. * @param $appid
  153. * @param $openId
  154. * date 2023/07/10 18:31
  155. */
  156. protected static function getUserInfo($app,$appid,$openId, $wechatInfoId){
  157. $user = $app->user->get($openId);
  158. return [
  159. 'mp_openid' => $openId,
  160. 'gzh_appid' => $appid,
  161. 'gzh_id' => $wechatInfoId,
  162. 'unionid' => $user['unionid'],
  163. 'is_subscribe' => $user['subscribe'],
  164. 'subscribe_time' => $user['subscribe_time'],
  165. 'subscribe_time_str' => get_date($user['subscribe_time']),
  166. 'uid' => !is_empty($user['unionid']) ? intval(DB::table('wechat_miniprogram_users')->where('unionid', $user['unionid'])->value('id')) : 0,
  167. ];
  168. }
  169. /**
  170. * 处理事件消息
  171. * name: handleEvent
  172. * @param $app
  173. * @param $wechatInfoId
  174. * @param $appid
  175. * @param $message
  176. * @return mixed|string
  177. * date 2023/07/11 09:15
  178. */
  179. private static function handleEvent($app, $wechatInfoId, $appid, $message)
  180. {
  181. switch ($message['Event']) {
  182. case 'unsubscribe':
  183. self::updateUserInfo($app, $appid, $message['FromUserName'], $wechatInfoId,false);
  184. return '取消关注';
  185. case 'subscribe':
  186. self::updateUserInfo($app, $appid, $message['FromUserName'],$wechatInfoId, true);
  187. return WechatSubscribeService::getContent($wechatInfoId);
  188. case 'CLICK':
  189. return WechatMenuService::getClickInfoContent($wechatInfoId,$message['EventKey']);
  190. default:
  191. return '收到其它消息';
  192. }
  193. return '';
  194. }
  195. }