Passed
Push — v6 ( 3606bd...432c9e )
by 光春
03:12
created

MiniService::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
15
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
16
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
17
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
18
// +----------------------------------------------------------------------
19
20
namespace DtApp\ThinkLibrary\service\wechat;
21
22
use DtApp\ThinkLibrary\exception\DtaException;
23
use DtApp\ThinkLibrary\Service;
24
use DtApp\ThinkLibrary\service\curl\HttpService;
25
use think\db\exception\DbException;
26
27
/**
28
 * 微信小程序
29
 * Class MiniService
30
 * @package DtApp\ThinkLibrary\service\WeChat
31
 */
32
class MiniService extends Service
33
{
34
    /**
35
     * @var
36
     */
37
    private $app_id, $app_secret;
38
    /**
39
     * @var string
40
     */
41
    private $grant_type = "client_credential";
42
43
    /**
44
     * 驱动方式
45
     * @var string
46
     */
47
    private $cache = "file";
48
49
    /**
50
     * @param string $appId
51
     * @return $this
52
     */
53
    public function appId(string $appId)
54
    {
55
        $this->app_id = $appId;
56
        return $this;
57
    }
58
59
    /**
60
     * @param string $appSecret
61
     * @return $this
62
     */
63
    public function appSecret(string $appSecret)
64
    {
65
        $this->app_secret = $appSecret;
66
        return $this;
67
    }
68
69
    /**
70
     * 驱动方式
71
     * @param string $cache
72
     * @return $this
73
     */
74
    public function cache(string $cache): self
75
    {
76
        $this->cache = $cache;
77
        return $this;
78
    }
79
80
    /**
81
     * 获取配置信息
82
     * @return $this
83
     */
84
    private function getConfig(): self
85
    {
86
        $this->cache = config('dtapp.wechat.mini.cache');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('dtapp.wechat.mini.cache') can also be of type boolean. However, the property $cache is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
87
        $this->app_id = config('dtapp.wechat.mini.app_id');
88
        $this->app_secret = config('dtapp.wechat.mini.app_secret');
89
        return $this;
90
    }
91
92
    /**
93
     * 用户支付完成后,获取该用户的 UnionId,无需用户授权
94
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.getPaidUnionId.html
95
     * @param string $openid
96
     * @return bool|mixed|string
97
     * @throws DbException
98
     * @throws DtaException
99
     */
100
    public function getPaidUnionId(string $openid)
101
    {
102
        // 获取数据
103
        $accessToken = $this->getAccessToken();
104
        $url = "https://api.weixin.qq.com/wxa/getpaidunionid?access_token={$accessToken['access_token']}&openid={$openid}";
105
        return HttpService::instance()
106
            ->url($url)
107
            ->toArray();
108
    }
109
110
    /**
111
     * 获取小程序二维码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制
112
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html
113
     * @param array $data
114
     * @return array|bool|mixed|string
115
     * @throws DbException
116
     * @throws DtaException
117
     */
118
    public function createWxaQrCode(array $data = [])
119
    {
120
        // 获取数据
121
        $accessToken = $this->getAccessToken();
122
        $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$accessToken['access_token']}";
123
        return HttpService::instance()
124
            ->url($url)
125
            ->data($data)
126
            ->post()
127
            ->toArray(false);
128
    }
129
130
    /**
131
     * 获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制
132
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.get.html
133
     * @param array $data
134
     * @return array|bool|mixed|string
135
     * @throws DbException
136
     * @throws DtaException
137
     */
138
    public function getWxaCode(array $data = [])
139
    {
140
        // 获取数据
141
        $accessToken = $this->getAccessToken();
142
        $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken['access_token']}";
143
        return HttpService::instance()
144
            ->url($url)
145
            ->data($data)
146
            ->post()
147
            ->toArray(false);
148
    }
149
150
    /**
151
     * 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制
152
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
153
     * @param array $data
154
     * @return array|bool|mixed|string
155
     * @throws DbException
156
     * @throws DtaException
157
     */
158
    public function getWxaCodeUnLimit(array $data = [])
159
    {
160
        // 获取数据
161
        $accessToken = $this->getAccessToken();
162
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken['access_token']}";
163
        return HttpService::instance()
