浏览代码

Merge branch 'wx-kw' into feature-wechat

zqwang 1 年之前
父节点
当前提交
b9d2a7b487

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

@@ -130,13 +130,13 @@ class WechatOpenPlatformController extends CatchController
 
         $server = $openPlatform->server;
         $message = $server->getMessage();
-        // myLog("wx-xiaoxi")->info('-------开始处理---'.get_date());
-        // myLog("wx-xiaoxi")->info([
-        //     'authorizer_appid' => $authorizer_appid,
-        //     'component_appid' => $component_appid,
-        //     'param' => $request->all(),
-        //     'msg' => $message,
-        // ]);
+        myLog("wx-xiaoxi")->info('-------开始处理---'.get_date());
+        myLog("wx-xiaoxi")->info([
+            'authorizer_appid' => $authorizer_appid,
+            'component_appid' => $component_appid,
+            'param' => $request->all(),
+            'msg' => $message,
+        ]);
         $appInfo = WechatOpenPlatformService::getRefreshToken($authorizer_appid,$component_appid);
         $wechatAppId = getProp($appInfo,'id');
 
@@ -144,9 +144,9 @@ class WechatOpenPlatformController extends CatchController
         unset($appInfo);
 
         $app->server->push(function ($message) use($app,$authorizer_appid,$wechatAppId) {
-            // myLog("wx-xiaoxi")->info("----文本消息---");
-            // myLog("wx-xiaoxi")->info("体消息:");
-            // myLog("wx-xiaoxi")->info($message);
+            myLog("wx-xiaoxi")->info("----文本消息---");
+            myLog("wx-xiaoxi")->info("体消息:");
+            myLog("wx-xiaoxi")->info($message);
             return WechatCommonService::handleMessage($app,$wechatAppId ,$authorizer_appid,$message);
         });
         // myLog("wx-xiaoxi")->info('-------结束处理---'.get_date());

+ 33 - 28
modules/WechatPlatform/Http/Controllers/WechatMenuController.php

