刮刮后端接口
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.

205 lines
6.3 KiB

<?php
namespace app\controller;
use app\BaseController;
use app\common\lib\sms\AliSms\AliSms;
use app\model\Pincode;
use app\model\User as UserModel;
use app\validate\User as UserValidate;
use think\exception\ValidateException;
use think\facade\Cookie;
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('邀请码无效');
}
$checkCodeRes = validate(UserValidate::class)->checkCode($phone,$smsCode);
if ($checkCodeRes !== true) {
return $this->renderError($checkCodeRes);
}
// 注册用户
if ($userModel->register($data)) {
Cookie::delete('send_code'.$_SERVER['HTTP_HOST']);
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();
$count = 0;
$defaultCount = 3;
try {
$cookie_name = 'login_count'.$_SERVER['HTTP_HOST'];
// 验证用户输入
validate(UserValidate::class)->scene('login')->check($data);
# 验证码验证
if ($count = Cookie::get($cookie_name)) {
Cookie::set($cookie_name,$count+1);
} else {
Cookie::set($cookie_name,1);
}
if ($count > $defaultCount) {
$this->validate($data,['captcha|验证码'=>'require|captcha']);
}
// 用户登录
$userModel = new UserModel();
$user = $userModel->login($data);
if ($user['status']) {
$userinfo = ['id' => $user['data']['id'], 'avatar' => $user['data']['avatar']];
$token = ['token'=>signToken($userinfo)];
Cookie::delete($cookie_name);
return $this->renderSuccess('登陆成功',$token);
} else {
throw new ValidateException($user['msg']);
}
} catch (ValidateException $exception) {
$data = ['captcha_img' => ''];
if ($count >= $defaultCount) $data['captcha_img'] = captcha_src();
return $this->renderError($exception->getMessage(),$data);
}
}
/**
* 发送短信验证码
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function sendCode()
{
$data = Request::param();
try {
$cookie_name = 'send_code'.$_SERVER['HTTP_HOST'];
$send_time = 60;
validate(UserValidate::class)->scene('sendCode')->check($data);
# 限制短信验证码60发送一次
if ($time = Cookie::get($cookie_name)) {
$s = time() - $time;
if ($s < $send_time) {
return $this->renderError("请等待 {$s} 秒后操作",['residue_time' => $s]);
} else {
Cookie::delete($cookie_name);
}
}
$phone = $data['phone'];
$code = rand(1000 , 9999);
# 发送短信
$result = AliSms::send($phone,$code);
if (!$result['status']) {
throw new ValidateException($result['msg']);
}
# 占记录数据库
$Pincode = new Pincode();
$res = $Pincode->sendSave($phone,$code);
if ($res['status']) {
# 记录短信验证码发送时间
Cookie::set($cookie_name,time());
return $this->renderSuccess('发送成功' , ['code' => $code]);
} else {
throw new ValidateException('发送失败');
}
} catch (ValidateException $exception) {
return $this->renderError($exception->getMessage());
}
}
/**
* 变换验证码图片
* @return array
*/
public function changeCaptcha()
{
return $this->renderSuccess('数据返回成功',['captcha_img' => captcha_src()]);
}
/**
* 找回密码
* @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());
}
}
}