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.
311 lines
9.9 KiB
311 lines
9.9 KiB
<?php
|
|
|
|
namespace app\controller;
|
|
|
|
use app\BaseController;
|
|
use app\common\lib\sms\AliSms\AliSms;
|
|
use app\logic\InitData;
|
|
use app\model\AdminUser;
|
|
use app\model\AgentUser;
|
|
use app\model\Pincode;
|
|
use app\model\User as UserModel;
|
|
use app\validate\Passport as PassportValidate;
|
|
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(PassportValidate::class)->scene('register')->check($data);
|
|
|
|
// 验证手机号短信验证码
|
|
$userModel = new UserModel();
|
|
#$smsCode = $data['sms_code'];
|
|
$phone = $data['phone'];
|
|
$invite_code = $data['invite_code'] ?? '';
|
|
|
|
$data['aid'] = $userModel->verifyInviteCode($invite_code);
|
|
if (!empty($invite_code) && !$data['aid']) {
|
|
return $this->renderError('邀请码无效');
|
|
}
|
|
|
|
// $checkCodeRes = validate(PassportValidate::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(PassportValidate::class)->scene('login')->check($data);
|
|
|
|
# 验证码验证
|
|
if ($count = Cookie::get($cookie_name) ?: 0) {
|
|
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' => get_image_url($user['data']['avatar']), 'time' => time()];
|
|
$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(PassportValidate::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(PassportValidate::class)->scene('retrieve')->check($data);
|
|
|
|
$userModel = new UserModel();
|
|
$phone = $data['phone'];
|
|
$smsCode = $data['sms_code'];
|
|
|
|
$checkCodeRes = validate(PassportValidate::class)->checkCode($phone,$smsCode);
|
|
if ($checkCodeRes !== true) {
|
|
return $this->renderError($checkCodeRes);
|
|
}
|
|
|
|
$user = $userModel->retrieve($data);
|
|
|
|
if ($user['status']) {
|
|
return $this->renderSuccess('密码重置成功');
|
|
} 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 adminLogin()
|
|
{
|
|
$data = Request::param();
|
|
|
|
$count = 0;
|
|
$defaultCount = 3;# 默认登陆三次提示验证码
|
|
try {
|
|
|
|
$InitData = new InitData();
|
|
$InitData->init();
|
|
|
|
$cookie_name = 'login_count'.$_SERVER['HTTP_HOST'];
|
|
// 验证用户输入
|
|
validate(PassportValidate::class)->scene('adminLogin')->check($data);
|
|
|
|
# 验证码验证
|
|
if ($count = Cookie::get($cookie_name) ?: 0) {
|
|
Cookie::set($cookie_name,$count+1);
|
|
} else {
|
|
Cookie::set($cookie_name,1);
|
|
}
|
|
if ($count > $defaultCount) {
|
|
$this->validate($data,['captcha|验证码'=>'require|captcha']);
|
|
}
|
|
|
|
// 管理员登陆
|
|
$adminUser = AdminUser::login($data);
|
|
|
|
if ($adminUser['status'] == 1) {
|
|
|
|
$userinfo = ['id' => $adminUser['data']['id'], 'avatar' => get_image_url($adminUser['data']['avatar']), 'time' => time()];
|
|
$token = ['token'=>signToken($userinfo)];
|
|
|
|
Cookie::delete($cookie_name);
|
|
return $this->renderSuccess('登陆成功',$token);
|
|
} else {
|
|
throw new ValidateException($adminUser['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 agentLogin()
|
|
{
|
|
$data = Request::param();
|
|
|
|
$count = 0;
|
|
$defaultCount = 3;# 默认登陆三次提示验证码
|
|
try {
|
|
|
|
$cookie_name = 'login_count'.$_SERVER['HTTP_HOST'];
|
|
// 验证用户输入
|
|
validate(PassportValidate::class)->scene('agentLogin')->check($data);
|
|
|
|
# 验证码验证
|
|
if ($count = Cookie::get($cookie_name) ?: 0) {
|
|
Cookie::set($cookie_name,$count+1);
|
|
} else {
|
|
Cookie::set($cookie_name,1);
|
|
}
|
|
if ($count > $defaultCount) {
|
|
$this->validate($data,['captcha|验证码'=>'require|captcha']);
|
|
}
|
|
|
|
// 管理员登陆
|
|
$agentUser = AgentUser::login($data);
|
|
|
|
if ($agentUser['status'] == 1) {
|
|
|
|
$userinfo = ['id' => $agentUser['data']['id'], 'avatar' => get_image_url($agentUser['data']['avatar']), 'time' => time()];
|
|
$token = ['token'=>signToken($userinfo)];
|
|
|
|
Cookie::delete($cookie_name);
|
|
return $this->renderSuccess('登陆成功',$token);
|
|
} else {
|
|
throw new ValidateException($agentUser['msg']);
|
|
}
|
|
} catch (ValidateException $exception) {
|
|
|
|
$data = ['captcha_img' => ''];
|
|
if ($count >= $defaultCount) $data['captcha_img'] = captcha_src();
|
|
return $this->renderError($exception->getMessage(),$data);
|
|
}
|
|
}
|
|
}
|