// +---------------------------------------------------------------------- namespace app\services\system\admin; use app\model\system\admin\SystemMenus; use qiniu\basic\BaseServices; use qiniu\exceptions\AdminException; use qiniu\services\CacheService; use qiniu\utils\Arr; use think\Collection; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; /** * 权限菜单 * Class SystemMenusServices * @package app\services\system * @mixin SystemMenus */ class SystemMenusServices extends BaseServices { /** * @var string[] */ protected $type = [ 1 => 'admin',//平台 2 => 'store',//门店 ]; /** * 初始化 * SystemMenusServices constructor. * @param SystemMenus $model */ public function __construct(SystemMenus $model) { $this->model = $model; } /** * 获取菜单没有被修改器修改的数据 * @param $menusList * @param int $type * @return array */ public function getMenusData($menusList, int $type = 1) { $data = []; foreach ($menusList as $item) { // $item['expand'] = true; $item['selected'] = false; $item['title'] = $item['menu_name']; $item['menu_path'] = preg_replace('/^\/' . ($this->type[$type] ?? 'admin') . '/', '', $item['menu_path']); $item['extend'] = json_decode($item['extend'], true); $data[] = $item->getData(); } return $data; } /** * 获取后台权限菜单和权限 * @param $rouleId * @param int $level * @param int $type * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getMenusList($rouleId, int $level, int $type = 1) { $rulesStr = ''; if ($level) {//超级管理员查询所有菜单 /** @var SystemRoleServices $systemRoleServices */ $systemRoleServices = app()->make(SystemRoleServices::class); $rules = $systemRoleServices->getRoleArray(['status' => 1, 'id' => $rouleId], 'rules'); $rulesStr = Arr::unique($rules); } $menusList = $this->getMenusRole(['type' => $type, 'route' => $level ? $rulesStr : '']); $unique = $this->getMenusUnique(['type' => $type, 'unique' => $level ? $rulesStr : '']); return [Arr::getMenuIviewList($this->getMenusData($menusList, $type)), $unique]; } /** * 获取后台菜单树型结构列表 * @param array $where * @return array * @throws DataNotFoundException * @throws \think\db\exception\DbException * @throws ModelNotFoundException */ public function getList(array $where) { $where = array_merge($where); $menusList = $this->search($where)->order('sort DESC,id ASC')->select(); $menusList = $this->getMenusData($menusList); return get_tree_children($menusList); } public function create($data) { if (parent::create($data)) { CacheService::redisHandler('system_menus')->clear(); return true; } else { throw new AdminException('添加失败'); } } public function update($id, array $data, ?string $key = null) { if (parent::update($id, $data)) { CacheService::redisHandler('system_menus')->clear(); return true; } else { throw new AdminException('修改失败'); } } public function delete($id, ?string $key = null) { if ($this->be(['pid' => $id])) { throw new AdminException('请先删除改菜单下的子菜单'); } if (parent::delete($id)) { CacheService::redisHandler('system_menus')->clear(); return true; } else { throw new AdminException('删除失败,请稍候再试!'); } } /** * 获取权限菜单列表 * @param array $where * @param array|null $field * @return Collection * @throws DbException * @throws DataNotFoundException * @throws ModelNotFoundException */ public function getMenusRole(array $where, ?array $field = []) { if (!$field) { $field = ['id', 'menu_name', 'icon', 'pid', 'sort', 'menu_path', 'is_show', 'is_show_path', 'extend']; } return $this->search($where)->field($field)->order('sort DESC,id DESC')->failException(false)->select(); } /** * 获取菜单中的唯一权限 * @param array $where * @return array */ public function getMenusUnique(array $where) { return $this->search($where)->where('unique_auth', '<>', '')->column('unique_auth', ''); } /** * 获取添加身份规格 * @param $roles * @param int $type * @param int $is_show * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getMenus($roles, int $type = 1, int $is_show = 1): array { $field = ['menu_name', 'pid', 'id']; $where = ['type' => $type]; if ($is_show) $where['is_show'] = 1; if (!$roles) { $menus = $this->getMenusRole($where, $field); } else { /** @var SystemRoleServices $service */ $service = app()->make(SystemRoleServices::class); //拼接有长度限制 // $ids = $service->value([['id', 'in', $roles]], 'GROUP_CONCAT(rules) as ids'); $roles = is_string($roles) ? explode(',', $roles) : $roles; $ids = $service->getRoleIds($roles); $menus = $this->getMenusRole(['rule' => $ids] + $where, $field); } return $this->tidyMenuTier(false, $menus); } /** * 组合菜单数据 * @param bool $adminFilter * @param mixed $menusList * @param int $pid * @param array $navList * @return array */ public function tidyMenuTier(bool $adminFilter = false, $menusList = [], int $pid = 0, array $navList = []): array { foreach ($menusList as $k => $menu) { $menu = $menu->getData(); $menu['title'] = $menu['menu_name']; unset($menu['menu_name']); if ($menu['pid'] == $pid) { unset($menusList[$k]); $menu['children'] = $this->tidyMenuTier($adminFilter, $menusList, $menu['id']); if ($pid == 0 && !count($menu['children'])) continue; if ($menu['children']) $menu['expand'] = true; $navList[] = $menu; } } return $navList; } /** * 根据访问地址获得菜单名 * @param string $rule * @return mixed */ public function getVisitName(string $rule) { return $this->search(['url' => $rule])->value('menu_name'); } }