Passed
Push — v6 ( 533a60...c76d97 )
by 光春
03:18
created

EJiAoFei::createStrParam()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 4
nc 3
nop 0
1
<?php
2
3
namespace DtApp\ThinkLibrary\service\decent;
4
5
use DtApp\ThinkLibrary\exception\DtaException;
6
use DtApp\ThinkLibrary\facade\Xmls;
7
use DtApp\ThinkLibrary\Service;
8
use think\exception\HttpException;
9
10
/**
11
 * 缴费平台
12
 * Class EJiAoFei
13
 * @package DtApp\ThinkLibrary\service\decent
14
 */
15
class EJiAoFei extends Service
16
{
17
    /**
18
     * 待请求的链接
19
     * @var string
20
     */
21
    private $api, $method = '';
22
23
    /**
24
     * 由鼎信商务提供
25
     * @var
26
     */
27
    private $userid, $pwd, $key = '';
28
29
    /**
30
     * 需要发送的的参数
31
     * @var
32
     */
33
    private $param;
34
35
    /**
36
     * 响应内容
37
     * @var
38
     */
39
    private $output;
40
41
    /**
42
     * ip:端口
43
     * @param string $api
44
     * @return $this
45
     */
46
    public function api(string $api): self
47
    {
48
        $this->api = $api;
49
        return $this;
50
    }
51
52
    /**
53
     * 由鼎信商务提供
54
     * @param string $userid
55
     * @return $this
56
     */
57
    public function userid(string $userid): self
58
    {
59
        $this->userid = $userid;
60
        return $this;
61
    }
62
63
    /**
64
     * 由鼎信商务提供
65
     * @param string $pwd
66
     * @return $this
67
     */
68
    public function pwd(string $pwd): self
69
    {
70
        $this->pwd = $pwd;
71
        return $this;
72
    }
73
74
    /**
75
     * 由鼎信商务提供
76
     * @param string $key
77
     * @return $this
78
     */
79
    public function key(string $key): self
80
    {
81
        $this->key = $key;
82
        return $this;
83
    }
84
85
    /**
86
     * 请求参数
87
     * @param array $param
88
     * @return $this
89
     */
90
    public function param(array $param): self
91
    {
92
        $this->param = $param;
93
        return $this;
94
    }
95
96
    /**
97
     * 话费充值
98
     * @return $this
99
     */
100
    public function chongZhi(): self
101
    {
102
        $this->method = 'chongzhi_jkorders';
103
        return $this;
104
    }
105
106
    /**
107
     * 通用查询
108
     * @return $this
109
     */
110
    public function query(): self
111
    {
112
        $this->method = 'query_jkorders';
113
        return $this;
114
    }
115
116
    /**
117
     * 用户余额查询
118
     * @return $this
119
     */
120
    public function money(): self
121
    {
122
        $this->method = 'money_jkuser';
123
        return $this;
124
    }
125
126
    /**
127
     * 腾讯充值
128
     * @return $this
129
     */
130
    public function txchongzhi(): self
131
    {
132
        $this->method = 'txchongzhi';
133
        return $this;
134
    }
135
136
    /**
137
     * 可充值腾讯产品查询
138
     * @return $this
139
     */
140
    public function queryTXproduct(): self
141
    {
142
        $this->method = 'queryTXproduct';
143
        return $this;
144
    }
145
146
    /**
147
     * 流量充值
148
     * @return $this
149
     */
150
    public function gprsChongzhiAdvance(): self
151
    {
152
        $this->method = 'queryTXproduct';
153
        return $this;
154
    }
155
156
    /**
157
     * 会员订单成本价查询
158
     * @return $this
159
     */
160
    public function checkCost(): self
161
    {
162
        $this->method = 'checkCost';
163
        return $this;
164
    }
165
166
    /**
167
     * @throws DtaException
168
     */
169
    public function toArray()
170
    {
171
        //首先检测是否支持curl
172
        if (!extension_loaded("curl")) {
173
            throw new HttpException(404, '请开启curl模块!');
174
        }
175
        if (empty($this->api)) {
176
            throw new DtaException('请检查api参数');
177
        }
178
        $this->http();
179
        // 正常
180
        if (is_array($this->output)) {
0 ignored issues
show
introduced by
The condition is_array($this->output) is always false.
Loading history...
181
            return $this->output;
182
        };
183
        if (is_object($this->output)) {
0 ignored issues
show
introduced by
The condition is_object($this->output) is always false.
Loading history...
184
            $this->output = json_encode($this->output, JSON_UNESCAPED_UNICODE);
185
        }
186
        $this->output = json_decode($this->output, true);
187
        return $this->output;
188
    }
189
190
    /**
191
     * 网络请求
192
     * @throws DtaException
193
     */
194
    private function http(): void
195
    {
196
        //生成签名
197
        $sign = $this->createSign();
198
        //组织参数
199
        $strParam = $this->createStrParam();
200
        $strParam .= '&userkey=' . $sign;
201
        $url = "http://" . $this->api . "/" . $this->method . ".do?{$strParam}";
202
        $result = file_get_contents($url);
203
        $result = Xmls::toArray($result);
204
        $this->output = $result;
205
    }
206
207
    /**
208
     * 签名
209
     * @return string
210
     * @throws DtaException
211
     */
212
    private function createSign(): string
213
    {
214
        if (empty($this->key)) {
215
            throw new DtaException('请检查key参数');
216
        }
217
        if (empty($this->userid)) {
218
            throw new DtaException('请检查userid参数');
219
        }
220
        if (empty($this->pwd)) {
221
            throw new DtaException('请检查pwd参数');
222
        }
223
        $this->param['userid'] = $this->userid;
224
        $this->param['pwd'] = $this->pwd;
225
        $sign = "userid{$this->userid}pwd{$this->pwd}";
226
        ksort($this->param);
227
        foreach ($this->param as $key => $val) {
228
            if ($key !== '' && $val !== '') {
229
                $sign .= $key . $val;
230
            }
231
        }
232
        $sign .= $this->key;
233
        $sign = strtoupper(md5($sign));
234
        return $sign;
235
    }
236
237
    /**
238
     * 组参
239
     * @return string
240
     */
241
    private function createStrParam(): string
242
    {
243
        $strParam = '';
244
        foreach ($this->param as $key => $val) {
245
            if ($key !== '' && $val !== '') {
246
                $strParam .= $key . '=' . urlencode($val) . '&';
247
            }
248
        }
249
        return substr($strParam, 0, -1);
250
    }
251
}