init: 项目初始代码
This commit is contained in:
157
app/api/controller/v1/Auth.php
Normal file
157
app/api/controller/v1/Auth.php
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace app\api\controller\v1;
|
||||
|
||||
use app\api\model\Admin as model;
|
||||
use app\api\validate\Admin as AdminValidate;
|
||||
use app\api\model\Role as RoleModel;
|
||||
|
||||
use encryption\Rsa;
|
||||
use sso\SSOClient;
|
||||
use think\exception\ValidateException;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Config;
|
||||
use think\facade\Env;
|
||||
use think\facade\Filesystem;
|
||||
|
||||
/**
|
||||
* Class Auth
|
||||
* @package app\api\controller
|
||||
*/
|
||||
class Auth extends Base
|
||||
{
|
||||
const EMPTY_ADMIN = -100;
|
||||
const ERR_PASSWORD = -101;
|
||||
const ERR_TOKEN = -102;
|
||||
const EMPTY_ROLE = -103;
|
||||
const ERR_CLOSED = -104;
|
||||
const ERR_PWD = -300;
|
||||
const EMPTY_HEADIMG = -105;
|
||||
const ERR_MOBILE = -301;
|
||||
const ERR_SAMEPWD = -302;
|
||||
|
||||
const ERROR = [
|
||||
self::EMPTY_ADMIN => '该用户不存在',
|
||||
self::ERR_PASSWORD => '用户名或密码错误',
|
||||
self::ERR_TOKEN => 'token解析失败',
|
||||
self::EMPTY_ROLE => '当前用户没有分配角色',
|
||||
self::ERR_CLOSED => '该用户被禁用',
|
||||
self::ERR_PWD => '旧密码错误',
|
||||
self::EMPTY_HEADIMG => '请选择头像',
|
||||
self::ERR_MOBILE => '手机号格式错误',
|
||||
self::ERR_SAMEPWD => '新密码不能与旧密码相同'
|
||||
];
|
||||
|
||||
//后台管理员控制器
|
||||
protected $validate = null;
|
||||
//定义管理员模型
|
||||
protected $model = null;
|
||||
//定义管理员模型
|
||||
protected $roleModel = null;
|
||||
|
||||
public function initialize()
|
||||
{
|
||||
parent::initialize();
|
||||
$this->model = new model();
|
||||
$this->validate = new AdminValidate();
|
||||
$this->roleModel = new RoleModel();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 后台登录接口
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
//获取输入数据
|
||||
$inputData = $this->request->param(['username', 'password']);
|
||||
//验证器验证
|
||||
$this->commonValidate(__FUNCTION__,$inputData);
|
||||
|
||||
$adminInfo = $this->model->getInfoByUsername($inputData['username']);
|
||||
|
||||
if (empty($adminInfo)) {
|
||||
return $this->msg(self::EMPTY_ADMIN, self::ERROR[self::EMPTY_ADMIN]);
|
||||
}
|
||||
|
||||
if ($inputData['password'] != $adminInfo['password']) {
|
||||
return $this->msg(self::ERR_PASSWORD, self::ERROR[self::ERR_PASSWORD]);
|
||||
}
|
||||
|
||||
if ($adminInfo['status'] == 1) {
|
||||
return $this->msg(self::ERR_CLOSED, self::ERROR[self::ERR_CLOSED]);
|
||||
}
|
||||
unset($adminInfo['password']);
|
||||
$login_token = $this->jwt->getToken($adminInfo);
|
||||
Cache::set('api-info',$adminInfo,300);
|
||||
$data = [
|
||||
'login_token' => $login_token,
|
||||
'exp' => Env::get('jwt_config.exp'),
|
||||
'need_update_pwd' => $inputData['password'] == md5('Aa-1221') ? 1 : 0
|
||||
];
|
||||
return $this->msg(200, '登录成功', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员基本信息
|
||||
*/
|
||||
public function info()
|
||||
{
|
||||
$role = $this->roleModel->getInfo($this->adminInfo);
|
||||
if (empty($role)) {
|
||||
return $this->msg(self::EMPTY_ROLE, self::ERROR[self::EMPTY_ROLE]);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'roles' => [$role['role_name'] ?? $role['name']],
|
||||
'name' => $this->adminInfo['username'],
|
||||
'avatar' => $this->adminInfo['avatar']
|
||||
];
|
||||
|
||||
return $this->msg(200, '获取用户信息成功', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户所有权限
|
||||
*/
|
||||
public function getAuth()
|
||||
{
|
||||
$data = [
|
||||
'auth_list' => $this->adminInfo['auth_id_list']
|
||||
];
|
||||
return $this->msg(200, '获取权限成功', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
if (!Session::has('signed_in') || !Session::get('signed_in')) {
|
||||
return success('操作成功');
|
||||
}
|
||||
$sso = new SSOClient();
|
||||
$access_token = Session::get('access_token');
|
||||
if (empty($access_token)) {
|
||||
return error('请选登录');
|
||||
}
|
||||
$result = $sso->notifySSOServerSigninOut($access_token, Session::get('uc_sessid'));
|
||||
if (empty($result)) {
|
||||
return error('操作失败');
|
||||
}
|
||||
if (0 != $result['errno']) {
|
||||
return error($result['errmsg']);
|
||||
}
|
||||
Session::clear();
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员列表
|
||||
*/
|
||||
public function list(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user