-
Notifications
You must be signed in to change notification settings - Fork 0
/
XiiRequest.php
executable file
·276 lines (240 loc) · 6.66 KB
/
XiiRequest.php
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Xii2 --- Mr Xie's Php class file.
* Most of them are designed for Yii2 project.
* That's why named Xii2.
*
* Xii2 Serial Class - XiiRequest (No Print)
*
* 作者: EricXie | 邮箱: keigonec@126.com | 版本: Version 1.0 (2015)
*
* 说明: 信息请求类, 基于XiiCurl类的功能扩展
*
* Public方法结果返回:
* 类型:
* Array
* 格式:
* [
* 'status' => true, // 成功 true;失败 false
* 'errorCode' => xxx, // 成功 xxx;失败 xxxx
* 'errorMsg' => '...', // 文字描述
* 'data' => mixed(optional) // 数据
* ]
* [
* 'status' => true, // 成功 true;失败 false
* 'errorCode' => xxx, // 成功 xxx;失败 xxxx
* 'errorMsg' => '...', // 文字描述
* 'data' => [
* 'data' => mixed(optional) // 数据
* 'pager' => mixed(optional) //分页(obj | str)
* ]
* ]
*
* What's new ?
* Build 20150919
* - 基于XiiCurl类的GET方式信息请求类
* - 提供请求API之前,先进行memcache读取功能
* - 提供请求API之前,先进行redis读取功能
* - 支持XiiToken内部验证
*
*/
namespace app\xii;
use Yii;
use yii\web\Response;
use app\xii;
use app\xii\XiiError;
use app\xii\XiiVersion;
use app\xii\XiiToken;
use app\xii\XiiUtil;
use app\xii\XiiCurl;
class XiiRequest
{
const XII_VERSION = 'Xii Request/1.0.0919';
const MEMCACHE_DURATION = 3600;
const MSG_NO_STATUS = 'No Status';
const MSG_NO_ERRORCODE = 'No ErrorCode';
const MSG_NO_ERRORMSG = 'No ErrorMsg';
const DATA_FROM_MEMCACHE = 1;
const DATA_FROM_REDIS = 2;
const DATA_FROM_API = 3;
protected static $_getFromMemcache = false;
protected static $_getFromRedis = false;
private static $_init = true;
private static $_para;
private static $_apiUrl;
private static $_apiCondition;
private static $_cacheId;
private static $_dataFrom;
private static $_outputData;
private static $_requestError = [];
private static $_getConfigYiiParams = 'XiiRequest';
private static $_getConfigFields = [
'_getFromMemcache',
'_getFromRedis',
];
public static function init()
{
XiiVersion::run(self::XII_VERSION);
self::$_outputData = null;
if(self::$_init)
{
self::getConfig();
}
}
public static function run($para, $cacheId = '')
{
//init
self::init();
self::$_para = $para;
self::$_cacheId = trim($cacheId);
//get data
self::getFromMemcache();
self::getFromRedis();
self::getFromApi();
self::outputData();
//end
Yii::$app->end();
}
public static function blockConfig()
{
self::$_init = false;
}
public static function openCache()
{
self::blockConfig();
self::$_getFromMemcache = true;
self::$_getFromRedis = true;
}
public static function onlyMemcache()
{
self::blockConfig();
self::$_getFromMemcache = true;
self::$_getFromRedis = false;
}
public static function onlyRedis()
{
self::blockConfig();
self::$_getFromRedis = true;
self::$_getFromMemcache = false;
}
private static function getFromMemcache()
{
if(!self::$_getFromMemcache)
{
return;
}
if(!empty(self::$_outputData))
{
return;
}
if(self::$cacheId == '')
{
return;
}
$servers = Yii::$app->memcache->getServers();
$memcache = false;
foreach ($servers as $v)
{
$readay = @memcache_connect($v->host, $v->port);
if($readay)
{
$memcache = true;
break;
}
}
XiiError::ignoreError();
if(!$memcache)
{
self::$_requestError[] = 'Fail to connect MemCache Service';
}
else
{
self::$_outputData = XiiUtil::JsonDecode(Yii::$app->memcache->get(self::$_cacheId));
self::$_dataFrom = self::DATA_FROM_MEMCACHE;
}
}
private static function getFromRedis()
{
if(!self::$_getFromRedis)
{
return;
}
if(!empty(self::$_outputData))
{
return;
}
if(self::$cacheId == '')
{
return;
}
$redis = @stream_socket_client(Yii::$app->redis->hostname . ':' . Yii::$app->redis->port, $errno, $errstr, 1);
XiiError::ignoreError();
if(!$redis)
{
self::$_requestError[] = 'Fail to connect Redis Service';
}
else
{
self::$_outputData = XiiUtil::JsonDecode(Yii::$app->redis->get(self::$_outputName));
self::$_dataFrom = self::DATA_FROM_REDIS;
}
}
private static function getFromApi()
{
if(!empty(self::$_outputData))
{
return ;
}
self::config();
$tmp = XiiCurl::Run(['url' => self::$_apiUrl , 'data' => self::$_apiCondition]);
if($tmp['errorCode'])
{
self::$_outputData = XiiUtil::JsonDecode($tmp['data']);
self::$_dataFrom = self::DATA_FROM_API;
}
else
{
self::$_requestError[] = $tmp['errorMsg'];
}
}
private static function outputData()
{
if(!empty(self::$_requestError))
{
$self::$_outputData['status'] = false;
$self::$_outputData['errorCode'] = 0;
$self::$_outputData['errorMsg'] = implode(';', self::$_requestError);
}
$self::$_outputData['errorCode'] .= '_' . self::$_dataFrom;
return self::$_outputData;
}
private static function config()
{
foreach (self::$_para as $key => $value)
{
if($key == 'url')
{
self::$_apiUrl = $value;
}
else
{
self::$_apiCondition[$key] = $value;
}
}
}
private static function getConfig()
{
if(isset(Yii::$app->params[self::$_getConfigYiiParams]))
{
$params = Yii::$app->params[self::$_getConfigYiiParams];
foreach (self::$_getConfigFields as $v)
{
if(isset($params[$v]))
{
self::$$v = $params[$v];
}
}
}
}
}
?>