需要获取菜单信息
在app/adminapi/services/system/admin;/SystemAdminServices.php
中调整login
方法
/*** 管理员登陆* @param string $account* @param string $password* @return array|\think\Model* @throws \think\db\exception\DataNotFoundException* @throws \think\db\exception\DbException* @throws \think\db\exception\ModelNotFoundException*/public function verifyLogin(string $account, string $password){$adminInfo = $this->dao->accountByAdmin($account);if (!$adminInfo) {throw new AdminException('管理员不存在!');}if (!$adminInfo->status) {throw new AdminException('您已被禁止登录!');}if (!password_verify($password, $adminInfo->pwd)) {throw new AdminException('账号或密码错误,请重新输入');}$adminInfo->last_time = time();$adminInfo->last_ip = app('request')->ip();$adminInfo->login_count++;$adminInfo->save();return $adminInfo;}
/*** 后台登陆获取菜单获取token* @param string $account* @param string $password* @param string $type* @return array* @throws \think\db\exception\DataNotFoundException* @throws \think\db\exception\DbException* @throws \think\db\exception\ModelNotFoundException*/public function login(string $account, string $password, string $type){$adminInfo = $this->verifyLogin($account, $password);$tokenInfo = $this->createToken($adminInfo->id, $type);/** @var SystemMenusServices $services */$services = app()->make(SystemMenusServices::class);[$menus, $uniqueAuth] = $services->getMenusList($adminInfo->roles, (int)$adminInfo['level']);return ['menus' => $menus,'unique_auth' => $uniqueAuth,'token' => $tokenInfo['token'],'expire_time' => $tokenInfo['params']['exp'],'user_info' => ['id' => $adminInfo->getData('id'),'account' => $adminInfo->getData('account'),'head_pic' => $adminInfo->getData('head_pic'),],'logo' => '','version' => config('app.version'),];}
获取菜单实现
在SystemMenusServices
中实现getMenusList
方法
use xkadmin\utils\Arr;
......
......
....../*** 获取后台权限菜单和权限* @param $rouleId* @param int $level* @return array* @throws \think\db\exception\DataNotFoundException* @throws \think\db\exception\DbException* @throws \think\db\exception\ModelNotFoundException*/
public function getMenusList($rouleId, int $level)
{/** @var SystemRoleServices $systemRoleServices */$systemRoleServices = app()->make(SystemRoleServices::class);// 获取角色权限数组$rules = $systemRoleServices->getRoleArray(['status' => 1, 'id' => $rouleId], 'rules');// 去重$rulesStr = Arr::unique($rules);// 获取菜单权限$menusList = $this->dao->getMenusRoule(['route' => $level ? $rulesStr : '']);// 获取唯一菜单$unique = $this->dao->getMenusUnique(['unique' => $level ? $rulesStr : '']);return [Arr::getMenuEleList($this->getMenusData($menusList)), $unique];
}
在xkadmin\utils
命名空间下,新建Arr.php
文件,内容如下:
<?phpnamespace xkadmin\utils;/*** 操作数组帮助类* Class Arr* @package xkadmin\utils*/
class Arr
{/*** 对数组增加默认值* @param array $keys* @return array*/public static function getDefaultValue(array $keys, array $configList = []){$value = [];foreach ($keys as $val) {if (is_array($val)) {$k = $val[0] ?? '';$v = $val[1] ?? '';} else {$k = $val;$v = '';}$value[$k] = $configList[$k] ?? $v;}return $value;}/*** 获取Ele菜单列表* @param array $data* @return array*/public static function getMenuEleList(array $data){return Arr::toEleUi(Arr::getTree($data));}/*** 转化EleUi需要的key值* @param $data* @return array*/public static function toEleUi($data){$newData = [];foreach ($data as $k => $v) {$temp = [];$temp['path'] = $v['menu_path'];$temp['title'] = $v['menu_name'];$temp['icon'] = $v['icon'];$temp['header'] = $v['header'];$temp['is_header'] = $v['is_header'];if ($v['is_show_path']) {$temp['auth'] = ['hidden'];}if (!empty($v['children'])) {$temp['children'] = self::toEleUi($v['children']);}$newData[] = $temp;}return $newData;}/*** 获取树型菜单* @param $data* @param int $pid* @param int $level* @return array*/public static function getTree($data, $pid = 0, $level = 1){$childs = self::getChild($data, $pid, $level);$dataSort = array_column($childs, 'sort');array_multisort($dataSort, SORT_DESC, $childs);foreach ($childs as $key => $navItem) {$resChild = self::getTree($data, $navItem['id']);if (null != $resChild) {$childs[$key]['children'] = $resChild;}}return $childs;}/*** 获取子菜单* @param $arr* @param $id* @param $lev* @return array*/private static function getChild(&$arr, $id, $lev){$child = [];foreach ($arr as $k => $value) {if ($value['pid'] == $id) {$value['level'] = $lev;$child[] = $value;}}return $child;}/*** 格式化数据* @param array $array* @param $value* @param int $default* @return mixed*/public static function setValeTime(array $array, $value, $default = 0){foreach ($array as $item) {if (!isset($value[$item]))$value[$item] = $default;else if (is_string($value[$item]))$value[$item] = (float)$value[$item];}return $value;}/*** 获取二维数组中某个值的集合重新组成数组,并判断数组中的每一项是否为真* @param array $data* @param string $filed* @return array*/public static function getArrayFilterValeu(array $data, string $filed){return array_filter(array_unique(array_column($data, $filed)), function ($item) {if ($item) {return $item;}});}/*** 数组转字符串去重复* @param array $data* @return false|string[]*/public static function unique(array $data){return array_unique(explode(',', implode(',', $data)));}/*** 获取数组中去重复过后的指定key值* @param array $list* @param string $key* @return array*/public static function getUniqueKey(array $list, string $key){return array_unique(array_column($list, $key));}
}
开源地址:https://gitee.com/qqcode/xkadmin
至此完成。。。未有详尽之处,后续迭代。