瀏覽代碼

关键词修改

zqwang 1 年之前
父節點
當前提交
546904c21b

+ 0 - 23
modules/WechatPlatform/Http/Controllers/SettingController.php

@@ -1,23 +0,0 @@
-<?php
-/**
- *
- * @file:settingController.php
- * @Date: 2023/7/6
- * @Time: 10:58
- */
-
-
-namespace Modules\WechatPlatform\Http\Controllers;
-
-use Catch\Base\CatchController;
-use Illuminate\Http\Request;
-
-class SettingController extends CatchController
-{
-
-    public function getConfig(Request $request)
-    {
-        $key = $request->input('key');
-
-    }
-}

+ 24 - 0
modules/WechatPlatform/Http/Controllers/WechatKeywordsController.php

@@ -167,4 +167,28 @@ class WechatKeywordsController extends CatchController
         return WechatKeywordsService::WechaAccountAuthListInfo($id,$userId);
     }
 
+    public function getConfig($miniprogramId)
+    {
+
+        $userContext = $this->getUserContext(null);
+        $userId  = $userContext['loginUser']->id;
+        $puserId  = $userContext['loginUser']->pid;
+        return WechatKeywordsService:: getConfig($userId,$puserId,$miniprogramId);
+    }
+
+    public function setConfig($miniprogramId,Request $request)
+    {
+        if (!$request->has('value') ){
+            throw  new FailedExceptio("参数错误");
+        }
+        $param = $request->all(['value']);
+        if (!in_array($param['value'],[0,1]) ){
+            throw  new FailedExceptio("参数错误");
+        }
+        $userContext = $this->getUserContext(null);
+        $param['miniprogram_id'] = $miniprogramId;
+        $param['user_id'] = $userContext['loginUser']->id;
+        $param['puser_id'] = $userContext['loginUser']->pid;
+        return WechatKeywordsService::setConfig($param);
+    }
 }

+ 15 - 0
modules/WechatPlatform/Models/MiniprogramWechatGlobalConfig.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace Modules\WechatPlatform\Models;
+
+use Modules\Common\Models\BaseModel;
+
+
+class MiniprogramWechatGlobalConfig extends BaseModel
+{
+    protected $table = 'miniprogram_wechat_global_config';
+
+    protected $fillable = [
+        'id', 'user_id', 'puser_id', 'miniprogram_id', 'type', 'value', 'desc', 'updated_at', 'created_at',
+    ];
+}

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

@@ -10,7 +10,7 @@ class WechatAccountKeywordLog extends BaseModel
     protected $table = 'wechat_account_keyword_log';
 
     protected $fillable = [
-        'id', 'weacht_keyworld_id',"miniprogram_id", 'appid',"wechat_authorization_info_id", 'nick_name', 'keyword', 'content', 'status', 'send_total', 'updated_at', 'created_at',
+        'id','user_id', 'puser_id',  'weacht_keyworld_id',"miniprogram_id", 'appid',"wechat_authorization_info_id", 'nick_name', 'keyword', 'content', 'status', 'send_total', 'updated_at', 'created_at',
     ];
 
 }

+ 57 - 36
modules/WechatPlatform/Services/WechatKeywordsService.php

@@ -11,6 +11,7 @@ namespace Modules\WechatPlatform\Services;
 
 use Illuminate\Support\Facades\DB;
 use Modules\Common\Services\BaseService;
+use Modules\WechatPlatform\Models\MiniprogramWechatGlobalConfig;
 use Modules\WechatPlatform\Models\WechatAccountKeywordLog;
 use Modules\WechatPlatform\Models\WechatKeywords;
 
