MiniService::getPubTemplateTitleList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 1
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
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
15
// +----------------------------------------------------------------------
16
17
namespace DtApp\ThinkLibrary\service\wechat;
18
19
use DtApp\ThinkLibrary\exception\DtaException;
20
use DtApp\ThinkLibrary\Service;
21
use DtApp\ThinkLibrary\service\curl\HttpService;
22
use think\db\exception\DbException;
23
24
/**
25
 * 微信小程序
26
 * Class MiniService
27
 * @package DtApp\ThinkLibrary\service\WeChat
28
 */
29
class MiniService extends Service
30
{
31
    /**
32
     * @var
33
     */
34
    private $app_id, $app_secret;
35
36
    /**
37
     * @var string
38
     */
39
    private $grant_type = "client_credential";
40
41
    /**
42
     * 驱动方式
43
     * @var string
44
     */
45
    private $cache = "file";
46
47
    /**
48
     * @param string $appId
49
     * @return $this
50
     */
51
    public function appId(string $appId)
52
    {
53
        $this->app_id = $appId;
54
        return $this;
55
    }
56
57
    /**
58
     * @param string $appSecret
59
     * @return $this
60
     */
61
    public function appSecret(string $appSecret)
62
    {
63
        $this->app_secret = $appSecret;
64
        return $this;
65
    }
66
67
    /**
68
     * 驱动方式
69
     * @param string $cache
70
     * @return $this
71
     */
72
    public function cache(string $cache): self
73
    {
74
        $this->cache = $cache;
75
        return $this;
76
    }
77
78
    /**
79
     * 获取配置信息
80
     * @return $this
81
     */
82
    private function getConfig(): self
83
    {
84
        $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...
85
        $this->app_id = config('dtapp.wechat.mini.app_id');
86
        $this->app_secret = config('dtapp.wechat.mini.app_secret');
87
        return $this;
88
    }
89
90
    /**
91
     * 用户支付完成后,获取该用户的 UnionId,无需用户授权
92
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.getPaidUnionId.html
93
     * @param string $openid
94
     * @return bool|mixed|string
95
     * @throws DbException
96
     * @throws DtaException
97
     */
98
    public function getPaidUnionId(string $openid)
99
    {
100
        $accessToken = $this->getAccessToken();
101
        $url = "https://api.weixin.qq.com/wxa/getpaidunionid?access_token={$accessToken['access_token']}&openid={$openid}";
102
        return HttpService::instance()
103
            ->url($url)
104
            ->toArray();
105
    }
106
107
    /**
108
     * 获取小程序二维码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制
109
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html
110
     * @param array $data
111
     * @return array|bool|mixed|string
112
     * @throws DbException
113
     * @throws DtaException
114
     */
115
    public function createWxaQrCode(array $data = [])
116
    {
117
        $accessToken = $this->getAccessToken();
118
        $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$accessToken['access_token']}";
119
        return HttpService::instance()
120
            ->url($url)
121
            ->data($data)
122
            ->post()
123
            ->toArray(false);
124
    }
125
126
    /**
127
     * 获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制
128
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.get.html
129
     * @param array $data
130
     * @return array|bool|mixed|string
131
     * @throws DbException
132
     * @throws DtaException
133
     */
134
    public function getWxaCode(array $data = [])
135
    {
136
        $accessToken = $this->getAccessToken();
137
        $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken['access_token']}";
138
        return HttpService::instance()
139
            ->url($url)
140
            ->data($data)
141
            ->post()
142
            ->toArray(false);
143
    }
144
145
    /**
146
     * 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制
147
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
148
     * @param array $data
149
     * @return array|bool|mixed|string
150
     * @throws DbException
151
     * @throws DtaException
152
     */
153
    public function getWxaCodeUnLimit(array $data = [])
154
    {
155
        $accessToken = $this->getAccessToken();
156
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken['access_token']}";
157
        return HttpService::instance()
158
            ->url($url)
