123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- /**
- *
- * @file:WechatCommonSevice.php
- * @Date: 2023/7/7
- * @Time: 11:44
- */
- namespace Modules\WechatPlatform\Services;
- use Illuminate\Support\Facades\DB;
- use Modules\Common\Services\BaseService;
- use Modules\WechatPlatform\Models\WechatOfficialUserInfo;
- class WechatCommonService extends BaseService
- {
- /**
- * 播放页面地址
- * name: getPlayPageUrl
- * @param $videoId
- * @param $sequence
- * date 2023/07/07 11:52
- */
- public static function getPlayPageUrl($videoId,$sequence)
- {
- return ['url' => "/pages/video/index?video_id={$videoId}&sequence={$sequence}"];
- }
- /**
- * 微信消息推送处理
- * name: handleMessage
- * @param $app
- * @param $wechatInfoId 公众号授权信息id
- * @param $appid 公众号APPID
- * @param $message
- * @return string|void
- * date 2023/07/10 16:04
- */
- public static function handleMessage($app,$wechatInfoId,$appid, $message)
- {
- switch ($message['MsgType']) {
- case 'event':
- return '收到事件消息';
- break;
- case 'text':
- // 更新用户活跃时间
- self::updateUserActivityTime($app,$appid,$message['FromUserName']);
- return self::handleTextMessage($wechatInfoId,$message);
- case 'image':
- return '收到图片消息';
- break;
- case 'voice':
- return '收到语音消息';
- break;
- case 'video':
- return '收到视频消息';
- break;
- case 'location':
- return '收到坐标消息';
- break;
- case 'link':
- return '收到链接消息';
- break;
- case 'file':
- return '收到文件消息';
- default:
- return '收到其它消息';
- break;
- }
- return "";
- }
- /**
- * 文本纤细处理
- * name: handleTextMessage
- * @param $appid
- * @param $content
- * date 2023/07/10 15:03
- */
- private static function handleTextMessage($wechatAppId, $msg)
- {
- $content = $msg['Content'];
- if ($content == "pk"){
- return $msg['FromUserName'] ;
- }
- $back = WechatKeywordsService::getKeywordsByWords($content,$wechatAppId);
- $back = $back ?: "";
- return $back;
- }
- private static function updateUserActivityTime($app,$appid,$opendId)
- {
- $info = WechatOfficialUserInfo::query()->where('mp_openid',$opendId)->where('gzh_appid',$appid)->first();
- $data = [
- 'mp_openid' => $opendId,
- 'gzh_appid' => $appid,
- 'active_at' => time(),
- ];
- if (is_empty($info)){
- $user = $app->user->get();
- $data = [
- 'mp_openid' => $opendId,
- 'gzh_appid' => $appid,
- 'unionid' => $user['unionid'],
- 'is_subscribe' => $user['subscribe'],
- 'subscribe_time' => $user['subscribe_time'],
- 'subscribe_time_str' => get_date($user['subscribe_time']),
- 'active_at' => time(),
- 'uid' => intval(DB::table('wechat_miniprogram_users')->where('unionid',$user['unionid'])->value('id')),
- ];
- }
- WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'],'gzh_appid' => $data['gzh_appid']],$data);
- }
- }
|