Passed
Push — v6 ( 6c09a8...b3cd76 )
by 光春
02:47
created

JinBaoService::liveDetail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
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\pinduoduo;
21
22
use DtApp\ThinkLibrary\exception\DtaException;
23
use DtApp\ThinkLibrary\Service;
24
use think\exception\HttpException;
25
26
/**
27
 * 进宝
28
 * Class JinBaoService
29
 * @package DtApp\ThinkLibrary\service\PinDuoDuo
30
 */
31
class JinBaoService extends Service
32
{
33
    /**
34
     * 接口地址
35
     * @var
36
     */
37
    private $url = 'http://gw-api.pinduoduo.com/api/router';
38
39
    /**
40
     * API接口名称
41
     * @var string
42
     */
43
    private $type = '';
44
45
    /**
46
     * 开放平台分配的clientId
47
     * @var string
48
     */
49
    private $client_id = '';
50
51
    /**
52
     * 开放平台分配的clientSecret
53
     * @var string
54
     */
55
    private $client_secret = '';
56
57
    /**
58
     * 通过code获取的access_token(无需授权的接口,该字段不参与sign签名运算)
59
     * @var string
60
     */
61
    private $access_token = '';
0 ignored issues
show
introduced by
The private property $access_token is not used, and could be removed.
Loading history...
62
63
    /**
64
     * 响应格式,即返回数据的格式,JSON或者XML(二选一),默认JSON,注意是大写
65
     * @var string
66
     */
67
    private $data_type = 'JSON';
68
69
    /**
70
     * API协议版本号,默认为V1,可不填
71
     * @var string
72
     */
73
    private $version = 'v1';
74
75
    /**
76
     * 需要发送的的参数
77
     * @var
78
     */
79
    private $param;
80
81
    /**
82
     * 响应内容
83
     * @var
84
     */
85
    private $output;
86
87
    /*
88
     * 配置开放平台分配的clientId
89
     */
90
    public function clientId(string $clientId)
91
    {
92
        $this->client_id = $clientId;
93
        return $this;
94
    }
95
96
    /**
97
     * 配置开放平台分配的clientSecret
98
     * @param string $clientSecret
99
     * @return $this
100
     */
101
    public function clientSecret(string $clientSecret)
102
    {
103
        $this->client_secret = $clientSecret;
104
        return $this;
105
    }
106
107
    /**
108
     * 响应格式,即返回数据的格式,JSON或者XML(二选一),默认JSON,注意是大写
109
     * @param string $dataType
110
     * @return $this
111
     */
112
    public function dataType(string $dataType)
113
    {
114
        $this->data_type = $dataType;
115
        return $this;
116
    }
117
118
    /**
119
     * 请求参数
120
     * @param array $param
121
     * @return $this
122
     */
123
    public function param(array $param)
124
    {
125
        $this->param = $param;
126
        return $this;
127
    }
128
129
    /**
130
     * 网络请求
131
     * @return JinBaoService
132
     * @throws DtaException
133
     */
134
    private function http()
135
    {
136
        //生成签名
137
        $sign = $this->createSign();
138
        //组织参数
139
        $strParam = $this->createStrParam();
140
        $strParam .= 'sign=' . $sign;
141
        //访问服务
142
        $url = "{$this->url}?" . $strParam;
143
        $result = file_get_contents($url);
144
        $result = json_decode($result, true);
145
        $this->output = $result;
146
        return $this;
147
    }
148
149
    /**
150
     * 获取配置信息
151
     * @return $this
152
     */
153
    private function getConfig()
154
    {
155
        $this->client_id = config('dtapp.pinduoduo.jinbao.client_id');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('dtapp.pinduoduo.jinbao.client_id') can also be of type boolean. However, the property $client_id 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...
156
        $this->client_secret = config('dtapp.pinduoduo.jinbao.client_secret');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('dtapp.pinduoduo.jinbao.client_secret') can also be of type boolean. However, the property $client_secret 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...
157
        return $this;
158
    }
159
160
    /**
161
     * 获取商品信息 - 多多进宝商品查询
162
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.search
163
     * @return $this
164
     */
165
    public function goodsSearch()
166
    {
167
        $this->type = 'pdd.ddk.goods.search';
168
        return $this;
169
    }
170
171
    /**
172
     * 新增推广位 - 创建多多进宝推广位
173
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.pid.generate
174
     * @return $this
175
     */
176
    public function goodsPidGenerate()
177
    {
178
        $this->type = 'pdd.ddk.goods.pid.generate';
179
        return $this;
180
    }
181
182
    /**
183
     * 管理推广位 - 查询已经生成的推广位信息
184
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.pid.query
185
     * @return $this
186
     */
187
    public function goodsPidQuery()
188
    {
189
        $this->type = 'pdd.ddk.goods.pid.query';
190
        return $this;
191
    }
192
193
    /**
194
     * CPS订单数据 - 查询订单详情
195
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.order.detail.get
196
     * @return $this
197
     */
198
    public function orderDetailGet()
199
    {
200
        $this->type = 'pdd.ddk.order.detail.get';
201
        return $this;
202
    }
203
204
    /**
205
     * CPS订单数据 - 最后更新时间段增量同步推广订单信息
206
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.order.list.increment.get
207
     * @return $this
208
     */
209
    public function orderListIncrementGet()
210
    {
211
        $this->type = 'pdd.ddk.order.list.increment.get';
212
        return $this;
213
    }
214
215
    /**
216
     * CPS订单数据 - 用时间段查询推广订单接口
217
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.order.list.range.get
218
     * @return $this
219
     */
220
    public function orderListRangeGet()
221
    {
222
        $this->type = 'pdd.ddk.order.list.range.get';
223
        return $this;
224
    }
225
226
    /**
227
     * CPA效果数据 - 查询CPA数据
228
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.finance.cpa.query
229
     * @return $this
230
     */
231
    public function financeCpaQuery()
232
    {
233
        $this->type = 'pdd.ddk.finance.cpa.query';
234
        return $this;
235
    }
236
237
    /**
238
     * 单品推广- 多多进宝推广链接生成
239
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.promotion.url.generate
240
     * @return $this
241
     */
242
    public function goodsPromotionUrlGenerate()
243
    {
244
        $this->type = 'pdd.ddk.goods.promotion.url.generate';
245
        return $this;
246
    }
247
248
    /**
249
     * 单品推广- 多多客生成单品推广小程序二维码url
250
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.weapp.qrcode.url.gen
251
     * @return $this
252
     */
253
    public function weAppQrcodeUrlGen()
254
    {
255
        $this->type = 'pdd.ddk.weapp.qrcode.url.gen';
256
        return $this;
257
    }
258
259
    /**
260
     * 单品推广- 多多进宝转链接口
261
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.zs.unit.url.gen
262
     * @return $this
263
     */
264
    public function goodsZsUitUrlGen()
265
    {
266
        $this->type = 'pdd.ddk.goods.zs.unit.url.gen';
267
        return $this;
268
    }
269
270
    /**
271
     * 活动转链 - 生成多多进宝频道推广
272
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.resource.url.gen
273
     * @return $this
274
     */
275
    public function resourceUrlGen()
276
    {
277
        $this->type = 'pdd.ddk.resource.url.gen';
278
        return $this;
279
    }
280
281
    /**
282
     * 活动转链 - 多多进宝主题推广链接生成
283
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.theme.prom.url.generate
284
     * @return $this
285
     */
286
    public function themePromUrlGenerate()
287
    {
288
        $this->type = 'pdd.ddk.theme.prom.url.generate';
289
        return $this;
290
    }
291
292
    /**
293
     * 店铺推广 - 多多客生成店铺推广链接
294
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.mall.url.gen
295
     * @return $this
296
     */
297
    public function mallUrlGen()
298
    {
299
        $this->type = 'pdd.ddk.mall.url.gen';
300
        return $this;
301
    }
302
303
    /**
304
     * 营销工具 - 生成营销工具推广链接
305
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.rp.prom.url.generate
306
     * @return $this
307
     */
308
    public function rpPromUrlGenerate()
309
    {
310
        $this->type = 'pdd.ddk.rp.prom.url.generate';
311
        return $this;
312
    }
313
314
    /**
315
     * 获取商品信息 - 多多进宝商品详情查询
316
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.detail
317
     * @return $this
318
     */
319
    public function goodsDetail()
320
    {
321
        $this->type = 'pdd.ddk.goods.detail';
322
        return $this;
323
    }
324
325
    /**
326
     * 获取商品信息 - 查询商品的推广计划
327
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.unit.query
328
     * @return $this
329
     */
330
    public function goodsUnitQuery()
331
    {
332
        $this->type = 'pdd.ddk.goods.unit.query';
333
        return $this;
334
    }
335
336
    /**
337
     * 商品&店铺检索 - 获取商品基本信息接口
338
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.basic.info.get
339
     * @return $this
340
     */
341
    public function goodsBasicInfoGet()
342
    {
343
        $this->type = 'pdd.ddk.goods.basic.info.get';
344
        return $this;
345
    }
346
347
    /**
348
     * 商品&店铺检索 - 查询优惠券信息
349
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.coupon.info.query
350
     * @return $this
351
     */
352
    public function couponInfoQuery()
353
    {
354
        $this->type = 'pdd.ddk.coupon.info.query';
355
        return $this;
356
    }
357
358
    /**
359
     * 商品&店铺检索 - 查询店铺商品
360
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.mall.goods.list.get
361
     * @return $this
362
     */
363
    public function goodsListGet()
364
    {
365
        $this->type = 'pdd.ddk.mall.goods.list.get';
366
        return $this;
367
    }
368
369
    /**
370
     * 多多客获取爆款排行商品接口
371
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.top.goods.list.query
372
     * @return $this
373
     */
374
    public function topGoodsListQuery()
375
    {
376
        $this->type = 'pdd.ddk.top.goods.list.query';
377
        return $this;
378
    }
379
380
    /**
381
     * 爆品推荐 - 多多进宝商品推荐API
382
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.recommend.get
383
     * @return $this
384
     */
385
    public function goodsRecommendGet()
386
    {
387
        $this->type = 'pdd.ddk.goods.recommend.get';
388
        return $this;
389
    }
390
391
    /**
392
     * 爆品推荐 - 多多进宝主题列表查询
393
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.theme.list.get
394
     * @return $this
395
     */
396
    public function themeListGet()
397
    {
398
        $this->type = 'pdd.ddk.theme.list.get';
399
        return $this;
400
    }
401
402
    /**
403
     * 活动选品库 - 多多进宝主题商品查询
404
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.theme.goods.search
405
     * @return $this
406
     */
407
    public function themeGoodsSearch()
408
    {
409
        $this->type = 'pdd.ddk.theme.goods.search';
410
        return $this;
411
    }
412
413
    /**
414
     * 生成商城-频道推广链接
415
     * https://open.pinduoduo.com/application/document/api?id=pdd.ddk.cms.prom.url.
416
     * @return $this
417
     */
418
    public function cmsPromUrlGenerate()
419
    {
420
        $this->type = 'pdd.ddk.cms.prom.url.generate';
421
        return $this;
422
    }
423
424
    /**
425
     * 查询直播间详情
426
     * https://open.pinduoduo.com/application/document/api?id=pdd.ddk.live.detail
427
     * @return $this
428
     */
429
    public function liveDetail()
430
    {
431
        $this->type = 'pdd.ddk.live.detail';
432
        return $this;
433
    }
434
435
    /**
436
     * 查询直播间列表
437
     * https://open.pinduoduo.com/application/document/api?id=pdd.ddk.live.list
438
     * @return $this
439
     */
440
    public function liveList()
441
    {
442
        $this->type = 'pdd.ddk.live.list';
443
        return $this;
444
    }
445
446
    /**
447
     * 生成直播间推广链接
448
     * https://open.pinduoduo.com/application/document/api?id=pdd.ddk.live.url.gen
449
     * @return $this
450
     */
451
    public function liveUrlGen()
452
    {
453
        $this->type = 'pdd.ddk.live.url.gen';
454
        return $this;
455
    }
456
457
    /**
458
     * 多多客生成转盘抽免单url
459
     * https://open.pinduoduo.com/application/document/api?id=pdd.ddk.lottery.url.gen
460
     * @return $this
461
     */
462
    public function lotteryUrlGen()
463
    {
464
        $this->type = 'pdd.ddk.lottery.url.gen';
465
        return $this;
466
    }
467
468
    /**
469
     * 查询是否绑定备案
470
     * https://open.pinduoduo.com/application/document/api?id=pdd.ddk.member.authority.query
471
     * @return $this
472
     */
473
    public function memberAuthorityQuery()
474
    {
475
        $this->type = 'pdd.ddk.member.authority.query';
476
        return $this;
477
    }
478
479
    /**
480
     * 返回数组数据
481
     * @return array|mixed
482
     * @throws DtaException
483
     */
484
    public function toArray()
485
    {
486
        //首先检测是否支持curl
487
        if (!extension_loaded("curl")) {
488
            throw new HttpException(404, '请开启curl模块!');
489
        }
490
        if (empty($this->client_id)) {
491
            $this->getConfig();
492
        }
493
        if (empty($this->client_id)) {
494
            throw new DtaException('请检查client_id参数');
495
        }
496
        $this->param['type'] = $this->type;
497
        $this->param['client_id'] = $this->client_id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->client_id can also be of type boolean. However, the property $client_id 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...
498
        $this->param['timestamp'] = time();
499
        $this->param['data_type'] = $this->data_type;
500
        $this->param['version'] = $this->version;
501
        $this->http();
502
        if (isset($this->output['error_response'])) {
503
            // 错误
504
            if (is_array($this->output)) {
505
                return $this->output;
506
            }
507
            if (is_object($this->output)) {
508
                return $this->object2array($this->output);
509
            }
510
            return json_decode($this->output, true);
511
        }
512
        // 正常
513
        if (is_array($this->output)) {
514
            return $this->output;
515
        }
516
        if (is_object($this->output)) {
517
            $this->output = $this->object2array($this->output);
518
            return $this->output;
519
        }
520
        $this->output = json_decode($this->output, true);
521
        return $this->output;
522
    }
523
524
    /**
525
     * @param $object
526
     * @return array
527
     */
528
    private function object2array(&$object)
529
    {
530
        if (is_object($object)) {
531
            $arr = (array)($object);
532
        } else {
533
            $arr = &$object;
534
        }
535
        if (is_array($arr)) {
536
            foreach ($arr as $varName => $varValue) {
537
                $arr[$varName] = $this->object2array($varValue);
538
            }
539
        }
540
        return $arr;
541
    }
542
543
    /**
544
     * 签名
545
     * @return string
546
     * @throws DtaException
547
     */
548
    private function createSign()
549
    {
550
        if (empty($this->client_secret)) {
551
            $this->getConfig();
552
        }
553
        if (empty($this->client_secret)) {
554
            throw new DtaException('请检查client_secret参数}');
555
        }
556
        $sign = $this->client_secret;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->client_secret can also be of type boolean. However, the property $client_secret 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...
557
        ksort($this->param);
558
        foreach ($this->param as $key => $val) {
559
            if ($key != '' && $val != '') {
560
                $sign .= $key . $val;
561
            }
562
        }
563
        $sign .= $this->client_secret;
564
        $sign = strtoupper(md5($sign));
565
        return $sign;
566
    }
567
568
    /**
569
     * 组参
570
     * @return string
571
     */
572
    private function createStrParam()
573
    {
574
        $strParam = '';
575
        foreach ($this->param as $key => $val) {
576
            if ($key != '' && $val != '' && !is_array($val)) {
577
                $strParam .= $key . '=' . urlencode($val) . '&';
578
            }
579
        }
580
        return $strParam;
581
    }
582
583
    /**
584
     * 获取频道ID
585
     * @return array[]
586
     */
587
    public function getChannelTypeList()
588
    {
589
        return [
590
            [
591
                // https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.recommend.get
592
                'name' => '商品推荐',
593
                'list' => [
594
                    [
595
                        'name' => '1.9包邮',
596
                        'channel_type' => 0
597
                    ],
598
                    [
599
                        'name' => '今日爆款',
600
                        'channel_type' => 1
601
                    ],
602
                    [
603
                        'name' => '品牌清仓',
604
                        'channel_type' => 2
605
                    ],
606
                    [
607
                        'name' => '相似商品推荐',
608
                        'channel_type' => 3
609
                    ],
610
                    [
611
                        'name' => '猜你喜欢',
612
                        'channel_type' => 4
613
                    ],
614
                    [
615
                        'name' => '实时热销',
616
                        'channel_type' => 5
617
                    ],
618
                    [
619
                        'name' => '实时收益',
620
                        'channel_type' => 6
621
                    ],
622
                    [
623
                        'name' => '今日畅销',
624
                        'channel_type' => 7
625
                    ],
626
                    [
627
                        'name' => '高佣榜单',
628
                        'channel_type' => 8
629
                    ],
630
                ]
631
            ],
632
        ];
633
    }
634
635
    /**
636
     * 获取频道来源ID
637
     * @return array[]
638
     */
639
    public function getResourceTypeList()
640
    {
641
        return [
642
            [
643
                // https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.resource.url.gen
644
                'name' => '频道推广',
645
                'list' => [
646
                    [
647
                        'name' => '限时秒杀',
648
                        'resource_type' => 4
649
                    ],
650
                    [
651
                        'name' => '充值中心',
652
                        'resource_type' => 39997
653
                    ],
654
                    [
655
                        'name' => '转链',
656
                        'resource_type' => 39998
657
                    ],
658
                    [
659
                        'name' => '百亿补贴',
660
                        'resource_type' => 39996
661
                    ],
662
                ]
663
            ],
664
        ];
665
    }
666
}
667