159
            ->data($data)
160
            ->post()
161
            ->toArray(false);
162
    }
163
164
    /**
165
     * 组合模板并添加至帐号下的个人模板库
166
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.addTemplate.html
167
     * @param array $data
168
     * @return bool|mixed|string
169
     * @throws DbException
170
     * @throws DtaException
171
     */
172
    public function addTemplate(array $data = [])
173
    {
174
        $accessToken = $this->getAccessToken();
175
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token={$accessToken['access_token']}";
176
        return HttpService::instance()
177
            ->url($url)
178
            ->data($data)
179
            ->toArray();
180
    }
181
182
    /**
183
     * 删除帐号下的个人模板
184
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.deleteTemplate.html
185
     * @param string $priTmplId 要删除的模板id
186
     * @return bool|mixed|string
187
     * @throws DbException
188
     * @throws DtaException
189
     */
190
    public function deleteTemplate(string $priTmplId)
191
    {
192
        $accessToken = $this->getAccessToken();
193
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token={$accessToken['access_token']}";
194
        $data = [
195
            'priTmplId' => $priTmplId
196
        ];
197
        return HttpService::instance()
198
            ->url($url)
199
            ->data($data)
200
            ->toArray();
201
    }
202
203
    /**
204
     * 获取小程序账号的类目
205
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getCategory.html
206
     * @return bool|mixed|string
207
     * @throws DbException
208
     * @throws DtaException
209
     */
210
    public function getCategory()
211
    {
212
        $accessToken = $this->getAccessToken();
213
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token={$accessToken['access_token']}";
214
        return HttpService::instance()
215
            ->url($url)
216
            ->toArray();
217
    }
218
219
    /**
220
     * 获取模板标题下的关键词列表
221
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getPubTemplateKeyWordsById.html
222
     * @param string $tid 模板标题 id
223
     * @return bool|mixed|string
224
     * @throws DbException
225
     * @throws DtaException
226
     */
227
    public function getPubTemplateKeyWordsById(string $tid)
228
    {
229
        $accessToken = $this->getAccessToken();
230
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token={$accessToken['access_token']}";
231
        $data = [
232
            'tid' => $tid
233
        ];
234
        return HttpService::instance()
235
            ->url($url)
236
            ->data($data)
237
            ->toArray();
238
    }
239
240
    /**
241
     * 获取帐号所属类目下的公共模板标题
242
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getPubTemplateTitleList.html
243
     * @param array $data
244
     * @return bool|mixed|string
245
     * @throws DbException
246
     * @throws DtaException
247
     */
248
    public function getPubTemplateTitleList(array $data = [])
249
    {
250
        $accessToken = $this->getAccessToken();
251
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token={$accessToken['access_token']}";
252
        return HttpService::instance()
253
            ->url($url)
254
            ->data($data)
255
            ->toArray();
256
    }
257
258
    /**
259
     * 获取当前帐号下的个人模板列表
260
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
261
     * @return bool|mixed|string
262
     * @throws DbException
263
     * @throws DtaException
264
     */
265
    public function getTemplateList()
266
    {
267
        $accessToken = $this->getAccessToken();
268
        $url = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token={$accessToken['access_token']}";
269
        return HttpService::instance()
270
            ->url($url)
271
            ->toArray();
272
    }
273
274
    /**
275
     * 发送订阅消息
276
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
277
     * @param array $data
278
     * @return bool|mixed|string
279
     * @throws DbException
280
     * @throws DtaException
281
     */
282
    public function subscribeMessageSend(array $data = [])
283
    {
284
        $accessToken = $this->getAccessToken();
285
        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken['access_token']}";
286
        return HttpService::instance()
287
            ->url($url)
288
            ->data($data)
289
            ->post()
290
            ->toArray();
291
    }
292
293
    /**
294
     * 统一服务消息 - 下发小程序和公众号统一的服务消息
295
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/uniform-message/uniformMessage.send.html
296
     * @param array $data
297
     * @return bool|mixed|string
298
     * @throws DbException
299
     * @throws DtaException
300
     */
301
    public function uniformMessageSend(array $data = [])
302
    {
303
        $accessToken = $this->getAccessToken();
304
        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={$accessToken['access_token']}";
305
        return HttpService::instance()
306
            ->url($url)
307
            ->data($data)
308
            ->post()
309
            ->toArray();
310
    }
311
312
    /**
313
     * 登录凭证校验
314
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
315
     * @param string $js_code
316
     * @return bool|mixed|string
317
     * @throws DtaException
318
     */
319
    public function code2Session(string $js_code)
320
    {
321
        if (empty($this->app_id) || empty($this->app_secret)) {
322
            $this->getConfig();
323
        }
324
        if (empty($this->app_id)) {
325
            throw new DtaException('请检查app_id参数');
326
        }
327
        if (empty($this->app_secret)) {
328
            throw new DtaException('请检查app_secret参数');
329
        }
330
        $this->grant_type = "authorization_code";
331
        $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}";
332
        return HttpService::instance()
333
            ->url($url)
334
            ->toArray();
335
    }
336
337
    /**
338
     * 检验数据的真实性,并且获取解密后的明文.
339
     * @param string $js_code
340
     * @param string $encrypted_data
341
     * @param string $iv
342
     * @return bool|mixed
343
     * @throws DtaException
344
     */
345
    public function userInfo(string $js_code, string $encrypted_data, string $iv)
346
    {
347
        $session = $this->code2Session($js_code);
348
        if (!isset($session['openid'])) {
349
            return false;
350
        }
351
        $result = openssl_decrypt(base64_decode($encrypted_data), "AES-128-CBC", base64_decode($session['session_key']), 1, base64_decode($iv));
352
        return json_decode($result, true);
353
    }
354
355
    /**
356
     * 数据签名校验,并且获取解密后的明文.
357
     * @param string $js_code
358
     * @param string $encrypted_data
359
     * @param string $iv
360
     * @return mixed
361
     * @throws DtaException
362
     */
363
    public function userPhone(string $js_code, string $encrypted_data, string $iv)
364
    {
365
        $session = $this->code2Session($js_code);
366
        if (!isset($session['openid'])) {
367
            return false;
368
        }
369
        $result = openssl_decrypt(base64_decode($encrypted_data), "AES-128-CBC", base64_decode($session['session_key']), 1, base64_decode($iv));
370
        return json_decode($result, true);
371
    }
372
373
    /**
374
     * 数据签名校验,并且获取解密后的明文.
375
     * @param string $session_key
376
     * @param string $encrypted_data
377
     * @param string $iv
378
     * @return mixed
379
     */
380
    public function decode(string $session_key, string $encrypted_data, string $iv)
381
    {
382
        $result = openssl_decrypt(base64_decode($encrypted_data), "AES-128-CBC", base64_decode($session_key), 1, base64_decode($iv));
383
        return json_decode($result, true);
384
    }
385
386
    /**
387
     * 【小程序直播】直播间管理接口 - 创建直播间
388
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#1
389
     * @param array $data
390
     * @return array|bool|mixed|string
391
     * @throws DbException
392
     * @throws DtaException
393
     */
394
    public function broadcastRoomCreate(array $data = [])
395
    {
396
        $accessToken = $this->getAccessToken();
397
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token={$accessToken['access_token']}";
398
        return HttpService::instance()
399
            ->url($url)
400
            ->data($data)
401
            ->post()
402
            ->toArray();
403
    }
404
405
    /**
406
     * 【小程序直播】直播间管理接口 - 获取直播间列表
407
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#2
408
     * @param array $data
409
     * @return array|bool|mixed|string
410
     * @throws DbException
411
     * @throws DtaException
412
     */
413
    public function broadcastGetLiveInfos(array $data = [])
