浏览代码

Merge branch 'wx-kw' into test

zqwang 1 年之前
父节点
当前提交
7701de1e7e

+ 20 - 7
modules/WechatPlatform/Services/WechatCommonService.php

@@ -28,20 +28,24 @@ class WechatCommonService extends BaseService
     }
 
     /**
-     *  微信消息推送处理
-     * name: handelMessage
-     * @param $authorizer_appid
+     *   微信消息推送处理
+     * name: handleMessage
+     * @param $app
+     * @param $wechatAppId 微信公众号授权信息id
      * @param $message
-     * date 2023/07/10 14:53
+     * @return string|void
+     * date 2023/07/10 16:04
      */
-    public static function handleMessage($appid, $message)
+    public static function handleMessage($app,$wechatAppId, $message)
     {
         switch ($message['MsgType']) {
             case 'event':
                 return '收到事件消息';
                 break;
             case 'text':
-                return self::handleTextMessage($appid,$message);
+                // 更新用户活跃时间
+                self::updateUserActivityTime($app,$message['FromUserName']);
+                return self::handleTextMessage($wechatAppId,$message);
                 break;
             case 'image':
                 return '收到图片消息';
@@ -73,11 +77,20 @@ class WechatCommonService extends BaseService
      * @param $content
      * date 2023/07/10 15:03
      */
-    private static function handleTextMessage($appid, $msg)
+    private static function handleTextMessage($wechatAppId, $msg)
     {
         $content = $msg['Content'];
         if ($content == "pk"){
             return $msg['FromUserName'] ;
         }
+        $content =  WechatKeywordsService::getKeywordsByWords($content,$wechatAppId);
+        return $content ?: "";
+    }
+
+    private static function updateUserActivityTime($app, $openId)
+    {
+        $user = $app->user->get($openId);
+        myLog("wx-xiaoxi")->info("拉取用户信息");
+        myLog("wx-xiaoxi")->info($user);
     }
 }

+ 77 - 53
modules/WechatPlatform/Services/WechatKeywordsService.php

@@ -44,12 +44,12 @@ class WechatKeywordsService extends BaseService
      */
     public static function getKeywordsList(array $param)
     {
-        $sql = self::getQuery($param)->orderBy('id','desc');
-        $isAll = getProp($param,'is_all',false);
-        if($isAll){
+        $sql = self::getQuery($param)->orderBy('id', 'desc');
+        $isAll = getProp($param, 'is_all', false);
+        if ($isAll) {
             $list = $sql->get();
-        }else{
-            $list = $sql->paginate(getProp($param,'limit',10));
+        } else {
+            $list = $sql->paginate(getProp($param, 'limit', 10));
         }
         $list->makeHidden(self::$hideField);
         return $list;
@@ -57,7 +57,7 @@ class WechatKeywordsService extends BaseService
 
     private static function getQuery(array $param)
     {
-        $sql = WechatKeywords::query()->where('is_del',0);
+        $sql = WechatKeywords::query()->where('is_del', 0);
         if (getProp($param, 'puser_id')) {
             $sql->where('puser_id', $param['puser_id']);
         }
@@ -67,11 +67,11 @@ class WechatKeywordsService extends BaseService
         if (getProp($param, 'keyword')) {
             $sql->where('keyword', "like", "%" . $param['keyword'] . "%");
         }
-        if(getProp($param,'miniprogram_id')){
-            $sql->where('miniprogram_id',  $param['miniprogram_id']);
+        if (getProp($param, 'miniprogram_id')) {
+            $sql->where('miniprogram_id', $param['miniprogram_id']);
         }
-        if(getProp($param,'wechat_authorization_info_id')){
-            $sql->whereJsonContains('wechat_accounts->id',$param['wechat_authorization_info_id']);
+        if (getProp($param, 'wechat_authorization_info_id')) {
+            $sql->whereJsonContains('wechat_accounts->id', $param['wechat_authorization_info_id']);
         }
         return $sql;
     }
@@ -84,7 +84,7 @@ class WechatKeywordsService extends BaseService
      */
     public static function detail($id)
     {
-        return WechatKeywords::where('id', $id)->where('is_del', 0)->first()->makeHidden(array_merge(self::$hideField,['wechat_accounts']));
+        return WechatKeywords::where('id', $id)->where('is_del', 0)->first()->makeHidden(array_merge(self::$hideField, ['wechat_accounts']));
     }
 
     /**
@@ -106,10 +106,10 @@ class WechatKeywordsService extends BaseService
             // 修改关键词,需要查询词关键词是否已配置
             $appIds = WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->where('status', 1)->pluck('wechat_authorization_info_id')->toArray();
         }
-        WechatKeywords::query()->where('id',$id)->update($param);
-        if ($appIds){
-            WechatAccountKeywordLog::where('weacht_keyworld_id',$info->id)->update(['status' => 0]);
-            self::allocation($info->id,$appIds);
+        WechatKeywords::query()->where('id', $id)->update($param);
+        if ($appIds) {
+            WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->update(['status' => 0]);
+            self::allocation($info->id, $appIds);
         }
         return "操作成功";
     }
@@ -175,49 +175,49 @@ class WechatKeywordsService extends BaseService
         $data = [];
         $list = [];
         $errMsg = "";
-        if (empty($wxAuthIds)){
+        if (empty($wxAuthIds)) {
             $data['wechat_accounts'] = [];
-        }else{
+        } else {
             $wechatAccountInfos = DB::table('wechat_authorization_infos')
-                ->whereIn('id',$wxAuthIds)
-                ->where('is_enabled',1)
-                ->where('user_id',$info->user_id)
+                ->whereIn('id', $wxAuthIds)
+                ->where('is_enabled', 1)
+                ->where('user_id', $info->user_id)
                 ->get();
-            if($wechatAccountInfos->isEmpty()){
+            if ($wechatAccountInfos->isEmpty()) {
                 self::throwErrMsg("优化师对提交的公众号没有使用权限");
             }
 
             $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();
-            $canNotUsed = array_diff($wxAuthIds,$canNotUsed);
-            if (count($canNotUsed) > 0){
-                self::throwErrMsg("优化师对id:为:".implode(',',$canNotUsed)."的公众号没有使用权限");
+            $canNotUsed = array_diff($wxAuthIds, $canNotUsed);
+            if (count($canNotUsed) > 0) {
+                self::throwErrMsg("优化师对id:为:" . implode(',', $canNotUsed) . "的公众号没有使用权限");
             }
-            $keywords = explode(',',$info->keyword);
+            $keywords = explode(',', $info->keyword);
 
-            $allSet =   WechatAccountKeywordLog::where('user_id',$info->user_id)->get();
+            $allSet = WechatAccountKeywordLog::where('user_id', $info->user_id)->get();
 
-            foreach ($wechatAccountInfos as  $val){
+            foreach ($wechatAccountInfos as $val) {
                 $data['wechat_accounts'][] = [
-                    'id' =>  $val->id,
-                    'appid' =>$val->authorizer_appid,
+                    'id' => $val->id,
+                    'appid' => $val->authorizer_appid,
                     'nick_name' => $val->nick_name,
                     'component_appid' => $val->component_appid
                 ];
-                foreach ($keywords as  $kval){
-                    $has  = $allSet->where( 'keyword',$kval)
-                        ->where( 'wechat_authorization_info_id', $val->id)
-                    ->where( 'status', 1)
-                        ->where('weacht_keyworld_id','<>',$info->id)->first();
-                    if ($has){
-                        $errMsg .=  "{$val->nick_name}已重新配置关键词{$kval};";
+                foreach ($keywords as $kval) {
+                    $has = $allSet->where('keyword', $kval)
+                        ->where('wechat_authorization_info_id', $val->id)
+                        ->where('status', 1)
+                        ->where('weacht_keyworld_id', '<>', $info->id)->first();
+                    if ($has) {
+                        $errMsg .= "{$val->nick_name}已重新配置关键词{$kval};";
                     }
                     $list[] = [
                         'weacht_keyworld_id' => $info->id,
                         'user_id' => $info->user_id,
                         'puser_id' => $info->puser_id,
-                        'miniprogram_id' =>  $info->miniprogram_id,
+                        'miniprogram_id' => $info->miniprogram_id,
                         'appid' => $val->authorizer_appid,
-                        'wechat_authorization_info_id'=> $val->id,
+                        'wechat_authorization_info_id' => $val->id,
                         'nick_name' => $val->nick_name,
                         'keyword' => $kval,
                         'content' => $info->send_content,
@@ -225,43 +225,67 @@ class WechatKeywordsService extends BaseService
                     ];
                 }
             }
-            unset($wechatAccountInfos,$allSet);
+            unset($wechatAccountInfos, $allSet);
         }
         DB::beginTransaction();
         try {
-            WechatAccountKeywordLog::where('weacht_keyworld_id',$id)->update(['status' => 0]);
-            if (!empty($list)){
-                WechatAccountKeywordLog::where('user_id',$info->user_id)
-                    ->whereIn("keyword",$keywords)->whereIn('wechat_authorization_info_id',$wxAuthIds)->update(['status' => 0]);
-                foreach ($list as $val){
-                    WechatAccountKeywordLog::updateOrCreate(['weacht_keyworld_id' => $val['weacht_keyworld_id'],'keyword' => $val['keyword'],'wechat_authorization_info_id' => $val['wechat_authorization_info_id']],$val);
+            WechatAccountKeywordLog::where('weacht_keyworld_id', $id)->update(['status' => 0]);
+            if (!empty($list)) {
+                WechatAccountKeywordLog::where('user_id', $info->user_id)
+                    ->whereIn("keyword", $keywords)->whereIn('wechat_authorization_info_id', $wxAuthIds)->update(['status' => 0]);
+                foreach ($list as $val) {
+                    WechatAccountKeywordLog::updateOrCreate(['weacht_keyworld_id' => $val['weacht_keyworld_id'], 'keyword' => $val['keyword'], 'wechat_authorization_info_id' => $val['wechat_authorization_info_id']], $val);
                 }
             }
-            WechatKeywords::where('id',$id)->update($data);
+            WechatKeywords::where('id', $id)->update($data);
             DB::commit();
-        }catch (\Exception $exception){
+        } catch (\Exception $exception) {
             self::throwErrMsg('操作失败');
         }
-        return "操作成功".$errMsg;
+        return "操作成功" . $errMsg;
     }
 
     public static function getConfig($userId, $puserId, $miniprogramId)
     {
-        $set = MiniprogramWechatGlobalConfig::where(['user_id' =>  $userId,'puser_id' =>  $puserId,'miniprogram_id' =>  $miniprogramId])
-                ->where('type',self::KEYWORD_SET_KEY)->value('value');
-        return $set == 1 ? 1 :0;
+        $set = MiniprogramWechatGlobalConfig::where(['user_id' => $userId, 'puser_id' => $puserId, 'miniprogram_id' => $miniprogramId])
+            ->where('type', self::KEYWORD_SET_KEY)->value('value');
+        return $set == 1 ? 1 : 0;
     }
 
     public static function setConfig($param)
     {
         $param['type'] = self::KEYWORD_SET_KEY;
         $res = MiniprogramWechatGlobalConfig::updateOrCreate(
-            ['user_id' =>  $param['user_id'],'puser_id' => $param['puser_id'],'miniprogram_id' =>  $param['miniprogram_id'],'type' => self::KEYWORD_SET_KEY],
+            ['user_id' => $param['user_id'], 'puser_id' => $param['puser_id'], 'miniprogram_id' => $param['miniprogram_id'], 'type' => self::KEYWORD_SET_KEY],
             $param
         );
-        if($res){
+        if ($res) {
             return "操作成功";
         }
         return "操作失败";
     }
+
+    /**
+     *  更新关键词,授权公众号id匹配关键词
+     * name: getKeywordsByWords
+     * @param $keyword
+     * @param $wechatAppId
+     * @return mixed|string
+     * date 2023/07/10 16:31
+     */
+    public static function getKeywordsByWords($keyword, $wechatAppId)
+    {
+        // 匹配关键词
+        $info = WechatAccountKeywordLog::where(["
+            keyword" => $keyword,
+            'status' => 1,
+            'wechat_authorization_info_id' => $wechatAppId,
+        ])->first();
+        if (is_empty($info)) {
+            return "";
+        }
+        WechatAccountKeywordLog::where('id', $info->id)->increment('send_total', 1);
+        WechatKeywords::where('id', $info->weacht_keyworld_id)->increment('send_total', 1);
+        return $info->content;
+    }
 }