Commit 45fa38dd by 庄钊鑫

token修改,返回参数格式修改

parent 78039aee
......@@ -8,6 +8,7 @@ namespace app\api\controller;
use think\Controller;
use think\exception\HttpResponseException;
use think\Request;
use think\Cache;
class Api extends Controller
{
......@@ -159,6 +160,28 @@ class Api extends Controller
}
/**
* 验证header参数
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
*/
protected function checkHeader($key,$mobile)
{
$info = Request::instance()->header();
if(isset($info[$key])){
$getMobile = Cache::get($info[$key]);
$result = $getMobile ?
(($getMobile == $mobile) ? ['status' => 1,'message' => $info[$key]] : ['status' => 0,'message' => '手机号码不一致!'])
: ['status' => 0,'message' => 'token已失效!'];
return $result;
}else{
self::returnmsg(403, [], [], "", "param error", "获取token失败!");
}
}
/**
* 获取页码和每页数量
* @param int $defaultIndex
* @param int $defaultSize
......
......@@ -147,7 +147,9 @@ trait Send
{
switch ($code) {
case 200:
$error = "SUCCESS";
$error['success']['type'] = "SUCCESS";
$error['success']['reason'] = empty($reason) ? "param missing" : $reason;
$error['success']['message'] = empty($message) ? "请求体不完整" : $message;
break;
case 400:
$error['error']['type'] = "BAD REQUEST";
......
......@@ -15,6 +15,7 @@ use think\Controller;
use app\api\logic\app\SendLogic;
use app\common\tools\Guodulink;
use app\api\logic\app\UserLogic;
use think\Cache;
/**
* 所有资源类接都必须继承基类控制器
......@@ -93,7 +94,13 @@ class Send extends Base
if ($info === false) {
$this->returnmsg(403, [], [], "", [], "验证码错误");
}
$success['success']['message'] = '验证码正确!';
$this->render(200,$success);
$tokenId = md5(md5(time()));
Cache::set($tokenId,$request['mobile'],1800);
if(Cache::get($tokenId)){
$result['token'] = $tokenId;
}
$result['message'] = '验证码正确!';
$this->returnmsg(200,[],[],"validateCode","validateCode success",$result);
}
}
......@@ -101,7 +101,7 @@ class User extends Base
$this->check($requestData, 'Base.cmsLogin');
$result = UserModel::build()->login($requestData);
if ($result) {
$this->render(200,$result);
$this->returnmsg(200,[],[],"login","login success",$result);
} else {
$this->returnmsg(403, $data = [], $header = [], $type = "", "Login error", $message = "登录失败");
}
......@@ -151,7 +151,7 @@ class User extends Base
}
$data = UserLogic::publicLogin($requestData);
$data !== false ? $this->render($data) : $this->returnmsg(403,"","","","","登录失败");
$data !== false ? $this->render($data) : $this->returnmsg(403,[],[],"","","登录失败");
}
......@@ -164,10 +164,10 @@ class User extends Base
{
$request = $this->selectParam(['mobile','sms_type' => 'bind']);
$this->check($request, 'Base.sendSms');
$bool = UserLogic::checkUser($request['mobile']);
if(!empty($bool)){
$this->returnmsg(403, [], [],"", [], "该手机号已注册");
}
// $bool = UserLogic::checkUser($request['mobile']);
// if(!empty($bool)){
// $this->returnmsg(403, [], [],"", [], "该手机号已注册");
// }
$success['success']['message'] = '手机号码验证成功';
$this->render(200,$success);
}
......@@ -191,18 +191,12 @@ class User extends Base
$this->returnmsg(403, [], [],"", [], "该手机号已注册");
}
}
$tokenId = md5(md5(time()));
Cache::set($tokenId,$request['mobile'],1800);
if(Cache::get($tokenId)){
$info = true;
//$info = SendLogic::SendSms($request['mobile'], $request['sms_type']);
if ($info === false) {
$this->returnmsg(403, [], [],"", [], "信息发送出现了一点小问题哟~");
}
$success['success']['token'] = $tokenId;
$success['success']['message'] = '验证码发送成功';
$this->render(200,$success);
//$info = true;
$info = SendLogic::SendSms($request['mobile'], $request['sms_type']);
if ($info === false) {
$this->returnmsg(403, [], [],"", [], "信息发送出现了一点小问题哟~");
}
$this->returnmsg(200,[],[],"sendCode","sendCode success",'验证码发送成功');
}
/**
......@@ -215,11 +209,11 @@ class User extends Base
*/
public function register()
{
$request = $this->selectParam(['mobile', 'password','password2','token']);
$request = $this->selectParam(['mobile', 'password','password2']);
$this->check($request, 'Base.register');
$result = Token::validateToken2($request['token'],$request['mobile']);
if($result['status'] == 0){
$this->returnmsg(403, [], [],"", [], $result['message']);
$headerResult = $this->checkHeader('regitser-access-token',$request['mobile']);
if($headerResult['status'] == 0){
$this->returnmsg(403, [], [],"", [], $headerResult['message']);
}
$bool = UserLogic::checkUser($request['mobile']);
if(!empty($bool)){
......@@ -227,9 +221,8 @@ class User extends Base
}
$result = UserModel::build()->register($request);
if ($result) {
Cache::rm($request['token']);
$success['success']['message'] = '注册成功!';
$this->render(200,$success);
Cache::rm($headerResult['message']);
$this->returnmsg(200,[],[],"register","register success",$result);
} else {
$this->returnmsg(403, $data = [], $header = [], $type = "", "Login error", $message = "注册失败");
}
......
......@@ -68,14 +68,17 @@ class Token extends Model
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已失效!'];
$result = $getMobile ?
(($getMobile == $mobile) ? ['status' => 1] : ['status' => 0,'message' => '手机号码不一致!'])
: ['status' => 0,'message' => 'token已失效!'];
// if($getMobile){
// if($getMobile == $mobile){
// return ['status' => 1];
// }else{
// return ['status' => 0,'message' => '手机号码不一致!'];
// }
// }
return $result;
}
/**
......
......@@ -85,14 +85,15 @@ class User extends Model
$user['name'] = replaceToStar($request['mobile']);
$user['create_time'] = timeToDate(time());
$user['password'] = md5(md5($request['password']));
$result = self::build()->insert($user);
self::build()->insert($user);
$result = $this->login(['mobile' => $request['mobile'],'password' => $request['password']]);
if (!$result) {
Db::rollback();
return false;
}
Db::commit();
return true;
return $result;
}
......
......@@ -38,7 +38,7 @@ class Base extends Validate
'keyword' => 'chsDash',
'refresh_token' => 'require|length:32',
'collect_type' => 'require|in:school,speciality',
'token' => 'require',
'name' => 'require|chsAlphaNum|length:0,64',
'gender' => 'require|in:男,女',
'birth' => 'require|chsAlphaNum|length:0,64',
......@@ -63,7 +63,7 @@ class Base extends Validate
'password.require' => '密码必须',
'password.min' => '密码过短',
'password2.confirm' => '两次密码输入不一致',
'token.require' => 'token必须',
'message_type' => '消息类型错误',
'province' => '省份设置错误',
'city.json' => '城市设置错误',
......@@ -89,7 +89,7 @@ class Base extends Validate
'id' => ['id'],
'uuid' => ['uuid'],
'sendSms' => ['mobile', 'sms_type'],
'register' => ['mobile','password','password2','token'],
'register' => ['mobile','password','password2'],
'sendPartnerSms' => ['mobile', 'sms_type_partner'],
'validatePartnerCode' => ['mobile', 'sms_type_partner', 'code'],
......
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