414
    {
415
        $accessToken = $this->getAccessToken();
416
        $url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$accessToken['access_token']}";
417
        return HttpService::instance()
418
            ->url($url)
419
            ->data($data)
420
            ->post()
421
            ->toArray();
422
    }
423
424
    /**
425
     * 【小程序直播】直播间管理接口 - 获取直播间回放
426
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#3
427
     * @param array $data
428
     * @return array|bool|mixed|string
429
     * @throws DbException
430
     * @throws DtaException
431
     */
432
    public function broadcastGetLiveInfo(array $data = [])
433
    {
434
        $accessToken = $this->getAccessToken();
435
        $url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$accessToken['access_token']}";
436
        return HttpService::instance()
437
            ->url($url)
438
            ->data($data)
439
            ->post()
440
            ->toArray();
441
    }
442
443
    /**
444
     * 【小程序直播】直播间管理接口 - 直播间导入商品
445
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#4
446
     * @param array $data
447
     * @return array|bool|mixed|string
448
     * @throws DbException
449
     * @throws DtaException
450
     */
451
    public function broadcastRoomAddGoods(array $data = [])
452
    {
453
        $accessToken = $this->getAccessToken();
454
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods?access_token={$accessToken['access_token']}";
455
        return HttpService::instance()
456
            ->url($url)
457
            ->data($data)
458
            ->post()
459
            ->toArray();
460
    }
461
462
    /**
463
     * 【小程序直播】直播商品管理接口 - 商品添加并提审
464
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#1
465
     * @param array $data
466
     * @return array|bool|mixed|string
467
     * @throws DbException
468
     * @throws DtaException
469
     */
470
    public function broadcastGoodsAdd(array $data = [])
471
    {
472
        $accessToken = $this->getAccessToken();
473
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/add?access_token={$accessToken['access_token']}";
474
        return HttpService::instance()
475
            ->url($url)
476
            ->data($data)
477
            ->post()
478
            ->toArray();
479
    }
480
481
    /**
482
     * 【小程序直播】直播商品管理接口 - 撤回审核
483
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#2
484
     * @param array $data
485
     * @return array|bool|mixed|string
486
     * @throws DbException
487
     * @throws DtaException
488
     */
489
    public function broadcastGoodsResetAudit(array $data = [])
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
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#3
503
     * @param array $data
504
     * @return array|bool|mixed|string
505
     * @throws DbException
506
     * @throws DtaException
507
     */
508
    public function broadcastGoodsAudit(array $data = [])
509
    {
510
        $accessToken = $this->getAccessToken();
511
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/audit?access_token={$accessToken['access_token']}";
512
        return HttpService::instance()
513
            ->url($url)
514
            ->data($data)
515
            ->post()
516
            ->toArray();
517
    }
518
519
    /**
520
     * 【小程序直播】直播商品管理接口 - 删除商品
521
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#4
522
     * @param array $data
523
     * @return array|bool|mixed|string
524
     * @throws DbException
525
     * @throws DtaException
526
     */
527
    public function broadcastGoodsDelete(array $data = [])
528
    {
529
        $accessToken = $this->getAccessToken();
530
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/delete?access_token={$accessToken['access_token']}";
531
        return HttpService::instance()
532
            ->url($url)
533
            ->data($data)
534
            ->post()
535
            ->toArray();
536
    }
537
538
    /**
539
     * 【小程序直播】直播商品管理接口 - 更新商品
540
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#5
541
     * @param array $data
542
     * @return array|bool|mixed|string
543
     * @throws DbException
544
     * @throws DtaException
545
     */
546
    public function broadcastGoodsUpdate(array $data = [])
547
    {
548
        $accessToken = $this->getAccessToken();
549
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/update?access_token={$accessToken['access_token']}";
550
        return HttpService::instance()
551
            ->url($url)
552
            ->data($data)
553
            ->post()
554
            ->toArray();
555
    }
