<?php

namespace app\controller;

use app\common;
use think\facade\Cache;

class HardToken extends common
{


    public function getToken()
    {
        $OperatorID = config('hard.OperatorID');
        $OperatorSecret = config('hard.OperatorSecret');

        $time = date('YmdHis');
        $Seq = config('hard.Seq');
        $mess = array('OperatorID' => $OperatorID, 'OperatorSecret' => $OperatorSecret);

        $data = json_encode($mess);
        $aes = new Aes();
        $en_data = $aes->encrypt($data);


        $de_Sig = $OperatorID . $en_data . $time . $Seq;
        $hmacmd5 = new HMACMD5();
        $Sig = $hmacmd5->HMAC($de_Sig);


        $kk['OperatorID'] = $OperatorID;
        $kk['Data'] = $en_data;
        $kk['TimeStamp'] = $time;
        $kk['Seq'] = $Seq;
        $kk['Sig'] = $Sig;
        $datas = $this->CurlSend(config('hard.url') . '/query_token', $kk);

        
        $array_data = (array)$datas;
        if ($array_data['Ret'] == 0) {
            $token = $array_data['Data'];
            $de_data = $aes->decrypt($token);

            $array_token = json_decode($de_data, true);
            $token = $array_token['AccessToken'];
            Cache::set('token', $token, 7200);
            
            return $token;
        } else {
            return 1;
        }
    }


}
