SystemMenusServices.php 11 KB

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