| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace common\services;
- use biz\message\services\TMessageService;
- use biz\wx\services\WxOpenService;
- use Yii;
- use common\components\wxUtil;
- use common\components\configDict;
- use common\models\xhWxMenu;
- class xhWxMenuService
- {
-
- /**
- * 初始化菜单
- */
- public static function applyInit($merchant, $isOpen = 0)
- {
- $merchantId = isset($merchant['id']) ? $merchant['id'] : 0;
- //删除之前存在的菜单
- self::delMenuList($merchantId);
- $menuOption = configDict::getConfig('wxMenuOption');
- $mallDomain = Yii::$app->params['mallDomain'];
- $shopDomain = Yii::$app->params['shopDomain'];
- //强制转成https方式
- if (strpos($mallDomain, 'https') === false) {
- $mallDomain = str_replace('http', 'https', $mallDomain);
- $shopDomain = str_replace('http', 'https', $shopDomain);
- }
- if ($isOpen == 0) {
- // $button = [
- // ["type" => 'view', "name" => "鲜花速递", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => $mallDomain . '/#/?account=' . $merchantId,],
- // ["type" => 'view', "name" => "会员服务", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => $mallDomain . '/#/?account=' . $merchantId,],
- // ["type" => 'view', "name" => "我的订单", "menuOption" => $menuOption['redirectToCommonUseUrl'], "url" => $mallDomain . '/#/?account=' . $merchantId,],
- // ];
- $button = [
- ["type" => 'miniprogram', "name" => "鲜花速递", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/index", "url" => $mallDomain . '/#/?account=' . $merchantId,],
- ["type" => 'miniprogram', "name" => "会员服务", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/user", "url" => $mallDomain . '/#/?account=' . $merchantId,],
- ["type" => 'miniprogram', "name" => "我的订单", "appid" => $merchant['miniAppId'], "pagepath" => "pages/order/list", "url" => $mallDomain . '/#/?account=' . $merchantId,],
- ];
-
- } else {
- // $button = [
- // ["type" => "view", "name" => "官网", "menuOption" => $menuOption['redirectToCustomUrl'], "url" => $mallDomain . '/#/?account=' . $merchantId,],
- // ["type" => "view", "name" => "后台", "menuOption" => $menuOption['redirectToCustomUrl'], "url" => $mallDomain . '/#/?account=' . $merchantId,],
- // ];
- $button = [
- ["type" => 'miniprogram', "name" => "官网", "appid" => $merchant['miniAppId'], "pagepath" => "admin/home/console", "url" => $mallDomain . '/#/?account=' . $merchantId,],
- ["type" => 'miniprogram', "name" => "申请", "appid" => $merchant['miniAppId'], "pagepath" => "admin/home/console", "url" => $mallDomain . '/#/?account=' . $merchantId,],
- ["type" => 'miniprogram', "name" => "后台", "appid" => $merchant['miniAppId'], "pagepath" => "admin/home/console", "url" => $mallDomain . '/#/?account=' . $merchantId,],
- ];
- }
- $wxUtil = new wxUtil($merchant);
- $menuList = $button;
- foreach ($button as $key => $val) {
- unset($button[$key]['keyName']);
- }
- $menu['button'] = $button;
- $info = $wxUtil->userMenuToWeiXin($menu);
- $info = json_decode($info, true);
- print_r($info);
- die;
- xhMerchantExtendService::updateByMerchantId($merchantId, ['wxMenuInit' => 1]);
- $now = time();
- $date = date("Y-m-d H:i:s", $now);
- $mapped = configDict::getConfig('commonUseUrlMapped');
- $redirectUrl = configDict::getConfig('replyType', 'redirectUrl');//回复类型 跳转链接
- if ($info['errcode'] != 0) {
- return false;
- }
- foreach ($menuList as $key => $val) {
- $addData = [];
- $addData['parentId'] = 0;
- $addData['merchantId'] = $merchantId;
- $addData['menuOrder'] = $key;
- $addData['menuOption'] = $val['menuOption'];
- $addData['hasSonMenu'] = isset($val['sub_button']) && !empty($val['sub_button']) ? 1 : 0;
- $addData['name'] = $val['name'];
- $addData['menuKey'] = $merchantId . '_' . $key;
- $addData['createTime'] = $date;
- $addData['replyType'] = $redirectUrl;//回复类型:常用链接、自定义网址、文字、报名类活动
- $addData['targetId'] = isset($val['keyName']) ? $mapped[$val['keyName']] : 0;//常用链接的链接选项id;活动id等
- $addData['replyUrl'] = isset($val['url']) ? $val['url'] : '';
- $parentMenu = self::add($addData);
- if (isset($val['sub_button']) && !empty($val['sub_button'])) {
- foreach ($val['sub_button'] as $sonKey => $sonVal) {
- $sonAddData = [];
- $sonAddData['parentId'] = $parentMenu['id'];
- $sonAddData['merchantId'] = $merchantId;
- $sonAddData['menuOrder'] = $sonKey;
- $sonAddData['menuOption'] = $sonVal['menuOption'];//回复类型
- $sonAddData['hasSonMenu'] = 0;
- $sonAddData['name'] = $sonVal['name'];
- $sonAddData['menuKey'] = $merchantId . '_' . $key . '_' . $sonKey;
- $sonAddData['createTime'] = $date;
- $sonAddData['replyType'] = $redirectUrl;//回复类型:常用链接、自定义网址、文字、报名类活动
- $sonAddData['targetId'] = isset($sonVal['keyName']) ? $mapped[$sonVal['keyName']] : 0;//常用链接选项id、活动id
- $sonAddData['replyUrl'] = isset($sonVal['url']) ? $sonVal['url'] : '';
- self::add($sonAddData);
- }
- }
- }
- return true;
- }
-
- public static function getByMenuKey($menuKey)
- {
- return xhWxMenu::find()->where(['menuKey' => $menuKey])->asArray()->one();
- }
-
- public static function getMenuList($merchantId)
- {
- $list = self::getAllMenu($merchantId);
- return $list;
- }
-
- /**
- * 取出商家所有菜单
- */
- public static function getAllMenu($merchantId, $getKeyName = false)
- {
- $list = self::getMenuById(0, $merchantId, $getKeyName);
- return $list;
- }
-
- /**
- * 根据菜单id取子菜单
- * $getKeyName false 默认不带键名 true 返回带键名的
- */
- public static function getMenuById($parentId, $merchantId, $getKeyName = false)
- {
- $condition = ['parentId' => $parentId, 'merchantId' => $merchantId];
- $wxMenu = xhWxMenu::getAllByCondition($condition, 'menuOrder asc');
- $data = [];
- if (empty($wxMenu)) {
- return $data;
- }
- foreach ($wxMenu as $k => $v) {
- $id = $v['id'];
- $cate = [
- 'id' => $id,
- 'name' => $v['name'],
- 'menuKey' => $v['menuKey'],
- 'menuOption' => $v['menuOption'],
- 'parentId' => $v['parentId'],
- 'merchantId' => $v['merchantId'],
- 'replyType' => $v['replyType'],
- 'replyUrl' => $v['replyUrl'],
- 'replyText' => $v['replyText'],
- 'replyNews' => $v['replyNews'],
- 'replyMultiNews' => $v['replyMultiNews'],
- 'hasSonMenu' => $v['hasSonMenu'],
- 'menuOrder' => $v['menuOrder'],
- 'createTime' => $v['createTime'],
- 'updateTime' => $v['updateTime'],
- 'child' => self::getMenuById($id, $merchantId, $getKeyName),
- ];
- if ($getKeyName) {
- $data[$id] = $cate;
- } else {
- $data[] = $cate;
- }
- }
- return $data;
- }
-
- public static function delMenuList($merchantId)
- {
- xhWxMenu::deleteByCondition(['merchantId' => $merchantId]);
- }
-
- public static function add($data)
- {
- return xhWxMenu::add($data);
- }
-
- public static function getFormatData($wxMenuContent, $wxMenuOption, $merchant)
- {
- $addData = [];
- $wxMenuOptionList = configDict::getConfig('wxMenuOption');
- $commonUseUrl = configDict::getConfig('commonUseUrl');
- switch ($wxMenuOption) {
- case $wxMenuOptionList['redirectToCommonUseUrl']://跳转到常用链接
- foreach ($commonUseUrl as $comKey => $comVal) {
- if ($comKey == $wxMenuContent) {
- $addData['replyUrl'] = Yii::$app->params['frontUrl'] . $comVal['url'] . '?account=' . $merchant['id'];
- $addData['targetId'] = $wxMenuContent;
- }
- }
- break;
- case $wxMenuOptionList['redirectToCustomUrl']://跳转到自定义网址
- $addData['replyUrl'] = urldecode($wxMenuContent);
- break;
- case $wxMenuOptionList['showText']://回复文字
- $addData['replyText'] = $wxMenuContent;
- break;
- default:
- }
- return $addData;
- }
-
- }
|