SystemLangServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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;
  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 SystemLangServices 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. $data[] = $item->getData();
  50. }
  51. return $data;
  52. }
  53. /**
  54. * 获取后台权限菜单和权限
  55. * @param $rouleId
  56. * @param int $level
  57. * @return array
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function getMenusList($rouleId, int $level)
  63. {
  64. /** @var SystemRoleServices $systemRoleServices */
  65. $systemRoleServices = app()->make(SystemRoleServices::class);
  66. $rules = $systemRoleServices->getRoleArray(['status' => 1, 'id' => $rouleId], 'rules');
  67. $rulesStr = Arr::unique($rules);
  68. $menusList = $this->dao->getMenusRoule(['route' => $level ? $rulesStr : '']);
  69. $unique = $this->dao->getMenusUnique(['unique' => $level ? $rulesStr : '']);
  70. return [Arr::getMenuIviewList($this->getMenusData($menusList)), $unique];
  71. }
  72. /**
  73. * 获取后台菜单树型结构列表
  74. * @param array $where
  75. * @return array
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getList(array $where)
  81. {
  82. $menusList = $this->dao->getMenusList($where);
  83. $menusList = $this->getMenusData($menusList);
  84. return get_tree_children($menusList);
  85. }
  86. /**
  87. * 获取form表单所需要的所要的菜单列表
  88. * @return array[]
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. protected function getFormSelectMenus()
  94. {
  95. $menuList = $this->dao->getMenusRoule(['is_del' => 0], ['id', 'pid', 'menu_name']);
  96. $list = sort_list_tier($this->getMenusData($menuList), '0', 'pid', 'id');
  97. $menus = [['value' => 0, 'label' => '顶级按钮']];
  98. foreach ($list as $menu) {
  99. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['menu_name']];
  100. }
  101. return $menus;
  102. }
  103. /**
  104. * @return array
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. protected function getFormCascaderMenus(int $value = 0)
  110. {
  111. $menuList = $this->dao->getMenusRoule(['is_del' => 0], ['id as value', 'pid', 'menu_name as label']);
  112. $menuList = $this->getMenusData($menuList);
  113. if ($value) {
  114. $data = get_tree_value($menuList, $value);
  115. } else {
  116. $data = [];
  117. }
  118. return [get_tree_children($menuList, 'children', 'value'), array_reverse($data)];
  119. }
  120. /**
  121. * 创建权限规格生表单
  122. * @param array $formData
  123. * @return mixed
  124. * @throws \FormBuilder\Exception\FormBuilderException
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function createMenusForm(array $formData = [])
  130. {
  131. $field[] = Form::input('menu_name', '按钮名称', $formData['menu_name'] ?? '')->required('按钮名称必填');
  132. // $field[] = Form::select('pid', '父级id', $formData['pid'] ?? 0)->setOptions($this->getFormSelectMenus())->filterable(1);
  133. $field[] = Form::input('menu_path', '路由名称', $formData['menu_path'] ?? '')->placeholder('请输入前台跳转路由地址')->required('请填写前台路由地址');
  134. $field[] = Form::input('unique_auth', '权限标识', $formData['unique_auth'] ?? '')->placeholder('不填写则后台自动生成');
  135. $params = $formData['params'] ?? '';
  136. // $field[] = Form::input('params', '参数', is_array($params) ? '' : $params)->placeholder('举例:a/123/b/234');
  137. $field[] = Form::frameInput('icon', '图标', $this->url(config('app.admin_prefix', 'admin') . '/widget.widgets/icon', ['fodder' => 'icon']), $formData['icon'] ?? '')->icon('md-add')->height('505px')->modal(['footer-hide' => true]);
  138. $field[] = Form::number('sort', '排序', (int)($formData['sort'] ?? 0))->precision(0);
  139. $field[] = Form::radio('auth_type', '类型', $formData['auth_type'] ?? 1)->options([['value' => 2, 'label' => '接口'], ['value' => 1, 'label' => '菜单(包含页面按钮)']]);
  140. $field[] = Form::radio('is_show', '状态', $formData['is_show'] ?? 1)->options([['value' => 0, 'label' => '关闭'], ['value' => 1, 'label' => '开启']]);
  141. $field[] = Form::radio('is_show_path', '是否为前端隐藏菜单', $formData['is_show_path'] ?? 0)->options([['value' => 1, 'label' => '是'], ['value' => 0, 'label' => '否']]);
  142. [$menuList, $data] = $this->getFormCascaderMenus((int)($formData['pid'] ?? 0));
  143. $field[] = Form::cascader('menu_list', '父级id', $data)->data($menuList)->filterable(true);
  144. return $field;
  145. }
  146. /**
  147. * 新增权限表单
  148. * @return array
  149. * @throws \FormBuilder\Exception\FormBuilderException
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function createMenus()
  155. {
  156. return create_form('添加权限', $this->createMenusForm(), $this->url('/setting/save'));
  157. }
  158. /**
  159. * 修改权限菜单
  160. * @param int $id
  161. * @return array
  162. * @throws \FormBuilder\Exception\FormBuilderException
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\DbException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. */
  167. public function updateMenus(int $id)
  168. {
  169. $menusInfo = $this->dao->get($id);
  170. if (!$menusInfo) {
  171. throw new AdminException(100026);
  172. }
  173. return create_form('修改权限', $this->createMenusForm($menusInfo->getData()), $this->url('/setting/update/' . $id), 'PUT');
  174. }
  175. /**
  176. * 获取一条数据
  177. * @param int $id
  178. * @return mixed
  179. */
  180. public function find(int $id)
  181. {
  182. $menusInfo = $this->dao->get($id);
  183. if (!$menusInfo) {
  184. throw new AdminException(100026);
  185. }
  186. $menu = $menusInfo->getData();
  187. $menu['pid'] = (int)$menu['pid'];
  188. $menu['auth_type'] = (int)$menu['auth_type'];
  189. $menu['is_header'] = (int)$menu['is_header'];
  190. $menu['is_show'] = (int)$menu['is_show'];
  191. $menu['is_show_path'] = (int)$menu['is_show_path'];
  192. if (!$menu['path']) {
  193. [$menuList, $data] = $this->getFormCascaderMenus($menu['pid']);
  194. $menu['path'] = $data;
  195. } else {
  196. $menu['path'] = explode('/', $menu['path']);
  197. if (is_array($menu['path'])) {
  198. $menu['path'] = array_map(function ($item) {
  199. return (int)$item;
  200. }, $menu['path']);
  201. }
  202. }
  203. return $menu;
  204. }
  205. /**
  206. * 删除菜单
  207. * @param int $id
  208. * @return mixed
  209. */
  210. public function delete(int $id)
  211. {
  212. if ($this->dao->count(['pid' => $id])) {
  213. throw new AdminException(400613);
  214. }
  215. return $this->dao->delete($id);
  216. }
  217. /**
  218. * 获取添加身份规格
  219. * @param $roles
  220. * @return array
  221. * @throws \think\db\exception\DataNotFoundException
  222. * @throws \think\db\exception\DbException
  223. * @throws \think\db\exception\ModelNotFoundException
  224. */
  225. public function getMenus($roles): array
  226. {
  227. $field = ['menu_name', 'pid', 'id'];
  228. $where = ['is_del' => 0];
  229. if (!$roles) {
  230. $menus = $this->dao->getMenusRoule($where, $field);
  231. } else {
  232. /** @var SystemRoleServices $service */
  233. $service = app()->make(SystemRoleServices::class);
  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. }