MenuService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace services\common;
  3. use addons\ReservationService\common\services\BackendService;
  4. use common\enums\AddonsEnum;
  5. use common\enums\AppEnum;
  6. use common\enums\RedisKeyEnum;
  7. use Yii;
  8. use yii\helpers\Json;
  9. use common\helpers\ArrayHelper;
  10. use common\components\Service;
  11. use common\enums\StatusEnum;
  12. use common\enums\WhetherEnum;
  13. use common\helpers\StringHelper;
  14. use common\helpers\Auth;
  15. use common\helpers\TreeHelper;
  16. use common\models\backend\Menu;
  17. use common\models\backend\MenuCate;
  18. /**
  19. * Class MenuService
  20. * @package services\sys
  21. * @author qimall
  22. */
  23. class MenuService extends Service
  24. {
  25. public static $permissionsMenu = [
  26. 1 => [ //合规模式需要隐藏的菜单
  27. 'mall/finance/commission-log',
  28. ]
  29. ];
  30. /**
  31. * @param string $addons_name 插件名称
  32. */
  33. public function delByAddonsName($addons_name)
  34. {
  35. Menu::deleteAll(['addons_name' => $addons_name]);
  36. }
  37. /**
  38. * @param MenuCate $cate
  39. */
  40. public function delByCate(MenuCate $cate)
  41. {
  42. Menu::deleteAll(['app_id' => $cate->app_id, 'addons_name' => $cate->addons_name]);
  43. }
  44. /**
  45. * @param array $menus
  46. * @param MenuCate $cate
  47. * @param int $pid
  48. * @param int $level
  49. * @param Menu $parent
  50. */
  51. public function createByAddons(array $menus, MenuCate $cate, $pid = 0, $level = 1, $parent = '')
  52. {
  53. $cacheArr = [];
  54. // 重组数组
  55. $menus = ArrayHelper::regroupMapToArr($menus);
  56. foreach ($menus as $menu) {
  57. $model = new Menu();
  58. $model->attributes = $menu;
  59. // 增加父级
  60. !empty($parent) && $model->setParent($parent);
  61. if ($model->params) {
  62. $params = [];
  63. foreach ($model->params as $key => $value) {
  64. $params[$key]= $value;
  65. }
  66. $model->params = $params;
  67. }
  68. $model->url = StringHelper::toUnderScore($cate->addons_name) . '/'. $menu['route'];
  69. $model->pid = $pid;
  70. $model->level = $level;
  71. $model->cate_id = $cate->id;
  72. $model->app_id = $cate->app_id;
  73. $model->addons_name = $cate->addons_name;
  74. $model->is_addon = $cate->is_addon;
  75. $res = $model->save(false);
  76. $cate->app_id == AppEnum::BACKEND && $pid > 0 && $cacheArr[] = $model->toArray();// 将对象转为数组
  77. if (isset($menu['child']) && !empty($menu['child'])) {
  78. $this->createByAddons($menu['child'], $cate, $model->id, $model->level + 1, $model);
  79. }
  80. }
  81. // 生成目录保存至缓存中
  82. if ($cacheArr) {
  83. $cache = Yii::$app->cache;
  84. $cache_key = RedisKeyEnum::suffix(RedisKeyEnum::BACKEND_MENU,$cate->addons_name,true);
  85. $cache->set($cache_key, $cacheArr);
  86. }
  87. }
  88. /**
  89. * 获取下拉
  90. *
  91. * @param MenuCate $menuCate
  92. * @param string $id
  93. * @return array
  94. */
  95. public function getDropDown(MenuCate $menuCate, $app_id, $id = '')
  96. {
  97. $list = Menu::find()
  98. ->where(['>=', 'status', StatusEnum::DISABLED])
  99. ->andWhere(['app_id' => $app_id])
  100. ->andWhere(['is_addon' => $menuCate->is_addon])
  101. ->andFilterWhere(['addons_name' => $menuCate->addons_name])
  102. ->andFilterWhere(['<>', 'id', $id])
  103. ->select(['id', 'title', 'pid', 'level'])
  104. ->orderBy('cate_id asc, sort asc')
  105. ->asArray()
  106. ->all();
  107. $models = ArrayHelper::itemsMerge($list);
  108. $data = ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
  109. return ArrayHelper::merge([0 => '顶级菜单'], $data);
  110. }
  111. /**
  112. * @return array
  113. */
  114. public function getOnAuthList($catid, $addonsName = '', $mallId = 0)
  115. {
  116. $models = $this->findAll($catid, $addonsName);
  117. // 获取权限信息
  118. foreach ($models as $key => &$model) {
  119. if (!empty($model['url'])) {
  120. $params = !is_array($model['params']) ? Json::decode($model['params'], true) : $model['params'];
  121. (empty($params) || !is_array($params)) && $params = [];
  122. $model['fullUrl'][] = $model['url'];
  123. foreach ($params as $param) {
  124. if (!empty($param['key'])) {
  125. $model['fullUrl'][$param['key']] = $param['value'];
  126. }
  127. }
  128. } else {
  129. $model['fullUrl'] = '#';
  130. }
  131. // 系统菜单校验 添加overview验证
  132. //if($model['url']!='mall/overview/index'){
  133. if ($model['is_addon'] == WhetherEnum::DISABLED && Auth::verify($model['url']) === false) {
  134. unset($models[$key]);
  135. }
  136. //}
  137. // 插件菜单校验
  138. if ($model['is_addon'] == WhetherEnum::ENABLED) {
  139. if (Auth::verify($model['url']) === false) {
  140. unset($models[$key]);
  141. }
  142. unset($tmpUrl);
  143. }
  144. }
  145. return $models;
  146. }
  147. /**
  148. * 过滤菜单显示
  149. * $param $menu
  150. * $return array
  151. * */
  152. public function filterMenu($menuList, $type = 1)
  153. {
  154. $newMenuList = [];
  155. $permissionsMenuList = self::$permissionsMenu[$type] ?? [];
  156. foreach ($menuList as $menu) {
  157. if (in_array($menu['url'], $permissionsMenuList)) {
  158. continue;
  159. }
  160. $childMenuList = [];
  161. if (isset($menu['children']) && $menu['children']) {
  162. foreach ($menu['children'] as $childMenu) {
  163. if (!in_array($childMenu['url'],$permissionsMenuList)) {
  164. $childMenuList[] = $childMenu;
  165. }
  166. }
  167. $menu['children'] = $childMenuList;
  168. }
  169. $newMenuList[] = $menu;
  170. }
  171. return $newMenuList;
  172. }
  173. /**
  174. * @return array|\yii\db\ActiveRecord[]
  175. */
  176. public function findAll($catid, $addonsName = '')
  177. {
  178. $where['status'] = StatusEnum::ENABLED;
  179. $where['app_id'] = Yii::$app->id;
  180. if (!empty($addonsName)) {
  181. $where['addons_name'] = ['addons_name' => $addonsName];
  182. } else {
  183. $where['cate_id'] = $catid;
  184. $where['is_addon'] = StatusEnum::DISABLED;
  185. }
  186. return $data = Menu::find()->where($where)
  187. ->orderBy('sort asc, id asc')
  188. ->asArray()
  189. ->all();
  190. }
  191. /**
  192. * @param $tree
  193. * @param $id
  194. * @return array|\yii\db\ActiveRecord[]
  195. */
  196. public function findChildByID($tree, $id)
  197. {
  198. return Menu::find()
  199. ->where(['like', 'tree', $tree . TreeHelper::prefixTreeKey($id) . '%', false])
  200. ->select(['id', 'level', 'tree', 'pid'])
  201. ->asArray()
  202. ->all();
  203. }
  204. }