123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace Modules\WechatPlatform\Services;
- use Illuminate\Support\Facades\DB;
- use Modules\Common\Services\BaseService;
- use Modules\WechatPlatform\Models\WechatOfficialUserInfo;
- class WechatCommonService extends BaseService
- {
-
- public static function getPlayPageUrl($videoId, $sequence)
- {
- return ['url' => "/pages/video/index?video_id={$videoId}&sequence={$sequence}"];
- }
-
- public static function handleMessage($app, $wechatInfoId, $appid, $message)
- {
- switch ($message['MsgType']) {
- case 'event':
- return self::handleEvent($app, $wechatInfoId, $appid, $message);
- case 'text':
-
- self::updateUserActivityTime($app,$appid, $message['FromUserName'],$wechatInfoId );
- return self::handleTextMessage($wechatInfoId, $message);
-
-
-
-
-
-
-
-
-
-
-
-
- default:
- self::updateUserActivityTime($app, $appid, $message['FromUserName'],$wechatInfoId);
- return '收到其它消息';
- }
- return "";
- }
-
- private static function handleTextMessage($wechatAppId, $msg)
- {
- $content = $msg['Content'];
- if ($content == "pk") {
- return $msg['FromUserName'];
- }
- $back = WechatKeywordsService::getKeywordsByWords($content, $wechatAppId);
- return $back ?: "";
- }
-
- private static function updateUserActivityTime($app, $appid, $openId, $wechatInfoId)
- {
- $info = WechatOfficialUserInfo::query()->where('mp_openid', $openId)->where('gzh_appid', $appid)->first();
- $data = [
- 'mp_openid' => $openId,
- 'gzh_appid' => $appid,
- 'gzh_id' => $wechatInfoId,
- 'active_at' => time(),
- ];
- if (is_empty($info)) {
- $data = self::getUserInfo($app,$appid,$openId,$wechatInfoId);
- $data['active_at'] = time();
- }else{
- if ($info->uid == 0 && !empty($info->unionid)){
- $data['uid'] = intval(DB::table('wechat_miniprogram_users')->where('unionid', $info->unionid)->value('id'));
- }
- }
- WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
- }
-
- private static function updateUserInfo($app, $appid, $openId, $wechatInfoId, $is_gz)
- {
- if (!$is_gz) {
- $data = [
- 'mp_openid' => $openId,
- 'gzh_appid' => $appid,
- 'gzh_id' => $wechatInfoId,
- 'active_at' => time(),
- 'is_subscribe' => 0,
- 'unsubscribe_at' => get_date(),
- ];
- } else {
- $data = [
- 'mp_openid' => $openId,
- 'gzh_appid' => $appid,
- 'gzh_id' => $wechatInfoId,
- 'is_subscribe' => 1,
- 'subscribe_time' => time(),
- 'subscribe_time_str' => get_date(),
- ];
- }
- $info = WechatOfficialUserInfo::query()->where('mp_openid', $openId)->where('gzh_appid', $appid)->first();
- if ($is_gz == true && is_empty($info)) {
- $data = self::getUserInfo($app,$appid,$openId,$wechatInfoId);
- WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
- }else{
- if (!is_empty($info)){
- if ($info->uid == 0 && !empty($info->unionid)){
- $data['uid'] = intval(DB::table('wechat_miniprogram_users')->where('unionid', $info->unionid)->value('id'));
- }
- WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
- }
- }
- }
-
- protected static function getUserInfo($app,$appid,$openId, $wechatInfoId){
- $user = $app->user->get($openId);
- return [
- 'mp_openid' => $openId,
- 'gzh_appid' => $appid,
- 'gzh_id' => $wechatInfoId,
- 'unionid' => $user['unionid'],
- 'is_subscribe' => $user['subscribe'],
- 'subscribe_time' => $user['subscribe_time'],
- 'subscribe_time_str' => get_date($user['subscribe_time']),
- 'uid' => !is_empty($user['unionid']) ? intval(DB::table('wechat_miniprogram_users')->where('unionid', $user['unionid'])->value('id')) : 0,
- ];
- }
-
- private static function handleEvent($app, $wechatInfoId, $appid, $message)
- {
- switch ($message['Event']) {
- case 'unsubscribe':
- self::updateUserInfo($app, $appid, $message['FromUserName'], $wechatInfoId,false);
- return '取消关注';
- case 'subscribe':
- self::updateUserInfo($app, $appid, $message['FromUserName'],$wechatInfoId, true);
- return WechatSubscribeService::getContent($wechatInfoId);
- case 'CLICK':
- return WechatMenuService::getClickInfoContent($wechatInfoId,$message['EventKey']);
- default:
- return '收到其它消息';
- }
- return '';
- }
- }
|