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.
122 lines
3.6 KiB
122 lines
3.6 KiB
<?php
|
|
|
|
namespace app\controller;
|
|
|
|
use app\BaseController;
|
|
use app\model\User as UserModel;
|
|
use app\validate\User as UserValidate;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Request;
|
|
|
|
class Passport extends BaseController
|
|
{
|
|
/**
|
|
* 用户注册
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function register()
|
|
{
|
|
$data = Request::param();
|
|
|
|
try {
|
|
// 验证用户输入
|
|
validate(UserValidate::class)->scene('register')->check($data);
|
|
|
|
// 验证手机号短信验证码
|
|
$userModel = new UserModel();
|
|
$smsCode = $data['sms_code'];
|
|
$phone = $data['phone'];
|
|
|
|
if (!isset($data['invite_code'])) $data['invite_code'] = '';
|
|
$invite_code = $data['invite_code'];
|
|
|
|
if (!empty($invite_code) && !$userModel->verifyInviteCode($invite_code)) {
|
|
return $this->renderError('邀请码无效');
|
|
}
|
|
|
|
if (!$userModel->verifySmsCode($phone, $smsCode)) {
|
|
return $this->renderError('短信验证码错误');
|
|
}
|
|
|
|
// 注册用户
|
|
if ($userModel->register($data)) {
|
|
return $this->renderSuccess('注册成功');
|
|
} else {
|
|
return $this->renderSuccess('手机号已注册');
|
|
}
|
|
} catch (ValidateException $exception) {
|
|
return $this->renderError($exception->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 用户登录
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function login()
|
|
{
|
|
$data = Request::param();
|
|
|
|
try {
|
|
// 验证用户输入
|
|
validate(UserValidate::class)->scene('login')->check($data);
|
|
|
|
// 用户登录
|
|
$userModel = new UserModel();
|
|
$user = $userModel->login($data);
|
|
|
|
if ($user['status']) {
|
|
|
|
$userinfo = ['id' => $user['data']['id'], 'username' => $user['data']['username']];
|
|
$token = ['token'=>signToken($userinfo)];
|
|
|
|
return $this->renderSuccess('登陆成功',$token);
|
|
} else {
|
|
return $this->renderError($user['msg']);
|
|
}
|
|
} catch (ValidateException $exception) {
|
|
return $this->renderError($exception->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 找回密码
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function retrieve()
|
|
{
|
|
$data = Request::param();
|
|
|
|
try {
|
|
// 验证用户输入
|
|
validate(UserValidate::class)->scene('retrieve')->check($data);
|
|
|
|
$userModel = new UserModel();
|
|
$phone = $data['phone'];
|
|
$smsCode = $data['sms_code'];
|
|
|
|
if (!$userModel->verifySmsCode($phone, $smsCode)) {
|
|
return $this->renderError('短信验证码错误');
|
|
}
|
|
|
|
$user = $userModel->retrieve($data);
|
|
|
|
if ($user['status']) {
|
|
return $this->renderSuccess('密码重置成功');
|
|
} else {
|
|
return $this->renderError($user['msg']);
|
|
}
|
|
} catch (ValidateException $exception) {
|
|
return $this->renderError($exception->getMessage());
|
|
}
|
|
}
|
|
}
|