SystemMenusServices.php 11 KB

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