mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-06 03:58:00 +08:00
modified 完善细节
This commit is contained in:
parent
f62885ed67
commit
7ecc2c4b06
@ -22,8 +22,10 @@ class Login extends Base {
|
||||
|
||||
/**
|
||||
* 用户登录【账号密码登录】
|
||||
* @return \think\Response
|
||||
* @throws \think\Exception
|
||||
* @return Response
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public function index(): Response {
|
||||
@ -37,7 +39,7 @@ class Login extends Base {
|
||||
} else {
|
||||
$password = Tools::userMd5($password);
|
||||
}
|
||||
$userInfo = AdminUser::find(['username' => $username, 'password' => $password]);
|
||||
$userInfo = (new AdminUser())->where('username', $username)->where('password', $password)->find();
|
||||
if (!empty($userInfo)) {
|
||||
if ($userInfo['status']) {
|
||||
//更新用户数据
|
||||
@ -45,13 +47,13 @@ class Login extends Base {
|
||||
$data = [];
|
||||
if ($userData) {
|
||||
$userData->login_times++;
|
||||
$userData->last_login_ip = $this->request->ip(1);
|
||||
$userData->last_login_ip = sprintf("%u", ip2long($this->request->ip()));
|
||||
$userData->last_login_time = time();
|
||||
$userData->save();
|
||||
} else {
|
||||
$data['login_times'] = 1;
|
||||
$data['uid'] = $userInfo['id'];
|
||||
$data['last_login_ip'] = $this->request->ip(1);
|
||||
$data['last_login_ip'] = sprintf("%u", ip2long($this->request->ip()));
|
||||
$data['last_login_time'] = time();
|
||||
$data['head_img'] = '';
|
||||
AdminUserData::create($data);
|
||||
@ -65,7 +67,7 @@ class Login extends Base {
|
||||
return $this->buildFailed(ReturnCode::LOGIN_ERROR, '用户名密码不正确');
|
||||
}
|
||||
$userInfo['access'] = $this->getAccess($userInfo['id']);
|
||||
$userInfo['menu'] = $this->getAccessMenu($userInfo['id']);
|
||||
$userInfo['menu'] = $this->getAccessMenuData($userInfo['id']);
|
||||
|
||||
$apiAuth = md5(uniqid() . time());
|
||||
cache('Login:' . $apiAuth, json_encode($userInfo), config('apiadmin.ONLINE_TIME'));
|
||||
@ -73,51 +75,60 @@ class Login extends Base {
|
||||
|
||||
$userInfo['apiAuth'] = $apiAuth;
|
||||
|
||||
return $this->buildSuccess($userInfo, '登录成功');
|
||||
return $this->buildSuccess($userInfo->toArray(), '登录成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
* @return mixed
|
||||
* @return Response
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public function getUserInfo() {
|
||||
public function getUserInfo(): Response {
|
||||
return $this->buildSuccess($this->userInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登出
|
||||
* @return array
|
||||
* @return Response
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public function logout() {
|
||||
$ApiAuth = $this->request->header('ApiAuth');
|
||||
public function logout(): Response {
|
||||
$ApiAuth = $this->request->header('Api-Auth');
|
||||
cache('Login:' . $ApiAuth, null);
|
||||
cache('Login:' . $this->userInfo['id'], null);
|
||||
|
||||
return $this->buildSuccess([], '登出成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户的允许菜单
|
||||
* @return Response
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public function getAccessMenu(): Response {
|
||||
return $this->buildSuccess($this->getAccessMenuData($this->userInfo['id']));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户的允许菜单
|
||||
* @param int $uid
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\exception\DbException
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public function getAccessMenu($uid = 0) {
|
||||
if ($uid == 0) {
|
||||
$uid = $this->userInfo['id'];
|
||||
}
|
||||
public function getAccessMenuData(int $uid): array {
|
||||
$returnData = [];
|
||||
$isSupper = Tools::isAdministrator($uid);
|
||||
if ($isSupper) {
|
||||
$access = (new AdminMenu())->where('router', '<>', '')->select();
|
||||
$returnData = Tools::listToTree(Tools::buildArrFromObj($access));
|
||||
} else {
|
||||
$groups = AdminAuthGroupAccess::get(['uid' => $uid]);
|
||||
$groups = (new AdminAuthGroupAccess())->where('uid', $uid)->find();
|
||||
if (isset($groups) && $groups->group_id) {
|
||||
$access = (new AdminAuthRule())->whereIn('group_id', $groups->group_id)->select();
|
||||
$access = array_unique(array_column(Tools::buildArrFromObj($access), 'url'));
|
||||
@ -127,28 +138,28 @@ class Login extends Base {
|
||||
RouterTool::buildVueRouter($returnData);
|
||||
}
|
||||
}
|
||||
if ($uid == 0) {
|
||||
return $this->buildSuccess($returnData);
|
||||
} else {
|
||||
return $returnData;
|
||||
}
|
||||
|
||||
return $returnData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户权限数据
|
||||
* @param $uid
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public function getAccess($uid) {
|
||||
public function getAccess(int $uid): array {
|
||||
$isSupper = Tools::isAdministrator($uid);
|
||||
if ($isSupper) {
|
||||
$access = AdminMenu::all();
|
||||
$access = (new AdminMenu())->select();
|
||||
$access = Tools::buildArrFromObj($access);
|
||||
|
||||
return array_values(array_filter(array_column($access, 'url')));
|
||||
} else {
|
||||
$groups = AdminAuthGroupAccess::get(['uid' => $uid]);
|
||||
$groups = (new AdminAuthGroupAccess())->where('uid', $uid)->find();
|
||||
if (isset($groups) && $groups->group_id) {
|
||||
$access = (new AdminAuthRule())->whereIn('group_id', $groups->group_id)->select();
|
||||
$access = Tools::buildArrFromObj($access);
|
||||
|
@ -17,7 +17,7 @@ class AdminAuth {
|
||||
*/
|
||||
public function handle($request, \Closure $next): Response {
|
||||
$header = config('apiadmin.CROSS_DOMAIN');
|
||||
$ApiAuth = $request->header('apiAuth', '');
|
||||
$ApiAuth = $request->header('Api-Auth', '');
|
||||
if ($ApiAuth) {
|
||||
$userInfo = cache('Login:' . $ApiAuth);
|
||||
$userInfo = json_decode($userInfo, true);
|
||||
|
@ -18,7 +18,7 @@ class AdminLog {
|
||||
*/
|
||||
public function handle($request, \Closure $next): Response {
|
||||
$userInfo = $request->API_ADMIN_USER_INFO;
|
||||
$menuInfo = AdminMenu::get(['url' => $request->path()]);
|
||||
$menuInfo = (new AdminMenu())->where('url', $request->pathinfo())->find();
|
||||
|
||||
if ($menuInfo) {
|
||||
$menuInfo = $menuInfo->toArray();
|
||||
@ -26,7 +26,7 @@ class AdminLog {
|
||||
|
||||
return json([
|
||||
'code' => ReturnCode::INVALID,
|
||||
'msg' => '当前路由非法:' . $request->path(),
|
||||
'msg' => '当前路由非法:' . $request->pathinfo(),
|
||||
'data' => []
|
||||
])->header(config('apiadmin.CROSS_DOMAIN'));
|
||||
}
|
||||
@ -36,7 +36,7 @@ class AdminLog {
|
||||
'uid' => $userInfo['id'],
|
||||
'nickname' => $userInfo['nickname'],
|
||||
'add_time' => time(),
|
||||
'url' => $request->path(),
|
||||
'url' => $request->pathinfo(),
|
||||
'data' => json_encode($request->param())
|
||||
]);
|
||||
|
||||
|
@ -15,7 +15,7 @@ class WikiAuth {
|
||||
*/
|
||||
public function handle($request, \Closure $next) {
|
||||
$header = config('apiadmin.CROSS_DOMAIN');
|
||||
$ApiAuth = $request->header('apiAuth', '');
|
||||
$ApiAuth = $request->header('Api-Auth', '');
|
||||
if ($ApiAuth) {
|
||||
$userInfo = cache('Login:' . $ApiAuth);
|
||||
if (!$userInfo) {
|
||||
|
@ -85,12 +85,12 @@ class Tools {
|
||||
|
||||
/**
|
||||
* 将查询的二维对象转换成二维数组
|
||||
* @param array $res
|
||||
* @param $res
|
||||
* @param string $key 允许指定索引值
|
||||
* @return array
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public static function buildArrFromObj(array $res, string $key = ''): array {
|
||||
public static function buildArrFromObj($res, string $key = ''): array {
|
||||
$arr = [];
|
||||
foreach ($res as $value) {
|
||||
$value = $value->toArray();
|
||||
@ -106,8 +106,8 @@ class Tools {
|
||||
|
||||
/**
|
||||
* 将二维数组变成指定key
|
||||
* @param $array
|
||||
* @param $keyName
|
||||
* @param array $array
|
||||
* @param string $keyName
|
||||
* @return array
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
@ -129,7 +129,13 @@ class Tools {
|
||||
* @param string $root
|
||||
* @return array
|
||||
*/
|
||||
public static function listToTree(array $list, string $pk = 'id', string $pid = 'fid', string $child = 'children', string $root = '0'): array {
|
||||
public static function listToTree(
|
||||
array $list,
|
||||
string $pk = 'id',
|
||||
string $pid = 'fid',
|
||||
string $child = 'children',
|
||||
string $root = '0'
|
||||
): array {
|
||||
$tree = array();
|
||||
if (is_array($list)) {
|
||||
$refer = array();
|
||||
@ -152,8 +158,16 @@ class Tools {
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将层级数组遍历成一维数组
|
||||
* @param array $list
|
||||
* @param int $lv
|
||||
* @param string $title
|
||||
* @return array
|
||||
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
public static function formatTree(array $list, int $lv = 0, string $title = 'title'): array {
|
||||
$formatTree = array();
|
||||
$formatTree = [];
|
||||
foreach ($list as $key => $val) {
|
||||
$title_prefix = '';
|
||||
for ($i = 0; $i < $lv; $i++) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user