浏览代码

菜单更新

zqwang 1 年之前
父节点
当前提交
572324e5cf

+ 100 - 23
modules/WechatPlatform/Http/Controllers/WechatMenuController.php

@@ -39,12 +39,61 @@ class WechatMenuController extends CatchController
 
     public function add(WechatMenuRequest $request)
     {
-        $param  = $this->handelParam($request->validated());
 
+        $param  = $this->handleParam($request->validated());
+        $userContext = $this->getUserContext(null);
+        $param['user_id'] = $userContext['loginUser']->id;
+        $param['puser_id'] = $userContext['loginUser']->pid;
+        $param['wechat_accounts'] = [];
+        return WechatMenuService::addMenu($param);
+    }
+
+    public function edit($id,WechatMenuRequest $request)
+    {
+        $param  = $this->handleParam($request->validated());
+        return WechatMenuService::updateMenu($id,$param);
+    }
+
+    public function detail($id){
+        return WechatMenuService::detail($id);
     }
 
-    // 处理参数
-    private function handelParam($param)
+    public function authList($id){
+        $userId = $this->getLoginUserId();
+        return WechatMenuService::authList($id, $userId);
+    }
+
+    public function del(Request $request)
+    {
+        $ids = $request->input('ids');
+        if (empty($ids)) {
+            WechatMenuService::throwErrMsg('要删除的数据参数错误');
+        }
+
+        $ids = explode(',', $ids);
+        return WechatMenuService::del($ids);
+    }
+
+    /**
+     *  分配
+     * name: allocation
+     * @param $id
+     * date 2023/07/12 07:18
+     */
+    public function  allocation($id,Request $request)
+    {
+        if (!$request->has('wx_auth_ids')) {
+            WechatMenuService::throwErrMsg("参数错误");
+        }
+        if (!empty($request->input('wx_auth_ids'))) {
+            $wxAuthIds = explode(',', $request->input('wx_auth_ids'));
+        } else {
+            $wxAuthIds = [];
+        }
+        return WechatMenuService::allocation($id,$wxAuthIds);
+    }
+        // 处理参数
+    private function handleParam($param)
     {
         $info = DB::table('miniprogram')->where('id', $param['miniprogram_id'])->first();
         if (empty($info)) {
@@ -61,35 +110,63 @@ class WechatMenuController extends CatchController
         if (empty($info)) {
             WechatMenuService::throwErrMsg("没有此小程序的使用权限");
         }
-        $errs = "";
         if (count($param['content']) >3){
             WechatMenuService::throwErrMsg("底部菜单不能超过3个");
         }
-        $menus = [];
+        $clicks = [];
         foreach ($param['content'] as  $val){
-            $temp = ['name' => $val['name']];
-            if (getProp($val,'sub_button')){
-                // 有二级菜单
-                if (count($val['sub_button']) > 5){
-                    $errs .= getProp($val,'name')."的二级菜单超过5个";
-                    continue ;
-                }
-                foreach ($val['sub_button'] as   $sub){
-                    $res = $this->checkMenuItem($sub);
+            if (!empty(getProp($val,'sub_button'))){
+                foreach ($val['sub_button'] as $sub){
+                    if (getProp($sub,'type') == 'miniprogram'){
+                        $sub['appid'] = $param['miniprogram_appid'];
+                        $sub['pagepath'] = $sub['url'];
+                    }
+                    if (getProp($sub,'type') == 'click'){
+                        $clicks[] = $sub['key'];
+                    }
                 }
             }else{
-                // 一级菜单
-                $res = $this->checkMenuItem($sub);
+                if (getProp($val,'type') == 'miniprogram'){
+                    $val['appid'] = $param['miniprogram_appid'];
+                    $val['pagepath'] = $val['url'];
+                }
+                if (getProp($val,'type') == 'click'){
+                    $clicks[] = $val['key'];
+                }
             }
         }
-    }
-
-    private function checkMenuItem($sub)
-    {
-        $type = getProp($sub,'type');
-        if (!in_array($type,["click",'miniprogram','scancode_waitmsg'])){
-            return ['status' => 0,'msg' =>  "类型非法"];
+        unset($val,$sub,$info);
+        $param['msg_content']= [];
+        // 有点击事件
+        if (!empty($clicks)){
+            $msgContent = [];
+            $msg = getProp($param,'msg_content');
+            if (empty($msg) || !is_array($msg)){
+                WechatMenuService::throwErrMsg("发送的文本信息不能为空");
+            }
+            foreach ($clicks as $val){
+                $temp = getProp($msg,$val);
+                if (empty($temp) || !is_array($temp)){
+                    WechatMenuService::throwErrMsg("发送的文本信息格式不正确");
+                }
+                $contents = "";
+                foreach ($temp as  & $item){
+                    if (getProp($item,'url')){
+                        $item['content'] = "<a href=\"\" data-miniprogram-appid=\"{$param['miniprogram_appid']}\" data-miniprogram-path=\"{$val['url']}\">{$val['title']}</a>";
+                    }else{
+                        $item['content'] = $item['tittle'];
+                    }
+                    $contents .=  $item['content']."\n";
+                }
+                rtrim($contents, "\n");
+                $temp['content'] = $contents;
+                $msgContent = [$val => $temp];
+            }
+            $param['msg_content'] = $msgContent;
+            unset($msgContent);
+            unset($temp,$val,$item);
         }
+        return $param;
     }
 
     public function test(){

+ 7 - 3
modules/WechatPlatform/Http/Requests/WechatMenuRequest.php

@@ -9,11 +9,13 @@
 
 namespace Modules\WechatPlatform\Http\Requests;
 
+
 use Illuminate\Foundation\Http\FormRequest;
 use Modules\WechatPlatform\Services\WechatMenuService;
 
 class WechatMenuRequest extends FormRequest
 {
+
     /**
      * rules
      *
@@ -22,13 +24,13 @@ class WechatMenuRequest extends FormRequest
     public function rules(): array
     {
         return [
+            'title' => "required",
             'type' => [
                 "required",
                 function ($attribute, $value, $fail) {
                     $types = array_column(WechatMenuService::getWechatMenuSystemType(), 'value');
                     if (!in_array($value, $types)) {
-                        $fail("系统类型不正确!");
-                        exit();
+                        $fail("关键词类型不正确!");
                     }
                 }
             ],
@@ -37,7 +39,7 @@ class WechatMenuRequest extends FormRequest
                 'Integer',
                 "gt:0",
             ],
-            "content" =>   "required|array",
+            "content" => "required|array",
         ];
     }
 
@@ -50,6 +52,7 @@ class WechatMenuRequest extends FormRequest
     public function messages(): array
     {
         return [
+            'title.required' => '菜单名称必须填写',
             'type.required' => '系统类型必须填写',
             "miniprogram_id.required" => "小程序必须填写",
             "content.required" => "菜单必须填写",
@@ -59,3 +62,4 @@ class WechatMenuRequest extends FormRequest
 }
 
 
+

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

@@ -12,5 +12,4 @@ class WechatAccountKeywordLog extends BaseModel
     protected $fillable = [
         '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',
     ];
-
 }

+ 0 - 1
modules/WechatPlatform/Models/WechatAccountSubscribeDetail.php

@@ -11,5 +11,4 @@ class WechatAccountSubscribeDetail extends BaseModel
     protected $fillable = [
         'id', 'user_id', 'puser_id', 'subscribe_id', 'appid', 'wechat_authorization_info_id', 'miniprogram_id', 'nick_name', 'content', 'status', 'send_total', 'updated_at', 'created_at',
     ];
-
 }

+ 224 - 0
modules/WechatPlatform/Services/WechatMenuService.php

@@ -9,10 +9,16 @@
 
 namespace Modules\WechatPlatform\Services;
 
+use Illuminate\Support\Facades\DB;
 use Modules\Common\Services\BaseService;
+use Modules\WechatPlatform\Models\WechatAccountMenuDetail;
+use Modules\WechatPlatform\Models\WechatMenu;
+use function PHPUnit\Framework\isNan;
 
 class WechatMenuService extends BaseService
 {
+
+
     /**
      *  添加菜单
      * name: addMenu
@@ -21,8 +27,226 @@ class WechatMenuService extends BaseService
      */
     public static function addMenu($param)
     {
+        $res = WechatMenu::create($param);
+        if ($res) {
+            return ['msg' => '操作成功'];
+        }
+        self::throwErrMsg('操作失败');
+    }
+
+    /**
+     *  更新菜单
+     * name: updateMenu
+     * @param $id
+     * @param $param
+     * @return string[]
+     * date 2023/07/12 07:17
+     */
+    public static function updateMenu($id, $param)
+    {
+        $info = WechatMenu::where('id', $id)->where('is_del', 0)->first();
+        if (is_empty($info)) {
+            self::throwErrMsg('该菜单不存在或已删除');
+        }
+        $appIds = WechatAccountMenuDetail::where(['status' => 1, 'meun_id' => $info->id])->pluck('id')->toArray();
+        WechatMenu::query()->where('id', $id)->update($param);
+        $data = [
+            'type' => $param['type'],
+            'content' => $param['content'],
+            'msg_content' => $param['msg_content']
+        ];
+        WechatAccountMenuDetail::where('meun_id', $id)->update($data);
+        // 更新公众号菜单
+        self::createWechatMenus($appIds);
+        return ['msg' => '操作成功'];
+    }
+
+    /**
+     *  创建微信菜单
+     * name: createWechatMenus
+     * @param array $appIds
+     * @return bool
+     * date 2023/07/12 07:16
+     */
+    private static function createWechatMenus(array $appIds)
+    {
+        if (empty($appIds)) {
+            return true;
+        }
+        $info = WechatAccountMenuDetail::query()->whereIn('id', $appIds)->get();
+        foreach ($info as $val) {
+
+        }
+        return true;
+    }
 
+    /**
+     * 菜单详情
+     * name: detail
+     * @param $id
+     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
+     * date 2023/07/12 07:17
+     */
+    public static function detail($id)
+    {
+        return WechatMenu::query()->where('id', $id)->first();
     }
 
+    /**
+     *  公众号列表
+     * name: authList
+     * @param $id
+     * @param $userId
+     * @return mixed
+     * date 2023/07/12 07:16
+     */
+    public static function authList($id, $userId)
+    {
+        $list = DB::table('wechat_authorization_infos')
+            ->where('user_id', $userId)
+            ->select('id', 'nick_name', 'is_enabled')->get();
+        $authList = WechatAccountMenuDetail::where('menu_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: del
+     * @param $ids
+     * @return string[]
+     * date 2023/07/12 07:14
+     */
+    public static function del($ids)
+    {
+        WechatMenu::query()->whereIn('id', $ids)->update(['is_del' => 1, 'del_at' => get_date()]);
+        $applyIds = WechatAccountMenuDetail::query()->whereIn('menu_id', $ids)->where('status', 1)->pluck('id')->toArray();
+        if ($applyIds) {
+            WechatAccountMenuDetail::query()->whereIn('menu_id', $ids)->update(['status' => 0]);
+            // 删除菜单
+            self::delWechatAccountMenu($applyIds);
+        }
+        return ['msg' => "操作成功"];
+
+    }
+
+    private static function delWechatAccountMenu(array $applyIds)
+    {
+        $info = WechatAccountMenuDetail::query()->whereIn('id', $applyIds)->get();
+        foreach ($info as $val) {
+
+        }
+        return "操作成功";
+    }
+
+    /**
+     *  分配公众号
+     * name: allocation
+     * @param $id
+     * @param array $wxAuthIds
+     * date 2023/07/12 07:20
+     */
+    public static function allocation($id, $wxAuthIds)
+    {
+        $info = WechatMenu::query()->where('id', $id)->where('is_del', 0)->first();
+        if (is_empty($info)) {
+            self::throwErrMsg('该菜单不存在或已删除');
+        }
+        // 已分配的
+        $appIds = WechatAccountMenuDetail::where(['status' => 1, 'meun_id' => $info->id])->pluck('id')->toArray();
+        $list = [];
+        $delAppId = [];
+        $createAddIp  = [];
+
+        if (empty($wxAuthIds)) {
+            $data['wechat_accounts'] = [];
+            $delAppId = $appIds;
+        } 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 ($appIds as  $val){
+                if (!in_array($val,$wxAuthIds)){
+                    $delAppId[] = $val;
+                }
+            }
+            $allSet = WechatAccountMenuDetail::query()->where('user_id', $info->user_id)->get();
+            foreach ($wechatAccountInfos as $val) {
+                if (in_array($val->id,$appIds)){
+                    $createAddIp[] = $val->id;
+                }
+                $data['wechat_accounts'][] = [
+                    'id' => $val->id,
+                    'appid' => $val->authorizer_appid,
+                    'nick_name' => $val->nick_name,
+                    'component_appid' => $val->component_appid
+                ];
+                $appIds[] = $val->authorizer_appid;
+                $list[] = [
+                    'meun_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,
+                    'content' => $info->content,
+                    'msg_content' =>  $info->msg_content,
+                    'status' => 1,
+                ];
+            }
+            unset($wechatAccountInfos,$appIds,$val,$canNotUsed);
+        }
+        DB::beginTransaction();
+        try {
+            if (!empty($delAppId)){
+                WechatAccountMenuDetail::whereIn('id',$delAppId)->update(['status' => 0]);
+                foreach ($list as $val){
+                    WechatAccountMenuDetail::updateOrCreate(
+                        [
+                            'meun_id' => $val['meun_id'],
+                            'miniprogram_id' => $val['miniprogram_id'],
+                            'wechat_authorization_info_id' => $val['wechat_authorization_info_id']
+                        ],$val);
+                }
+            }else{
+                WechatAccountMenuDetail::where('meun_id',$id)->update(['status' => 0]);
+            }
+            WechatMenu::query()->where('id',$id)->update($data);
+            DB::commit();
+        }catch (\Exception $exception){
+            self::throwErrMsg('操作失败');
+        }
+        if (!empty($createAddIp)){
+            self::createWechatMenus($createAddIp);
+        }
+        if(!empty($delAppId)){
+            self::delWechatAccountMenu($delAppId);
+        }
+        return ['msg' => "操作成功"];
+    }
 
 }

+ 0 - 2
storage/logs/.gitignore

@@ -1,2 +0,0 @@
-*
-!.gitignore