Explorar o código

关键词回复增加用户互动时间戳

zqwang hai 1 ano
pai
achega
b49dd6fb9d

+ 3 - 3
modules/Channel/Http/Controllers/WechatOpenPlatformController.php

@@ -141,15 +141,15 @@ class WechatOpenPlatformController extends CatchController
 
         $app = $openPlatform->officialAccount($authorizer_appid, getProp($appInfo,'authorizer_refresh_token'));
         unset($appInfo);
-        $app->server->push(function ($message) use($app,$wechatAppId) {
+
+        $app->server->push(function ($message) use($app,$authorizer_appid,$wechatAppId) {
             myLog("wx-xiaoxi")->info("----文本消息---");
             myLog("wx-xiaoxi")->info("体消息:");
             myLog("wx-xiaoxi")->info($message);
-            return WechatCommonService::handleMessage($app,$wechatAppId ,$message);
+            return WechatCommonService::handleMessage($app,$wechatAppId ,$authorizer_appid,$message);
         });
 
         myLog("wx-xiaoxi")->info('-------结束处理---'.get_date());
-
         return $app->server->serve()->send();
     }
 }

+ 1 - 1
modules/Channel/Services/WechatOpenPlatform/WechatOpenPlatformService.php

@@ -87,6 +87,6 @@ class WechatOpenPlatformService
             'component_appid' => $component_appid,
             'authorizer_appid' => $authorizer_appid,
             'is_enabled'  =>  1,
-        ])->select('authorizer_refresh_token','id')->first();
+        ])->first();
     }
 }

+ 16 - 0
modules/WechatPlatform/Models/WechatOfficialUserInfo.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace Modules\WechatPlatform\Models;
+
+use Modules\Common\Models\BaseModel;
+
+
+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',
+    ];
+
+}

+ 33 - 11
modules/WechatPlatform/Services/WechatCommonService.php

@@ -9,7 +9,9 @@
 
 namespace Modules\WechatPlatform\Services;
 
+use Illuminate\Support\Facades\DB;
 use Modules\Common\Services\BaseService;
+use Modules\WechatPlatform\Models\WechatOfficialUserInfo;
 
 class WechatCommonService extends BaseService
 {
@@ -31,12 +33,13 @@ class WechatCommonService extends BaseService
      *   微信消息推送处理
      * name: handleMessage
      * @param $app
-     * @param $wechatAppId 微信公众号授权信息id
+     * @param $wechatInfoId 公众号授权信息id
+     * @param $appid 公众号APPID
      * @param $message
      * @return string|void
      * date 2023/07/10 16:04
      */
-    public static function handleMessage($app,$wechatAppId, $message)
+    public static function handleMessage($app,$wechatInfoId,$appid, $message)
     {
         switch ($message['MsgType']) {
             case 'event':
@@ -44,8 +47,8 @@ class WechatCommonService extends BaseService
                 break;
             case 'text':
                 // 更新用户活跃时间
-                self::updateUserActivityTime($app,$message['FromUserName']);
-                return self::handleTextMessage($wechatAppId,$message);
+                self::updateUserActivityTime($app,$appid,$message['FromUserName']);
+                return self::handleTextMessage($wechatInfoId,$message);
             case 'image':
                 return '收到图片消息';
                 break;
@@ -63,10 +66,12 @@ class WechatCommonService extends BaseService
                 break;
             case 'file':
                 return '收到文件消息';
+
             default:
                 return '收到其它消息';
                 break;
         }
+        return "";
     }
 
     /**
@@ -79,21 +84,38 @@ class WechatCommonService extends BaseService
     private static function handleTextMessage($wechatAppId, $msg)
     {
         $content = $msg['Content'];
-        myLog("wx-xiaoxi")->info('关键词处理'.$content);
-        myLog("wx-xiaoxi")->info($msg);
         if ($content == "pk"){
             return $msg['FromUserName'] ;
         }
         $back  =  WechatKeywordsService::getKeywordsByWords($content,$wechatAppId);
         $back = $back  ?: "";
-        myLog("wx-xiaoxi")->info(['title' => "回复信息",'wechatAppId' => $wechatAppId,'msg' =>  $content,'back' => $back]);
         return  $back;
     }
 
-    private static function updateUserActivityTime($app, $openId)
+    private static function updateUserActivityTime($app,$appid,$opendId)
     {
-        $user = $app->user->get($openId);
-        myLog("wx-xiaoxi")->info("拉取用户信息");
-        myLog("wx-xiaoxi")->info($user);
+
+        $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);
     }
+
+
 }