556
557
    /**
558
     * 【小程序直播】直播商品管理接口 - 获取商品状态
559
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#6
560
     * @param array $data
561
     * @return array|bool|mixed|string
562
     * @throws DbException
563
     * @throws DtaException
564
     */
565
    public function broadcastGetGoodsWarehouse(array $data = [])
566
    {
567
        $accessToken = $this->getAccessToken();
568
        $url = "https://api.weixin.qq.com/wxa/business/getgoodswarehouse?access_token={$accessToken['access_token']}";
569
        return HttpService::instance()
570
            ->url($url)
571
            ->data($data)
572
            ->post()
573
            ->toArray();
574
    }
575
576
    /**
577
     * 【小程序直播】直播商品管理接口 - 获取商品列表
578
     * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html#7
579
     * @param array $data
580
     * @return array|bool|mixed|string
581
     * @throws DbException
582
     * @throws DtaException
583
     */
584
    public function broadcastGoodsGetAppRoved(array $data = [])
585
    {
586
        $accessToken = $this->getAccessToken();
587
        $url = "https://api.weixin.qq.com/wxaapi/broadcast/goods/getapproved?access_token={$accessToken['access_token']}";
588
        return HttpService::instance()
589
            ->url($url)
590
            ->data($data)
591
            ->post()
592
            ->toArray();
593
    }
594
595
    /**
596
     * 数据分析 - 获取用户访问小程序日留存
597
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/data-analysis/visit-retain/analysis.getDailyRetain.html
598
     * @param array $data
599
     * @return array|bool|mixed|string
600
     * @throws DbException
601
     * @throws DtaException
602
     */
603
    public function analysisGetDailyRetain(array $data = [])
604
    {
605
        $accessToken = $this->getAccessToken();
606
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo?access_token={$accessToken['access_token']}";
607
        return HttpService::instance()
608
            ->url($url)
609
            ->data($data)
610
            ->post()
611
            ->toArray();
612
    }
613
614
    /**
615
     * 数据分析 - 获取用户访问小程序月留存
616
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/data-analysis/visit-retain/analysis.getMonthlyRetain.html
617
     * @param array $data
618
     * @return array|bool|mixed|string
619
     * @throws DbException
620
     * @throws DtaException
621
     */
622
    public function analysisGetMonthlyRetain(array $data = [])
623
    {
624
        $accessToken = $this->getAccessToken();
625
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyretaininfo?access_token={$accessToken['access_token']}";
626
        return HttpService::instance()
627
            ->url($url)
628
            ->data($data)
629
            ->post()
630
            ->toArray();
631
    }
632
633
    /**
634
     * 数据分析 - 获取用户访问小程序周留存
635
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/data-analysis/visit-retain/analysis.getWeeklyRetain.html
636
     * @param array $data
637
     * @return array|bool|mixed|string
638
     * @throws DbException
639
     * @throws DtaException
640
     */
641
    public function analysisGetWeeklyRetain(array $data = [])
642
    {
643
        $accessToken = $this->getAccessToken();
644
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappidweeklyretaininfo?access_token={$accessToken['access_token']}";
645
        return HttpService::instance()
646
            ->url($url)
647
            ->data($data)
648
            ->post()
649
            ->toArray();
650
    }
651
652
    /**
653
     * 数据分析 - 获取用户访问小程序数据概况
654
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/data-analysis/analysis.getDailySummary.html
655
     * @param array $data
656
     * @return array|bool|mixed|string
657
     * @throws DbException
658
     * @throws DtaException
659
     */
660
    public function analysisGetDailySummary(array $data = [])
661
    {
662
        $accessToken = $this->getAccessToken();
663
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailysummarytrend?access_token={$accessToken['access_token']}";
664
        return HttpService::instance()
665
            ->url($url)
666
            ->data($data)
667
            ->post()
668
            ->toArray();
669
    }