@@ -22,6 +22,7 @@ use Modules\WechatPlatform\Services\WechatMenuService;
 class WechatMenuController extends CatchController
 {
     use UserTrait;
+
     /**
      *  关键词列表
      * name: List
@@ -42,7 +43,7 @@ class WechatMenuController extends CatchController
     {
         $param = $request->validated();
         $param['msg_content'] = $request->input('msg_content');
-        $param  = $this->handleParam($param);
+        $param = $this->handleParam($param);
         $userContext = $this->getUserContext(null);
         $param['user_id'] = $userContext['loginUser']->id;
         $param['puser_id'] = $userContext['loginUser']->pid;
@@ -50,19 +51,21 @@ class WechatMenuController extends CatchController
         return WechatMenuService::addMenu($param);
     }
 
-    public function edit($id,WechatMenuRequest $request)
+    public function edit($id, WechatMenuRequest $request)
     {
         $param = $request->validated();
         $param['msg_content'] = $request->input('msg_content');
-        $param  = $this->handleParam($param);
-        return WechatMenuService::updateMenu($id,$param);
+        $param = $this->handleParam($param);
+        return WechatMenuService::updateMenu($id, $param);
     }
 
-    public function detail($id){
+    public function detail($id)
+    {
         return WechatMenuService::detail($id);
     }
 
-    public function authList($id){
+    public function authList($id)
+    {
         $userId = $this->getLoginUserId();
         return WechatMenuService::authList($id, $userId);
     }
@@ -84,7 +87,7 @@ class WechatMenuController extends CatchController
      * @param $id
      * date 2023/07/12 07:18
      */
-    public function  allocation($id,Request $request)
+    public function allocation($id, Request $request)
     {
         if (!$request->has('wx_auth_ids')) {
             WechatMenuService::throwErrMsg("参数错误");
@@ -94,9 +97,10 @@ class WechatMenuController extends CatchController
         } else {
             $wxAuthIds = [];
         }
-        return WechatMenuService::allocation($id,$wxAuthIds);
+        return WechatMenuService::allocation($id, $wxAuthIds);
     }
-        // 处理参数
+
+    // 处理参数
     private function handleParam($param)
     {
         $info = DB::table('miniprogram')->where('id', $param['miniprogram_id'])->first();
@@ -114,35 +118,40 @@ class WechatMenuController extends CatchController
         if (empty($info)) {
             WechatMenuService::throwErrMsg("没有此小程序的使用权限");
         }
-        if (count($param['content']) >3){
+        if (count($param['content']) > 3) {
             WechatMenuService::throwErrMsg("底部菜单不能超过3个");
         }
         // $clicks = [];
         $msgContent = [];
-        foreach ($param['content'] as  &$val){
-            if (!empty(getProp($val,'sub_button'))){
-                foreach ($val['sub_button'] as &$sub){
-                    if (getProp($sub,'type') == 'miniprogram'){
+        foreach ($param['content'] as &$val) {
+            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'){
+                    if (getProp($sub, 'type') == 'click') {
                         // $clicks[] = $sub['key'];
-                        $msgContent[] =[$sub['key'] =>  $sub['content']];
+                        $msgContent[] = [$sub['key'] => $sub['content']];
                     }
                 }
-            }else{
-                if (getProp($val,'type') == 'miniprogram'){
+            } else {
+                if (getProp($val, 'type') == 'miniprogram') {
                     $val['appid'] = $param['miniprogram_appid'];
                     $val['pagepath'] = $val['url'];
                 }
-                if (getProp($val,'type') == 'click'){
+                if (getProp($val, 'type') == 'click') {
                     // $clicks[] = $val['key'];
-                    $msgContent[] =[$val['key'] =>  $val['content']];
+                    $msgContent[] = [$val['key'] => $val['content']];
                 }
+                if (getProp($val, 'type') == 'view') {
+                    unset($val['url']);
+                }
+
             }
+
         }
-        $param['msg_content']= $msgContent;
+        $param['msg_content'] = $msgContent;
         // unset($val,$sub,$info);
         // // 有点击事件
         // if (!empty($clicks)){
@@ -179,12 +188,8 @@ class WechatMenuController extends CatchController
         return $param;
     }
 
-    public function test(){
-        $info = WechatOpenPlatformService::getAppInfoById(8);
-        $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($info->component_appid);
-        $openPlatform = WechatOpenPlatformService::buildApplication($componentInfo);
-        $app = $openPlatform->officialAccount($info->authorizer_appid, getProp($info,'authorizer_refresh_token'));
-        unset($appInfo);
-        return $app->menu->list();
+    public function test()
+    {
+        // return  WechatMenuService::getClickInfoContent(8,'1-3');
     }
 }

+ 2 - 0
modules/WechatPlatform/Services/WechatCommonService.php

@@ -200,6 +200,8 @@ class WechatCommonService extends BaseService
             case 'subscribe':
                 self::updateUserInfo($app, $appid, $message['FromUserName'],$wechatInfoId, true);
                 return WechatSubscribeService::getContent($wechatInfoId);
+            case 'CLICK':
+                return  WechatMenuService::getClickInfoContent($wechatInfoId,$message['EventKey']);
             default:
                 return '收到其它消息';
         }

+ 12 - 1
modules/WechatPlatform/Services/WechatKeywordsService.php

@@ -52,6 +52,13 @@ class WechatKeywordsService extends BaseService
             $list = $sql->paginate(getProp($param, 'limit', 10));
         }
         $list->makeHidden(self::$hideField);
+
+        foreach ($list as  $val){
+            $val->wechat_accounts =  WechatAccountKeywordLog::query()->where(
+                ['weacht_keyworld_id' => $val->id,
+                    'status' => 1
+                ])->select('wechat_authorization_info_id as id',"appid",'nick_name')->get();;
+        }
         return $list;
     }
 
@@ -71,7 +78,11 @@ class WechatKeywordsService extends BaseService
             $sql->where('miniprogram_id', $param['miniprogram_id']);
         }
         if (getProp($param, 'wechat_authorization_info_id')) {
-            $sql->whereJsonContains('wechat_accounts->id', $param['wechat_authorization_info_id']);
+            $ids = WechatAccountKeywordLog::query()->where([
+                'wechat_authorization_info_id' => $param['wechat_authorization_info_id'],
+                'status' => 1,
+            ])->pluck('weacht_keyworld_id')->toArray();
+            $sql->whereIn('id',$ids);
         }
         return $sql;
     }

+ 87 - 21
modules/WechatPlatform/Services/WechatMenuService.php

@@ -49,7 +49,6 @@ class WechatMenuService extends BaseService
         if (is_empty($info)) {
             self::throwErrMsg('该菜单不存在或已删除');
         }
-        $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('id')->toArray();
         WechatMenu::where('id', $id)->update($param);
         $data = [
             'type' => $param['type'],
@@ -58,34 +57,47 @@ class WechatMenuService extends BaseService
         ];
         WechatAccountMenuDetail::where('menu_id', $id)->update($data);
         // 更新公众号菜单
-        self::createWechatMenus($appIds);
+        $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('wechat_authorization_info_id')->toArray();
+        self::createWechatMenus($appIds,$id);
         return ['msg' => '操作成功'];
     }
 
     /**
-     *  创建微信菜单
+     * 创建微信菜单
      * name: createWechatMenus
      * @param array $appIds
+     * @param $menuId
      * @return bool
-     * date 2023/07/12 07:16
+     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
+     * @throws \GuzzleHttp\Exception\GuzzleException
+     * date 2023/07/12 14:29
      */
