DataDownload   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 24
c 1
b 0
f 1
dl 0
loc 44
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A request() 0 22 3
1
<?php
2
3
namespace tinymeng\Chinaums\Service\Contract;
4
5
use tinymeng\Chinaums\Service\Contract\Base;
6
use tinymeng\Chinaums\Tools\DES;
7
use Exception;
8
9
/**
10
 * 省市区行业数据下载接口
11
 */
12
class DataDownload extends Base
13
{
14
    /**
15
     * @var string 接口地址
16
     */
17
    protected $api = '/self-contract-nmrs/interface/autoReg';
18
    /**
19
     * @var array $body 请求参数
20
     */
21
    protected $body = [
22
        'service' => 'data_download',
23
        'sign_type' => 'SHA-256',
24
    ];
25
    /**
26
     * 必传的值
27
     * @var array
28
     */
29
    protected $require = ['service', 'accesser_id', 'sign_type', 'request_date', 'request_seq', 'data_type'];
30
31
    /**
32
     * @return false|mixed|string
33
     */
34
    public function request()
35
    {
36
        $data = $this->params;
37
        $data['accesser_id'] = $this->config['accesser_id'];
38
        $key = $this->config['private_key'];
39
        if ($data) {
40
            $this->body = array_merge($this->body, $data);
41
        }
42
        try {
43
            $this->validate();
44
            $data = $this->body;
45
            $gateway  = $this->config['gateway'] . $this->api;
46
            $data = json_encode($data);
47
            $sign = hash('sha256', $data);
48
            $method = $this->method;
49
            $des = new DES($key, $method, DES::OUTPUT_HEX);
50
            // 加密
51
            $str = $des->encrypt($data);
52
            $url = $gateway . '?sign_data=' . $sign . '&json_data=' . $str . '&accesser_id=' . $this->config['accesser_id'];
53
            return json_encode(['res_code' => '0000', 'res_msg' => 'success', 'url' => $url], JSON_UNESCAPED_SLASHES);
54
        } catch (Exception $e) {
55
            return json_encode(['res_code' => -1, 'res_msg' => $e->getMessage(), 'request_seq' => null]);
56
        }
57
    }
58
}
59