protected $beforeActionList = [ 'valid_token', ]; //验证token protected function
valid_token(){ // request()->isAjax() or die('非法请求'); $token =
request()->param('token'); if(empty($token)) { echo
js(array('errcode'=>201,'errmsg'=>'没有token')); die; } $stu =
Db::name('student')->where('token',$token)->find(); if(empty($stu)) { echo
js(array('errcode'=>202,'errmsg'=>'token无效')); die; }
if(time()>$stu['expire_time']){ //token过期 echo
js(array('errcode'=>203,'errmsg'=>'token过期')); die; } $this->classid =
$stu['classid']; $this->no = $stu['no']; $this->name = $stu['name']; }
以上是在调用各接口之前,会触发的事件。

登录的是放在另一个控制器:
public function login(){ $user = input('?user') ? input('user') : ''; $pwd =
input('?pwd') ? input('pwd') : ''; if(empty($user) || empty($pwd)){ return
js(array('errcode'=>101,'errmsg'=>'帐号或密码不能为空')); } $token =
md5(uniqid(md5(microtime(true)),true)); //生成一个不会重复的字符串 // $token =
sha1($token); //加密 $time_out = time()+3600; //过期时间 //查询学生 $where =
array('no'=>$user,'pwd'=>md5($pwd)); $stu =
Db::name('student')->where($where)->find(); if($stu){
Db::name('student')->where($where)->update(array('token'=>$token,'expire_time'=>$time_out));
return js(array('token'=>$token,'yz'=>'student')); } }
这样我们登录之后,验证token才能获取其他接口的信息。

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信