UnionService   B
last analyzed

Complexity

Total Complexity 44

Size/Duplication

Total Lines 495
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 44
eloc 156
c 1
b 0
f 0
dl 0
loc 495
rs 8.8798

25 Methods

Rating   Name   Duplication   Size   Complexity  
A orderBonusQuery() 0 4 1
A accessToken() 0 4 1
A goodsQuery() 0 4 1
A http() 0 11 1
A getEliteIdList() 0 98 1
A secretKey() 0 4 1
A param() 0 4 1
A createStrParam() 0 9 4
A setMethod() 0 4 1
A createSign() 0 18 6
A promotionByUnionIdGet() 0 4 1
B toArray() 0 41 10
A appKey() 0 4 1
A userPidGet() 0 4 1
A couponGiftGet() 0 4 1
A orderRowQuery() 0 4 1
A promotionBySubUnionIdGet() 0 4 1
A goodsJIngFenQuery() 0 10 3
A goodsPromotionGoodsInfoQuery() 0 4 1
A couponGiftStop() 0 4 1
A positionCreate() 0 4 1
A getConfig() 0 5 1
A promotionCommonGet() 0 4 1
A positionQuery() 0 4 1
A statisticsGifTCouponQuery() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like UnionService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use UnionService, and based on these observations, apply Extract Interface, too.

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