|
|
@@ -0,0 +1,114 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace business\controllers;
|
|
|
+
|
|
|
+use common\components\configDict;
|
|
|
+use common\components\util;
|
|
|
+use common\components\weixinUtil;
|
|
|
+use common\services\xhWxMenuService;
|
|
|
+use Yii;
|
|
|
+
|
|
|
+class WxMenuController extends BaseController
|
|
|
+{
|
|
|
+
|
|
|
+ //更新菜单
|
|
|
+ public function actionReplace()
|
|
|
+ {
|
|
|
+ //xhWxMenuService::delMenuList($this->merchantId);
|
|
|
+ $now = time();
|
|
|
+ $date = date("Y-m-d H:i:s", $now);
|
|
|
+
|
|
|
+ $submitToWx = isset($post['submitToWx']) ? $post['submitToWx'] : '';
|
|
|
+ $level1String = '0◇◇0◇◇商城◇◇0◇◇1◇◇0◆◆1◇◇0◇◇申请接入◇◇1◇◇http://o.huahb.com/join/apply◇◇0◆◆2◇◇1◇◇申请公众号◇◇1◇◇http://o.huahb.com/main/apply◇◇0◆◆';
|
|
|
+ $level2String = '0◇◇2◇◇子菜单名2◇◇0◇◇2◇◇0◆◆1◇◇2◇◇子菜单名◇◇0◇◇2◇◇0◆◆';
|
|
|
+ $level1String = rtrim($level1String, '◆◆');
|
|
|
+ $level1 = explode('◆◆', $level1String);
|
|
|
+ $parentIdList = [];//记录一级菜单的数据库id
|
|
|
+
|
|
|
+ foreach ($level1 as $key => $val) {//一级菜单数据整理
|
|
|
+ $arr = explode('◇◇', $val);
|
|
|
+ $menuOrder = $arr[0];//一级菜单序号
|
|
|
+ $wxMenuOption = $arr[3];//选项
|
|
|
+ $wxMenuContent = $arr[4];//内容
|
|
|
+ $addData = [];
|
|
|
+ $addData['parentId'] = 0;
|
|
|
+ $addData['merchantId'] = $this->merchantId;
|
|
|
+ $addData['menuOrder'] = $menuOrder;
|
|
|
+ $addData['menuOption'] = $wxMenuOption;
|
|
|
+ $addData['hasSonMenu'] = $arr[1];
|
|
|
+ $addData['name'] = $arr[2];
|
|
|
+ $addData['menuKey'] = $this->merchantId . '_' . $menuOrder;
|
|
|
+ $addData['createTime'] = $date;
|
|
|
+ $addData['replyType'] = $arr[5];
|
|
|
+ if (!empty($wxMenuContent)) {
|
|
|
+ $formatData = xhWxMenuService::getFormatData($wxMenuContent, $wxMenuOption, $this->merchant);
|
|
|
+ $addData = array_merge($addData, $formatData);
|
|
|
+ }
|
|
|
+ $return = xhWxMenuService::add($addData);
|
|
|
+ $parentIdList[$menuOrder] = $return['id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($level2String)) {
|
|
|
+ $level2String = rtrim($level2String, '◆◆');
|
|
|
+ $level2 = explode('◆◆', $level2String);
|
|
|
+ foreach ($level2 as $key => $val) {//二级菜单数据整理
|
|
|
+ $arr = explode('◇◇', $val);
|
|
|
+ $menuOrder = $arr[0];
|
|
|
+ $parentMenuOrder = $arr[1];
|
|
|
+ $wxMenuOption = $arr[3];
|
|
|
+ $wxMenuContent = $arr[4];
|
|
|
+ $addData['parentId'] = $parentIdList[$parentMenuOrder];
|
|
|
+ $addData['merchantId'] = $this->merchantId;
|
|
|
+ $addData['menuOrder'] = $menuOrder;
|
|
|
+ $addData['menuOption'] = $wxMenuOption;
|
|
|
+ $addData['name'] = $arr[2];
|
|
|
+ $addData['menuKey'] = $this->merchantId . '_' . $parentMenuOrder . '_' . $menuOrder;
|
|
|
+ $addData['createTime'] = $date;
|
|
|
+ $addData['replyType'] = $arr[5];
|
|
|
+ if (!empty($wxMenuContent)) {
|
|
|
+ $formatData = xhWxMenuService::getFormatData($wxMenuContent, $wxMenuOption, $this->merchant);
|
|
|
+ $addData = array_merge($addData, $formatData);
|
|
|
+ }
|
|
|
+ xhWxMenuService::add($addData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($submitToWx)) {
|
|
|
+ util::successInfo();
|
|
|
+ }
|
|
|
+ $wxMenu = xhWxMenuService::getMenuList($this->merchantId);
|
|
|
+ if (empty($wxMenu)) {
|
|
|
+ util::failInfo();
|
|
|
+ }
|
|
|
+ $replyType = configDict::getConfig('replyType');
|
|
|
+ $menuData = [];
|
|
|
+ foreach ($wxMenu as $wxKey => $wxVal) {
|
|
|
+ $child = $wxVal['child'];
|
|
|
+ if (!empty($child)) {
|
|
|
+ $menuData[$wxKey] = ['type' => 'view', 'name' => $wxVal['name']];
|
|
|
+ foreach ($child as $chKey => $chVal) {
|
|
|
+ if ($chVal['replyType'] == $replyType['redirectUrl']) {
|
|
|
+ $menuData[$wxKey]['sub_button'][$chKey] = ['type' => 'view', 'name' => $chVal['name'], 'url' => $chVal['replyUrl']];
|
|
|
+ } else {
|
|
|
+ $menuData[$wxKey]['sub_button'][$chKey] = ['type' => 'click', 'name' => $chVal['name'], 'key' => $chVal['menuKey']];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if ($wxVal['replyType'] == $replyType['redirectUrl']) {
|
|
|
+ $menuData[$wxKey] = ['type' => 'view', 'name' => $wxVal['name'], 'url' => $wxVal['replyUrl']];
|
|
|
+ } else {
|
|
|
+ $menuData[$wxKey] = ['type' => 'click', 'name' => $wxVal['name'], 'key' => $wxVal['menuKey']];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $subData['button'] = $menuData;
|
|
|
+ $wxUtil = new weixinUtil($this->merchant);
|
|
|
+ $json = $wxUtil->userMenuToWeiXin($subData);
|
|
|
+ $r = json_decode($json, true);
|
|
|
+ if ($r['errcode'] == 0) {
|
|
|
+ util::ok('操作成功,5分钟后生效');
|
|
|
+ }
|
|
|
+ util::fail('发布失败');
|
|
|
+ }
|
|
|
+
|
|
|
+}
|