<?php
namespace app\api\model;

use think\Model;

/**
 * 分类
 */
class Token extends Model
{

    /**
     * 表名,
     */
    protected $table = 'oauth_user';

    public function vali($json){
//        var_dump($json);die;
        $where="app_id='{$json->app_id}' and app_secret='{$json->app_secret}'";
        $list=$this->where($where)->field('group_id')->find();
        if($list){
            return $list['group_id'];
        }else{
            return false;
        }


    }
    public function save_data($data){
       $token_db= db('token');
        $where="uuid ='{$data['uuid']}'";
        $status=$token_db->where($where)->field('*')->find();
        if($status){
            $list=$token_db->where($where)->update($data);

        }else{
            $list=$token_db->insert($data);
        }


        if($list){
            return true;

        }else{
            return false;
        }


    }
    public function update_token($refresh_token){
        $where="refresh_token='$refresh_token'";
        $token_db=db('token');
        $list=$token_db->where($where)->field('*')->find();
        if($list){
//            $data['create_time']=date("Y-m-d H:i:s",time());
            $data['update_time']=date("Y-m-d H:i:s",time());
            $data['expiry_time']=date("Y-m-d H:i:s",time()+604800);
            $data['accress_token']=md5(md5( $list['uuid'].time().rand(10,19999)));
            $data['refresh_token']=md5(md5( $list['uuid'].time().rand(10,19999)));
            $up=$token_db->where('refresh_token',$refresh_token)->update($data);
            if($up){
                $data['uuid']=$list['uuid'];
                $data['remake']=$list['remake'];
                return $data;
            }else{
                return false;
            }
        }else{
            return false;
        }


    }

}