WechatCommonService.php 6.4 KB

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