164
            ->url($url)
165
            ->data($data)
166
            ->post()
167
            ->toArray(false);
168
    }
169
170
    /**
171
     * 组合模板并添加至帐号下的个人模板库
172
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.addTemplate.html
173
     * @param array $data
174
     * @return bool|mixed|string
175
     * @throws DbException
176
     * @throws DtaException
177
     */
178
    public function addTemplate(array $data = [])
179
    {
180
        // 获取数据
181
        $accessToken = $this->getAccessToken();
182
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token={$accessToken['access_token']}";
183
        return HttpService::instance()
184
            ->url($url)
185
            ->data($data)
186
            ->toArray();
187
    }
188
189
    /**
190
     * 删除帐号下的个人模板
191
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.deleteTemplate.html
192
     * @param string $priTmplId 要删除的模板id
193
     * @return bool|mixed|string
194
     * @throws DbException
195
     * @throws DtaException
196
     */
197
    public function deleteTemplate(string $priTmplId)
198
    {
199
        // 获取数据
200
        $accessToken = $this->getAccessToken();
201
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token={$accessToken['access_token']}";
202
        $data = [
203
            'priTmplId' => $priTmplId
204
        ];
205
        return HttpService::instance()
206
            ->url($url)
207
            ->data($data)
208
            ->toArray();
209
    }
210
211
    /**
212
     * 获取小程序账号的类目
213
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getCategory.html
214
     * @return bool|mixed|string
215
     * @throws DbException
216
     * @throws DtaException
217
     */
218
    public function getCategory()
219
    {
220
        // 获取数据
221
        $accessToken = $this->getAccessToken();
222
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token={$accessToken['access_token']}";
223
        return HttpService::instance()
224
            ->url($url)
225
            ->toArray();
226
    }
227
228
    /**
229
     * 获取模板标题下的关键词列表
230
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getPubTemplateKeyWordsById.html
231
     * @param string $tid 模板标题 id
232
     * @return bool|mixed|string
233
     * @throws DbException
234
     * @throws DtaException
235
     */
236
    public function getPubTemplateKeyWordsById(string $tid)
237
    {
238
        // 获取数据
239
        $accessToken = $this->getAccessToken();
240
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token={$accessToken['access_token']}";
241
        $data = [
242
            'tid' => $tid
243
        ];
244
        return HttpService::instance()
245
            ->url($url)
246
            ->data($data)
247
            ->toArray();
248
    }
249
250
    /**
251
     * 获取帐号所属类目下的公共模板标题
252
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getPubTemplateTitleList.html
253
     * @param array $data
254
     * @return bool|mixed|string
255
     * @throws DbException
256
     * @throws DtaException
257
     */
258
    public function getPubTemplateTitleList(array $data = [])
259
    {
260
        // 获取数据
261
        $accessToken = $this->getAccessToken();
262
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token={$accessToken['access_token']}";
263
        return HttpService::instance()
264
            ->url($url)
265
            ->data($data)
266
            ->toArray();
267
    }
268
269
    /**
270
     * 获取当前帐号下的个人模板列表
271
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
272
     * @return bool|mixed|string
273
     * @throws DbException
274
     * @throws DtaException
275
     */
276
    public function getTemplateList()
277
    {
278
        // 获取数据
279
        $accessToken = $this->getAccessToken();
280
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token={$accessToken['access_token']}";
281
        return HttpService::instance()
282
            ->url($url)
283
            ->toArray();
284
    }
285
286
    /**
287
     * 发送订阅消息
288
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
289
     * @param array $data
290
     * @return bool|mixed|string
291
     * @throws DbException
292
     * @throws DtaException
293
     */
294
    public function subscribeMessageSend(array $data = [])
295
    {
296
        // 获取数据
297
        $accessToken = $this->getAccessToken();
298
        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken['access_token']}";
299
        return HttpService::instance()
300
            ->url($url)
301
            ->data($data)
302
            ->post()
303
            ->toArray();
304
    }
305
306
    /**
307
     * 登录凭证校验
308
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
309
     * @param string $js_code
310
     * @return bool|mixed|string
311
     * @throws DtaException
312
     */
