Commit 78039aee by 庄钊鑫

完整注册模块登录模块

parent 5b1320a3
...@@ -93,6 +93,7 @@ class Send extends Base ...@@ -93,6 +93,7 @@ class Send extends Base
if ($info === false) { if ($info === false) {
$this->returnmsg(403, [], [], "", [], "验证码错误"); $this->returnmsg(403, [], [], "", [], "验证码错误");
} }
$this->render(200,['success'=>'验证码正确']); $success['success']['message'] = '验证码正确!';
$this->render(200,$success);
} }
} }
...@@ -3,12 +3,13 @@ ...@@ -3,12 +3,13 @@
namespace app\api\controller\v1\app; namespace app\api\controller\v1\app;
use app\api\controller\Api; use app\api\controller\Api;
use app\api\model\UserToken; use app\api\model\Token;
use app\common\tools\wechatUtil; use app\common\tools\wechatUtil;
use think\Db; use think\Db;
use app\api\logic\app\SendLogic; use app\api\logic\app\SendLogic;
use app\api\model\User as UserModel; use app\api\model\User as UserModel;
use app\api\logic\app\UserLogic; use app\api\logic\app\UserLogic;
use think\Cache;
/** /**
* 所有资源类接都必须继承基类控制器 * 所有资源类接都必须继承基类控制器
...@@ -96,16 +97,11 @@ class User extends Base ...@@ -96,16 +97,11 @@ class User extends Base
*/ */
public function login() public function login()
{ {
$requestData = $this->selectParam(['mobile', 'code','oauth_uuid'=>""]); $requestData = $this->selectParam(['mobile', 'password']);
$this->check($requestData, 'Base.login'); $this->check($requestData, 'Base.cmsLogin');
$requestData['sms_type'] = 'login'; $result = UserModel::build()->login($requestData);
$bool = SendLogic::validateCode($requestData);
if (!$bool) {
self::returnmsg(403, [], [], 'service_message', 'code error.', '验证码错误');
}
$result = UserModel::build()->login($requestData['mobile'],$requestData['oauth_uuid']);
if ($result) { if ($result) {
$this->render($result); $this->render(200,$result);
} else { } else {
$this->returnmsg(403, $data = [], $header = [], $type = "", "Login error", $message = "登录失败"); $this->returnmsg(403, $data = [], $header = [], $type = "", "Login error", $message = "登录失败");
} }
...@@ -166,9 +162,14 @@ class User extends Base ...@@ -166,9 +162,14 @@ class User extends Base
*/ */
public function validateMobile() public function validateMobile()
{ {
$requestData = $this->selectParam(['mobile','sms_type' => 'bind']); $request = $this->selectParam(['mobile','sms_type' => 'bind']);
$this->check($requestData, 'Base.sendSms'); $this->check($request, 'Base.sendSms');
$this->render(200,['success'=>'手机号码验证成功']); $bool = UserLogic::checkUser($request['mobile']);
if(!empty($bool)){
$this->returnmsg(403, [], [],"", [], "该手机号已注册");
}
$success['success']['message'] = '手机号码验证成功';
$this->render(200,$success);
} }
/** /**
...@@ -190,13 +191,18 @@ class User extends Base ...@@ -190,13 +191,18 @@ class User extends Base
$this->returnmsg(403, [], [],"", [], "该手机号已注册"); $this->returnmsg(403, [], [],"", [], "该手机号已注册");
} }
} }
//$info = true; $tokenId = md5(md5(time()));
$info = SendLogic::SendSms($request['mobile'], $request['sms_type']); Cache::set($tokenId,$request['mobile'],1800);
if(Cache::get($tokenId)){
$info = true;
//$info = SendLogic::SendSms($request['mobile'], $request['sms_type']);
if ($info === false) { if ($info === false) {
$this->returnmsg(403, [], [],"", [], "信息发送出现了一点小问题哟~"); $this->returnmsg(403, [], [],"", [], "信息发送出现了一点小问题哟~");
} }
//发送成功 $success['success']['token'] = $tokenId;
$this->render(200,['success'=>'验证码发送成功']); $success['success']['message'] = '验证码发送成功';
$this->render(200,$success);
}
} }
/** /**
...@@ -209,15 +215,21 @@ class User extends Base ...@@ -209,15 +215,21 @@ class User extends Base
*/ */
public function register() public function register()
{ {
$request = $this->selectParam(['mobile', 'password','password2']); $request = $this->selectParam(['mobile', 'password','password2','token']);
$this->check($request, 'Base.register'); $this->check($request, 'Base.register');
$result = Token::validateToken2($request['token'],$request['mobile']);
if($result['status'] == 0){
$this->returnmsg(403, [], [],"", [], $result['message']);
}
$bool = UserLogic::checkUser($request['mobile']); $bool = UserLogic::checkUser($request['mobile']);
if(!empty($bool)){ if(!empty($bool)){
$this->returnmsg(403, [], [],"", [], "该手机号已注册"); $this->returnmsg(403, [], [],"", [], "该手机号已注册");
} }
$result = UserModel::build()->register($request); $result = UserModel::build()->register($request);
if ($result) { if ($result) {
$this->render(200,['success'=>'注册成功!']); Cache::rm($request['token']);
$success['success']['message'] = '注册成功!';
$this->render(200,$success);
} else { } else {
$this->returnmsg(403, $data = [], $header = [], $type = "", "Login error", $message = "注册失败"); $this->returnmsg(403, $data = [], $header = [], $type = "", "Login error", $message = "注册失败");
} }
......
...@@ -3,16 +3,16 @@ ...@@ -3,16 +3,16 @@
namespace app\api\model; namespace app\api\model;
use think\Model; use think\Model;
use think\Cache;
/** /**
* TOKEN * TOKEN
*/ */
class UserToken extends Model class Token extends Model
{ {
/** /**
* 表名 * 表名
*/ */
protected $table = 'user_token'; protected $table = 'token';
protected $dateFormat = false; protected $dateFormat = false;
/** /**
...@@ -55,6 +55,30 @@ class UserToken extends Model ...@@ -55,6 +55,30 @@ class UserToken extends Model
} }
/** /**
* @author:
* @time: 2019年3月
* description
* @param $token
* @return array|bool|false|\PDOStatement|string|Model
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function validateToken2($tokenId,$mobile)
{
$getMobile = Cache::get($tokenId);
if($getMobile){
if($getMobile == $mobile){
return ['status' => 1];
}else{
return ['status' => 0,'message' => '手机号码不一致!'];
}
}
return ['status' => 0,'message' => 'token已失效!'];
}
/**
* @author: Airon * @author: Airon
* @time: 2018年5月 * @time: 2018年5月
* description * description
......
...@@ -37,55 +37,32 @@ class User extends Model ...@@ -37,55 +37,32 @@ class User extends Model
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException * @throws \think\exception\DbException
*/ */
public function login($mobile,$oauth_uuid) public function login($requestData)
{ {
$user = self::build()->where(['mobile' => $mobile]) $user = self::build()->where(['mobile' => $requestData['mobile'],'password' => md5(md5($requestData['password']))])
->field(['nickname','is_deleted','ban_time'],true)->find(); ->field(['password'],true)->find();
Db::startTrans();
if ($user) { if ($user) {
if ($user['is_banned']) { // if ($user['is_banned']) {
ResponseDirect('当前账号已被禁用',403); // ResponseDirect('当前账号已被禁用',403);
} // }
if(!empty($oauth_uuid)){ // if(!empty($oauth_uuid)){
$oauth= UserOauth::build()->where(['uuid'=>$oauth_uuid])->find(); // $oauth= UserOauth::build()->where(['uuid'=>$oauth_uuid])->find();
if(!empty($oauth)){ // if(!empty($oauth)){
UserOauth::build()->where(['user_uuid'=>$user['uuid']])->update(['user_uuid'=>""]); // UserOauth::build()->where(['user_uuid'=>$user['uuid']])->update(['user_uuid'=>""]);
UserOauth::build()->where(['uuid'=>$oauth_uuid])->update(['user_uuid'=>$user['uuid']]); // UserOauth::build()->where(['uuid'=>$oauth_uuid])->update(['user_uuid'=>$user['uuid']]);
//
} // }
} // }
$token = UserToken::storeToken($user['uuid']); $token = Token::storeToken($user['uuid']);
} else { }else{
//创建新用户
$user['uuid'] = uuid();
$user['mobile'] = $mobile;
$user['name'] = replaceToStar($mobile);
$user['create_time'] = timeToDate(time());
// $user['invite_code'] = $this->get_unique_invite_code();
$result = self::build()->insert($user);
if ($result) {
//创建token
$token = UserToken::storeToken($user['uuid']);
if(!empty($oauth_uuid)){
$oauth= UserOauth::build()->where(['uuid'=>$oauth_uuid])->find();
if(!empty($oauth)){
UserOauth::build()->where(['uuid'=>$oauth_uuid])->update(['user_uuid'=>$user['uuid']]);
}
}
} else {
Db::rollback();
return false; return false;
} }
}
Db::commit();
$users['token'] = $token; $users['token'] = $token;
$users['user_info'] = self::build() $users['user_info'] = self::build()
->where(['mobile'=>$mobile]) ->where(['mobile' => $requestData['mobile'],'password' => md5(md5($requestData['password']))])
->field(['nickname','is_deleted','ban_time'],true) ->field(['name'],false)
->find(); ->find();
return $users; return $users;
...@@ -107,6 +84,7 @@ class User extends Model ...@@ -107,6 +84,7 @@ class User extends Model
$user['mobile'] = $request['mobile']; $user['mobile'] = $request['mobile'];
$user['name'] = replaceToStar($request['mobile']); $user['name'] = replaceToStar($request['mobile']);
$user['create_time'] = timeToDate(time()); $user['create_time'] = timeToDate(time());
$user['password'] = md5(md5($request['password']));
$result = self::build()->insert($user); $result = self::build()->insert($user);
if (!$result) { if (!$result) {
Db::rollback(); Db::rollback();
...@@ -117,4 +95,6 @@ class User extends Model ...@@ -117,4 +95,6 @@ class User extends Model
return true; return true;
} }
} }
\ No newline at end of file
...@@ -23,8 +23,8 @@ class Base extends Validate ...@@ -23,8 +23,8 @@ class Base extends Validate
'sms_type' => 'require|in:login,bind,pay_password,pass', 'sms_type' => 'require|in:login,bind,pay_password,pass',
'partner_sms_type' => 'require|in:pass,set_pay,bind_card', 'partner_sms_type' => 'require|in:pass,set_pay,bind_card',
'code' => 'require|max:6', 'code' => 'require|max:6',
'password' => 'require|min:6|confirm:password2', 'password' => 'require|min:6',
'password2' => 'require', 'password2' => 'require|confirm:password',
'message_type' => 'require|in:con,chat,system', 'message_type' => 'require|in:con,chat,system',
'province' => 'chsAlpha', 'province' => 'chsAlpha',
'school_name' => 'chsAlpha', 'school_name' => 'chsAlpha',
...@@ -38,7 +38,7 @@ class Base extends Validate ...@@ -38,7 +38,7 @@ class Base extends Validate
'keyword' => 'chsDash', 'keyword' => 'chsDash',
'refresh_token' => 'require|length:32', 'refresh_token' => 'require|length:32',
'collect_type' => 'require|in:school,speciality', 'collect_type' => 'require|in:school,speciality',
'token' => 'require',
'name' => 'require|chsAlphaNum|length:0,64', 'name' => 'require|chsAlphaNum|length:0,64',
'gender' => 'require|in:男,女', 'gender' => 'require|in:男,女',
'birth' => 'require|chsAlphaNum|length:0,64', 'birth' => 'require|chsAlphaNum|length:0,64',
...@@ -62,7 +62,8 @@ class Base extends Validate ...@@ -62,7 +62,8 @@ class Base extends Validate
'code' => '验证码格式错误', 'code' => '验证码格式错误',
'password.require' => '密码必须', 'password.require' => '密码必须',
'password.min' => '密码过短', 'password.min' => '密码过短',
'password.confirm' => '两次密码输入不一致', 'password2.confirm' => '两次密码输入不一致',
'token.require' => 'token必须',
'message_type' => '消息类型错误', 'message_type' => '消息类型错误',
'province' => '省份设置错误', 'province' => '省份设置错误',
'city.json' => '城市设置错误', 'city.json' => '城市设置错误',
...@@ -88,7 +89,7 @@ class Base extends Validate ...@@ -88,7 +89,7 @@ class Base extends Validate
'id' => ['id'], 'id' => ['id'],
'uuid' => ['uuid'], 'uuid' => ['uuid'],
'sendSms' => ['mobile', 'sms_type'], 'sendSms' => ['mobile', 'sms_type'],
'register' => ['mobile','password','password2'], 'register' => ['mobile','password','password2','token'],
'sendPartnerSms' => ['mobile', 'sms_type_partner'], 'sendPartnerSms' => ['mobile', 'sms_type_partner'],
'validatePartnerCode' => ['mobile', 'sms_type_partner', 'code'], 'validatePartnerCode' => ['mobile', 'sms_type_partner', 'code'],
...@@ -103,7 +104,7 @@ class Base extends Validate ...@@ -103,7 +104,7 @@ class Base extends Validate
'refresh_token' => ['refresh_token'], 'refresh_token' => ['refresh_token'],
'feedback' => ['content'], 'feedback' => ['content'],
'collect_list' => ['collect_type'], 'collect_list' => ['collect_type'],
'cmsLogin' => ['name', 'password'], 'cmsLogin' => ['mobile', 'password'],
'admissionScore' => ['province','school_name'], 'admissionScore' => ['province','school_name'],
'batchCritical' => ['province'] 'batchCritical' => ['province']
......
<?php
//000000001800
exit();?>
s:11:"13302516724";
\ No newline at end of file
<?php
//000000001800
exit();?>
s:32:"1d6874d9fa1c1288e3cf7ed27fca63d2";
\ No newline at end of file
<?php
//000000001800
exit();?>
s:11:"13302516724";
\ No newline at end of file
<?php
//000000001800
exit();?>
s:32:"062a1c9363e70287295731c632e8efba";
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment