zhaoyang 1 yıl önce
ebeveyn
işleme
bd1bd90247

+ 1 - 0
modules/Channel/Http/Controllers/WechatOpenPlatformController.php

@@ -123,6 +123,7 @@ class WechatOpenPlatformController extends CatchController
     }
 
     public function infoCommand(Request $request, $authorizer_appid, $component_appid) {
+        myLog("wx-xiaoxi")->info(['authorizer_appid' => $authorizer_appid,'component_appid' => $component_appid,'param' => $request->all()]);
         $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
         $app = WechatOpenPlatformService::buildApplication($componentInfo);
 

+ 21 - 0
modules/WechatPlatform/Http/Controllers/CommonController.php

@@ -0,0 +1,21 @@
+<?php
+/**
+ *
+ * @file:CommonController.php
+ * @Date: 2023/7/6
+ * @Time: 18:05
+ */
+
+
+namespace Modules\WechatPlatform\Http\Controllers;
+
+use Catch\Base\CatchController;
+
+class CommonController extends CatchController
+{
+
+    public function getPageUrl()
+    {
+
+    }
+}

+ 3 - 1
modules/WechatPlatform/Http/Controllers/SettingController.php

@@ -9,7 +9,9 @@
 
 namespace Modules\WechatPlatform\Http\Controllers;
 
-class SettingController
+use Catch\Base\CatchController;
+
+class SettingController extends CatchController
 {
 
 }

+ 56 - 6
modules/WechatPlatform/Http/Controllers/WechatKeywordsController.php

@@ -29,7 +29,11 @@ class WechatKeywordsController extends CatchController
      */
     public function List(Request $request)
     {
-
+        $param  = $request->all();
+        $userContext = $this->getUserContext(null);
+        $param['user_id'] = $userContext['loginUser']->id;
+        $param['puser_id'] = $userContext['loginUser']->pid;
+        return WechatKeywordsService::getKeywordsList($param);
     }
 
     /**
@@ -41,14 +45,15 @@ class WechatKeywordsController extends CatchController
     public function add(WechatKeywordsRequest $request)
     {
         $param = $request->validated();
-        $param = $this->WechatKeywordsParam($param);
+        $param = $this->wechatKeywordsParam($param);
         $userContext = $this->getUserContext(null);
         $param['user_id'] = $userContext['loginUser']->id;
         $param['puser_id'] = $userContext['loginUser']->pid;
+        $param['wechat_accounts'] = [];
         return WechatKeywordsService::addKeyword($param);
     }
 
-    private function WechatKeywordsParam(mixed $param)
+    private function wechatKeywordsParam(mixed $param)
     {
         if (getProp($param,'type') != 'miniprogram'){
             return $param;
@@ -68,6 +73,21 @@ class WechatKeywordsController extends CatchController
         if(empty($info)){
             throw  new  FailedException("没有此小程序的使用权限");
         }
+        $param['send_content'] = "";
+        foreach ($param['content'] as & $val){
+            if (!is_array($val)){
+                throw  new  FailedException("回复内容格式不正确");
+            }
+            if (getProp($val,'url')){
+                // 跳转小程序
+                $val['content'] = "<a href=\"\" data-miniprogram-appid=\"{$param['miniprogram_appid']}\" data-miniprogram-path=\"{$val['url']}\">{$val['title']}</a>";
+            }else{
+                $val['content'] = $val['title'];
+                $val['url'] = "";
+            }
+            $param['send_content'] .= $val['content']."\n";
+        }
+        rtrim($param['send_content'],"\n");
         return  $param;
     }
     /**
@@ -79,7 +99,9 @@ class WechatKeywordsController extends CatchController
      */
     public function edit($id, WechatKeywordsRequest $request)
     {
-
+        $param = $request->validated();
+        $param = $this->wechatKeywordsParam($param);
+       return WechatKeywordsService::updateKeyWords($id,$param);
     }
 
     /**
@@ -90,7 +112,7 @@ class WechatKeywordsController extends CatchController
      */
     public function detail($id)
     {
-
+        return WechatKeywordsService::detail($id);
     }
 
     /**
@@ -102,7 +124,16 @@ class WechatKeywordsController extends CatchController
      */
     public function allocation($id,Request $request)
     {
+        if (!$request->has('wx_auth_ids')){
+            throw  new  FailedException("参数错误");
+        }
+        if (!empty($request->input('wx_auth_ids'))) {
+            $wxAuthIds = explode(',', $request->input('wx_auth_ids'));
+        }else{
+            $wxAuthIds = [];
+        }
 
+       return WechatKeywordsService::allocation($id,$wxAuthIds);
     }
 
     /**
@@ -111,10 +142,29 @@ class WechatKeywordsController extends CatchController
      * @param $id
      * date 2023/07/05 15:47
      */
-    public function del($id)
+    public function del(Request $request)
     {
+        $ids = $request->input('ids');
+        if (empty($ids)){
+            throw  new  FailedException('要删除的数据参数错误');
+        }
+
+        $ids = explode(',',$ids);
+
+        return WechatKeywordsService::delKeywords($ids);
 
     }
 
+    /**
+     *  公众号授权列表
+     * name: authList
+     * @param $id
+     * date 2023/07/06 15:17
+     */
+    public function authList($id)
+    {
+        $userId  = $this->getLoginUserId();
+        return WechatKeywordsService::WechaAccountAuthListInfo($id,$userId);
+    }
 
 }

+ 18 - 0
modules/WechatPlatform/Http/Requests/PlayPageUrlRequest.php

@@ -0,0 +1,18 @@
+<?php
+/**
+ *
+ * @file:PlayPageUrlRequest.php
+ * @Date: 2023/7/6
+ * @Time: 18:18
+ */
+
+
+namespace Modules\WechatPlatform\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class PlayPageUrlRequest extends FormRequest
+{
+    
+
+}

+ 2 - 2
modules/WechatPlatform/Http/Requests/WechatKeywordsRequest.php

@@ -38,7 +38,7 @@ class WechatKeywordsRequest extends FormRequest
                 }
             ],
             'miniprogram_id' => [
-                'required_if:type,miniprogram',
+                'required',
                 'Integer',
                 "gt:0",
             ],
@@ -57,7 +57,7 @@ class WechatKeywordsRequest extends FormRequest
         return [
             'type.required' => '关键词类型必须填写',
             'keyword.required' => '关键词必须填写',
-            "miniprogram_id.required_if" => "小程序必须填写",
+            "miniprogram_id.required" => "小程序必须填写",
             "content.required" => "内容必须填写",
             "content.array" => "内容格式不正确",
         ];

+ 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', 'appid', 'nick_name', 'sned_total', 'updated_at', 'created_at',
+        'id', 'weacht_keyworld_id', 'appid',"wechat_authorization_info_id", 'nick_name', 'keyword', 'content', 'status', 'send_total', 'updated_at', 'created_at',
     ];
 
 }

+ 2 - 3
modules/WechatPlatform/Models/WechatKeywords.php

@@ -10,8 +10,7 @@ class WechatKeywords extends BaseModel
     protected $table = 'wechat_keywords';
 
     protected $fillable = [
-        'id', 'keyword', 'user_id',"send_times", 'puser_id',"miniprogram_appid", 'miniprogram_id', 'type', 'content', 'is_del', 'del_at', 'updated_at', 'created_at',
+        'id', 'keyword','send_content', 'user_id',"send_total",'wechat_accounts', 'puser_id',"miniprogram_appid", 'miniprogram_id', 'type', 'content', 'is_del', 'del_at', 'updated_at', 'created_at',
     ];
-    protected $casts = ['content' => 'array'];
-
+    protected $casts = ['content' => 'array','wechat_accounts' => 'array'];
 }

+ 212 - 2
modules/WechatPlatform/Services/WechatKeywordsService.php

@@ -9,12 +9,16 @@
 
 namespace Modules\WechatPlatform\Services;
 
+use Illuminate\Support\Facades\DB;
 use Modules\Common\Services\BaseService;
