php管理和接口
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.
 
 
 
 
 
 

79 lines
2.3 KiB

<?php
namespace app\api\controller;
use app\api\middleware\LcJWTAuth;
use app\api\service\LcJWTService;
use app\api\service\UserService;
use think\Controller;
use think\exception\ValidateException;
use think\response\Json;
/**
* 用户账户控制器
* 功能:MVC 中的C,处理用户输入和反馈给用户
*/
class Passport extends ApiController
{
protected $middleware
= [
LcJWTAuth::class => [
'except' => ['login', 'register']
]
];
/**
* 登录
*/
public function login(): Json
{
if (!$this->request->isPost()) {
return $this->renderError('不支持GET请求');
}
$data = $this->postData();
try {
validate()->rule([
'uname|用户名' => 'require',
'upass|密码' => 'require',
])->check($data);
} catch (ValidateException $v) {
return $this->renderError($v->getMessage());
}
$model = new UserService;
if (($userInfo = $model->login($data['uname'], $data['upass'])) === false) {
return $this->renderError($model->getError() ?: '登录失败');
}
return $this->renderSuccess([
'userId' => $userInfo['uid'],
'token' => LcJWTService::createToken($userInfo['uid'], $data['uname'])
], '登录成功');
}
/**
* 用户注册
* @return Json
*/
public function register(): Json
{
if (!$this->request->isPost()) {
return $this->renderError('不支持GET请求');
}
$post = $this->postData();
try {
validate()->rule([
'uname|用户名' => 'require',
'upass|密码' => 'require',
'qr_upass|确认密码' => 'require|confirm:upass',
'phone|手机号' => 'require|mobile'
])->check($post);
} catch (ValidateException $v) {
return $this->renderError($v->getMessage());
}
$model = new UserService;
if (($userInfo = $model->register($post)) === false) {
return $this->renderError($model->getError() ?: '注册失败');
}
return $this->renderSuccess("注册成功");
}
}