WechatCommonService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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']);
  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']);
  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)
  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. 'active_at' => time(),
  93. ];
  94. if (is_empty($info)) {
  95. $data = self::getUserInfo($app,$appid,$openId);
  96. $data['active_at'] = time();
  97. }else{
  98. if ($info->uid == 0){
  99. $data['uid'] = intval(DB::table('wechat_miniprogram_users')->where('unionid', $info->unionid)->value('id'));
  100. }
  101. }
  102. WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
  103. }
  104. /**
  105. * 关注取消关注更新用户信息
  106. * name: updateUserInfo
  107. * @param $app
  108. * @param $appid
  109. * @param $openId
  110. * @param $is_gz
  111. * date 2023/07/10 18:31
  112. */
  113. private static function updateUserInfo($app, $appid, $openId, $is_gz)
  114. {
  115. if (!$is_gz) {
  116. $data = [
  117. 'mp_openid' => $openId,
  118. 'gzh_appid' => $appid,
  119. 'active_at' => time(),
  120. 'is_subscribe' => 0,
  121. 'unsubscribe_at' => get_date(),
  122. ];
  123. } else {
  124. $data = [
  125. 'mp_openid' => $openId,
  126. 'gzh_appid' => $appid,
  127. 'is_subscribe' => 1,
  128. 'subscribe_time' => time(),
  129. 'subscribe_time_str' => get_date(),
  130. ];
  131. }
  132. $info = WechatOfficialUserInfo::query()->where('mp_openid', $openId)->where('gzh_appid', $appid)->first();
  133. if ($is_gz == true && is_empty($info)) {
  134. $data = self::getUserInfo($app,$appid,$openId);
  135. WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
  136. }else{
  137. if (!is_empty($info)){
  138. if ($info->uid == 0){
  139. $data['uid'] = intval(DB::table('wechat_miniprogram_users')->where('unionid', $info->unionid)->value('id'));
  140. }
  141. WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
  142. }
  143. }
  144. }
  145. /**
  146. * 构建用户数据
  147. * name: getUserInfo
  148. * @param $app
  149. * @param $appid
  150. * @param $openId
  151. * date 2023/07/10 18:31
  152. */
  153. protected static function getUserInfo($app,$appid,$openId){
  154. $user = $app->user->get($openId);
  155. $data = [
  156. 'mp_openid' => $openId,
  157. 'gzh_appid' => $appid,
  158. 'unionid' => $user['unionid'],
  159. 'is_subscribe' => $user['subscribe'],
  160. 'subscribe_time' => $user['subscribe_time'],
  161. 'subscribe_time_str' => get_date($user['subscribe_time']),
  162. 'uid' => intval(DB::table('wechat_miniprogram_users')->where('unionid', $user['unionid'])->value('id')),
  163. ];
  164. }
  165. /**
  166. * 处理事件消息
  167. * name: handleEvent
  168. * @param $app
  169. * @param $wechatInfoId
  170. * @param $appid
  171. * @param $message
  172. * @return mixed|string
  173. * date 2023/07/11 09:15
  174. */
  175. private static function handleEvent($app, $wechatInfoId, $appid, $message)
  176. {
  177. switch ($message['Event']) {
  178. case 'unsubscribe':
  179. self::updateUserInfo($app, $appid, $message['FromUserName'], false);
  180. return '取消关注';
  181. case 'subscribe':
  182. self::updateUserInfo($app, $appid, $message['FromUserName'], true);
  183. return WechatSubscribeService::getContent($wechatInfoId);
  184. default:
  185. return '收到其它消息';
  186. }
  187. return '';
  188. }
  189. }