+use Modules\WechatPlatform\Models\WechatAccountKeywordLog;
 use Modules\WechatPlatform\Models\WechatKeywords;
 
 class WechatKeywordsService extends BaseService
 {
 
+    protected static array $hideField = ['updated_at', 'is_del', 'del_at', 'user_id', 'puser_id', 'send_content']; // 公用的影藏字段
+
     /**
      *  添加关键字
      * name: addKeyword
@@ -23,10 +27,216 @@ class WechatKeywordsService extends BaseService
      */
     public static function addKeyword(mixed $param)
     {
-        $res =   WechatKeywords::create($param);
-        if ($res){
+        $res = WechatKeywords::create($param);
+        if ($res) {
             return "操作成功";
         }
         return "操作失败";
     }
+
+    /**
+     *  关键词列表
+     * name: getKeywordsList
+     * @param array $param
+     * date 2023/07/06 13:53
+     */
+    public static function getKeywordsList(array $param)
+    {
+        $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));
+        }
+        $list->makeHidden(array_merge(self::$hideField,['content']));
+        return $list;
+    }
+
+    private static function getQuery(array $param)
+    {
+        $sql = WechatKeywords::query()->where('is_del',0);
+        if (getProp($param, 'puser_id')) {
+            $sql->where('puser_id', $param['puser_id']);
+        }
+        if (getProp($param, 'user_id')) {
+            $sql->where('user_id', $param['user_id']);
+        }
+        if (getProp($param, 'keyword')) {
+            $sql->where('keyword', "like", "%" . $param['keyword'] . "%");
+        }
+        if(getProp($param,'wechat_authorization_info_id')){
+            $sql->whereJsonContains('wechat_accounts->id',$param['wechat_authorization_info_id']);
+        }
+        return $sql;
+    }
+
+    /**
+     *  详情
+     * name: detail
+     * @param $id
+     * date 2023/07/06 13:55
+     */
+    public static function detail($id)
+    {
+        return WechatKeywords::where('id', $id)->where('is_del', 0)->first()->makeHidden(array_merge(self::$hideField,['wechat_accounts']));
+    }
+
+    /**
+     *  保存关键字
+     * name: updateKeyWords
+     * @param $id
+     * @param mixed $param
+     * @return string
+     * date 2023/07/06 14:51
+     */
+    public static function updateKeyWords($id, mixed $param)
+    {
+        $info = WechatKeywords::where('id', $id)->where('is_del', 0)->first();
+        if (is_empty($info)) {
+            self::throwErrMsg('关键词不存在或已删除');
+        }
+        if ($param['keyword'] != $info->keyword) {
+            // 修改关键词,需要查询词关键词是否已配置
+            if ($info->wechat_accounts) {
+                $appIds = WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->where('status', 1)->pluck('appid')->get();
+                if ($appIds) {
+                    $hasUsed = WechatAccountKeywordLog::where('keyword', $param['keyword'])->whereIn('appid', $appIds)->where('status', 1)->pluck('nick_name')->get();
+                    if ($hasUsed) {
+                        self::throwErrMsg("关键字{$param['keyword']}已配置了以下公众号" . implode(',', $hasUsed) . "请先解绑后再编辑");
+                    }
+                }
+            }
+        }
+        DB::beginTransaction();
+        try {
+            $info->save($param);
+            $data = [
+                'content' => $param['send_content'],
+                'keyword' => $param['keyword'],
+            ];
+            WechatAccountKeywordLog::where('weacht_keyworld_id', $info->id)->update($data);
+            DB::commit();
+        } catch (\Exception $exception) {
+            DB::rollBack();
+            self::throwErrMsg('编辑失败');
+        }
+        return "操作成功";
+    }
+
+    /**
+     *  配置公众号列表
+     * name: WechaAccountAuthListInfo
+     * @param $id
+     * @param mixed $userId
+     * date 2023/07/06 15:31
+     */
+    public static function WechaAccountAuthListInfo($id, mixed $userId)
+    {
+        $list = DB::table('wechat_authorization_infos')
+            ->where('user_id', $userId)
+            ->select('id', 'nick_name', 'is_enabled')->get();
+        $authList = WechatAccountKeywordLog::where('weacht_keyworld_id', $id)->get();
+        if (!$list->isEmpty()) {
+            foreach ($list as $val) {
+                $val->is_auth = 0;
+                $info = $authList->where('wechat_authorization_info_id', $val->id)->first();
+                if (getProp($info, 'status') == 1) {
+                    $val->is_auth = 1;
+                }
+                if ($val->is_enabled != 1) {
+                    $val->nick_name .= "(取消授权)";
+                }
+                unset($val->is_enabled);
+            }
+        }
+        return $list;
+    }
+
+    /**
+     *   删除关键词
+     * name: delKeywords
+     * @param array $ids
+     * date 2023/07/06 15:54
+     */
+    public static function delKeywords(array $ids)
+    {
+        if (empty($ids)) {
+            self::throwErrMsg('要删除的数据不能为空');
+        }
+        DB::beginTransaction();
+        try {
+            WechatKeywords::query()->whereIn('id', $ids)->update(['is_del' => 1, 'del_at' => get_date()]);
+            WechatAccountKeywordLog::query()->whereIn('weacht_keyworld_id', $ids)->update(['status' => 0]);
+            DB::commit();
+        } catch (\Exception $exception) {
+            DB::rollBack();
+            self::throwErrMsg('删除失败');
+        }
+        return "操作成功";
+    }
+
+    public static function allocation($id, $wxAuthIds)
+    {
+        $info = WechatKeywords::where('id', $id)->where('is_del', 0)->first();
+        if (is_empty($info)) {
+            self::throwErrMsg('关键词不存在或已删除');
+        }
+        $data = [];
+        $list = [];
+        if (empty($wxAuthIds)){
+            $data['wechat_accounts'] = [];
+        }else{
+            $wechatAccountInfos = DB::table('wechat_authorization_infos')
+                ->whereIn('id',$wxAuthIds)
+                ->where('is_enabled',1)
+                ->where('user_id',$info->user_id)
+                ->get();
+            if($wechatAccountInfos->isEmpty()){
+                self::throwErrMsg("优化师对提交的公众号没有使用权限");
+            }
+
+            $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();
+            $canNotUsed = array_diff($wxAuthIds,$canNotUsed);
+            if (count($canNotUsed) > 0){
+                self::throwErrMsg("优化师对id:为:".implode(',',$canNotUsed)."的公众号没有使用权限");
+            }
+
+            foreach ($wechatAccountInfos as  $val){
+                $data['wechat_accounts'][] = [
+                    'id' =>  $val->id,
+                    'appid' =>$val->authorizer_appid,
+                    '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,
+                ];
+            }
+        }
+
+        DB::beginTransaction();
+        try {
+            if (!empty($list)){
+                foreach ($list as $val){
+                    WechatAccountKeywordLog::updateOrCreate(['weacht_keyworld_id' => $val['weacht_keyworld_id'],'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 "操作成功";
+    }
+
+
 }

+ 19 - 2
modules/WechatPlatform/routes/route.php

@@ -1,8 +1,10 @@
 <?php
 
 use Illuminate\Support\Facades\Route;
+
 use Modules\WechatPlatform\Http\Controllers\KFMessageController;
 use Modules\WechatPlatform\Http\Controllers\WechatAuthorizationInfoController;
+use Modules\WechatPlatform\Http\Controllers\CommonController;
 use Modules\WechatPlatform\Http\Controllers\WechatKeywordsController;
 
 Route::prefix('wechatPlatform')->group(function(){
@@ -15,6 +17,7 @@ Route::prefix('wechatPlatform')->group(function(){
         Route::post('updateGZH', [KFMessageController::class, 'updateGZH']);
     });
 
+
     // 微信公众号设置
     Route::prefix('officialAccount')->group(function () {
         //公众号列表
@@ -31,9 +34,23 @@ Route::prefix('wechatPlatform')->group(function(){
             // 详情
             Route::any('detail/{id}',[WechatKeywordsController::class,'detail']);
             // 删除
-            Route::any('del/{id}',[WechatKeywordsController::class,'del']);
+            Route::any('del',[WechatKeywordsController::class,'del']);
             // 配置公众号
-            Route::post('allocation/id',[WechatKeywordsController::class,'allocation']);
+            Route::post('allocation/{id}',[WechatKeywordsController::class,'allocation']);
+
+            // 关键字公账号配置选择项
+            Route::any("auth_list/{id}",[WechatKeywordsController::class,'authList']);
         });
+    });    
+    // 公共部分
+    Route::prefix('common')->group(function (){
+        // 获取小程序页面链接
+        Route::post('get_page',[CommonController::class,'getPageUrl'])->withoutMiddleware(config('catch.route.middlewares'));
     });
+
+    // 设置
+    Route::prefix('setting')->group(function (){
+
+    });
+
 });