JinBaoService::couponInfoQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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