313
    public function code2Session(string $js_code)
314
    {
315
        if (empty($this->app_id) || empty($this->app_secret)) {
316
            $this->getConfig();
317
        }
318
        if (empty($this->app_id)) {
319
            throw new DtaException('请检查app_id参数');
320
        }
321
        if (empty($this->app_secret)) {
322
            throw new DtaException('请检查app_secret参数');
323
        }
324
        $this->grant_type = "authorization_code";
325
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->app_id}&secret={$this->app_secret}&js_code={$js_code}&grant_type={$this->grant_type}";
326
        return HttpService::instance()
327
            ->url($url)
328
            ->toArray();
329
    }
330
331
    /**
332
     * 检验数据的真实性,并且获取解密后的明文.
333
     * @param string $js_code
334
     * @param string $encrypted_data
335
     * @param string $iv
336
     * @return bool|mixed
337
     * @throws DtaException
338
     */
339
    public function userInfo(string $js_code, string $encrypted_data, string $iv)
340
    {
341
        $session = $this->code2Session($js_code);
342
        if (!isset($session['openid'])) {
343
            return false;
344
        }
345
        $result = openssl_decrypt(base64_decode($encrypted_data), "AES-128-CBC", base64_decode($session['session_key']), 1, base64_decode($iv));
346
        return json_decode($result, true);
347
    }
348
349
    /**
350
     * 数据签名校验,并且获取解密后的明文.
351
     * @param string $js_code
352
     * @param string $encrypted_data
353
     * @param string $iv
354
     * @return mixed
355
     * @throws DtaException
356
     */
357
    public function userPhone(string $js_code, string $encrypted_data, string $iv)
358
    {
359
        $session = $this->code2Session($js_code);
360
        if (!isset($session['openid'])) {
361
            return false;
362
        }
363
        $result = openssl_decrypt(base64_decode($encrypted_data), "AES-128-CBC", base64_decode($session['session_key']), 1, base64_decode($iv));
364
        return json_decode($result, true);
365
    }
366
367
    /**
368
     * 数据签名校验,并且获取解密后的明文.
369
     * @param string $session_key
370
     * @param string $encrypted_data
371
     * @param string $iv
372
     * @return mixed
373
     */
374
    public function decode(string $session_key, string $encrypted_data, string $iv)
375
    {
376
        $result = openssl_decrypt(base64_decode($encrypted_data), "AES-128-CBC", base64_decode($session_key), 1, base64_decode($iv));
377
        return json_decode($result, true);
378
    }
379
380
    /**
381
     * 【小程序直播】直播间管理接口 - 创建直播间
382
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#1
383
     * @param array $data
384
     * @return array|bool|mixed|string
385
     * @throws DbException
386
     * @throws DtaException
387
     */
388
    public function broadcastRoomCreate(array $data = [])
389
    {
390
        // 获取数据
391
        $accessToken = $this->getAccessToken();
392
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token={$accessToken['access_token']}";
393
        return HttpService::instance()
394
            ->url($url)
395
            ->data($data)
396
            ->post()
397
            ->toArray();
398
    }
399
400
    /**
401
     * 【小程序直播】直播间管理接口 - 获取直播间列表
402
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#2
403
     * @param array $data
404
     * @return array|bool|mixed|string
405
     * @throws DbException
406
     * @throws DtaException
407
     */
408
    public function broadcastGetLiveInfos(array $data = [])
409
    {
410
        // 获取数据
411
        $accessToken = $this->getAccessToken();
412
        $url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$accessToken['access_token']}";
413
        return HttpService::instance()
414
            ->url($url)
415
            ->data($data)
416
            ->post()
417
            ->toArray();
418
    }
419
420
    /**
421
     * 【小程序直播】直播间管理接口 - 获取直播间回放
422
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#3
423
     * @param array $data
424
     * @return array|bool|mixed|string
425
     * @throws DbException
426
     * @throws DtaException
427
     */
428
    public function broadcastGetLiveInfo(array $data = [])
429
    {
430
        // 获取数据
431
        $accessToken = $this->getAccessToken();
432
        $url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$accessToken['access_token']}";
433
        return HttpService::instance()
