Pārlūkot izejas kodu

Merge branch 'wx-kw' into test

zqwang 1 gadu atpakaļ
vecāks
revīzija
bfe8b67d6a

+ 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', 'user_id', 'puser_id', 'mp_openid', 'unionid', 'is_subscribe', 'subscribe_time', 'subscribe_time_str', 'active_end', 'updated_at', 'created_at',
+        'id', 'uid', 'gzh_appid', "unsubscribe_at", 'user_id', 'puser_id', 'mp_openid', 'unionid', 'is_subscribe', 'subscribe_time', 'subscribe_time_str', 'active_end', 'updated_at', 'created_at',
     ];
 
 }

+ 102 - 27
modules/WechatPlatform/Services/WechatCommonService.php

@@ -24,9 +24,9 @@ class WechatCommonService extends BaseService
      * @param $sequence
      * date 2023/07/07 11:52
      */
-    public static function getPlayPageUrl($videoId,$sequence)
+    public static function getPlayPageUrl($videoId, $sequence)
     {
-        return ['url' =>  "/pages/video/index?video_id={$videoId}&sequence={$sequence}"];
+        return ['url' => "/pages/video/index?video_id={$videoId}&sequence={$sequence}"];
     }
 
     /**
@@ -39,7 +39,7 @@ class WechatCommonService extends BaseService
      * @return string|void
      * date 2023/07/10 16:04
      */
-    public static function handleMessage($app,$wechatInfoId,$appid, $message)
+    public static function handleMessage($app, $wechatInfoId, $appid, $message)
     {
         switch ($message['MsgType']) {
             case 'event':
@@ -47,8 +47,8 @@ class WechatCommonService extends BaseService
                 break;
             case 'text':
                 // 更新用户活跃时间
-                self::updateUserActivityTime($app,$appid,$message['FromUserName']);
-                return self::handleTextMessage($wechatInfoId,$message);
+                self::updateUserActivityTime($app, $appid, $message['FromUserName']);
+                return self::handleTextMessage($wechatInfoId, $message);
             case 'image':
                 return '收到图片消息';
                 break;
@@ -61,12 +61,13 @@ class WechatCommonService extends BaseService
             case 'location':
                 return '收到坐标消息';
                 break;
-            case 'link':
-                return '收到链接消息';
+            case 'unsubscribe':
+                self::updateUserInfo($app, $appid, $message['FromUserName'], false);
+                return '取消关注';
                 break;
-            case 'file':
-                return '收到文件消息';
-
+            case 'subscribe':
+                self::updateUserInfo($app, $appid, $message['FromUserName'], true);
+                return self::handleSubscribeMessage($wechatInfoId, $message);
             default:
                 return '收到其它消息';
                 break;
@@ -84,37 +85,111 @@ class WechatCommonService extends BaseService
     private static function handleTextMessage($wechatAppId, $msg)
     {
         $content = $msg['Content'];
-        if ($content == "pk"){
-            return $msg['FromUserName'] ;
+        if ($content == "pk") {
+            return $msg['FromUserName'];
         }
-        $back  =  WechatKeywordsService::getKeywordsByWords($content,$wechatAppId);
-        $back = $back  ?: "";
-        return  $back;
+        $back = WechatKeywordsService::getKeywordsByWords($content, $wechatAppId);
+        return $back ?: "";
     }
 
-    private static function updateUserActivityTime($app,$appid,$openId)
+    /**
+     *  更新用户活跃时间
+     * name: updateUserActivityTime
+     * @param $app
+     * @param $appid
+     * @param $openId
+     * date 2023/07/10 18:30
+     */
+    private static function updateUserActivityTime($app, $appid, $openId)
     {
 
-        $info = WechatOfficialUserInfo::query()->where('mp_openid',$openId)->where('gzh_appid',$appid)->first();
+        $info = WechatOfficialUserInfo::query()->where('mp_openid', $openId)->where('gzh_appid', $appid)->first();
         $data = [
             'mp_openid' => $openId,
             'gzh_appid' => $appid,
-            'active_at' =>  time(),
+            'active_at' => time(),
         ];
-        if (is_empty($info)){
-            $user = $app->user->get($openId);
+        if (is_empty($info)) {
+            $data = self::getUserInfo($app,$appid,$openId);
+        }else{
+            if ($info->uid == 0){
+                $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);
+    }
+
+    /**
+     *  更新用户时间
+     * name: updateUserInfo
+     * @param $app
+     * @param $appid
+     * @param $openId
+     * @param $is_gz
+     * date 2023/07/10 18:31
+     */
+    private static function updateUserInfo($app, $appid, $openId, $is_gz)
+    {
+
+        if (!$is_gz) {
+            $data = [
+                'mp_openid' => $openId,
+                'gzh_appid' => $appid,
+                'active_at' => time(),
+                'is_subscribe' => 0,
+                'unsubscribe_at' => get_date(),
+            ];
+        } else {
             $data = [
                 'mp_openid' => $openId,
                 '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')),
+                'is_subscribe' => 1,
+                'subscribe_time' => time(),
+                'subscribe_time_str' => get_date(),
             ];
         }
-        WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'],'gzh_appid' => $data['gzh_appid']],$data);
+        $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);
+            WechatOfficialUserInfo::updateOrCreate(['mp_openid' => $data['mp_openid'], 'gzh_appid' => $data['gzh_appid']], $data);
+        }else{
+
+            if (!is_empty($info)){
+                if ($info->uid == 0){
+                    $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);
+            }
+        }
+    }
+
+    /**
+     *  构建用户数据
+     * name: getUserInfo
+     * @param $app
+     * @param $appid
+     * @param $openId
+     * date 2023/07/10 18:31
+     */
+    protected  static  function getUserInfo($app,$appid,$openId){
+        $user = $app->user->get($openId);
+        $data = [
+            'mp_openid' => $openId,
+            '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')),
+        ];
+    }
+
+    private static function handleSubscribeMessage($wechatInfoId)
+    {
+        $back = WechatSubscribeService::getContent($wechatAppId);
+        return $back ?: "";
     }
 
 

+ 22 - 0
modules/WechatPlatform/Services/WechatSubscribeService.php

@@ -265,4 +265,26 @@ class WechatSubscribeService extends BaseService
         return $sql;
     }
 
+    /**
+     * 获取关注回复内容
+     * name: getContent
+     * @param $wechatAppId
+     * @return mixed|string
+     * date 2023/07/10 18:37
+     */
+    public static function getContent($wechatAppId)
+    {
+        $info = WechatAccountSubscribeDetail::where([
+            'status' => 1,
+            'wechat_authorization_info_id' => $wechatAppId,
+        ])->first();
+        if (is_empty($info)) {
+            return "";
+        }
+        WechatAccountSubscribeDetail::where('id', $info->id)->increment('send_total', 1);
+        WechatSubscribeMsg::where('id', $info->weacht_keyworld_id)->increment('send_total', 1);
+
+        return $info->content;
+    }
+
 }