Token.php 793 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class Token extends Model
  7. {
  8. public function updateToken($userId){
  9. $info = $this->where('user_id',$userId)->find();
  10. $token = md5($userId.'-'.time().rand(1000,9999));
  11. if($info){
  12. $ret = $this->save(['code'=>$token],['id'=>$info['id']]);
  13. }else{
  14. $ret = $this->save([
  15. 'code' => $token,
  16. 'user_id' => $userId
  17. ]);
  18. }
  19. if(!$ret){
  20. return false;
  21. }
  22. $data = $this->where('user_id',$userId)->find();
  23. return $data->toArray();
  24. }
  25. public function logout($userId){
  26. $this->where('user_id',$userId)->setField('code','');
  27. return true;
  28. }
  29. }