<?php
namespace app\common\model;

use app\hander\HelpHander;
use think\Db;
use think\Model;

class Token extends Model
{

    public function updateToken($userId){
        $info = Db::name('token')->where('user_id',$userId)->find();
        $token = create_token($userId);
        if($info){
            $ret = Db::name('token')->where('id',$info['id'])->setField('token',$token);
        }else{
            $ret = Db::name('token')->insert([
                'token' => $token,
                'user_id' => $userId
            ]);
        }
        if(!$ret){
            $this->error = 'token更新失败';
            return false;
        }
        return $token;
    }

    public function logout($userId){
        $this->where('user_id',$userId)->setField('token','');
        return true;
    }
}