123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- use think\Db;
- /**
- * @param $account
- * @return string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- function makeToken($account){
- $now=time();
- $str = $account['username'].$account['salt'].$now;
- $token = base64_encode($str);
- $has = Db::name("account_token")->where(["accountid"=>$account['id']])->find();
- if($has){
- Db::name("account_token")->where(["accountid"=>$account['id']])->update(["token"=>$token,"expiretime"=>date("Y-m-d H:i:s",$now+1800)]);
- }else{
- Db::name("account_token")->insert(["token"=>$token,"expiretime"=>date("Y-m-d H:i:s",$now+1800),
- "accountid"=>$account['id']]);
- }
- return $token;
- }
- /**
- * @param $token
- */
- function verifyToken($token){
- $has = Db::name("account_token")->where(["token"=>$token])->find();
- if(!$has){
- return ["code"=>101,"msg"=>"token不存在"];
- }
- if(strtotime($has['expiretime'])<=time()){
- return ["code"=>102,"msg"=>"token已失效"];
- }
- $account = Db::name("account")->where(["id"=>$has['accountid'],"is_del"=>0])->find();
- if(!$account){
- return ["code"=>103,"msg"=>"未找到账户"];
- }
- if(strtotime($account['expiretime'])<=time()){
- return ["code"=>104,"msg"=>"账户已失效"];
- }
- $token_str = base64_decode($token);
- $account_str= substr($token_str,0,-10);
- if($account_str==$account['username'].$account['salt']){
- Db::name("account_token")->where(["token"=>$token])->update(["expiretime"=>date("Y-m-d H:i:s",time()+1800)]);
- return ["code"=>0,"msg"=>"账户有效"];
- }else{
- return ["code"=>105,"msg"=>"账户token无效"];
- }
- }
|