SystemMenusServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. if (isset($item['menu_path'])) {
  51. $item['menu_path'] = '/' . config('app.admin_prefix', 'admin') . $item['menu_path'];
  52. }
  53. $data[] = $item;
  54. }
  55. return $data;
  56. }
  57. /**
  58. * 获取后台权限菜单和权限
  59. * @param $rouleId
  60. * @param int $level
  61. * @return array
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function getMenusList($rouleId, int $level)
  67. {
  68. /** @var SystemRoleServices $systemRoleServices */
  69. $systemRoleServices = app()->make(SystemRoleServices::class);
  70. $rules = $systemRoleServices->getRoleArray(['status' => 1, 'id' => $rouleId], 'rules');
  71. $rulesStr = Arr::unique($rules);
  72. $menusList = $this->dao->getMenusRoule(['route' => $level ? $rulesStr : '', 'is_show_path' => 1]);
  73. $unique = $this->dao->getMenusUnique(['unique' => $level ? $rulesStr : '']);
  74. return [Arr::getMenuIviewList($this->getMenusData($menusList)), $unique];
  75. }
  76. /**
  77. * 获取后台菜单树型结构列表
  78. * @param array $where
  79. * @return array
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function getList(array $where, array $field = ['*'])
  85. {
  86. $menusList = $this->dao->getMenusList($where, $field);
  87. $menusList = $this->getMenusData($menusList);
  88. return get_tree_children($menusList);
  89. }
  90. /**
  91. * 获取form表单所需要的所要的菜单列表
  92. * @return array[]
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. protected function getFormSelectMenus()
  98. {
  99. $menuList = $this->dao->getMenusRoule(['is_del' => 0], ['id', 'pid', 'menu_name']);
  100. $list = sort_list_tier($this->getMenusData($menuList), '0', 'pid', 'id');
  101. $menus = [['value' => 0, 'label' => '顶级按钮']];
  102. foreach ($list as $menu) {
  103. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['menu_name']];
  104. }
  105. return $menus;
  106. }
  107. /**
  108. * @return array
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\DbException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. */
  113. protected function getFormCascaderMenus(int $value = 0, $auth_type = 0)
  114. {
  115. $where = ['is_del' => 0];
  116. $menuList = $this->dao->getMenusRoule($where, ['id as value', 'pid', 'menu_name as label']);
  117. $menuList = $this->getMenusData($menuList);
  118. if ($value) {
  119. $data = get_tree_value($menuList, $value);
  120. } else {
  121. $data = [];
  122. }
  123. return [get_tree_children($menuList, 'children', 'value'), array_reverse($data)];
  124. }
  125. /**
  126. * 创建权限规格生表单
  127. * @param array $formData
  128. * @return mixed
  129. * @throws \FormBuilder\Exception\FormBuilderException
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\DbException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. */
  134. public function createMenusForm(array $formData = [])
  135. {
  136. $field[] = Form::input('menu_name', '按钮名称', $formData['menu_name'] ?? '')->required('按钮名称必填');
  137. $field[] = Form::input('menu_path', '路由名称', $formData['menu_path'] ?? '')->placeholder('请输入前台跳转路由地址')->required('请填写前台路由地址');
  138. $field[] = Form::input('unique_auth', '权限标识', $formData['unique_auth'] ?? '')->placeholder('不填写则后台自动生成');
  139. $field[] = Form::frameInput('icon', '图标', $this->url(config('app.admin_prefix', 'admin') . '/widget.widgets/icon', ['fodder' => 'icon']), $formData['icon'] ?? '')->icon('md-add')->height('560px')->props(['footer' => false]);
  140. $field[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0))->precision(0);
  141. $field[] = Form::radio('auth_type', '类型', $formData['auth_type'] ?? 1)->options([['value' => 1, 'label' => '菜单'], ['value' => 3, 'label' => '按钮'], ['value' => 2, 'label' => '接口']]);
  142. $field[] = Form::radio('is_show', '权限状态', $formData['is_show'] ?? 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, '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), 3);
  145. $field[] = Form::cascader('menu_list', '父级id', $data)->options($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. $ids = $this->dao->column(['pid' => $id], 'id');
  215. if (count($ids)) {
  216. foreach ($ids as $value) {
  217. $this->delete($value);
  218. }
  219. }
  220. return $this->dao->delete($id);
  221. }
  222. /**
  223. * 获取添加身份规格
  224. * @param $roles
  225. * @return array
  226. * @throws \think\db\exception\DataNotFoundException
  227. * @throws \think\db\exception\DbException
  228. * @throws \think\db\exception\ModelNotFoundException
  229. */
  230. public function getMenus($roles, $check = []): array
  231. {
  232. $field = ['menu_name', 'pid', 'id'];
  233. $where = ['is_del' => 0, 'is_show_path' => 1];
  234. if (!$roles) {
  235. $menus = $this->dao->getMenusRoule($where, $field);
  236. } else {
  237. /** @var SystemRoleServices $service */
  238. $service = app()->make(SystemRoleServices::class);
  239. $roles = is_string($roles) ? explode(',', $roles) : $roles;
  240. $ids = $service->getRoleIds($roles);
  241. $menus = $this->dao->getMenusRoule(['rule' => $ids] + $where, $field);
  242. }
  243. foreach ($menus as &$item) {
  244. $item['checked'] = in_array($item['id'], $check);
  245. }
  246. return $this->tidyMenuTier(false, $menus);
  247. }
  248. /**
  249. * 组合菜单数据
  250. * @param bool $adminFilter
  251. * @param $menusList
  252. * @param int $pid
  253. * @param array $navList
  254. * @return array
  255. */
  256. public function tidyMenuTier(bool $adminFilter = false, $menusList, int $pid = 0, array $navList = []): array
  257. {
  258. foreach ($menusList as $k => $menu) {
  259. $menu = $menu->getData();
  260. $menu['title'] = $menu['menu_name'];
  261. unset($menu['menu_name']);
  262. if ($menu['pid'] == $pid) {
  263. unset($menusList[$k]);
  264. $menu['children'] = $this->tidyMenuTier($adminFilter, $menusList, $menu['id']);
  265. // if ($pid == 0 && !count($menu['children'])) continue;
  266. if ($menu['children']) $menu['expand'] = true;
  267. $navList[] = $menu;
  268. }
  269. }
  270. return $navList;
  271. }
  272. }