You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
6.0 KiB
169 lines
6.0 KiB
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\api\model\Manager;
|
|
use app\api\model\Token;
|
|
use auth\JwtAuth;
|
|
use edward\captcha\facade\CaptchaApi;
|
|
use service\ActionLog;
|
|
|
|
class Login
|
|
{
|
|
/**
|
|
* @OA\Post (
|
|
* path="login/login",tags={"公共分类"},summary="用户登录",description="用户登录",
|
|
* @OA\RequestBody(
|
|
* @OA\MediaType(mediaType="application/json",
|
|
* @OA\Schema (
|
|
* required={"username","password","key","code"},
|
|
* @OA\Property(property="username",type="string",minLength=2,maxLength=20,description="登录账号"),
|
|
* @OA\Property(property="password",type="string",minLength=6,maxLength=20,description="登录密码"),
|
|
* @OA\Property(property="key",type="string",minLength=1,maxLength=100,description="验证码安全标识 验证码接口返回"),
|
|
* @OA\Property(property="code",type="integer",minLength=1,maxLength=6,format="int15",description="验证码"),
|
|
* ),
|
|
* ),
|
|
* ),
|
|
* @OA\Response(
|
|
* response=200,
|
|
* description="返回管理员用户数据信息,token信息、管理员信息",
|
|
* ),
|
|
* )
|
|
* @Log [sys_name] 在 [sys_time] 登录了后台
|
|
*/
|
|
public function login(Manager $manager)
|
|
{
|
|
//获取参数
|
|
$_post = \request()->param();
|
|
|
|
//验证参数
|
|
$_rule = [
|
|
'username|登录用户名' => 'require|length:4,20',
|
|
'password|登录密码' => 'require|length:6,20',
|
|
'key|验证码' => 'require|length:6,200',
|
|
'code|验证码' => 'require|length:1,6',
|
|
];
|
|
|
|
$_message = [
|
|
'username.length' => '请输入4到20位的用户名!!!',
|
|
'password.length' => '请输入6到20位的密码!!!',
|
|
'code.length' => '请输入1到6位的验证码!!!',
|
|
'key.length' => '验证码不正确!!!',
|
|
];
|
|
|
|
env('app_debug') ?: validate($_rule, $_message)->check($_post);
|
|
|
|
//验证码检测
|
|
env('app_debug') ?: $this->checkCaptcha($_post['code'], $_post['key']);
|
|
|
|
//验证用户信息
|
|
$user_data = $manager->checkLogin($_post);
|
|
|
|
//发送JWT签证密钥
|
|
$token_obj = (JwtAuth::getInstance())->setToken($user_data);
|
|
|
|
//写入登录日志
|
|
defined('CURR_THIS') ?: define('CURR_THIS', get_called_class());
|
|
$user_data['user_id'] = $user_data['id'];
|
|
ActionLog::getInstance()->write($user_data);
|
|
|
|
//获取未读系统邮件
|
|
$message = new \app\api\model\Message();
|
|
$message->setUserMessage($user_data['user_id'],$user_data['site_id']);
|
|
|
|
//返回数据
|
|
$data = [
|
|
'access_token' => [
|
|
'token' => $token_obj->getToken(),
|
|
'expire' => config('jwtauth.web.expire_at'),
|
|
// 'refresh_token' => $token_obj->createRefreshToken(new Token()),
|
|
],
|
|
'userinfo' => $user_data,
|
|
];
|
|
return send_http_status($data);
|
|
}
|
|
|
|
/**
|
|
* 通过refresh_token重新生成token
|
|
*/
|
|
public function refreshToken(Token $token)
|
|
{
|
|
$refresh_token = request()->param('refresh_token');
|
|
$_data = $token->refreshToken($refresh_token);
|
|
if (!empty($_data)) {
|
|
return send_http_status($_data);
|
|
}
|
|
return send_http_status('', 40509, 406);
|
|
}
|
|
|
|
/**
|
|
* @OA\Get (
|
|
* path="login/captcha",tags={"公共分类"},summary="获取验证码",description="获取验证码",
|
|
* @OA\Response(
|
|
* response=200,
|
|
* description="返回验证码信息",
|
|
* @OA\JsonContent(
|
|
* @OA\Schema(
|
|
* required={"code","msg","count","data"},
|
|
* @OA\Property(
|
|
* property="code",
|
|
* type="integer",
|
|
* format="int32",
|
|
* description="状态码"
|
|
* ),
|
|
* @OA\Property(
|
|
* property="msg",
|
|
* type="string",
|
|
* description="提示消息"
|
|
* ),
|
|
* @OA\Property(
|
|
* property="count",
|
|
* type="integer",
|
|
* format="int32",
|
|
* description="记录总数",
|
|
* ),
|
|
* @OA\Property(
|
|
* property="data",
|
|
* type="array",
|
|
* description="请求结果",
|
|
* @OA\Items(
|
|
* @OA\Property(
|
|
* property="base64",
|
|
* type="string",
|
|
* description="base64图片"
|
|
* ),
|
|
* @OA\Property(
|
|
* property="key",
|
|
* type="string",
|
|
* description="验证码安全标识"
|
|
* ),
|
|
* @OA\Property(
|
|
* property="md5",
|
|
* type="string",
|
|
* description="验证码md5值"
|
|
* ),
|
|
* )
|
|
* ),
|
|
* ),
|
|
* ),
|
|
* ),
|
|
* )
|
|
* 前后端分离验证码生成
|
|
*/
|
|
public function captcha()
|
|
{
|
|
$_data = CaptchaApi::create();
|
|
return send_http_status($_data);
|
|
}
|
|
|
|
/**
|
|
* 前后端分离验证码检测
|
|
* @param string $code
|
|
* @param string $key
|
|
*/
|
|
public function checkCaptcha(string $code = '', string $key = '')
|
|
{
|
|
return CaptchaApi::check($code, $key) ?: send_http_status('', 40506);
|
|
}
|
|
}
|
|
|