用户管理

This commit is contained in:
wuyanwen 2019-12-12 18:52:24 +08:00
parent 5f6a7cf24e
commit 6c423e5fc5
4 changed files with 51 additions and 21 deletions

View File

@ -1,21 +1,14 @@
<?php <?php
namespace catchAdmin\login; namespace catchAdmin\user;
use catchAdmin\user\model\Users; use catchAdmin\user\model\Users;
use cather\exceptions\LoginFailedException; use catcher\exceptions\FailedException;
use catcher\exceptions\LoginFailedException;
use think\facade\Session; use think\facade\Session;
class Auth class Auth
{ {
protected $loginUser = 'admin_user'; protected const USER_KEY = 'admin_user';
/**
* Auth constructor.
*/
public function __construct()
{
$this->loginUser = md5($this->loginUser);
}
/** /**
* 登陆 * 登陆
@ -25,9 +18,14 @@ class Auth
* @throws LoginFailedException * @throws LoginFailedException
* @return bool * @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)) { if (!password_verify($params['password'], $user->password)) {
throw new LoginFailedException('登陆失败, 请检查用户名和密码'); throw new LoginFailedException('登陆失败, 请检查用户名和密码');
} }
@ -41,7 +39,7 @@ class Auth
$user->last_login_time = time(); $user->last_login_time = time();
$user->save(); $user->save();
Session::set($this->loginUser, $user); Session::set(self::getLoginUserKey(), $user);
return true; return true;
} }
@ -52,10 +50,20 @@ class Auth
* @time 2019年11月28日 * @time 2019年11月28日
* @return bool * @return bool
*/ */
public function logout() public static function logout(): bool
{ {
Session::delete($this->loginUser); Session::delete(self::getLoginUserKey());
return true; return true;
} }
public static function user()
{
return Session::get(self::getLoginUserKey(), null);
}
protected static function getLoginUserKey(): string
{
return md5(self::USER_KEY);
}
} }

View File

@ -58,4 +58,26 @@ class Users extends CatchModel
return $query->where('status', $search['status']); return $query->where('status', $search['status']);
})->paginate($search['limit'] ?? $this->limit); })->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);
}
} }

View File

@ -2,8 +2,8 @@
$router->resource('user', '\catchAdmin\user\controller\User'); $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/switch/status/<id>', '\catchAdmin\user\controller\User@switchStatus');
$router->put('user/recover/<id>', '\catchAdmin\user\controller\User/recover'); $router->put('user/recover/<id>', '\catchAdmin\user\controller\User@recover');
$router->get('user/get/roles', '\catchAdmin\user\controller\User/getRoles'); $router->get('user/get/roles', '\catchAdmin\user\controller\User@getRoles');

View File

@ -122,7 +122,6 @@
}); });
function recover(uid, username) { function recover(uid, username) {
console.log(username)
layer.confirm('确定要恢复“' + username + '”吗?', { layer.confirm('确定要恢复“' + username + '”吗?', {
skin: 'layui-layer-admin', skin: 'layui-layer-admin',
shade: .1 shade: .1
@ -145,6 +144,7 @@
title: (mUser ? '修改' : '添加') + '用户', title: (mUser ? '修改' : '添加') + '用户',
url: mUser ? '/user/'+mUser.id + '/edit':'/user/create', url: mUser ? '/user/'+mUser.id + '/edit':'/user/create',
data: mUser, // 传递数据到表单页面 data: mUser, // 传递数据到表单页面
area: '500px',
end: function () { end: function () {
if (admin.getLayerData(layIndex, 'formOk')) { // 判断表单操作成功标识 if (admin.getLayerData(layIndex, 'formOk')) { // 判断表单操作成功标识
insTb.reload(); // 成功刷新表格 insTb.reload(); // 成功刷新表格