Token.php
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace ysopen;
use fast\Http;
use think\Db;
class Token{
protected $AppKey = "7533d3ef6bba426aae2bb0402211ef10";
protected $Secret = "ca22701530d233992c5e979f3cf8090b";
public function _initialize(){
$this->checkToken();
}
public function getToken(){
$url = 'https://open.ys7.com/api/lapp/token/get';
$http = new Http;
$data['appKey'] = $this->AppKey;
$data['appSecret'] = $this->Secret;
$res = json_decode($http->post($url,$data),true);
if($res['code'] == '200'){
$code = Db::name('reservoir_hkws_token')->where('1=1')->update(['access_token'=>$res['data']['accessToken'],'expire_time'=>$res['data']['expireTime']]);
}
else{
Db::name('reservoir_hkws_log')->insert(['message_log'=>json_encode($res)]);
echo '获取token出错';
}
}
public function token(){
$info = Db::name('reservoir_hkws_token')->find();
$time = $this->getMillisecond();
if($info['expire_time'] && $time >= $info['expire_time']){
$this->getToken();
$this->checkToken();
}
else{
return $info['access_token'];
}
}
public static function getMillisecond(){
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectimes = substr($msectime,0,13);
}
}