SystemMenusServices.php 11 KB

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