434
            ->url($url)
435
            ->data($data)
436
            ->post()
437
            ->toArray();
438
    }
439
440
    /**
441
     * 【小程序直播】直播间管理接口 - 直播间导入商品
442
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#4
443
     * @param array $data
444
     * @return array|bool|mixed|string
445
     * @throws DbException
446
     * @throws DtaException
447
     */
448
    public function broadcastRoomAddGoods(array $data = [])
449
    {
450
        // 获取数据
451
        $accessToken = $this->getAccessToken();
452
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods?access_token={$accessToken['access_token']}";
453
        return HttpService::instance()
454
            ->url($url)
455
            ->data($data)
456
            ->post()
457
            ->toArray();
458
    }
459
460
    /**
461
     * 【小程序直播】直播商品管理接口 - 商品添加并提审
462
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#1
463
     * @param array $data
464
     * @return array|bool|mixed|string
465
     * @throws DbException
466
     * @throws DtaException
467
     */
468
    public function broadcastGoodsAdd(array $data = [])
469
    {
470
        // 获取数据
471
        $accessToken = $this->getAccessToken();
472
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/add?access_token={$accessToken['access_token']}";
473
        return HttpService::instance()
474
            ->url($url)
475
            ->data($data)
476
            ->post()
477
            ->toArray();
478
    }
479
480
    /**
481
     * 【小程序直播】直播商品管理接口 - 撤回审核
482
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#2
483
     * @param array $data
484
     * @return array|bool|mixed|string
485
     * @throws DbException
486
     * @throws DtaException
487
     */
488
    public function broadcastGoodsResetAudit(array $data = [])
489
    {
490
        // 获取数据
491
        $accessToken = $this->getAccessToken();
492
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/resetaudit?access_token={$accessToken['access_token']}";
493
        return HttpService::instance()
494
            ->url($url)
495
            ->data($data)
496
            ->post()
497
            ->toArray();
498
    }
499
500
501
    /**
502
     * 【小程序直播】直播商品管理接口 - 重新提交审核
503
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#3
504
     * @param array $data
505
     * @return array|bool|mixed|string
506
     * @throws DbException
507
     * @throws DtaException
508
     */
509
    public function broadcastGoodsAudit(array $data = [])
510
    {
511
        // 获取数据
512
        $accessToken = $this->getAccessToken();
513
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/audit?access_token={$accessToken['access_token']}";
514
        return HttpService::instance()
515
            ->url($url)
516
            ->data($data)
517
            ->post()
518
            ->toArray();
519
    }
520
521
522
    /**
523
     * 【小程序直播】直播商品管理接口 - 删除商品
524
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#4
525
     * @param array $data
526
     * @return array|bool|mixed|string
527
     * @throws DbException
528
     * @throws DtaException
529
     */
530
    public function broadcastGoodsDelete(array $data = [])
531
    {
532
        // 获取数据
533
        $accessToken = $this->getAccessToken();
534
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/delete?access_token={$accessToken['access_token']}";
535
        return HttpService::instance()
536
            ->url($url)
537
            ->data($data)
538
            ->post()
539
            ->toArray();
540
    }
541
542
543
    /**
544
     * 【小程序直播】直播商品管理接口 - 更新商品
545
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#5
546
     * @param array $data
547
     * @return array|bool|mixed|string
548
     * @throws DbException
549
     * @throws DtaException
550
     */
551
    public function broadcastGoodsUpdate(array $data = [])
552
    {
553
        // 获取数据
554
        $accessToken = $this->getAccessToken();
555
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/update?access_token={$accessToken['access_token']}";
556
        return HttpService::instance()
557
            ->url($url)
558
            ->data($data)
559
            ->post()
560
            ->toArray();
561
    }
562
563
564
    /**
565
     * 【小程序直播】直播商品管理接口 - 获取商品状态
566
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#6
567
     * @param array $data
568
     * @return array|bool|mixed|string
569
     * @throws DbException
570
     * @throws DtaException
571
     */
572
    public function broadcastGetGoodsWarehouse(array $data = [])
