|
@@ -43,34 +43,26 @@ class WechatCommonService extends BaseService
|
|
|
{
|
|
|
switch ($message['MsgType']) {
|
|
|
case 'event':
|
|
|
- return '收到事件消息';
|
|
|
- break;
|
|
|
+ return self::handleEvent($app, $wechatInfoId, $appid, $message);
|
|
|
case 'text':
|
|
|
// 更新用户活跃时间
|
|
|
- self::updateUserActivityTime($app, $appid, $message['FromUserName']);
|
|
|
+ self::updateUserActivityTime($app,$appid, $message['FromUserName'],$wechatInfoId );
|
|
|
return self::handleTextMessage($wechatInfoId, $message);
|
|
|
- case 'image':
|
|
|
- return '收到图片消息';
|
|
|
- break;
|
|
|
- case 'voice':
|
|
|
- return '收到语音消息';
|
|
|
- break;
|
|
|
- case 'video':
|
|
|
- return '收到视频消息';
|
|
|
- break;
|
|
|
- case 'location':
|
|
|
- return '收到坐标消息';
|
|
|
- break;
|
|
|
- case 'unsubscribe':
|
|
|
- self::updateUserInfo($app, $appid, $message['FromUserName'], false);
|
|
|
- return '取消关注';
|
|
|
- break;
|
|
|
- case 'subscribe':
|
|
|
- self::updateUserInfo($app, $appid, $message['FromUserName'], true);
|
|
|
- return self::handleSubscribeMessage($wechatInfoId, $message);
|
|
|
+ // case 'image':
|
|
|
+ // return '收到图片消息';
|
|
|
+ // case 'voice':
|
|
|
+ // return '收到语音消息';
|
|
|
+ // case 'video':
|
|
|
+ // return '收到视频消息';
|
|
|
+ // case 'location':
|
|
|
+ // return '收到坐标消息';
|
|
|
+ // case 'link':
|
|
|
+ // return '收到链接消息';
|
|
|
+ // case 'file':
|
|
|
+ // return '收到文件消息';
|
|
|
default:
|
|
|
+ self::updateUserActivityTime($app, $appid, $message['FromUserName'],$wechatInfoId);
|
|
|
return '收到其它消息';
|
|
|
- break;
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
@@ -100,19 +92,21 @@ class WechatCommonService extends BaseService
|
|
|
* @param $openId
|
|
|
* date 2023/07/10 18:30
|
|
|
*/
|
|
|
- private static function updateUserActivityTime($app, $appid, $openId)
|
|
|
+ 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);
|
|
|
+ $data = self::getUserInfo($app,$appid,$openId,$wechatInfoId);
|
|
|
+ $data['active_at'] = time();
|
|
|
}else{
|
|
|
- if ($info->uid == 0){
|
|
|
+ if ($info->uid == 0 && !empty($info->unionid)){
|
|
|
$data['uid'] = intval(DB::table('wechat_miniprogram_users')->where('unionid', $info->unionid)->value('id'));
|
|
|
}
|
|
|
}
|
|
@@ -121,7 +115,7 @@ class WechatCommonService extends BaseService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 更新用户时间
|
|
|
+ * 关注取消关注更新用户信息
|
|
|
* name: updateUserInfo
|
|
|
* @param $app
|
|
|
* @param $appid
|
|
@@ -129,13 +123,14 @@ class WechatCommonService extends BaseService
|
|
|
* @param $is_gz
|
|
|
* date 2023/07/10 18:31
|
|
|
*/
|
|
|
- private static function updateUserInfo($app, $appid, $openId, $is_gz)
|
|
|
+ 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(),
|
|
@@ -144,6 +139,7 @@ class WechatCommonService extends BaseService
|
|
|
$data = [
|
|
|
'mp_openid' => $openId,
|
|
|
'gzh_appid' => $appid,
|
|
|
+ 'gzh_id' => $wechatInfoId,
|
|
|
'is_subscribe' => 1,
|
|
|
'subscribe_time' => time(),
|
|
|
'subscribe_time_str' => get_date(),
|
|
@@ -151,12 +147,11 @@ class WechatCommonService extends BaseService
|
|
|
}
|
|
|
$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);
|
|
|
+ $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){
|
|
|
+ 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);
|
|
@@ -164,7 +159,7 @@ class WechatCommonService extends BaseService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
+ /**3
|
|
|
* 构建用户数据
|
|
|
* name: getUserInfo
|
|
|
* @param $app
|
|
@@ -172,24 +167,43 @@ class WechatCommonService extends BaseService
|
|
|
* @param $openId
|
|
|
* date 2023/07/10 18:31
|
|
|
*/
|
|
|
- protected static function getUserInfo($app,$appid,$openId){
|
|
|
+ protected static function getUserInfo($app,$appid,$openId, $wechatInfoId){
|
|
|
$user = $app->user->get($openId);
|
|
|
- $data = [
|
|
|
+ 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']),
|
|
|
- 'active_at' => time(),
|
|
|
- 'uid' => intval(DB::table('wechat_miniprogram_users')->where('unionid', $user['unionid'])->value('id')),
|
|
|
+ 'uid' => !is_empty($user['unionid']) ? intval(DB::table('wechat_miniprogram_users')->where('unionid', $user['unionid'])->value('id')) : 0,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- private static function handleSubscribeMessage($wechatAppId)
|
|
|
+ /**
|
|
|
+ * 处理事件消息
|
|
|
+ * name: handleEvent
|
|
|
+ * @param $app
|
|
|
+ * @param $wechatInfoId
|
|
|
+ * @param $appid
|
|
|
+ * @param $message
|
|
|
+ * @return mixed|string
|
|
|
+ * date 2023/07/11 09:15
|
|
|
+ */
|
|
|
+ private static function handleEvent($app, $wechatInfoId, $appid, $message)
|
|
|
{
|
|
|
- $back = WechatSubscribeService::getContent($wechatAppId);
|
|
|
- return $back ?: "";
|
|
|
+ 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);
|
|
|
+ default:
|
|
|
+ return '收到其它消息';
|
|
|
+ }
|
|
|
+ return '';
|
|
|
}
|
|
|
|
|
|
|