| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace common\services;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\util;
- use Yii;
- use common\components\wxUtil;
- use common\components\dict;
- use common\models\xhWxMenu;
- class xhWxMenuService
- {
- /**
- * 初始化菜单
- */
- public static function menuInit()
- {
- $ptStyle = dict::getDict('ptStyle');
- if (empty($ptStyle)) {
- util::stop('没有找到平台');
- }
- foreach ($ptStyle as $style) {
- if ($style == dict::getDict('ptStyle', 'hd')) {
- $merchant = WxOpenClass::getWxInfo();
- $url = Yii::$app->params['hdDomain'];
- self::menuGenerate($style, $merchant, $url);
- } elseif ($style == dict::getDict('ptStyle', 'ghs')) {
- $merchant = WxOpenClass::getGhsWxInfo();
- $url = Yii::$app->params['ghsDomain'];
- self::menuGenerate($style, $merchant, $url);
- } elseif ($style == dict::getDict('ptStyle', 'mall')) {
- $merchant = WxOpenClass::getMallWxInfo();
- $url = Yii::$app->params['mallDomain'];
- self::menuGenerate($style, $merchant, $url);
- }
- }
- }
- public static function menuGenerate($style, $merchant, $url)
- {
- if ($style == dict::getDict('ptStyle', 'mall')) {
- $button = [
- ["type" => 'miniprogram', "name" => "最近访问", "appid" => $merchant['miniAppId'], "pagepath" => "pages/home/recent", "url" => $url,],
- ];
- } else {
- $button = [
- ["type" => 'miniprogram', "name" => "官网", "appid" => $merchant['miniAppId'], "pagepath" => "pagesClient/official/index", "url" => $url,],
- ["type" => 'miniprogram', "name" => "后台", "appid" => $merchant['miniAppId'], "pagepath" => "admin/home/workbench", "url" => $url,],
- ];
- }
- foreach ($button as $key => $val) {
- unset($button[$key]['keyName']);
- }
- $menu['button'] = $button;
- $info = wxUtil::userMenuToWeiXin($menu, $merchant, $style);
- $info = json_decode($info, true);
- print_r($info);
- }
- public static function getByMenuKey($menuKey)
- {
- return xhWxMenu::find()->where(['menuKey' => $menuKey])->asArray()->one();
- }
- public static function getMenuList($sjId)
- {
- $list = self::getAllMenu($sjId);
- return $list;
- }
- /**
- * 取出商家所有菜单
- */
- public static function getAllMenu($sjId, $getKeyName = false)
- {
- $list = self::getMenuById(0, $sjId, $getKeyName);
- return $list;
- }
- /**
- * 根据菜单id取子菜单
- * $getKeyName false 默认不带键名 true 返回带键名的
- */
- public static function getMenuById($parentId, $sjId, $getKeyName = false)
- {
- $condition = ['parentId' => $parentId, 'sjId' => $sjId];
- $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'],
- 'sjId' => $v['sjId'],
- '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, $sjId, $getKeyName),
- ];
- if ($getKeyName) {
- $data[$id] = $cate;
- } else {
- $data[] = $cate;
- }
- }
- return $data;
- }
- public static function delMenuList($sjId)
- {
- xhWxMenu::deleteByCondition(['sjId' => $sjId]);
- }
- public static function add($data)
- {
- return xhWxMenu::add($data);
- }
- public static function getFormatData($wxMenuContent, $wxMenuOption, $merchant)
- {
- $addData = [];
- $wxMenuOptionList = dict::getDict('wxMenuOption');
- $commonUseUrl = dict::getDict('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;
- }
- }
|