Passed
Push — v6 ( 68f5c0...1bc81c )
by 光春
04:57
created

UnionService::orderBonusQuery()   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\jd;
21
22
use DtApp\ThinkLibrary\exception\DtaException;
23
use DtApp\ThinkLibrary\facade\Strings;
0 ignored issues
show
Bug introduced by
The type DtApp\ThinkLibrary\facade\Strings was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use DtApp\ThinkLibrary\Service;
25
use think\exception\HttpException;
26
27
/**
28
 * 京东联盟
29
 * Class UnionService
30
 * @package DtApp\ThinkLibrary\service\Jd
31
 */
32
class UnionService extends Service
33
{
34
    /**
35
     * 接口地址
36
     * @var string
37
     */
38
    private $url = "https://router.jd.com/api";
39
40
    /**
41
     * API接口名称
42
     * @var
43
     */
44
    private $method = '', $response = '';
0 ignored issues
show
introduced by
The private property $response is not used, and could be removed.
Loading history...
45
46
    /**
47
     * 联盟分配给应用的appkey
48
     * @var
49
     */
50
    private $app_key = '';
51
52
    /**
53
     * 联盟分配给应用的secretkey
54
     * @var
55
     */
56
    private $secret_key = '';
57
58
    /**
59
     * 根据API属性标签,如果需要授权,则此参数必传;如果不需要授权,则此参数不需要传
60
     * @var
61
     */
62
    private $access_token = '';
63
64
    /**
65
     * 响应格式,暂时只支持json
66
     * @var string
67
     */
68
    private $format = "json";
69
70
    /**
71
     * API协议版本,请根据API具体版本号传入此参数,一般为1.0
72
     * @var string
73
     */
74
    private $v = "1.0";
75
76
    /**
77
     * 签名的摘要算法,暂时只支持md5
78
     * @var string
79
     */
80
    private $sign_method = "md5";
81
82
    /**
83
     * 响应内容
84
     * @var
85
     */
86
    private $output;
87
88
    /**
89
     * 需要发送的的参数
90
     * @var
91
     */
92
    private $param, $params;
93
94
    /**
95
     * 联盟分配给应用的appkey
96
     * @param string $appKey
97
     * @return $this
98
     */
99
    public function appKey(string $appKey): self
100
    {
101
        $this->app_key = $appKey;
102
        return $this;
103
    }
104
105
    /**
106
     * 联盟分配给应用的secretkey
107
     * @param string $secretKey
108
     * @return $this
109
     */
110
    public function secretKey(string $secretKey): self
111
    {
112
        $this->secret_key = $secretKey;
113
        return $this;
114
    }
115
116
    /**
117
     * 根据API属性标签,如果需要授权,则此参数必传;如果不需要授权,则此参数不需要传
118
     * @param string $accessToken
119
     * @return $this
120
     */
121
    public function accessToken(string $accessToken): self
122
    {
123
        $this->access_token = $accessToken;
124
        return $this;
125
    }
126
127
    /**
128
     * 组参
129
     * @param array $param
130
     * @return $this
131
     */
132
    public function param(array $param): self
133
    {
134
        $this->param = $param;
135
        return $this;
136
    }
137
138
    /**
139
     * 网络请求
140
     * @throws DtaException
141
     */
142
    private function http(): void
143
    {
144
        //生成签名
145
        $sign = $this->createSign();
146
        //组织参数
147
        $strParam = $this->createStrParam();
148
        $strParam .= 'sign=' . $sign;
149
        //访问服务
150
        $result = file_get_contents("{$this->url}?{$strParam}");
151
        $result = json_decode($result, true);
152
        $this->output = $result;
153
    }
154
155
    /**
156
     * 获取配置信息
157
     * @return $this
158
     */
159
    private function getConfig(): self
160
    {
161
        $this->app_key = config('dtapp.jd.union.app_key');
162
        $this->secret_key = config('dtapp.jd.union.secret_key');
163
        return $this;
164
    }
165
166
    /**
167
     * 网站/APP获取推广链接接口
168
     * https://union.jd.com/openplatform/api/10421
169
     * @return $this
170
     */
171
    public function promotionCommonGet(): self
172
    {
173
        $this->method = 'jd.union.open.promotion.common.get';
174
        return $this;
175
    }
176
177
    /**
178
     * 社交媒体获取推广链接接口【申请】
179
     * https://union.jd.com/openplatform/api/10424
180
     * @return $this
181
     */
182
    public function promotionBySubUnionIdGet(): self
183
    {
184
        $this->method = 'jd.union.open.promotion.bysubunionid.get';
185
        return $this;
186
    }
187
188
    /**
189
     * 工具商获取推广链接接口【申请】
190
     * https://union.jd.com/openplatform/api/10425
191
     * @return $this
192
     */
193
    public function promotionByUnionIdGet(): self
194
    {
195
        $this->method = 'jd.union.open.promotion.byunionid.get';
196
        return $this;
197
    }
198
199
    /**
200
     * 订单行查询接口
201
     * https://union.jd.com/openplatform/api/12707
202
     * @return $this
203
     */
204
    public function orderRowQuery(): self
205
    {
206
        $this->method = 'jd.union.open.order.row.query';
207
        return $this;
208
    }
209
210
    /**
211
     * 奖励订单查询接口【申请】
212
     * https://union.jd.com/openplatform/api/11781
213
     * @return $this
214
     */
215
    public function orderBonusQuery(): self
216
    {
217
        $this->method = 'jd.union.open.order.bonus.query';
218
        return $this;
219
    }
220
221
    /**
222
     * 创建推广位【申请】
223
     * https://union.jd.com/openplatform/api/10429
224
     * @return $this
225
     */
226
    public function positionCreate(): self
227
    {
228
        $this->method = 'jd.union.open.position.create';
229
        return $this;
230
    }
231
232
    /**
233
     * 查询推广位【申请】
234
     * https://union.jd.com/openplatform/api/10428
235
     * @return $this
236
     */
237
    public function positionQuery(): self
238
    {
239
        $this->method = 'jd.union.open.position.query';
240
        return $this;
241
    }
242
243
    /**
244
     * 获取PID【申请】
245
     * https://union.jd.com/openplatform/api/10430
246
     * @return $this
247
     */
248
    public function userPidGet(): self
249
    {
250
        $this->method = 'jd.union.open.user.pid.get';
251
        return $this;
252
    }
253
254
    /**
255
     * 关键词商品查询接口【申请】
256
     * https://union.jd.com/openplatform/api/10421
257
     * @return $this
258
     */
259
    public function goodsQuery(): self
260
    {
261
        $this->method = 'jd.union.open.goods.query';
262
        return $this;
263
    }
264
265
    /**
266
     * 京粉精选商品查询接口
267
     * https://union.jd.com/openplatform/api/10417
268
     * @return $this
269
     */
270
    public function goodsJIngFenQuery(): self
271
    {
272
        if (!isset($this->param['pageIndex'])) {
273
            $this->param['pageIndex'] = 1;
274
        }
275
        if (!isset($this->param['pageSize'])) {
276
            $this->param['pageSize'] = 20;
277
        }
278
        $this->method = 'jd.union.open.goods.jingfen.query';
279
        return $this;
280
    }
281
282
    /**
283
     * 根据skuid查询商品信息接口
284
     * https://union.jd.com/openplatform/api/10422
285
     * @return $this
286
     */
287
    public function goodsPromotionGoodsInfoQuery(): self
288
    {
289
        $this->method = 'jd.union.open.goods.promotiongoodsinfo.query';
290
        return $this;
291
    }
292
293
    /**
294
     * 礼金创建【申请】
295
     * https://union.jd.com/openplatform/api/12246
296
     * @return $this
297
     */
298
    public function couponGiftGet(): self
299
    {
300
        $this->method = 'jd.union.open.coupon.gift.get';
301
        return $this;
302
    }
303
304
    /**
305
     * 礼金停止【申请】
306
     * https://union.jd.com/openplatform/api/12240
307
     * @return $this
308
     */
309
    public function couponGiftStop(): self
310
    {
311
        $this->method = 'jd.union.open.coupon.gift.stop';
312
        return $this;
313
    }
314
315
    /**
316
     * 礼金效果数据
317
     * https://union.jd.com/openplatform/api/12248
318
     * @return $this
319
     */
320
    public function statisticsGifTCouponQuery(): self
321
    {
322
        $this->method = 'jd.union.open.statistics.giftcoupon.query';
323
        return $this;
324
    }
325
326
    /**
327
     * 自定义接口
328
     * @param string $method
329
     * @return $this
330
     */
331
    public function setMethod($method = ''): self
332
    {
333
        $this->method = $method;
334
        return $this;
335
    }
336
337
    /**
338
     * 返回数组数据
339
     * @return array|mixed
340
     * @throws DtaException
341
     */
342
    public function toArray()
343
    {
344
        //首先检测是否支持curl
345
        if (!extension_loaded("curl")) {
346
            throw new HttpException(404, '请开启curl模块!');
347
        }
348
        if (empty($this->app_key)) {
349
            $this->getConfig();
350
        }
351
        if (empty($this->app_key)) {
352
            throw new DtaException('请检查app_key参数');
353
        }
354
        if (empty($this->method)) {
355
            throw new DtaException('请检查method参数');
356
        }
357
        $this->params['method'] = $this->method;
358
        $this->params['app_key'] = $this->app_key;
359
        $this->params['timestamp'] = date('Y-m-d H:i:s');
360
        $this->params['format'] = $this->format;
361
        $this->params['v'] = $this->v;
362
        $this->params['sign_method'] = $this->sign_method;
363
        $this->params['param_json'] = json_encode($this->param, JSON_UNESCAPED_UNICODE);
364
        $this->http();
365
        $response = Strings::replace('.', '_', $this->method) . "_response";
366
        if (isset($this->output[$response]['result'])) {
367
            if (is_array($this->output[$response]['result'])) {
368
                return $this->output[$response]['result'];
369
            }
370
            if (is_object($this->output[$response]['result'])) {
371
                $this->output = json_encode($this->output[$response]['result'], JSON_UNESCAPED_UNICODE);
372
            }
373
            return json_decode($this->output[$response]['result'], true);
374
        }
375
376
        if (is_array($this->output)) {
377
            return $this->output;
378
        }
379
        if (is_object($this->output)) {
380
            $this->output = json_encode($this->output, JSON_UNESCAPED_UNICODE);
381
        }
382
        return json_decode($this->output, true);
383
    }
384
385
    /**
386
     * 签名
387
     * @return string
388
     * @throws DtaException
389
     */
390
    private function createSign(): string
391
    {
392
        if (empty($this->secret_key)) {
393
            $this->getConfig();
394
        }
395
        if (empty($this->secret_key)) {
396
            throw new DtaException('请检查secret_key参数');
397
        }
398
        $sign = $this->secret_key;
399
        ksort($this->params);
400
        foreach ($this->params as $key => $val) {
401
            if ($key !== '' && $val !== '') {
402
                $sign .= $key . $val;
403
            }
404
        }
405
        $sign .= $this->secret_key;
406
        $sign = strtoupper(md5($sign));
407
        return $sign;
408
    }
409
410
    /**
411
     * 组参
412
     * @return string
413
     */
414
    private function createStrParam(): string
415
    {
416
        $strParam = '';
417
        foreach ($this->params as $key => $val) {
418
            if ($key !== '' && $val !== '') {
419
                $strParam .= $key . '=' . urlencode($val) . '&';
420
            }
421
        }
422
        return $strParam;
423
    }
424
425
    /**
426
     * 获取频道ID
427
     * @return array[]
428
     */
429
    public function getEliteIdList(): array
430
    {
431
        return [
432
            [
433
                // https://union.jd.com/openplatform/api/10417
434
                'name' => '京粉精选',
435
                'list' => [
436
                    [
437
                        'name' => '好券商品',
438
                        'elite_id' => 1
439
                    ],
440
                    [
441
                        'name' => '超级大卖场',
442
                        'elite_id' => 2
443
                    ],
444
                    [
445
                        'name' => '9.9专区',
446
                        'elite_id' => 10
447
                    ],
448
                    [
449
                        'name' => '热销爆品',
450
                        'elite_id' => 22
451
                    ],
452
                    [
453
                        'name' => '为你推荐',
454
                        'elite_id' => 23
455
                    ],
456
                    [
457
                        'name' => '数码家电',
458
                        'elite_id' => 24
459
                    ],
460
                    [
461
                        'name' => '超市',
462
                        'elite_id' => 25
463
                    ],
464
                    [
465
                        'name' => '母婴玩具',
466
                        'elite_id' => 26
467
                    ],
468
                    [
469
                        'name' => '家具日用',
470
                        'elite_id' => 27
471
                    ],
472
                    [
473
                        'name' => '美妆穿搭',
474
                        'elite_id' => 28
475
                    ],
476
                    [
477
                        'name' => '医药保健',
478
                        'elite_id' => 29
479
                    ],
480
                    [
481
                        'name' => '图书文具',
482
                        'elite_id' => 30
483
                    ],
484
                    [
485
                        'name' => '今日必推',
486
                        'elite_id' => 31
487
                    ],
488
                    [
489
                        'name' => '品牌好货',
490
                        'elite_id' => 32
491
                    ],
492
                    [
493
                        'name' => '秒杀商品',
494
                        'elite_id' => 33
495
                    ],
496
                    [
497
                        'name' => '拼购商品',
498
                        'elite_id' => 34
499
                    ],
500
                    [
501
                        'name' => '高收益',
502
                        'elite_id' => 40
503
                    ],
504
                    [
505
                        'name' => '自营热卖榜',
506
                        'elite_id' => 41
507
                    ],
508
                    [
509
                        'name' => '新品首发',
510
                        'elite_id' => 109
511
                    ],
512
                    [
513
                        'name' => '自营',
514
                        'elite_id' => 110
515
                    ],
516
                    [
517
                        'name' => '首购商品',
518
                        'elite_id' => 125
519
                    ],
520
                    [
521
                        'name' => '高佣榜单',
522
                        'elite_id' => 129
523
                    ],
524
                    [
525
                        'name' => '视频商品',
526
                        'elite_id' => 130
527
                    ],
528
                ]
529
            ],
530
        ];
531
    }
532
}
533