소스 검색

公众号菜单生效

zqwang 1 년 전
부모
커밋
3a4d48a384
1개의 변경된 파일59개의 추가작업 그리고 28개의 파일을 삭제
  1. 59 28
      modules/WechatPlatform/Services/WechatMenuService.php

+ 59 - 28
modules/WechatPlatform/Services/WechatMenuService.php

@@ -10,6 +10,7 @@
 namespace Modules\WechatPlatform\Services;
 
 use Illuminate\Support\Facades\DB;
+use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
 use Modules\Common\Services\BaseService;
 use Modules\WechatPlatform\Models\WechatAccountMenuDetail;
 use Modules\WechatPlatform\Models\WechatMenu;
@@ -75,11 +76,34 @@ class WechatMenuService extends BaseService
         }
         $info = WechatAccountMenuDetail::query()->whereIn('id', $appIds)->get();
         foreach ($info as $val) {
-
+            $app = self::getofficialAccount($info->wechat_authorization_info_id);
+            if ($val->wx_menuid) {
+                // 有菜单则删除菜单
+                $app->menu->delete($info->wx_menuid);
+            }
+            $matchRule = [
+                "client_platform_type" => $info->type == "iso" ? "1" : "2",
+            ];
+            $res = $app->menu->create($info->content, $matchRule);
+            $wxMenuId = getProp($res, 'menuid', "");
+            if ($wxMenuId) {
+                WechatAccountMenuDetail::query()->where('id', $val->id)->update(['wx_menuid' => $wxMenuId]);
+            }
         }
         return true;
     }
 
+    public static function getofficialAccount($id)
+    {
+        $info = WechatOpenPlatformService::getAppInfoById($id);
+        $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;
+    }
+
+
     /**
      * 菜单详情
      * name: detail
@@ -147,6 +171,13 @@ class WechatMenuService extends BaseService
         $info = WechatAccountMenuDetail::query()->whereIn('id', $applyIds)->get();
         foreach ($info as $val) {
 
+            if ($val->wx_menuid) {
+                // 有菜单则删除菜单
+                $app = self::getofficialAccount($info->wechat_authorization_info_id);
+                $app->menu->delete($info->wx_menuid);
+            }
+
+            WechatAccountMenuDetail::query()->where('id', $val->id)->update(['wx_menuid' => ""]);
         }
         return "操作成功";
     }
@@ -168,7 +199,7 @@ class WechatMenuService extends BaseService
         $appIds = WechatAccountMenuDetail::where(['status' => 1, 'menu_id' => $info->id])->pluck('id')->toArray();
         $list = [];
         $delAppId = [];
-        $createAddIp  = [];
+        $createAddIp = [];
 
         if (empty($wxAuthIds)) {
             $data['wechat_accounts'] = [];
@@ -188,15 +219,15 @@ class WechatMenuService extends BaseService
             if (count($canNotUsed) > 0) {
                 self::throwErrMsg("优化师对id:为:" . implode(',', $canNotUsed) . "的公众号没有使用权限");
             }
-            foreach ($appIds as  $val){
-                if (!in_array($val,$wxAuthIds)){
+            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)){
+                if (!in_array($val->id, $appIds)) {
                     $createAddIp[] = $val->id;
                 }
 
@@ -216,38 +247,38 @@ class WechatMenuService extends BaseService
                     'wechat_authorization_info_id' => $val->id,
                     'nick_name' => $val->nick_name,
                     'content' => $info->content,
-                    'msg_content' =>  $info->msg_content,
+                    'msg_content' => $info->msg_content,
                     'status' => 1,
                 ];
             }
-            unset($wechatAccountInfos,$appIds,$val,$canNotUsed);
+            unset($wechatAccountInfos, $appIds, $val, $canNotUsed);
         }
         DB::beginTransaction();
         try {
-            if (!empty($list)){
-                foreach ($list as $val){
+            if (!empty($list)) {
+                foreach ($list as $val) {
                     WechatAccountMenuDetail::updateOrCreate(
                         [
                             'menu_id' => $val['menu_id'],
                             'miniprogram_id' => $val['miniprogram_id'],
                             'wechat_authorization_info_id' => $val['wechat_authorization_info_id']
-                        ],$val);
+                        ], $val);
                 }
-            }else{
-                WechatAccountMenuDetail::where('menu_id',$id)->update(['status' => 0]);
+            } else {
+                WechatAccountMenuDetail::where('menu_id', $id)->update(['status' => 0]);
             }
-            if(!empty($delAppId)){
-                WechatAccountMenuDetail::whereIn('id',$delAppId)->update(['status' => 0]);
+            if (!empty($delAppId)) {
+                WechatAccountMenuDetail::whereIn('id', $delAppId)->update(['status' => 0]);
             }
-            WechatMenu::query()->where('id',$id)->update($data);
+            WechatMenu::query()->where('id', $id)->update($data);
             DB::commit();
-        }catch (\Exception $exception){
+        } catch (\Exception $exception) {
             self::throwErrMsg('操作失败');
         }
-        if (!empty($createAddIp)){
+        if (!empty($createAddIp)) {
             self::createWechatMenus($createAddIp);
         }
-        if(!empty($delAppId)){
+        if (!empty($delAppId)) {
             self::delWechatAccountMenu($delAppId);
         }
         return ['msg' => "操作成功"];
@@ -261,19 +292,19 @@ class WechatMenuService extends BaseService
      */
     public static function list(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));
         }
         return $list;
     }
 
     private static function getQuery(array $param)
     {
-        $sql = WechatMenu::query()->where('is_del',0);
+        $sql = WechatMenu::query()->where('is_del', 0);
         if (getProp($param, 'puser_id')) {
             $sql->where('puser_id', $param['puser_id']);
         }
@@ -283,11 +314,11 @@ class WechatMenuService 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;
     }