Browse Source

更换控制器

master
wanghongjun 2 years ago
parent
commit
e5911f906d
  1. 128
      app/controller/Passport.php
  2. 114
      app/controller/User.php
  3. 9
      route/app.php

128
app/controller/Passport.php

@ -0,0 +1,128 @@
<?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
* @author whj
* @date 2023-08-22 17:04
*/
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
* @author whj
* @date 2023-08-22 17:04
*/
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
* @author whj
* @date 2023-08-22 17:22
*/
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());
}
}
}

114
app/controller/User.php

@ -9,121 +9,7 @@ use think\facade\Request;
class User extends BaseController
{
/**
* 用户注册
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author whj
* @date 2023-08-22 17:04
*/
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
* @author whj
* @date 2023-08-22 17:04
*/
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
* @author whj
* @date 2023-08-22 17:22
*/
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());
}
}
/**
* 找回密码

9
route/app.php

@ -16,10 +16,13 @@ Route::get('think', function () {
});
Route::group('passport',function (){
Route::post('register','passport/register');
Route::post('login','passport/login');
Route::post('retrieve','passport/retrieve');
});
Route::group('user',function (){
Route::post('register','user/register');
Route::post('login','user/login');
Route::post('retrieve','user/retrieve');
Route::post('modifyPassword','user/modifyPassword')->middleware(CheckToken::class);
});

Loading…
Cancel
Save