670
671
    /**
672
     * 数据分析 - 获取小程序新增或活跃用户的画像分布数据。时间范围支持昨天、最近7天、最近30天。其中,新增用户数为时间范围内首次访问小程序的去重用户数,活跃用户数为时间范围内访问过小程序的去重用户数
673
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/data-analysis/analysis.getUserPortrait.html
674
     * @param array $data
675
     * @return array|bool|mixed|string
676
     * @throws DbException
677
     * @throws DtaException
678
     */
679
    public function analysisGetUserPortrait(array $data = [])
680
    {
681
        $accessToken = $this->getAccessToken();
682
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappiduserportrait?access_token={$accessToken['access_token']}";
683
        return HttpService::instance()
684
            ->url($url)
685
            ->data($data)
686
            ->post()
687
            ->toArray();
688
    }
689
690
    /**
691
     * 数据分析 - 获取用户访问小程序数据概况
692
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/data-analysis/analysis.getVisitDistribution.html
693
     * @param array $data
694
     * @return array|bool|mixed|string
695
     * @throws DbException
696
     * @throws DtaException
697
     */
698
    public function analysisGetVisitDistribution(array $data = [])
699
    {
700
        $accessToken = $this->getAccessToken();
701
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappidvisitdistribution?access_token={$accessToken['access_token']}";
702
        return HttpService::instance()
703
            ->url($url)
704
            ->data($data)
705
            ->post()
706
            ->toArray();
707
    }
708
709
    /**
710
     * 数据分析 - 访问页面。目前只提供按 page_visit_pv 排序的 top200
711
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/data-analysis/analysis.getVisitPage.html
712
     * @param array $data
713
     * @return array|bool|mixed|string
714
     * @throws DbException
715
     * @throws DtaException
716
     */
717
    public function analysisGetVisitPage(array $data = [])
718
    {
719
        $accessToken = $this->getAccessToken();
720
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappidvisitpage?access_token={$accessToken['access_token']}";
721
        return HttpService::instance()
722
            ->url($url)
723
            ->data($data)
724
            ->post()
725
            ->toArray();
726
    }
727
728
    /**
729
     * 客服消息 - 获取客服消息内的临时素材。即下载临时的多媒体文件。目前小程序仅支持下载图片文件。
730
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.getTempMedia.html
731
     * @param string $media_id
732
     * @return array|bool|mixed|string
733
     * @throws DbException
734
     * @throws DtaException
735
     */
736
    public function customerServiceMessageGetTempMedia(string $media_id = '')
737
    {
738
        $accessToken = $this->getAccessToken();
739
        $url = "https://api.weixin.qq.com/cgi-bin/media/get";
740
        return HttpService::instance()
741
            ->url($url)
742
            ->data([
743
                'access_token' => $accessToken['access_token'],
744
                'media_id' => $media_id
745
            ])
746
            ->get()
747
            ->toArray();
748
    }
749
750
    /**
751
     * 客服消息 - 发送客服消息给用户。详细规则见 发送客服消息
752
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.send.html
753
     * @param array $data
754
     * @return array|bool|mixed|string
755
     * @throws DbException
756
     * @throws DtaException
757
     */
758
    public function customerServiceMessageSend(array $data = [])
759
    {
760
        $accessToken = $this->getAccessToken();
761
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$accessToken['access_token']}";
762
        return HttpService::instance()
763
            ->url($url)
764
            ->data($data)
765
            ->post()
766
            ->toArray();
767
    }
768
769
    /**
770
     * 客服消息 - 下发客服当前输入状态给用户。详见 客服消息输入状态
771
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.setTyping.html
772
     * @param array $data
773
     * @return array|bool|mixed|string
774
     * @throws DbException
775
     * @throws DtaException
776
     */
777
    public function customerServiceMessageSetTyping(array $data = [])
778
    {
779
        $accessToken = $this->getAccessToken();
780
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token={$accessToken['access_token']}";
781
        return HttpService::instance()
782
            ->url($url)
783
            ->data($data)
784
            ->post()
785
            ->toArray();
786
    }