@@ -18,6 +19,7 @@ class WechatKeywordsService extends BaseService
 {
 
     protected static array $hideField = ['updated_at', 'is_del', 'del_at', 'user_id', 'puser_id', 'send_content']; // 公用的影藏字段
+    const KEYWORD_SET_KEY = "miniprogram_wechat_keyword";
 
     /**
      *  添加关键字
@@ -96,31 +98,15 @@ class WechatKeywordsService extends BaseService
         if (is_empty($info)) {
             self::throwErrMsg('关键词不存在或已删除');
         }
+        $appIds = [];
         if ($param['keyword'] != $info->keyword) {
             // 修改关键词,需要查询词关键词是否已配置
-            if ($info->wechat_accounts) {
-                $appIds = WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->where('status', 1)->pluck('appid')->toArray();
-                if ($appIds) {
-                    $hasUsed = WechatAccountKeywordLog::where('keyword', $param['keyword'])->whereIn('appid', $appIds)->where('status', 1)->pluck('nick_name')->toArray();
-                    if ($hasUsed) {
-                        self::throwErrMsg("关键字{$param['keyword']}已配置了以下公众号" . implode(',', $hasUsed) . "请先解绑后再编辑");
-                    }
-                }
-            }
+            $appIds = WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->where('status', 1)->pluck('wechat_authorization_info_id')->toArray();
         }
-        DB::beginTransaction();
-        try {
-            $info->save($param);
-            $data = [
-                'content' => $param['send_content'],
-                'keyword' => $param['keyword'],
-                'miniprogram_id' => $info->miniprogram_id,
-            ];
-            WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->update($data);
-            DB::commit();
-        } catch (\Exception $exception) {
-            DB::rollBack();
-            self::throwErrMsg('编辑失败');
+        $info->save($param);
+        if ($appIds){
+            WechatAccountKeywordLog::where('weacht_keyworld_id',$info->id)->update(['status' => 0]);
+            self::allocation($info->id,$appIds);
         }
         return "操作成功";
     }
@@ -185,6 +171,7 @@ class WechatKeywordsService extends BaseService
         }
         $data = [];
         $list = [];
+        $errMsg = "";
         if (empty($wxAuthIds)){
             $data['wechat_accounts'] = [];
         }else{
@@ -202,6 +189,9 @@ class WechatKeywordsService extends BaseService
             if (count($canNotUsed) > 0){
                 self::throwErrMsg("优化师对id:为:".implode(',',$canNotUsed)."的公众号没有使用权限");
             }
+            $keywords = explode(',',$info->keyword);
+
+            $allSet =   WechatAccountKeywordLog::where('user_id',$info->user_id)->get();
 
             foreach ($wechatAccountInfos as  $val){
                 $data['wechat_accounts'][] = [
@@ -210,34 +200,65 @@ class WechatKeywordsService extends BaseService
                     'nick_name' => $val->nick_name,
                     'component_appid' => $val->component_appid
                 ];
-                $list[] = [
-                    'weacht_keyworld_id' => $info->id,
-                    'appid' => $val->authorizer_appid,
-                    'wechat_authorization_info_id'=> $val->id,
-                    'nick_name' => $val->nick_name,'keyword' => $info->keyword,
-                    'content' => $info->send_content,
-                    'status' => 1,
-                ];
+                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,
+                        'appid' => $val->authorizer_appid,
+                        'wechat_authorization_info_id'=> $val->id,
+                        'nick_name' => $val->nick_name,
+                        'keyword' => $kval,
+                        'content' => $info->send_content,
+                        'status' => 1,
+                    ];
+                }
             }
         }
-
+        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'],'wechat_authorization_info_id' => $val['wechat_authorization_info_id']],$val);
+                    WechatAccountKeywordLog::updateOrCreate(['weacht_keyworld_id' => $val['weacht_keyworld_id'],'keyword' => $val['keyword'],'wechat_authorization_info_id' => $val['wechat_authorization_info_id']],$val);
                 }
-            }else{
-                WechatAccountKeywordLog::where('weacht_keyworld_id',$id)->update(['status' => 0]);
             }
             WechatKeywords::where('id',$id)->update($data);
             DB::commit();
         }catch (\Exception $exception){
-            DB::rollBack();
             self::throwErrMsg('操作失败');
         }
-        return "操作成功";
+        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;
+    }
 
+    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],
+            $param
+        );
+        if($res){
+            return "操作成功";
+        }
+        return "操作失败";
+    }
 }

+ 4 - 1
modules/WechatPlatform/routes/route.php

@@ -17,6 +17,10 @@ Route::prefix('wechatPlatform')->group(function(){
 
     // 关键字列表
     Route::prefix('keyword')->group(function () {
+        // 获取关键词设置
+        Route::any('getConfig/{miniprogramId}',[WechatKeywordsController::class,'getConfig'])->withoutMiddleware(config('catch.route.middlewares'));
+        // 关键词全局设置
+        Route::post('setConfig/{miniprogramId}',[WechatKeywordsController::class,'setConfig'])->withoutMiddleware(config('catch.route.middlewares'));
         // 列表
         Route::any("list",[WechatKeywordsController::class,'list']);
         // 添加
@@ -29,7 +33,6 @@ Route::prefix('wechatPlatform')->group(function(){
         Route::any('del',[WechatKeywordsController::class,'del']);
         // 配置公众号
         Route::post('allocation/{id}',[WechatKeywordsController::class,'allocation']);
-
         // 关键字公账号配置选择项
         Route::any("auth_list/{id}",[WechatKeywordsController::class,'authList']);
     });