-    private static function createWechatMenus(array $appIds)
+    private static function createWechatMenus(array $appIds,$menuId)
     {
         if (empty($appIds)) {
             return true;
         }
-        $info = WechatAccountMenuDetail::query()->whereIn('id', $appIds)->get();
+        $info = WechatAccountMenuDetail::query()->whereIn('wechat_authorization_info_id', $appIds)->where('menu_id',$menuId)->get();
         foreach ($info as $val) {
-            $app = self::getofficialAccount($info->wechat_authorization_info_id);
+            $app = self::getofficialAccount($val->wechat_authorization_info_id);
             if ($val->wx_menuid) {
                 // 有菜单则删除菜单
-                $app->menu->delete($info->wx_menuid);
+                $app->menu->delete($val->wx_menuid);
             }
             $matchRule = [
-                "client_platform_type" => $info->type == "iso" ? "1" : "2",
+                "client_platform_type" => $val->type == "ios" ? "1" : "2",
             ];
-            $res = $app->menu->create($info->content, $matchRule);
+            $res = $app->menu->create($val->content, $matchRule);
+            myLog('wx-meun')->info(['res' => $res]);
+
+            $errcode = getProp($res,'errcode',"");
+            if (intval($errcode) ==  65303){
+                $res = $app->menu->create($val->content);
+            }
+            myLog('wx-meun')->info(['res' => $res]);
             $wxMenuId = getProp($res, 'menuid', "");
+            $res = $app->menu->create($val->content, $matchRule);
+            myLog('wx-meun')->info(['res' => $res,'mid' => $wxMenuId]);
             if ($wxMenuId) {
                 WechatAccountMenuDetail::query()->where('id', $val->id)->update(['wx_menuid' => $wxMenuId]);
             }
@@ -173,11 +185,11 @@ class WechatMenuService extends BaseService
 
             if ($val->wx_menuid) {
                 // 有菜单则删除菜单
-                $app = self::getofficialAccount($info->wechat_authorization_info_id);
+                $app = self::getofficialAccount($val->wechat_authorization_info_id);
                 $app->menu->delete($info->wx_menuid);
             }
 
-            WechatAccountMenuDetail::query()->where('id', $val->id)->update(['wx_menuid' => ""]);
+            WechatAccountMenuDetail::query()->where('wechat_authorization_info_id', $val->id)->update(['wx_menuid' => ""]);
         }
         return "操作成功";
     }
@@ -196,7 +208,7 @@ class WechatMenuService extends BaseService
             self::throwErrMsg('该菜单不存在或已删除');
         }
         // 已分配的
-        $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('id')->toArray();
+        $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('wechat_authorization_info_id')->toArray();
         $list = [];
         $delAppId = [];
         $createAddIp = [];
@@ -225,12 +237,8 @@ class WechatMenuService extends BaseService
                 }
             }
             // $allSet = WechatAccountMenuDetail::query()->where('user_id', $info->user_id)->get();
-
+            $appIds = [];
             foreach ($wechatAccountInfos as $val) {
-                if (!in_array($val->id, $appIds)) {
-                    $createAddIp[] = $val->id;
-                }
-
                 $data['wechat_accounts'][] = [
                     'id' => $val->id,
                     'appid' => $val->authorizer_appid,
@@ -263,6 +271,7 @@ class WechatMenuService extends BaseService
                             'miniprogram_id' => $val['miniprogram_id'],
                             'wechat_authorization_info_id' => $val['wechat_authorization_info_id']
                         ], $val);
+
                 }
             } else {
                 WechatAccountMenuDetail::where('menu_id', $id)->update(['status' => 0]);
@@ -275,11 +284,13 @@ class WechatMenuService extends BaseService
         } catch (\Exception $exception) {
             self::throwErrMsg('操作失败');
         }
