SystemMenusServices.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\admin;
  12. use app\model\system\admin\SystemMenus;
  13. use qiniu\basic\BaseServices;
  14. use qiniu\exceptions\AdminException;
  15. use qiniu\services\CacheService;
  16. use qiniu\utils\Arr;
  17. use think\Collection;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. * 权限菜单
  23. * Class SystemMenusServices
  24. * @package app\services\system
  25. * @mixin SystemMenus
  26. */
  27. class SystemMenusServices extends BaseServices
  28. {
  29. /**
  30. * @var string[]
  31. */
  32. protected $type = [
  33. 1 => 'admin',//平台
  34. 2 => 'store',//门店
  35. ];
  36. /**
  37. * 初始化
  38. * SystemMenusServices constructor.
  39. * @param SystemMenus $model
  40. */
  41. public function __construct(SystemMenus $model)
  42. {
  43. $this->model = $model;
  44. }
  45. /**
  46. * 获取菜单没有被修改器修改的数据
  47. * @param $menusList
  48. * @param int $type
  49. * @return array
  50. */
  51. public function getMenusData($menusList, int $type = 1)
  52. {
  53. $data = [];
  54. foreach ($menusList as $item) {
  55. // $item['expand'] = true;
  56. $item['selected'] = false;
  57. $item['title'] = $item['menu_name'];
  58. $item['menu_path'] = preg_replace('/^\/' . ($this->type[$type] ?? 'admin') . '/', '', $item['menu_path']);
  59. $item['extend'] = json_decode($item['extend'], true);
  60. $data[] = $item->getData();
  61. }
  62. return $data;
  63. }
  64. /**
  65. * 获取后台权限菜单和权限
  66. * @param $rouleId
  67. * @param int $level
  68. * @param int $type
  69. * @return array
  70. * @throws DataNotFoundException
  71. * @throws DbException
  72. * @throws ModelNotFoundException
  73. */
  74. public function getMenusList($rouleId, int $level, int $type = 1)
  75. {
  76. $rulesStr = '';
  77. if ($level) {//超级管理员查询所有菜单
  78. /** @var SystemRoleServices $systemRoleServices */
  79. $systemRoleServices = app()->make(SystemRoleServices::class);
  80. $rules = $systemRoleServices->getRoleArray(['status' => 1, 'id' => $rouleId], 'rules');
  81. $rulesStr = Arr::unique($rules);
  82. }
  83. $menusList = $this->getMenusRole(['type' => $type, 'route' => $level ? $rulesStr : '']);
  84. $unique = $this->getMenusUnique(['type' => $type, 'unique' => $level ? $rulesStr : '']);
  85. return [Arr::getMenuIviewList($this->getMenusData($menusList, $type)), $unique];
  86. }
  87. /**
  88. * 获取后台菜单树型结构列表
  89. * @param array $where
  90. * @return array
  91. * @throws DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws ModelNotFoundException
  94. */
  95. public function getList(array $where)
  96. {
  97. $where = array_merge($where);
  98. $menusList = $this->search($where)->order('sort DESC,id ASC')->select();
  99. $menusList = $this->getMenusData($menusList);
  100. return get_tree_children($menusList);
  101. }
  102. public function create($data)
  103. {
  104. if (parent::create($data)) {
  105. CacheService::redisHandler('system_menus')->clear();
  106. return true;
  107. } else {
  108. throw new AdminException('添加失败');
  109. }
  110. }
  111. public function update($id, array $data, ?string $key = null)
  112. {
  113. if (parent::update($id, $data)) {
  114. CacheService::redisHandler('system_menus')->clear();
  115. return true;
  116. } else {
  117. throw new AdminException('修改失败');
  118. }
  119. }
  120. public function delete($id, ?string $key = null)
  121. {
  122. if ($this->be(['pid' => $id])) {
  123. throw new AdminException('请先删除改菜单下的子菜单');
  124. }
  125. if (parent::delete($id)) {
  126. CacheService::redisHandler('system_menus')->clear();
  127. return true;
  128. } else {
  129. throw new AdminException('删除失败,请稍候再试!');
  130. }
  131. }
  132. /**
  133. * 获取权限菜单列表
  134. * @param array $where
  135. * @param array|null $field
  136. * @return Collection
  137. * @throws DbException
  138. * @throws DataNotFoundException
  139. * @throws ModelNotFoundException
  140. */
  141. public function getMenusRole(array $where, ?array $field = [])
  142. {
  143. if (!$field) {
  144. $field = ['id', 'menu_name', 'icon', 'pid', 'sort', 'menu_path', 'is_show', 'is_show_path', 'extend'];
  145. }
  146. return $this->search($where)->field($field)->order('sort DESC,id DESC')->failException(false)->select();
  147. }
  148. /**
  149. * 获取菜单中的唯一权限
  150. * @param array $where
  151. * @return array
  152. */
  153. public function getMenusUnique(array $where)
  154. {
  155. return $this->search($where)->where('unique_auth', '<>', '')->column('unique_auth', '');
  156. }
  157. /**
  158. * 获取添加身份规格
  159. * @param $roles
  160. * @param int $type
  161. * @param int $is_show
  162. * @return array
  163. * @throws DataNotFoundException
  164. * @throws DbException
  165. * @throws ModelNotFoundException
  166. */
  167. public function getMenus($roles, int $type = 1, int $is_show = 1): array
  168. {
  169. $field = ['menu_name', 'pid', 'id'];
  170. $where = ['type' => $type];
  171. if ($is_show) $where['is_show'] = 1;
  172. if (!$roles) {
  173. $menus = $this->getMenusRole($where, $field);
  174. } else {
  175. /** @var SystemRoleServices $service */
  176. $service = app()->make(SystemRoleServices::class);
  177. //拼接有长度限制
  178. // $ids = $service->value([['id', 'in', $roles]], 'GROUP_CONCAT(rules) as ids');
  179. $roles = is_string($roles) ? explode(',', $roles) : $roles;
  180. $ids = $service->getRoleIds($roles);
  181. $menus = $this->getMenusRole(['rule' => $ids] + $where, $field);
  182. }
  183. return $this->tidyMenuTier(false, $menus);
  184. }
  185. /**
  186. * 组合菜单数据
  187. * @param bool $adminFilter
  188. * @param mixed $menusList
  189. * @param int $pid
  190. * @param array $navList
  191. * @return array
  192. */
  193. public function tidyMenuTier(bool $adminFilter = false, $menusList = [], int $pid = 0, array $navList = []): array
  194. {
  195. foreach ($menusList as $k => $menu) {
  196. $menu = $menu->getData();
  197. $menu['title'] = $menu['menu_name'];
  198. unset($menu['menu_name']);
  199. if ($menu['pid'] == $pid) {
  200. unset($menusList[$k]);
  201. $menu['children'] = $this->tidyMenuTier($adminFilter, $menusList, $menu['id']);
  202. if ($pid == 0 && !count($menu['children'])) continue;
  203. if ($menu['children']) $menu['expand'] = true;
  204. $navList[] = $menu;
  205. }
  206. }
  207. return $navList;
  208. }
  209. /**
  210. * 根据访问地址获得菜单名
  211. * @param string $rule
  212. * @return mixed
  213. */
  214. public function getVisitName(string $rule)
  215. {
  216. return $this->search(['url' => $rule])->value('menu_name');
  217. }
  218. }