catchAdmin/catch/user/Auth.php

113 lines
2.6 KiB
PHP
Raw Normal View History

2019-12-02 23:04:43 +08:00
<?php
2019-12-12 18:52:24 +08:00
namespace catchAdmin\user;
2019-12-02 23:04:43 +08:00
2019-12-15 13:38:02 +08:00
use catchAdmin\permissions\model\Permissions;
2019-12-22 09:37:52 +08:00
use catchAdmin\permissions\model\Roles;
2019-12-12 09:13:44 +08:00
use catchAdmin\user\model\Users;
2019-12-12 18:52:24 +08:00
use catcher\exceptions\LoginFailedException;
2019-12-22 09:37:52 +08:00
use catcher\Tree;
use thans\jwt\facade\JWTAuth;
2019-12-02 23:04:43 +08:00
use think\facade\Session;
class Auth
{
2019-12-22 09:37:52 +08:00
protected const USER_ID = 'catch_uid';
2019-12-02 23:04:43 +08:00
/**
* 登陆
*
* @time 2019年11月28日
* @param $params
* @return bool
2019-12-15 13:38:02 +08:00
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws LoginFailedException
2019-12-02 23:04:43 +08:00
*/
2019-12-12 18:52:24 +08:00
public static function login($params)
2019-12-02 23:04:43 +08:00
{
2019-12-12 18:52:24 +08:00
$user = Users::where('email', $params['email'])->find();
if (!$user) {
throw new LoginFailedException('登陆失败, 请检查用户名和密码');
}
2019-12-12 09:13:44 +08:00
if (!password_verify($params['password'], $user->password)) {
2019-12-02 23:04:43 +08:00
throw new LoginFailedException('登陆失败, 请检查用户名和密码');
}
2019-12-12 09:13:44 +08:00
2019-12-22 09:37:52 +08:00
// Session::set(self::getLoginUserKey(), $user);
2019-12-02 23:04:43 +08:00
2019-12-22 09:37:52 +08:00
return JWTAuth::builder([self::USER_ID => $user->id]);
2019-12-02 23:04:43 +08:00
}
/**
* 退出登陆
*
* @time 2019年11月28日
* @return bool
*/
2019-12-12 18:52:24 +08:00
public static function logout(): bool
2019-12-02 23:04:43 +08:00
{
2019-12-12 18:52:24 +08:00
Session::delete(self::getLoginUserKey());
2019-12-02 23:04:43 +08:00
return true;
}
2019-12-12 18:52:24 +08:00
2019-12-15 13:38:02 +08:00
/**
*
* @time 2019年12月15日
* @return mixed
*/
2019-12-12 18:52:24 +08:00
public static function user()
{
2019-12-22 09:37:52 +08:00
$user = Users::where('id', JWTAuth::auth()[self::USER_ID])
->field(['id', 'username', 'status'])->find();
return $user;
}
public static function getUserInfo()
{
$user = self::user();
$roles = $user->getRoles();
2019-12-28 21:21:05 +08:00
$user->permissions = Permissions::whereIn('id', $user->getPermissionsBy())
->field(['permission_name as title', 'route', 'icon'])
->select();
2019-12-22 09:37:52 +08:00
$user->roles = $roles;
return $user;
2019-12-12 18:52:24 +08:00
}
2019-12-15 13:38:02 +08:00
/**
*
* @time 2019年12月15日
* @return string
*/
2019-12-12 18:52:24 +08:00
protected static function getLoginUserKey(): string
{
2019-12-22 09:37:52 +08:00
// return md5(self::USER_KEY);
2019-12-12 18:52:24 +08:00
}
2019-12-15 13:38:02 +08:00
/**
*
* @time 2019年12月15日
* @param $mark
* @param $module
* @return bool
*/
public static function hasPermissions($mark, $module): bool
{
$permissionIds = self::user()->get->getPermissionsBy();
$permissionId = Permissions::where('module', $module)
->where('permission_mark', $mark)->value('id');
return in_array($permissionId, $permissionIds);
}
2019-12-02 23:04:43 +08:00
}