WechatCommonService.php 5.9 KB

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