SystemMenusServices.php 11 KB

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