WechatCommonService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 'link':
  58. return '收到链接消息';
  59. break;
  60. case 'file':
  61. return '收到文件消息';
  62. default:
  63. return '收到其它消息';
  64. break;
  65. }
  66. return "";
  67. }
  68. /**
  69. * 文本纤细处理
  70. * name: handleTextMessage
  71. * @param $appid
  72. * @param $content
  73. * date 2023/07/10 15:03
  74. */
  75. private static function handleTextMessage($wechatAppId, $msg)
  76. {
  77. $content = $msg['Content'];
  78. if ($content == "pk"){
  79. return $msg['FromUserName'] ;
  80. }
  81. $back = WechatKeywordsService::getKeywordsByWords($content,$wechatAppId);
  82. $back = $back ?: "";
  83. return $back;
  84. }
  85. private static function updateUserActivityTime($app,$appid,$opendId)
  86. {
  87. $info = WechatOfficialUserInfo::query()->where('mp_openid',$opendId)->where('gzh_appid',$appid)->first();
  88. $data = [
  89. 'mp_openid' => $opendId,
  90. 'gzh_appid' => $appid,
  91. 'active_at' => time(),
  92. ];
  93. if (is_empty($info)){
  94. $user = $app->user->get();
  95. $data = [
  96. 'mp_openid' => $opendId,
  97. 'gzh_appid' => $appid,
  98. 'unionid' => $user['unionid'],
  99. 'is_subscribe' => $user['subscribe'],
  100. 'subscribe_time' => $user['subscribe_time'],
  101. 'subscribe_time_str' => get_date($user['subscribe_time']),
  102. 'active_at' => time(),
  103. 'uid' => intval(DB::table('wechat_miniprogram_users')->where('unionid',$user['unionid'])->value('id')),
  104. ];
  105. }
  106. WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'],'gzh_appid' => $data['gzh_appid']],$data);
  107. }
  108. }