573
    {
574
        // 获取数据
575
        $accessToken = $this->getAccessToken();
576
        $url = "https://api.weixin.qq.com/wxa/business/getgoodswarehouse?access_token={$accessToken['access_token']}";
577
        return HttpService::instance()
578
            ->url($url)
579
            ->data($data)
580
            ->post()
581
            ->toArray();
582
    }
583
584
585
    /**
586
     * 【小程序直播】直播商品管理接口 - 获取商品列表
587
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#7
588
     * @param array $data
589
     * @return array|bool|mixed|string
590
     * @throws DbException
591
     * @throws DtaException
592
     */
593
    public function broadcastGoodsGetAppRoved(array $data = [])
594
    {
595
        // 获取数据
596
        $accessToken = $this->getAccessToken();
597
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/getapproved?access_token={$accessToken['access_token']}";
598
        return HttpService::instance()
599
            ->url($url)
600
            ->data($data)
601
            ->post()
602
            ->toArray();
603
    }
604
605
    /**
606
     * 获取小程序全局唯一后台接口调用凭据(access_token)
607
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
608
     * @return bool|mixed|string
609
     * @throws DbException
610
     * @throws DtaException
611
     */
612
    public function accessToken()
613
    {
614
        // 获取数据
615
        return $this->getAccessToken();
616
    }
617
618
    /**
619
     * 获取access_token信息
620
     * @return array|bool|mixed|string|string[]
621
     * @throws DbException
622
     * @throws DtaException
623
     */
624
    private function getAccessToken()
625
    {
626
        if (empty($this->cache) || empty($this->app_id) || empty($this->app_secret)) {
627
            $this->getConfig();
628
        }
629
        if (empty($this->cache)) {
630
            throw new DtaException('请检查cache参数');
631
        }
632
        if (empty($this->app_id)) {
633
            throw new DtaException('请检查app_id参数');
634
        }
635
        if (empty($this->app_secret)) {
636
            throw new DtaException('请检查app_secret参数');
637
        }
638
        $this->grant_type = "client_credential";
639
        if ($this->cache === "file") {
640
            // 文件名
641
            $file = "{$this->app->getRootPath()}runtime/{$this->app_id}_access_token.json";
642
            // 获取数据
643
            $accessToken = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
644
            if (empty($accessToken) || !is_array($accessToken)) {
645
                $accessToken = [
646
                    'access_token' => '',
647
                    'expires_in' => '',
648
                    'expires_time' => '',
649
                ];
650
            }
651
            if (empty($accessToken['expires_time'])) {
652
                $accessToken_res = HttpService::instance()
653
                    ->url("https://api.weixin.qq.com/cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
654
                    ->toArray();
655
                $accessToken_res['expires_time'] = time() + 6000;
656
                file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
657
                $accessToken = $accessToken_res;
658
            } else if (!isset($accessToken['access_token'])) {
659
                $accessToken_res = HttpService::instance()
660
                    ->url("https://api.weixin.qq.com/cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
661
                    ->toArray();
662
                $accessToken_res['expires_time'] = time() + 6000;
663
                file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
664
                $accessToken = $accessToken_res;
665
            } else if ($accessToken['expires_time'] <= time()) {
666
                $accessToken_res = HttpService::instance()
667
                    ->url("https://api.weixin.qq.com/cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
668
                    ->toArray();
669
                $accessToken_res['expires_time'] = time() + 6000;
670
                file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
671
                $accessToken = $accessToken_res;
672
            }
673
            return $accessToken;
674
        }
675
676
        if ($this->cache === "mysql") {
677
            $access_token = [];
678
            // 文件名
679
            $file = "{$this->app_id}_access_token";
680
            // 获取数据
681
            $cache_mysql_value = dtacache($file);
682
            if (!empty($cache_mysql_value)) {
683
                $access_token['access_token'] = $cache_mysql_value;
684
            } else {
685
                $accessToken_res = HttpService::instance()
686
                    ->url("https://api.weixin.qq.com/cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
687
                    ->toArray();
688
                dtacache($file, $accessToken_res['access_token'], 6000);
689
                $access_token['access_token'] = $accessToken_res['access_token'];
690
            }
691
            return $access_token;
692
        }
693
694
        throw new DtaException("驱动方式错误");
695
    }
696
}
697