You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemMenusServices.php 11KB

пре 2 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\services\system;
  3. use app\dao\system\SystemMenusDao;
  4. use app\services\BaseServices;
  5. use crmeb\exceptions\AdminException;
  6. use crmeb\services\FormBuilder as Form;
  7. use crmeb\utils\Arr;
  8. /**
  9. * 权限菜单
  10. * Class SystemMenusServices
  11. * @package app\services\system
  12. * @method save(array $data) 保存数据
  13. * @method get(int $id, ?array $field = []) 获取数据
  14. * @method update($id, array $data, ?string $key = null) 修改数据
  15. * @method getSearchList() 主页搜索
  16. * @method getColumn(array $where, string $field, ?string $key = '') 主页搜索
  17. * @method getVisitName(string $rule) 根据访问地址获得菜单名
  18. */
  19. class SystemMenusServices extends BaseServices
  20. {
  21. /**
  22. * 初始化
  23. * SystemMenusServices constructor.
  24. * @param SystemMenusDao $dao
  25. */
  26. public function __construct(SystemMenusDao $dao)
  27. {
  28. $this->dao = $dao;
  29. }
  30. /**
  31. * 获取菜单没有被修改器修改的数据
  32. * @param $menusList
  33. * @return array
  34. */
  35. public function getMenusData($menusList)
  36. {
  37. $data = [];
  38. foreach ($menusList as $item) {
  39. // $item['expand'] = true;
  40. $item['selected'] = false;
  41. $item['title'] = $item['menu_name'];
  42. $data[] = $item->getData();
  43. }
  44. return $data;
  45. }
  46. /**
  47. * 获取后台权限菜单和权限
  48. * @param $rouleId
  49. * @param int $level
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function getMenusList($rouleId, int $level, int $type = 1)
  56. {
  57. /** @var SystemRoleServices $systemRoleServices */
  58. $systemRoleServices = app()->make(SystemRoleServices::class);
  59. $rules = $systemRoleServices->getRoleArray(['status' => 1, 'id' => $rouleId], 'rules');
  60. $rulesStr = Arr::unique($rules);
  61. $menusList = $this->dao->getMenusRoule(['type' => $type, 'route' => $level ? $rulesStr : '']);
  62. $unique = $this->dao->getMenusUnique(['type' => $type, 'unique' => $level ? $rulesStr : '']);
  63. return [Arr::getMenuIviewList($this->getMenusData($menusList)), $unique];
  64. }
  65. /**
  66. * 获取后台菜单树型结构列表
  67. * @param array $where
  68. * @return array
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function getList(array $where)
  74. {
  75. $menusList = $this->dao->getMenusList($where);
  76. $menusList = $this->getMenusData($menusList);
  77. return get_tree_children($menusList);
  78. }
  79. /**
  80. * 获取form表单所需要的所要的菜单列表
  81. * @return array[]
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. protected function getFormSelectMenus()
  87. {
  88. $menuList = $this->dao->getMenusRoule(['is_del' => 0], ['id', 'pid', 'menu_name']);
  89. $list = get_tree_children($this->getMenusData($menuList), '0', 'pid', 'id');
  90. $menus = [['value' => 0, 'label' => '顶级按钮']];
  91. foreach ($list as $menu) {
  92. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['menu_name']];
  93. }
  94. return $menus;
  95. }
  96. /**
  97. * @return array
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\DbException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. */
  102. protected function getFormCascaderMenus(int $value = 0, int $type = 1)
  103. {
  104. $menuList = $this->dao->getMenusRoule(['is_del' => 0, 'type' => $type], ['id as value', 'pid', 'menu_name as label']);
  105. $menuList = $this->getMenusData($menuList);
  106. if ($value) {
  107. $data = get_tree_value($menuList, $value);
  108. } else {
  109. $data = [];
  110. }
  111. return [get_tree_children($menuList, 'children', 'value'), array_reverse($data)];
  112. }
  113. /**
  114. * 创建权限规格生表单
  115. * @param array $formData
  116. * @return mixed
  117. * @throws \FormBuilder\Exception\FormBuilderException
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public function createMenusForm(array $formData = [], int $type = 1)
  123. {
  124. $field[] = Form::hidden('type', $type);
  125. $field[] = Form::input('menu_name', '按钮名称', $formData['menu_name'] ?? '')->required('按钮名称必填');
  126. // $field[] = Form::select('pid', '父级id', $formData['pid'] ?? 0)->setOptions($this->getFormSelectMenus())->filterable(1);
  127. $field[] = Form::input('menu_path', '路由名称', $formData['menu_path'] ?? '')->placeholder('请输入前台跳转路由地址')->required('请填写前台路由地址');
  128. $field[] = Form::input('unique_auth', '权限标识', $formData['unique_auth'] ?? '')->placeholder('不填写则后台自动生成');
  129. $params = $formData['params'] ?? '';
  130. $field[] = Form::input('params', '参数', is_array($params) ? '' : $params)->placeholder('举例:a/123/b/234');
  131. $field[] = Form::frameInput('icon', '图标', $this->url('admin/widget.widgets/icon', ['fodder' => 'icon']), $formData['icon'] ?? '')->icon('md-add')->height('500px');
  132. $field[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0))->min(0);
  133. $field[] = Form::radio('auth_type', '类型', $formData['auth_type'] ?? 1)->options([['value' => 2, 'label' => '接口'], ['value' => 1, 'label' => '菜单(菜单只显示三级)']]);
  134. $field[] = Form::radio('is_show', '状态', $formData['is_show'] ?? 1)->options([['value' => 0, 'label' => '关闭'], ['value' => 1, 'label' => '开启']]);
  135. $field[] = Form::radio('is_show_path', '是否为前端隐藏菜单', $formData['is_show_path'] ?? 0)->options([['value' => 1, 'label' => '是'], ['value' => 0, 'label' => '否']]);
  136. [$menuList, $data] = $this->getFormCascaderMenus((int)($formData['pid'] ?? 0), $type);
  137. $field[] = Form::cascader('menu_list', '父级id', $data)->data($menuList)->filterable(true);
  138. return $field;
  139. }
  140. /**
  141. * 新增权限表单
  142. * @return array
  143. * @throws \FormBuilder\Exception\FormBuilderException
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. public function createMenus(int $type = 1)
  149. {
  150. return create_form('添加权限', $this->createMenusForm([], $type), $this->url('/setting/save'));
  151. }
  152. /**
  153. * 修改权限菜单
  154. * @param int $id
  155. * @return array
  156. * @throws \FormBuilder\Exception\FormBuilderException
  157. * @throws \think\db\exception\DataNotFoundException
  158. * @throws \think\db\exception\DbException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. */
  161. public function updateMenus(int $id)
  162. {
  163. $menusInfo = $this->dao->get($id);
  164. if (!$menusInfo) {
  165. throw new AdminException('数据不存在');
  166. }
  167. $menusInfo = $menusInfo->getData();
  168. return create_form('修改权限', $this->createMenusForm($menusInfo, $menusInfo['type'] ?? 1), $this->url('/setting/update/' . $id), 'PUT');
  169. }
  170. /**
  171. * 获取一条数据
  172. * @param int $id
  173. * @return mixed
  174. */
  175. public function find(int $id)
  176. {
  177. $menusInfo = $this->dao->get($id);
  178. if (!$menusInfo) {
  179. throw new AdminException('数据不存在');
  180. }
  181. $menu = $menusInfo->getData();
  182. $menu['pid'] = (int)$menu['pid'];
  183. $menu['auth_type'] = (int)$menu['auth_type'];
  184. $menu['is_header'] = (int)$menu['is_header'];
  185. $menu['is_show'] = (int)$menu['is_show'];
  186. $menu['is_show_path'] = (int)$menu['is_show_path'];
  187. if (!$menu['path']) {
  188. [$menuList, $data] = $this->getFormCascaderMenus($menu['pid']);
  189. $menu['path'] = $data;
  190. } else {
  191. $menu['path'] = explode('/', $menu['path']);
  192. if (is_array($menu['path'])) {
  193. $menu['path'] = array_map(function ($item) {
  194. return (int)$item;
  195. }, $menu['path']);
  196. }
  197. }
  198. return $menu;
  199. }
  200. /**
  201. * 删除菜单
  202. * @param int $id
  203. * @return mixed
  204. */
  205. public function delete(int $id)
  206. {
  207. if ($this->dao->count(['pid' => $id])) {
  208. throw new AdminException('请先删除改菜单下的子菜单');
  209. }
  210. return $this->dao->delete($id);
  211. }
  212. /**
  213. * 获取添加身份规格
  214. * @param $roles
  215. * @param int $type
  216. * @param int $is_show
  217. * @return array
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\DbException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. */
  222. public function getMenus($roles, int $type = 1, int $is_show = 1): array
  223. {
  224. $field = ['menu_name', 'pid', 'id'];
  225. $where = ['is_del' => 0, 'type' => $type];
  226. if ($is_show) $where['is_show'] = 1;
  227. if (!$roles) {
  228. $menus = $this->dao->getMenusRoule($where, $field);
  229. } else {
  230. /** @var SystemRoleServices $service */
  231. $service = app()->make(SystemRoleServices::class);
  232. //拼接有长度限制
  233. // $ids = $service->value([['id', 'in', $roles]], 'GROUP_CONCAT(rules) as ids');
  234. $roles = is_string($roles) ? explode(',', $roles) : $roles;
  235. $ids = $service->getRoleIds($roles);
  236. $menus = $this->dao->getMenusRoule(['rule' => $ids] + $where, $field);
  237. }
  238. return $this->tidyMenuTier(false, $menus);
  239. }
  240. /**
  241. * 组合菜单数据
  242. * @param bool $adminFilter
  243. * @param $menusList
  244. * @param int $pid
  245. * @param array $navList
  246. * @return array
  247. */
  248. public function tidyMenuTier(bool $adminFilter = false, $menusList, int $pid = 0, array $navList = []): array
  249. {
  250. foreach ($menusList as $k => $menu) {
  251. $menu = $menu->getData();
  252. $menu['title'] = $menu['menu_name'];
  253. unset($menu['menu_name']);
  254. if ($menu['pid'] == $pid) {
  255. unset($menusList[$k]);
  256. $menu['children'] = $this->tidyMenuTier($adminFilter, $menusList, $menu['id']);
  257. if ($pid == 0 && !count($menu['children'])) continue;
  258. if ($menu['children']) $menu['expand'] = true;
  259. $navList[] = $menu;
  260. }
  261. }
  262. return $navList;
  263. }
  264. }