1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
namespace common\components;
use Yii;
use yii\base\Model;
/**
* Base
* 基本类
* -----
* @author Verdient。
* @version 1.0.0
*/
class Base extends Model
{
/**
* @var protected String $_key
* 签名密钥
* ---------------------------
* @method Config
* @author Verdient。
*/
protected $_key;
/**
* @var public $_url
* URL 路径
* -----------------
* @author Verdient。
*/
protected $_url;
/**
* @var protected $_code
* 状态码
* ---------------------
* @author Verdient。
*/
protected $_code;
/**
* @var protected $_data
* 数据
* ---------------------
* @author Verdient。
*/
protected $_data;
/**
* @var protected $_header
* 响应头部信息
* -----------------------
* @author Verdient。
*/
protected $_header;
/**
* @var protected $_response
* 响应原文
* -------------------------
* @author Verdient。
*/
protected $_response;
/**
* @var protected $_error
* 错误信息
* ----------------------
* @author Verdient。
*/
protected $_error;
/**
* __construct(String $key, String $url[, Mixed $id = null])
* 初始化
* ---------------------------------------------------------
* @param String $key 签名密钥
* @param String $url url
* @param Mixed $id 实例编号
* --------------------------
* @author Verdient。
*/
public function __construct($key, $url){
$this->_key = $key;
$this->_url = $url;
parent::__construct();
}
/**
* getCode()
* 获取响应码
* ---------
* @return Integer
* @author Verdient。
*/
public function getCode(){
return $this->_code;
}
/**
* getData()
* 获取响应数据
* -----------
* @return Array
* @author Verdient。
*/
public function getData(){
return $this->_data['data'];
}
/**
* getHeader()
* 获取响应头数据
* -------------
* @return Array
* @author Verdient。
*/
public function getHeader(){
return $this->_header;
}
/**
* getError()
* 获取响应错误
* -----------
* @return Array
* @author Verdient。
*/
public function getError(){
return $this->_error;
}
/**
* getResponse()
* 获取响应原文
* -------------
* @return Array
* @author Verdient。
*/
public function getResponse(){
return $this->_response;
}
/**
* formName()
* 获取表单名称
* -----------
* @inheritdoc
* -----------
* @return String
* @author Verdient。
*/
public function formName(){
return '';
}
/**
* setAttributes(Array $values[, Boolean $safeOnly = false])
* 设置属性
* ---------------------------------------------------------
* @param Array $values 要设置的属性值
* @param Boolean $safeOnly 是否仅设置安全的属性
* -------------------------------------------
* @inheritdoc
* -----------
* @author Verdient。
*/
public function setAttributes($values, $safeOnly = false){
return parent::setAttributes($values, $safeOnly);
}
/**
* _formatError(Array $errors)
* 格式化错误
* ---------------------------
* @return Array
* @author Verdient。
*/
protected function _formatError(Array $errors){
$errorMap = [];
foreach($errors as $error){
$errorMap[$error['field']][] = $error['message'];
}
return $errorMap;
}
/**
* _send(String $method, String $url[, Array $data = [], Array $query = []])
* 发送数据
* -------------------------------------------------------------------------
* @param String $method 访问方法
* @param String $url URL路径
* @param Array $data 发送的数据
* @param Array $query 查询条件
* ------------------------------
* @return Array
* @author Verdient。
*/
protected function _send($method, $url, Array $data = [], Array $query = []){
$method = strtolower($method);
if(!empty($query)){
Yii::$app->cUrl->setQuery($query);
}
$signature = new Signature();
$signature->key = $this->_key;
$signature->verb = $method;
$signature->contentType = 'application/json';
$signature->signatureMethod = 'sha256';
if(!empty($data)){
Yii::$app->cUrl->setData($data, 'JSON');
}
$signature->content = $data;
Yii::$app->cUrl->setHeader([
'Signature' => $signature->signature,
'Date' => $signature->date,
'Verb' => $signature->verb,
'Signature-Method' => $signature->signatureMethod,
'Signature-Version' => $signature->signatureVersion
]);
$options = Yii::$app->cUrl->getOptions();
$response = Yii::$app->cUrl->$method($url, 'JSON');
Yii::trace(['url' => $url, 'cUrl' => $options, 'response' => $response], __METHOD__);
$this->_response = $response;
$this->_code = isset($response['content']['code']) ? $response['content']['code'] : null;
if(isset($response['content']['id'])){
$this->_id = $response['content']['id'];
}
$this->_data = isset($response['content']) ? $response['content'] : null;
$this->_header = isset($response['header']) ? $response['header'] : null;
}
}