<?php
/**
 * Created by PhpStorm.
 * User: liupei
 * Date: 2018/1/30
 * Time: 14:25
 */

namespace app\login\controller;

use think\Controller;
use app\login\model\Token as TokenModel;

/**
 * 处理用户token
 * @package app\access\controller
 */
class Token extends Controller
{

	/**
	 * 检测token是否合法
	 * @return array
	 */
	public function check()
	{
		$token = input('post.token');

		$tokenModel = new TokenModel();
		$checkResult = $tokenModel->check($token);

		if ($checkResult == false) {

			return json(code([], $tokenModel->getErrCode(), $tokenModel->getError()));
		} else {
			return json(code(['bind_info' => json_decode($checkResult)]));
		}
	}

	public  function login(){
        $code = input('post.code');
        //var_dump($code);
        //$access_token= Session::get('access_token');

        $User =new Dingtalk\User();
        $userid =$User->code($code);
       $result= $this->create($userid);
       var_dump($result);die();
        if($userid===false){
            $data['status']=-10001;
            $data['errmsg']='登录失败';
        }else{
            $data['status']=0;
            $data['errmsg']='登录成功';
            $data['userid']=$userid;
        }
//        return json($data);
        return json(code([$data], 0, 'ok'));
    }
    public function create($uid)
    {


        //$client = data('client', 'pc');// 客户端标识phone,pc,pc_msg目前只支持3 种


        $tokenModel = new TokenModel();
//        if ($getOld == 1) {
//            $exist = $tokenModel->getUserToken($uid, $client);
//            if (!empty($exist)) {
//                // 更新过期时间
//                $tokenModel->updateExpire($exist, config('token_expire_second'));
//                return code(['token' => $exist, 'expire_second' => config('token_expire_second')]);
//            }
//        }


        $token = $tokenModel->createToken($uid);
        if ($token == false) {
            return json(code([], $tokenModel->getErrCode(), $tokenModel->getError()));
        } else {
            return json(code(['token' => $token, 'expire_second' => config('token_expire_second')]));
        }
    }

}