WechatCommonService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. myLog("wx-xiaoxi")->info('-------文本---'.get_date());
  42. // 更新用户活跃时间
  43. self::updateUserActivityTime($app,$appid, $message['FromUserName'],$wechatInfoId );
  44. return self::handleTextMessage($wechatInfoId, $message);
  45. // case 'image':
  46. // return '收到图片消息';
  47. // case 'voice':
  48. // return '收到语音消息';
  49. // case 'video':
  50. // return '收到视频消息';
  51. // case 'location':
  52. // return '收到坐标消息';
  53. // case 'link':
  54. // return '收到链接消息';
  55. // case 'file':
  56. // return '收到文件消息';
  57. default:
  58. self::updateUserActivityTime($app, $appid, $message['FromUserName'],$wechatInfoId);
  59. return '收到其它消息';
  60. }
  61. return "";
  62. }
  63. /**
  64. * 文本纤细处理
  65. * name: handleTextMessage
  66. * @param $appid
  67. * @param $content
  68. * date 2023/07/10 15:03
  69. */
  70. private static function handleTextMessage($wechatAppId, $msg)
  71. {
  72. $content = $msg['Content'];
  73. if ($content == "pk") {
  74. return $msg['FromUserName'];
  75. }
  76. $back = WechatKeywordsService::getKeywordsByWords($content, $wechatAppId);
  77. return $back ?: "";
  78. }
  79. /**
  80. * 更新用户活跃时间
  81. * name: updateUserActivityTime
  82. * @param $app
  83. * @param $appid
  84. * @param $openId
  85. * date 2023/07/10 18:30
  86. */
  87. private static function updateUserActivityTime($app, $appid, $openId, $wechatInfoId)
  88. {
  89. $info = WechatOfficialUserInfo::query()->where('mp_openid', $openId)->where('gzh_appid', $appid)->first();
  90. $data = [
  91. 'mp_openid' => $openId,
  92. 'gzh_appid' => $appid,
  93. 'gzh_id' => $wechatInfoId,
  94. 'active_at' => time(),
  95. ];
  96. if (is_empty($info)) {
  97. $data = self::getUserInfo($app,$appid,$openId,$wechatInfoId);
  98. $data['active_at'] = time();
  99. }else{
  100. if ($info->uid == 0 && !empty($info->unionid)){
  101. $data['uid'] = intval(DB::table('wechat_miniprogram_users')->where('unionid', $info->unionid)->value('id'));
  102. }
  103. }
  104. WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
  105. }
  106. /**
  107. * 关注取消关注更新用户信息
  108. * name: updateUserInfo
  109. * @param $app
  110. * @param $appid
  111. * @param $openId
  112. * @param $is_gz
  113. * date 2023/07/10 18:31
  114. */
  115. private static function updateUserInfo($app, $appid, $openId, $wechatInfoId, $is_gz)
  116. {
  117. if (!$is_gz) {
  118. $data = [
  119. 'mp_openid' => $openId,
  120. 'gzh_appid' => $appid,
  121. 'gzh_id' => $wechatInfoId,
  122. 'active_at' => time(),
  123. 'is_subscribe' => 0,
  124. 'unsubscribe_at' => get_date(),
  125. ];
  126. } else {
  127. $data = [
  128. 'mp_openid' => $openId,
  129. 'gzh_appid' => $appid,
  130. 'gzh_id' => $wechatInfoId,
  131. 'is_subscribe' => 1,
  132. 'subscribe_time' => time(),
  133. 'subscribe_time_str' => get_date(),
  134. ];
  135. }
  136. $info = WechatOfficialUserInfo::query()->where('mp_openid', $openId)->where('gzh_appid', $appid)->first();
  137. if ($is_gz == true && is_empty($info)) {
  138. $data = self::getUserInfo($app,$appid,$openId,$wechatInfoId);
  139. WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
  140. }else{
  141. if (!is_empty($info)){
  142. if ($info->uid == 0 && !empty($info->unionid)){
  143. $data['uid'] = intval(DB::table('wechat_miniprogram_users')->where('unionid', $info->unionid)->value('id'));
  144. }
  145. WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
  146. }
  147. }
  148. }
  149. /**3
  150. * 构建用户数据
  151. * name: getUserInfo
  152. * @param $app
  153. * @param $appid
  154. * @param $openId
  155. * date 2023/07/10 18:31
  156. */
  157. protected static function getUserInfo($app,$appid,$openId, $wechatInfoId){
  158. $user = $app->user->get($openId);
  159. return [
  160. 'mp_openid' => $openId,
  161. 'gzh_appid' => $appid,
  162. 'gzh_id' => $wechatInfoId,
  163. 'unionid' => $user['unionid'],
  164. 'is_subscribe' => $user['subscribe'],
  165. 'subscribe_time' => $user['subscribe_time'],
  166. 'subscribe_time_str' => get_date($user['subscribe_time']),
  167. 'uid' => !is_empty($user['unionid']) ? intval(DB::table('wechat_miniprogram_users')->where('unionid', $user['unionid'])->value('id')) : 0,
  168. ];
  169. }
  170. /**
  171. * 处理事件消息
  172. * name: handleEvent
  173. * @param $app
  174. * @param $wechatInfoId
  175. * @param $appid
  176. * @param $message
  177. * @return mixed|string
  178. * date 2023/07/11 09:15
  179. */
  180. private static function handleEvent($app, $wechatInfoId, $appid, $message)
  181. {
  182. switch ($message['Event']) {
  183. case 'unsubscribe':
  184. self::updateUserInfo($app, $appid, $message['FromUserName'], $wechatInfoId,false);
  185. return '取消关注';
  186. case 'subscribe':
  187. self::updateUserInfo($app, $appid, $message['FromUserName'],$wechatInfoId, true);
  188. return WechatSubscribeService::getContent($wechatInfoId);
  189. default:
  190. return '收到其它消息';
  191. }
  192. return '';
  193. }
  194. }