Passed
Push — master ( c11190...b979a3 )
by ma
03:25
created

Base::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 0
c 1
b 0
f 1
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace tinymeng\Chinaums\Service\Contract;
4
5
use tinymeng\Chinaums\Tools\Http;
6
use tinymeng\Chinaums\Tools\DES;
7
use Exception;
8
9
class Base extends \tinymeng\Chinaums\Service\Common\Base
10
{
11
12
    /**
13
     * 加密算法
14
     *
15
     * @var string
16
     */
17
    protected $method = 'DES-EDE3';
18
    /**
19
     * http请求方式 
20
     *
21
     * @var string get post
22
     */
23
    protected $httpMethod = 'get';
24
25
26
    public function request($data = [])
27
    {
28
        $data['accesser_id'] = $this->config['accesser_id'];
29
        $key = $this->config['private_key'];
30
        if ($data) {
31
            $this->body = array_merge($this->body, $data);
32
        }
33
        try {
34
            $this->validate();
35
            $data = $this->body;
36
            $gateway  = $this->config['gateway'] . $this->api;
37
            $data = json_encode($data);
38
            $sign = hash('sha256', $data);
39
            $method = $this->method;
40
            $des = new DES($key, $method, DES::OUTPUT_HEX);
41
            // 加密
42
            $str = $des->encrypt($data);
43
            if ('cli' == php_sapi_name()) {
44
                echo 'api:' . $gateway . PHP_EOL;
45
                echo 'request:' . $data . PHP_EOL;
46
            }
47
            $headers = [
48
                'Content-Type: application/json',
49
            ];
50
            $headers = $headers;
51
            $options = [
52
                CURLOPT_HTTPHEADER => $headers,
53
                CURLOPT_TIMEOUT => 60,
54
                CURLOPT_CONNECTTIMEOUT => 30
55
            ];
56
            $params = [
57
                'json_data' => $str,
58
                'sign_data' => $sign,
59
                'accesser_id' => $this->config['accesser_id']
60
            ];
61
            $httpMethod = strtolower($this->httpMethod);
62
            $response = Http::$httpMethod($gateway, $params, $options);
63
            return $response;
64
        } catch (Exception $e) {
65
            return json_encode(['res_code' => -1, 'res_msg' => $e->getMessage(), 'request_seq' => null]);
66
        }
67
    }
68
69
}
70