用户管理
This commit is contained in:
parent
5f6a7cf24e
commit
6c423e5fc5
@ -1,21 +1,14 @@
|
||||
<?php
|
||||
namespace catchAdmin\login;
|
||||
namespace catchAdmin\user;
|
||||
|
||||
use catchAdmin\user\model\Users;
|
||||
use cather\exceptions\LoginFailedException;
|
||||
use catcher\exceptions\FailedException;
|
||||
use catcher\exceptions\LoginFailedException;
|
||||
use think\facade\Session;
|
||||
|
||||
class Auth
|
||||
{
|
||||
protected $loginUser = 'admin_user';
|
||||
|
||||
/**
|
||||
* Auth constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->loginUser = md5($this->loginUser);
|
||||
}
|
||||
protected const USER_KEY = 'admin_user';
|
||||
|
||||
/**
|
||||
* 登陆
|
||||
@ -25,9 +18,14 @@ class Auth
|
||||
* @throws LoginFailedException
|
||||
* @return bool
|
||||
*/
|
||||
public function login($params)
|
||||
public static function login($params)
|
||||
{
|
||||
$user = Users::where('username', $params['name'])->find();
|
||||
$user = Users::where('email', $params['email'])->find();
|
||||
|
||||
if (!$user) {
|
||||
throw new LoginFailedException('登陆失败, 请检查用户名和密码');
|
||||
}
|
||||
|
||||
if (!password_verify($params['password'], $user->password)) {
|
||||
throw new LoginFailedException('登陆失败, 请检查用户名和密码');
|
||||
}
|
||||
@ -41,7 +39,7 @@ class Auth
|
||||
$user->last_login_time = time();
|
||||
$user->save();
|
||||
|
||||
Session::set($this->loginUser, $user);
|
||||
Session::set(self::getLoginUserKey(), $user);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -52,10 +50,20 @@ class Auth
|
||||
* @time 2019年11月28日
|
||||
* @return bool
|
||||
*/
|
||||
public function logout()
|
||||
public static function logout(): bool
|
||||
{
|
||||
Session::delete($this->loginUser);
|
||||
Session::delete(self::getLoginUserKey());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function user()
|
||||
{
|
||||
return Session::get(self::getLoginUserKey(), null);
|
||||
}
|
||||
|
||||
protected static function getLoginUserKey(): string
|
||||
{
|
||||
return md5(self::USER_KEY);
|
||||
}
|
||||
}
|
||||
|
@ -58,4 +58,26 @@ class Users extends CatchModel
|
||||
return $query->where('status', $search['status']);
|
||||
})->paginate($search['limit'] ?? $this->limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取权限
|
||||
*
|
||||
* @time 2019年12月12日
|
||||
* @param $uid
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array
|
||||
*/
|
||||
public function getPermissionsBy($uid = 0): array
|
||||
{
|
||||
$roles = $uid ? $this->findBy($uid)->getRoles() : $this->getRoles();
|
||||
|
||||
$permissionIds = [];
|
||||
foreach ($roles as $role) {
|
||||
$permissionIds = array_merge($permissionIds, $role->getPermissions()->column('id'));
|
||||
}
|
||||
|
||||
return array_unique($permissionIds);
|
||||
}
|
||||
}
|
@ -2,8 +2,8 @@
|
||||
|
||||
$router->resource('user', '\catchAdmin\user\controller\User');
|
||||
// 用户列表
|
||||
$router->get('users', '\catchAdmin\user\controller\User/list');
|
||||
$router->get('users', '\catchAdmin\user\controller\User@list');
|
||||
// 切换状态
|
||||
$router->put('user/switch/status/<id>', '\catchAdmin\user\controller\User/switchStatus');
|
||||
$router->put('user/recover/<id>', '\catchAdmin\user\controller\User/recover');
|
||||
$router->get('user/get/roles', '\catchAdmin\user\controller\User/getRoles');
|
||||
$router->put('user/switch/status/<id>', '\catchAdmin\user\controller\User@switchStatus');
|
||||
$router->put('user/recover/<id>', '\catchAdmin\user\controller\User@recover');
|
||||
$router->get('user/get/roles', '\catchAdmin\user\controller\User@getRoles');
|
||||
|
@ -122,7 +122,6 @@
|
||||
});
|
||||
|
||||
function recover(uid, username) {
|
||||
console.log(username)
|
||||
layer.confirm('确定要恢复“' + username + '”吗?', {
|
||||
skin: 'layui-layer-admin',
|
||||
shade: .1
|
||||
@ -145,6 +144,7 @@
|
||||
title: (mUser ? '修改' : '添加') + '用户',
|
||||
url: mUser ? '/user/'+mUser.id + '/edit':'/user/create',
|
||||
data: mUser, // 传递数据到表单页面
|
||||
area: '500px',
|
||||
end: function () {
|
||||
if (admin.getLayerData(layIndex, 'formOk')) { // 判断表单操作成功标识
|
||||
insTb.reload(); // 成功刷新表格
|
||||
|
Loading…
x
Reference in New Issue
Block a user