+        $createAddIp = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('wechat_authorization_info_id')->toArray();
+        myLog('wx-meun')->info(['c' => $createAddIp,'del' => $delAppId]);
         if (!empty($createAddIp)) {
-            self::createWechatMenus($createAddIp);
+            self::createWechatMenus($createAddIp,$id);
         }
         if (!empty($delAppId)) {
-            self::delWechatAccountMenu($delAppId);
+            self::delWechatAccountMenuByMeunId($delAppId,$id);
         }
         return ['msg' => "操作成功"];
     }
@@ -299,6 +310,12 @@ class WechatMenuService extends BaseService
         } else {
             $list = $sql->paginate(getProp($param, 'limit', 10));
         }
+        foreach ($list as $val){
+            $val->wechat_accounts =  WechatAccountMenuDetail::query()->where(
+                ['subscribe_id' => $val->id,
+                    'status' => 1
+                ])->select('wechat_authorization_info_id as id',"appid",'nick_name')->get();
+        }
         return $list;
     }
 
@@ -318,9 +335,58 @@ class WechatMenuService extends BaseService
             $sql->where('miniprogram_id', $param['miniprogram_id']);
         }
         if (getProp($param, 'wechat_authorization_info_id')) {
-            $sql->whereJsonContains('wechat_accounts->id', $param['wechat_authorization_info_id']);
+            $ids = WechatAccountMenuDetail::query()->where([
+                'wechat_authorization_info_id' => $param['wechat_authorization_info_id'],
+                'status' => 1,
+            ])->pluck('weacht_keyworld_id')->toArray();
         }
         return $sql;
     }
 
+    private static function delWechatAccountMenuByMeunId(array $delAppId, $menuId)
+    {
+        $info = WechatAccountMenuDetail::query()->whereIn('wechat_authorization_info_id', $delAppId)->where('menu_id',$menuId)->get();
+
+        foreach ($info as $val) {
+
+            if ($val->wx_menuid) {
+                // 有菜单则删除菜单
+                $app = self::getofficialAccount($info->wechat_authorization_info_id);
+                $app->menu->delete($val->wx_menuid);
+            }
+
+            WechatAccountMenuDetail::query()->where('wechat_authorization_info_id', $val->id)->update(['wx_menuid' => ""]);
+        }
+        return "操作成功";
+    }
+
+    /**
+     *  点击事件回复
+     * name: getClickInfoContent
+     * @param $wechatInfoId
+     * date 2023/07/12 16:37
+     */
+    public static function getClickInfoContent($wechatAppId,$key)
+    {
+        $info = WechatAccountMenuDetail::query()->where([
+            'status' => 1,
+            'wechat_authorization_info_id' => $wechatAppId,
+        ])->first();
+        if (is_empty($info)){
+            return "";
+        }
+        $content  = $info->msg_content;
+        if (empty($content)){
+            return  "";
+        }
+        foreach ($content as  $val){
+
+            if (isset($val[$key])){
+               $content  =  $val[$key];
+               break ;
+            }
+        }
+        return  $content ?: "";
+    }
+
 }

+ 11 - 1
modules/WechatPlatform/Services/WechatSubscribeService.php

@@ -241,6 +241,12 @@ class WechatSubscribeService extends BaseService
             $list = $sql->paginate(getProp($param,'limit',10));
         }
         $list->makeHidden(array_merge(self::$hideField));
+        foreach ($list as  $val){
+            $val->wechat_accounts =  WechatAccountSubscribeDetail::query()->where(
+                ['subscribe_id' => $val->id,
+                    'status' => 1
+                ])->select('wechat_authorization_info_id as id',"appid",'nick_name')->get();
+        }
         return $list;
     }
 
@@ -260,7 +266,11 @@ class WechatSubscribeService extends BaseService
             $sql->where('miniprogram_id',  $param['miniprogram_id']);
         }
         if(getProp($param,'wechat_authorization_info_id')){
-            $sql->whereJsonContains('wechat_accounts->id',$param['wechat_authorization_info_id']);
+            $ids = WechatAccountSubscribeDetail::query()->where([
+                    'wechat_authorization_info_id' => $param['wechat_authorization_info_id'],
+                    'status' => 1,
+                ])->pluck('weacht_keyworld_id')->toArray();
+            $sql->whereIn('id',$ids);
         }
         return $sql;
     }