10
10
11
11
12
12
use Curl \Http ;
13
- use Illuminate \Support \Facades \Storage ;
13
+ use Illuminate \Support \Facades \Cache ;
14
14
15
15
class AccessToken
16
16
{
17
+ /**
18
+ * @var int 默认缓存 30 天
19
+ */
17
20
protected $ expiresIn = 2592000 ;
18
21
19
22
/**
@@ -28,26 +31,11 @@ class AccessToken
28
31
*/
29
32
protected $ header = [];
30
33
31
- /**
32
- * @var string|array 模拟表单提交的数据,发送 JSON 数据必须定义为 String
33
- * 如:{"username":"admin","password":"123456"}
34
- */
35
- protected $ params = 'api=11959004&grant_type=client_credentials&client_id=yxGSiFRIbfdl7WwGGXIGlmnR&client_secret=nE2rXH6ScEUg8MifDPbTzCrD5SHoC4vh ' ;
36
-
37
- private $ fileName = '' ;
38
-
39
- private $ url ;
40
34
private static $ instance ;
41
35
42
36
/**
43
37
* TokenUtil constructor.
44
38
* @param array $config
45
- *
46
- * $config = [
47
- * 'url' => "https://api.yii2.wang/v1/authorize",
48
- * 'expiresIn' => '86400', // 24H
49
- * 'params' => ['username' => '13776036576', 'password' => '123456'],
50
- * ];
51
39
*/
52
40
private function __construct ($ config )
53
41
{
@@ -70,22 +58,30 @@ public static function getInstance($config = [])
70
58
* @param bool $bool 是否强制刷新缓存 token
71
59
* @return bool|string
72
60
*/
73
- public function getToken ($ bool = false )
61
+ public function getBaiDuToken ($ bool = false )
74
62
{
75
- try {
76
- $ data = Storage::get ('token.txt ' );
77
- } catch (\Exception $ e ) {
78
- return $ this ->buildAccessToken ();
79
- }
80
- $ store = json_decode ($ data , true );
81
- // 1. 强制获取,文件不存在,文件过期 =》 刷新 Token
82
- if ($ bool || empty ($ store ['create_at ' ]) || time () - $ store ['create_at ' ] > $ this ->expiresIn ) {
83
- // HTTP 请求获取 token 并写入文件缓存
84
- return $ this ->buildAccessToken ();
85
- }
86
-
87
- // 2. 在有效期内,直接读取返回
88
- return $ store ['access_token ' ];
63
+ $ bool && Cache::forget ('baidu_access_token ' );
64
+
65
+ return Cache::remember ('baidu_access_token ' , $ this ->expiresIn , function () {
66
+ $ url = env ('BAIDU_token_url ' ) ?? 'https://aip.baidubce.com/oauth/2.0/token ' ;
67
+ $ curl = new Http ();
68
+ $ params = [
69
+ 'api ' => env ('BAIDU_api ' ),
70
+ 'grant_type ' => env ('BAIDU_grant_type ' ),
71
+ 'client_id ' => env ('BAIDU_client_id ' ),
72
+ 'client_secret ' => env ('BAIDU_client_secret ' ),
73
+ ];
74
+
75
+ $ httpResult = $ curl ->request ($ url , $ params , 'post ' , 5 , $ this ->header );
76
+
77
+ // 2. 写入缓存
78
+ if (!empty ($ httpResult ['content ' ])) {
79
+ $ content = json_decode ($ httpResult ['content ' ], true );
80
+ return $ content ['access_token ' ] ?? '' ;
81
+ } else {
82
+ return '' ;
83
+ }
84
+ });
89
85
}
90
86
91
87
/**
@@ -98,37 +94,12 @@ public function setInterval()
98
94
$ interval = $ this ->expiresIn ;//每隔一定时间运行
99
95
do {
100
96
try {
101
- $ this ->buildAccessToken ();
97
+ $ this ->getBaiDuToken ();
102
98
} catch (\Exception $ e ) {
103
99
echo $ e ->getMessage ();
104
100
}
105
101
sleep ($ interval ); // 等待时间,进行下一次操作。
106
102
} while (true );
107
103
}
108
104
109
- /**
110
- * 获取 access_token 并保存到 token.txt 文件中
111
- * @return string|bool 返回 access_token
112
- */
113
- private function buildAccessToken ()
114
- {
115
- if (empty ($ this ->url )) {
116
- return false ;
117
- }
118
-
119
- $ curl = new Http ();
120
- $ httpResult = $ curl ->request ($ this ->url , $ this ->params , 'post ' , 6 , $ this ->header );
121
-
122
- // 2. 写入文件缓存
123
- if (!empty ($ httpResult ['content ' ])) {
124
- $ content = json_decode ($ httpResult ['content ' ], true );
125
- $ response = array_merge ($ content , ['create_at ' => time ()]);
126
- $ json = json_encode ($ response , JSON_UNESCAPED_UNICODE );
127
-
128
- // 2018-10-11 使用 Laravel Storage 存储数据
129
- return Storage::disk ('local ' )->put ('token.txt ' , $ json ) ? $ content ['access_token ' ] : false ;
130
- } else {
131
- return false ;
132
- }
133
- }
134
105
}
0 commit comments