瀏覽代碼

微信公众号用户信息表增加公众号id

zqwang 1 年之前
父節點
當前提交
194f1e8ba5

+ 1 - 1
modules/WechatPlatform/Models/WechatOfficialUserInfo.php

@@ -10,7 +10,7 @@ class WechatOfficialUserInfo extends BaseModel
     protected $table = 'wechat_official_user_info';
 
     protected $fillable = [
-        'id', 'uid', 'gzh_appid', "active_at", "unsubscribe_at", 'user_id', 'puser_id', 'mp_openid', 'unionid', 'is_subscribe', 'subscribe_time', 'subscribe_time_str', 'active_end', 'updated_at', 'created_at',
+        'id', 'uid', 'gzh_appid', "gzh_id", "active_at", "unsubscribe_at", 'user_id', 'puser_id', 'mp_openid', 'unionid', 'is_subscribe', 'subscribe_time', 'subscribe_time_str', 'active_end', 'updated_at', 'created_at',
     ];
 
 }

+ 12 - 8
modules/WechatPlatform/Services/WechatCommonService.php

@@ -46,7 +46,7 @@ class WechatCommonService extends BaseService
                 return self::handleEvent($app, $wechatInfoId, $appid, $message);
             case 'text':
                 // 更新用户活跃时间
-                self::updateUserActivityTime($app, $appid, $message['FromUserName']);
+                self::updateUserActivityTime($app,$wechatInfoId ,$appid, $message['FromUserName']);
                 return self::handleTextMessage($wechatInfoId, $message);
             // case 'image':
             //     return '收到图片消息';
@@ -92,17 +92,18 @@ 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 && !empty($info->unionid)){
@@ -122,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(),
@@ -137,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(),
@@ -144,7 +147,7 @@ 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)){
@@ -164,11 +167,12 @@ 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 = [
             'mp_openid' => $openId,
             'gzh_appid' => $appid,
+            'gzh_id' => $wechatInfoId,
             'unionid' => $user['unionid'],
             'is_subscribe' => $user['subscribe'],
             'subscribe_time' => $user['subscribe_time'],
@@ -191,10 +195,10 @@ class WechatCommonService extends BaseService
     {
         switch ($message['Event']) {
             case 'unsubscribe':
-                self::updateUserInfo($app, $appid, $message['FromUserName'], false);
+                self::updateUserInfo($app, $appid, $message['FromUserName'], $wechatInfoId,false);
                 return '取消关注';
             case 'subscribe':
-                self::updateUserInfo($app, $appid, $message['FromUserName'], true);
+                self::updateUserInfo($app, $appid, $message['FromUserName'],$wechatInfoId, true);
                 return WechatSubscribeService::getContent($wechatInfoId);
             default:
                 return '收到其它消息';