787
788
    /**
789
     * 客服消息 - 把媒体文件上传到微信服务器。目前仅支持图片。用于发送客服消息或被动回复用户消息
790
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.uploadTempMedia.html
791
     * @param array $data
792
     * @return array|bool|mixed|string
793
     * @throws DbException
794
     * @throws DtaException
795
     */
796
    public function customerServiceMessageUploadTempMedia(array $data = [])
797
    {
798
        $accessToken = $this->getAccessToken();
799
        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$accessToken['access_token']}";
800
        return HttpService::instance()
801
            ->url($url)
802
            ->data($data)
803
            ->post()
804
            ->toArray();
805
    }
806
807
    /**
808
     * 获取小程序全局唯一后台接口调用凭据(access_token)
809
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
810
     * @return bool|mixed|string
811
     * @throws DbException
812
     * @throws DtaException
813
     */
814
    public function accessToken()
815
    {
816
        return $this->getAccessToken();
817
    }
818
819
    /**
820
     * 获取access_token信息
821
     * @return array|bool|mixed|string|string[]
822
     * @throws DbException
823
     * @throws DtaException
824
     */
825
    private function getAccessToken()
826
    {
827
        if (empty($this->cache) || empty($this->app_id) || empty($this->app_secret)) {
828
            $this->getConfig();
829
        }
830
        if (empty($this->cache)) {
831
            throw new DtaException('请检查cache参数');
832
        }
833
        if (empty($this->app_id)) {
834
            throw new DtaException('请检查app_id参数');
835
        }
836
        if (empty($this->app_secret)) {
837
            throw new DtaException('请检查app_secret参数');
838
        }
839
        $this->grant_type = "client_credential";
840
        if ($this->cache === "file") {
841
            // 文件名
842
            $file = "{$this->app->getRootPath()}runtime/{$this->app_id}_access_token.json";
843
            // 获取数据
844
            $accessToken = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
845
            if (empty($accessToken) || !is_array($accessToken)) {
846
                $accessToken = [
847
                    'access_token' => '',
848
                    'expires_in' => '',
849
                    'expires_time' => '',
850
                ];
851
            }
852
            if (empty($accessToken['expires_time'])) {
853
                $accessToken_res = HttpService::instance()
854
                    ->url("https://api.weixin.qq.com/cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
855
                    ->toArray();
856
                $accessToken_res['expires_time'] = time() + 6000;
857
                file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
858
                $accessToken = $accessToken_res;
859
            } else if (!isset($accessToken['access_token'])) {
860
                $accessToken_res = HttpService::instance()
861
                    ->url("https://api.weixin.qq.com/cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
862
                    ->toArray();
863
                $accessToken_res['expires_time'] = time() + 6000;
864
                file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
865
                $accessToken = $accessToken_res;
866
            } else if ($accessToken['expires_time'] <= time()) {
867
                $accessToken_res = HttpService::instance()
868
                    ->url("https://api.weixin.qq.com/cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
869
                    ->toArray();
870
                $accessToken_res['expires_time'] = time() + 6000;
871
                file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
872
                $accessToken = $accessToken_res;
873
            }
874
            return $accessToken;
875
        }
876
877
        if ($this->cache === "mysql") {
878
            $access_token = [];
879
            // 文件名
880
            $file = "{$this->app_id}_access_token";
881
            // 获取数据
882
            $cache_mysql_value = dtacache($file);
883
            if (!empty($cache_mysql_value)) {
884
                $access_token['access_token'] = $cache_mysql_value;
885
            } else {
886
                $accessToken_res = HttpService::instance()
887
                    ->url("https://api.weixin.qq.com/cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
888
                    ->toArray();
889
                dtacache($file, $accessToken_res['access_token'], 6000);
890
                $access_token['access_token'] = $accessToken_res['access_token'];
891
            }
892
            return $access_token;
893
        }
894
895
        throw new DtaException("驱动方式错误");
896
    }
897
}
898