ming_node 2.2.2 → 2.2.7
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +11 -1
- package/beforeTest/ApiCloudBaseRpcApiTest.js +17 -0
- package/beforeTest/FileBaseRpcApiTest.js +8 -0
- package/beforeTest/MemorDbTest.js +16 -0
- package/beforeTest/MemoryBaseRestApiTest.js +8 -0
- package/beforeTest/MemoryBaseRpcApiTest.js +8 -0
- package/beforeTest/MiApiCloudClientTest.js +16 -0
- package/beforeTest/MongoDbBaseRestApiTest.js +22 -0
- package/beforeTest/MongoDbBaseRpcApiTest.js +19 -0
- package/beforeTest/MySqlBaseRestApiTest.js +13 -0
- package/beforeTest/MysqlBaseRpcApiTest.js +18 -0
- package/beforeTest/graphql_test.js +12 -0
- package/beforeTest/installPluginTest.js +10 -0
- package/beforeTest/sseserver.js +44 -0
- package/beforeTest/static/ssetest.html +21 -0
- package/index.js +198 -44
- package/ming_node.md +4 -4
- package/module/BaseMapper.js +90 -35
- package/module/MemoryDb.js +136 -0
- package/module/MiApiCloudClient.js +649 -0
- package/package.json +1 -1
- package/plugins/BaseGraphqlApi/getGraphqlSchema.js +145 -0
- package/plugins/BaseGraphqlApi/getGraphqlSchemaDemo.js +76 -0
- package/plugins/BaseGraphqlApi/index.js +23 -0
- package/plugins/BaseRestApi/AbstractBaseRestApi.js +59 -0
- package/plugins/BaseRestApi/ApiCloudBaseRestApi.js +56 -0
- package/plugins/BaseRestApi/FileBaseRestApi.js +62 -0
- package/plugins/BaseRestApi/MemoryBaseRestApi.js +50 -0
- package/plugins/BaseRestApi/MongoDbBaseRestApi.js +75 -0
- package/plugins/BaseRestApi/MysqlBaseRestApi.js +72 -0
- package/plugins/BaseRpcApi/AbstractBaseRpcApi.js +72 -0
- package/plugins/BaseRpcApi/ApiCloudBaseRpcApi.js +57 -0
- package/plugins/BaseRpcApi/FileBaseRpcApi.js +62 -0
- package/plugins/BaseRpcApi/MemoryBaseRpcApi.js +50 -0
- package/plugins/BaseRpcApi/MongoDbBaseRpcApi.js +75 -0
- package/plugins/BaseRpcApi/MysqlBaseRpcApi.js +72 -0
- package/utils/common/CollectionUtils.js +28 -0
- package/beforeTest/t1.js +0 -18
@@ -0,0 +1,649 @@
|
|
1
|
+
var http = require('http');
|
2
|
+
var https = require('https');
|
3
|
+
var url_module = require('url');
|
4
|
+
var M=require("../index")
|
5
|
+
function myAxios(axiosConfig) {
|
6
|
+
axiosConfig.body=M.urlStringify(axiosConfig.data)
|
7
|
+
axiosConfig.headers.host = "";
|
8
|
+
var urlObj = url_module.parse(axiosConfig.url)
|
9
|
+
var options = {
|
10
|
+
hostname: urlObj.hostname,
|
11
|
+
port: urlObj.port,
|
12
|
+
path: urlObj.path,
|
13
|
+
method: axiosConfig.method.toLocaleUpperCase(),
|
14
|
+
headers: {
|
15
|
+
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
16
|
+
...axiosConfig.headers
|
17
|
+
}
|
18
|
+
}
|
19
|
+
|
20
|
+
let reqHttp = http;
|
21
|
+
if (axiosConfig.url.startsWith("https")) {
|
22
|
+
reqHttp = https;
|
23
|
+
}
|
24
|
+
var html = '';
|
25
|
+
return new Promise((resolve, reject) => {
|
26
|
+
var req = reqHttp.request(options, function (res) {
|
27
|
+
options = M.httpBefore(options);
|
28
|
+
if (options == false) {
|
29
|
+
return;
|
30
|
+
}
|
31
|
+
res.setEncoding('utf-8');
|
32
|
+
res.on('data', function (chunk) {
|
33
|
+
html += chunk;
|
34
|
+
});
|
35
|
+
res.on('end', function () {
|
36
|
+
resolve(JSON.parse(html));
|
37
|
+
});
|
38
|
+
|
39
|
+
});
|
40
|
+
req.on('error', function (err) {
|
41
|
+
console.error(err);
|
42
|
+
});
|
43
|
+
if(axiosConfig.body){
|
44
|
+
req.write(axiosConfig.body);
|
45
|
+
}
|
46
|
+
req.end();
|
47
|
+
})
|
48
|
+
}
|
49
|
+
|
50
|
+
|
51
|
+
const $1={}
|
52
|
+
$1.ajax= function (options){
|
53
|
+
return {
|
54
|
+
success(callback){
|
55
|
+
myAxios(options).then(d=>{
|
56
|
+
callback(d)
|
57
|
+
})
|
58
|
+
return {
|
59
|
+
fail(callback){
|
60
|
+
|
61
|
+
}
|
62
|
+
}
|
63
|
+
},
|
64
|
+
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
'use strict';
|
69
|
+
/**
|
70
|
+
* Created by andy on 14-12-25.
|
71
|
+
*/
|
72
|
+
/**
|
73
|
+
* APICloud-rest-SHA1.js ___start
|
74
|
+
* Secure Hash Algorithm (SHA1)
|
75
|
+
* http://www.webtoolkit.info/
|
76
|
+
*
|
77
|
+
**/
|
78
|
+
|
79
|
+
function SHA1(msg) {
|
80
|
+
|
81
|
+
function rotate_left(n, s) {
|
82
|
+
var t4 = (n << s) | (n >>> (32 - s));
|
83
|
+
return t4;
|
84
|
+
};
|
85
|
+
|
86
|
+
function lsb_hex(val) {
|
87
|
+
var str = "";
|
88
|
+
var i;
|
89
|
+
var vh;
|
90
|
+
var vl;
|
91
|
+
|
92
|
+
for (i = 0; i <= 6; i += 2) {
|
93
|
+
vh = (val >>> (i * 4 + 4)) & 0x0f;
|
94
|
+
vl = (val >>> (i * 4)) & 0x0f;
|
95
|
+
str += vh.toString(16) + vl.toString(16);
|
96
|
+
}
|
97
|
+
return str;
|
98
|
+
};
|
99
|
+
|
100
|
+
function cvt_hex(val) {
|
101
|
+
var str = "";
|
102
|
+
var i;
|
103
|
+
var v;
|
104
|
+
|
105
|
+
for (i = 7; i >= 0; i--) {
|
106
|
+
v = (val >>> (i * 4)) & 0x0f;
|
107
|
+
str += v.toString(16);
|
108
|
+
}
|
109
|
+
return str;
|
110
|
+
};
|
111
|
+
|
112
|
+
|
113
|
+
function Utf8Encode(string) {
|
114
|
+
string = string.replace(/\r\n/g, "\n");
|
115
|
+
var utftext = "";
|
116
|
+
|
117
|
+
for (var n = 0; n < string.length; n++) {
|
118
|
+
|
119
|
+
var c = string.charCodeAt(n);
|
120
|
+
|
121
|
+
if (c < 128) {
|
122
|
+
utftext += String.fromCharCode(c);
|
123
|
+
}
|
124
|
+
else if ((c > 127) && (c < 2048)) {
|
125
|
+
utftext += String.fromCharCode((c >> 6) | 192);
|
126
|
+
utftext += String.fromCharCode((c & 63) | 128);
|
127
|
+
}
|
128
|
+
else {
|
129
|
+
utftext += String.fromCharCode((c >> 12) | 224);
|
130
|
+
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
|
131
|
+
utftext += String.fromCharCode((c & 63) | 128);
|
132
|
+
}
|
133
|
+
|
134
|
+
}
|
135
|
+
|
136
|
+
return utftext;
|
137
|
+
};
|
138
|
+
|
139
|
+
var blockstart;
|
140
|
+
var i, j;
|
141
|
+
var W = new Array(80);
|
142
|
+
var H0 = 0x67452301;
|
143
|
+
var H1 = 0xEFCDAB89;
|
144
|
+
var H2 = 0x98BADCFE;
|
145
|
+
var H3 = 0x10325476;
|
146
|
+
var H4 = 0xC3D2E1F0;
|
147
|
+
var A, B, C, D, E;
|
148
|
+
var temp;
|
149
|
+
|
150
|
+
msg = Utf8Encode(msg);
|
151
|
+
|
152
|
+
var msg_len = msg.length;
|
153
|
+
|
154
|
+
var word_array = new Array();
|
155
|
+
for (i = 0; i < msg_len - 3; i += 4) {
|
156
|
+
j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
|
157
|
+
msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
|
158
|
+
word_array.push(j);
|
159
|
+
}
|
160
|
+
|
161
|
+
switch (msg_len % 4) {
|
162
|
+
case 0:
|
163
|
+
i = 0x080000000;
|
164
|
+
break;
|
165
|
+
case 1:
|
166
|
+
i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
|
167
|
+
break;
|
168
|
+
|
169
|
+
case 2:
|
170
|
+
i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
|
171
|
+
break;
|
172
|
+
|
173
|
+
case 3:
|
174
|
+
i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
|
175
|
+
break;
|
176
|
+
}
|
177
|
+
|
178
|
+
word_array.push(i);
|
179
|
+
|
180
|
+
while ((word_array.length % 16) != 14) word_array.push(0);
|
181
|
+
|
182
|
+
word_array.push(msg_len >>> 29);
|
183
|
+
word_array.push((msg_len << 3) & 0x0ffffffff);
|
184
|
+
|
185
|
+
|
186
|
+
for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
|
187
|
+
|
188
|
+
for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
|
189
|
+
for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
|
190
|
+
|
191
|
+
A = H0;
|
192
|
+
B = H1;
|
193
|
+
C = H2;
|
194
|
+
D = H3;
|
195
|
+
E = H4;
|
196
|
+
|
197
|
+
for (i = 0; i <= 19; i++) {
|
198
|
+
temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
|
199
|
+
E = D;
|
200
|
+
D = C;
|
201
|
+
C = rotate_left(B, 30);
|
202
|
+
B = A;
|
203
|
+
A = temp;
|
204
|
+
}
|
205
|
+
|
206
|
+
for (i = 20; i <= 39; i++) {
|
207
|
+
temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
|
208
|
+
E = D;
|
209
|
+
D = C;
|
210
|
+
C = rotate_left(B, 30);
|
211
|
+
B = A;
|
212
|
+
A = temp;
|
213
|
+
}
|
214
|
+
|
215
|
+
for (i = 40; i <= 59; i++) {
|
216
|
+
temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
|
217
|
+
E = D;
|
218
|
+
D = C;
|
219
|
+
C = rotate_left(B, 30);
|
220
|
+
B = A;
|
221
|
+
A = temp;
|
222
|
+
}
|
223
|
+
|
224
|
+
for (i = 60; i <= 79; i++) {
|
225
|
+
temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
|
226
|
+
E = D;
|
227
|
+
D = C;
|
228
|
+
C = rotate_left(B, 30);
|
229
|
+
B = A;
|
230
|
+
A = temp;
|
231
|
+
}
|
232
|
+
|
233
|
+
H0 = (H0 + A) & 0x0ffffffff;
|
234
|
+
H1 = (H1 + B) & 0x0ffffffff;
|
235
|
+
H2 = (H2 + C) & 0x0ffffffff;
|
236
|
+
H3 = (H3 + D) & 0x0ffffffff;
|
237
|
+
H4 = (H4 + E) & 0x0ffffffff;
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
|
242
|
+
|
243
|
+
return temp.toLowerCase();
|
244
|
+
|
245
|
+
}
|
246
|
+
var api={};
|
247
|
+
api.ajax=function(ajaxConfig,callback){
|
248
|
+
$1.ajax(ajaxConfig).success(function (data, status, header) {
|
249
|
+
callback(data,status)
|
250
|
+
}).fail(function (header, status, errorThrown) {
|
251
|
+
callback(data,errorThrown)
|
252
|
+
})
|
253
|
+
}
|
254
|
+
'use strict';
|
255
|
+
function copy(obj) {
|
256
|
+
if (obj == null || typeof (obj) != 'object')
|
257
|
+
return obj;
|
258
|
+
|
259
|
+
var temp = obj.constructor();
|
260
|
+
// changed
|
261
|
+
|
262
|
+
for (var key in obj) {
|
263
|
+
if (obj.hasOwnProperty(key)) {
|
264
|
+
temp[key] = copy(obj[key]);
|
265
|
+
}
|
266
|
+
}
|
267
|
+
return temp;
|
268
|
+
}
|
269
|
+
|
270
|
+
function isType(type) {
|
271
|
+
return function (obj) {
|
272
|
+
return {}.toString.call(obj) == "[object " + type + "]"
|
273
|
+
}
|
274
|
+
}
|
275
|
+
|
276
|
+
var isFunction = isType("Function");
|
277
|
+
|
278
|
+
function Resource(appId, appKey, baseurl) {
|
279
|
+
var now = Date.now();
|
280
|
+
this.appId = appId;
|
281
|
+
this.baseurl = baseurl || "https://d.apicloud.com/mcm/api";
|
282
|
+
this.appCode = SHA1(appId + "UZ" + appKey + "UZ" + now) + "." + now;
|
283
|
+
this.defaultactions = {
|
284
|
+
'get': { method: 'GET', params: ["_id", "_relation"] },
|
285
|
+
'save': { method: 'POST', params: ["_id", "_relation"] },
|
286
|
+
'query': { method: 'GET', params: ["filter"] },
|
287
|
+
'delete': { method: 'DELETE', params: ["_id", "_relation"] },
|
288
|
+
'login': { method: "POST", params: ["username", "password"] },
|
289
|
+
'logout': { method: "POST" },
|
290
|
+
'count': { method: "GET", params: ["_id", "_relation", "filter"] },
|
291
|
+
'exists': { method: "GET", params: ["_id"] },
|
292
|
+
'findOne': { method: 'GET', params: ["filter"] },
|
293
|
+
'verify': { method: "POST", params: ["email", "language", "username"], alias: "verifyEmail" },
|
294
|
+
'reset': { method: "POST", params: ["email", "language", "username"], alias: "resetRequest" },
|
295
|
+
'sendvercode': { method: "POST" },
|
296
|
+
'checkvercode': { method: "POST", params: ["mobile", "vercode"] },
|
297
|
+
'resetByMobile': { method: "POST", params: ["mobile", "vercode", "username", "password"] },
|
298
|
+
};
|
299
|
+
this.headers = {};
|
300
|
+
this.setHeaders("X-APICloud-AppId", this.appId);
|
301
|
+
this.setHeaders("X-APICloud-AppKey", this.appCode);
|
302
|
+
this.setHeaders("Content-Type", "application/json;");
|
303
|
+
}
|
304
|
+
Resource.prototype.setHeaders = function (key, value) {
|
305
|
+
this.headers[key] = value;
|
306
|
+
}
|
307
|
+
Resource.prototype.batch = function (requests, callback) {
|
308
|
+
for (var i = 0, len = requests.length; i < len; i++) {
|
309
|
+
var request = requests[i];
|
310
|
+
if (request["method"] && request["method"].toUpperCase() === "GET" && request["body"] && request["body"]["filter"]) {
|
311
|
+
var url = request["path"];
|
312
|
+
var index = url.indexOf('?');
|
313
|
+
request["path"] = url.substring(0, index) + "?filter=" + JSON.stringify(request["body"]["filter"]);
|
314
|
+
delete request["body"];
|
315
|
+
}
|
316
|
+
}
|
317
|
+
var ajaxConfig = {
|
318
|
+
url: this.baseurl + "/batch",
|
319
|
+
method: "POST",
|
320
|
+
data: {
|
321
|
+
body: JSON.stringify({ requests: requests })
|
322
|
+
}
|
323
|
+
}
|
324
|
+
ajaxConfig["headers"] = {};
|
325
|
+
for (var header in this.headers) {
|
326
|
+
ajaxConfig["headers"][header] = this.headers[header];
|
327
|
+
}
|
328
|
+
api.ajax(ajaxConfig, function (ret, err) {
|
329
|
+
callback(ret, err)
|
330
|
+
});
|
331
|
+
}
|
332
|
+
Resource.prototype.upload = function (modelName, isFilter, item, params, callback) {
|
333
|
+
if (typeof params == "function") {
|
334
|
+
callback = params;
|
335
|
+
params = {};
|
336
|
+
}
|
337
|
+
var filepath = item.path;
|
338
|
+
var values = item.values || {};
|
339
|
+
var url = params["_id"] && params["_relation"] ? ("/" + modelName + "/" + params["_id"] + "/" + params["_relation"]) : "/file";
|
340
|
+
var isPut = (!params["_relation"]) && params["_id"];
|
341
|
+
var fileUrl = this.baseurl + url + (isPut ? ("/" + params["_id"]) : "");
|
342
|
+
var filename = filepath.substr(filepath.lastIndexOf("/") + 1, filepath.length);
|
343
|
+
if (!values["filename"]) values["filename"] = filename;
|
344
|
+
var ajaxConfig = {
|
345
|
+
url: fileUrl,
|
346
|
+
method: isPut ? "PUT" : "POST",
|
347
|
+
data: {
|
348
|
+
values: values,
|
349
|
+
files: {
|
350
|
+
file: filepath
|
351
|
+
}
|
352
|
+
}
|
353
|
+
}
|
354
|
+
ajaxConfig["headers"] = {};
|
355
|
+
for (var header in this.headers) {
|
356
|
+
if (header == "Content-Type") continue;
|
357
|
+
ajaxConfig["headers"][header] = this.headers[header];
|
358
|
+
}
|
359
|
+
api.ajax(ajaxConfig, function (ret, err) {
|
360
|
+
if (ret && ret.id && !err) {
|
361
|
+
var newobj = {};
|
362
|
+
if (isFilter) {
|
363
|
+
newobj["id"] = ret["id"];
|
364
|
+
newobj["name"] = ret["name"];
|
365
|
+
newobj["url"] = ret["url"];
|
366
|
+
callback(null, newobj)
|
367
|
+
} else {
|
368
|
+
callback(null, ret);
|
369
|
+
}
|
370
|
+
} else {
|
371
|
+
callback(ret || err, null)
|
372
|
+
}
|
373
|
+
});
|
374
|
+
}
|
375
|
+
|
376
|
+
Resource.prototype.Factory = function (modelName) {
|
377
|
+
var self = this;
|
378
|
+
var route = new Route(modelName, self.baseurl);
|
379
|
+
var actions = copy(this.defaultactions);
|
380
|
+
var resourceFactory = new Object();
|
381
|
+
Object.keys(actions).forEach(function (name) {
|
382
|
+
if (modelName != "user" && ["login", "logout", "verify", "reset", "sendvercode", "checkvercode", "resetByMobile"].indexOf(name) != -1) { return; }
|
383
|
+
resourceFactory[name] = function (a1, a2, a3) {
|
384
|
+
var action = copy(actions[name]);
|
385
|
+
var params = {}, data, callback;
|
386
|
+
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method);
|
387
|
+
switch (arguments.length) {
|
388
|
+
case 3:
|
389
|
+
params = a1;
|
390
|
+
data = a2;
|
391
|
+
callback = a3;
|
392
|
+
break;
|
393
|
+
case 2:
|
394
|
+
if (hasBody)
|
395
|
+
data = a1;
|
396
|
+
else
|
397
|
+
params = a1;
|
398
|
+
callback = a2;
|
399
|
+
break;
|
400
|
+
case 1:
|
401
|
+
if (isFunction(a1))
|
402
|
+
callback = a1;
|
403
|
+
else if (hasBody)
|
404
|
+
data = a1;
|
405
|
+
else
|
406
|
+
params = a1;
|
407
|
+
break;
|
408
|
+
case 0:
|
409
|
+
break;
|
410
|
+
default:
|
411
|
+
throw new Error("鍙傛暟鏈€澶氫负3涓�");
|
412
|
+
}
|
413
|
+
if (hasBody && name != "logout") {
|
414
|
+
var fileCount = 0;
|
415
|
+
Object.keys(data).forEach(function (key) {
|
416
|
+
var item = data[key];
|
417
|
+
if (item && item.isFile) {
|
418
|
+
var isFilter = true;
|
419
|
+
if (modelName == "file" || item.isFileClass) {
|
420
|
+
isFilter = false;
|
421
|
+
}
|
422
|
+
fileCount++;
|
423
|
+
self.upload(modelName, isFilter, item, (modelName == "file" ? params : {}), function (err, returnData) {
|
424
|
+
if (err) {
|
425
|
+
return callback(null, err);
|
426
|
+
} else {
|
427
|
+
if (!isFilter)
|
428
|
+
return callback(returnData, null);
|
429
|
+
data[key] = returnData;
|
430
|
+
fileCount--;
|
431
|
+
if (fileCount == 0) {
|
432
|
+
next();
|
433
|
+
}
|
434
|
+
}
|
435
|
+
})
|
436
|
+
}
|
437
|
+
});
|
438
|
+
if (fileCount == 0) {
|
439
|
+
next();
|
440
|
+
}
|
441
|
+
} else {
|
442
|
+
next();
|
443
|
+
}
|
444
|
+
function next() {
|
445
|
+
var httpConfig = {};
|
446
|
+
httpConfig["headers"] = {};
|
447
|
+
for (var header in self.headers) {
|
448
|
+
httpConfig["headers"][header] = self.headers[header];
|
449
|
+
}
|
450
|
+
if (name === "logout" && !httpConfig["headers"]["authorization"]) {
|
451
|
+
return callback({ status: 0, msg: "鏈缃產uthorization鍙傛暟,鏃犳硶娉ㄩ攢!" }, null);
|
452
|
+
}
|
453
|
+
if (hasBody) {
|
454
|
+
httpConfig.data = {
|
455
|
+
body: JSON.stringify(data)
|
456
|
+
};
|
457
|
+
}
|
458
|
+
|
459
|
+
if (params && (name == "save") && params["_id"] && (!params["_relation"]) && (!params["_relationid"])) {
|
460
|
+
action.method = "PUT";
|
461
|
+
}
|
462
|
+
if (params && (name == "save") && params["_id"] && params["_relation"] && params["_relationid"]) {
|
463
|
+
action.method = "PUT";
|
464
|
+
}
|
465
|
+
for (var key in action) {
|
466
|
+
if (key != 'params' && key != "alias") {
|
467
|
+
httpConfig[key] = copy(action[key]);
|
468
|
+
}
|
469
|
+
}
|
470
|
+
|
471
|
+
var curparams = {};
|
472
|
+
action.params = action.params || [];
|
473
|
+
for (var k = 0, len = action.params.length; k < len; k++) {
|
474
|
+
var tempkey = action.params[k];
|
475
|
+
if (params[tempkey]) {
|
476
|
+
curparams[tempkey] = copy(params[tempkey]);
|
477
|
+
}
|
478
|
+
}
|
479
|
+
if (["login", "logout", "count", "exists", "verify", "reset", "findOne", "sendvercode", "checkvercode", "resetByMobile"].indexOf(name) != -1) {
|
480
|
+
curparams["_custom"] = action.alias || name;
|
481
|
+
}
|
482
|
+
route.setUrlParams(httpConfig, curparams);
|
483
|
+
httpConfig.cache = true;
|
484
|
+
// console.log(httpConfig.method + "\t" + httpConfig.url);
|
485
|
+
api.ajax(httpConfig, function (ret, err) {
|
486
|
+
return callback(ret, err);
|
487
|
+
})
|
488
|
+
}
|
489
|
+
};
|
490
|
+
});
|
491
|
+
return resourceFactory;
|
492
|
+
};
|
493
|
+
|
494
|
+
function Route(template, baseurl) {
|
495
|
+
this.template = template;
|
496
|
+
this.baseurl = baseurl;
|
497
|
+
}
|
498
|
+
|
499
|
+
Route.prototype = {
|
500
|
+
setUrlParams: function (config, params) {
|
501
|
+
var url = "/:_class/:_id/:_relation/:_custom/:_relationid";
|
502
|
+
url = url.replace(":_class", this.template);
|
503
|
+
var parArr = [];
|
504
|
+
Object.keys(params).forEach(function (ckey) {
|
505
|
+
if (ckey.charAt(0) == '_') {
|
506
|
+
url = url.replace(":" + ckey, params[ckey]);
|
507
|
+
delete params[ckey];
|
508
|
+
} else {
|
509
|
+
if (ckey == "filter") {
|
510
|
+
parArr.push(ckey + "=" + JSON.stringify(params[ckey]));
|
511
|
+
}
|
512
|
+
}
|
513
|
+
});
|
514
|
+
url = url.replace(/:[^/]+/ig, '/');
|
515
|
+
if (parArr.length > 0) {
|
516
|
+
url += ("?" + parArr.join("&"));
|
517
|
+
}
|
518
|
+
url = url.replace(/\/+/g, '/');
|
519
|
+
url = url.replace(/\/$/, '');
|
520
|
+
config.url = this.baseurl + url;
|
521
|
+
}
|
522
|
+
};
|
523
|
+
|
524
|
+
/////////////////////////////////APICloud-rest-SHA1.js ___end/////////////////////////////////////////////////////
|
525
|
+
|
526
|
+
class MiApiCloudClient {
|
527
|
+
constructor(appid, appkey) {
|
528
|
+
this.appid = appid;
|
529
|
+
var now = Date.now();
|
530
|
+
this._preappKey=appkey;
|
531
|
+
this.appKey = SHA1(this.appid + "UZ" + this._preappKey+ "UZ" + now) + "." + now
|
532
|
+
this._apiHost = 'https://d.apicloud.com/mcm/api'
|
533
|
+
this.resource= new Resource(this.appid, this._preappKey)
|
534
|
+
MiApiCloudClient.MiApiCloudClient_this=this;
|
535
|
+
MiApiCloudClient.begin=function(){}
|
536
|
+
}
|
537
|
+
|
538
|
+
defaultGetConfig(url, data){
|
539
|
+
return {
|
540
|
+
"url": url,
|
541
|
+
"method": "GET",
|
542
|
+
"cache": false,
|
543
|
+
"headers": {
|
544
|
+
"X-APICloud-AppId": this.appid ,
|
545
|
+
"X-APICloud-AppKey": this.appKey
|
546
|
+
},
|
547
|
+
"data": data
|
548
|
+
}
|
549
|
+
}
|
550
|
+
defaultPostConfig(url, data){
|
551
|
+
return {
|
552
|
+
"url": url,
|
553
|
+
"method": "POST",
|
554
|
+
"cache": false,
|
555
|
+
"headers": {
|
556
|
+
"X-APICloud-AppId": this.appid,
|
557
|
+
"X-APICloud-AppKey": this.appKey
|
558
|
+
},
|
559
|
+
"data": data
|
560
|
+
}
|
561
|
+
}
|
562
|
+
tableClient(tableName) {
|
563
|
+
let tclient = {}
|
564
|
+
tclient.tableClient = MiApiCloudClient.MiApiCloudClient_this.resource.Factory(tableName);
|
565
|
+
tclient.tableName=tableName;
|
566
|
+
|
567
|
+
tclient.add = function (data) {
|
568
|
+
MiApiCloudClient.begin({tableName,data,"action":"add"})
|
569
|
+
return new Promise(function (reslove, reject) {
|
570
|
+
$1.ajax(MiApiCloudClient.MiApiCloudClient_this.defaultPostConfig(MiApiCloudClient.MiApiCloudClient_this._apiHost + "/"+tableName, data)).success(function (data, status, header) {
|
571
|
+
reslove(data)
|
572
|
+
}).fail(function (header, status, errorThrown) {
|
573
|
+
reject(errorThrown)
|
574
|
+
})
|
575
|
+
}
|
576
|
+
)
|
577
|
+
}
|
578
|
+
|
579
|
+
tclient.delete = function (data) {
|
580
|
+
MiApiCloudClient.begin({tableName,data,"action":"delete"})
|
581
|
+
return new Promise(function (reslove, reject) {
|
582
|
+
$1.ajax(
|
583
|
+
MiApiCloudClient.MiApiCloudClient_this.defaultPostConfig(MiApiCloudClient.MiApiCloudClient_this._apiHost + "/"+tableName+"/" + data.id, {
|
584
|
+
"_method": "DELETE"
|
585
|
+
})
|
586
|
+
).success(function (data, status, header) {
|
587
|
+
reslove(data)
|
588
|
+
}).fail(function (header, status, errorThrown) {
|
589
|
+
reject(errorThrown)
|
590
|
+
})
|
591
|
+
}
|
592
|
+
)
|
593
|
+
}
|
594
|
+
|
595
|
+
tclient.update = function (data) {
|
596
|
+
//console.log("AAAAAAAA",data)
|
597
|
+
MiApiCloudClient.begin({tableName,data,"action":"update"})
|
598
|
+
data._method = "PUT";
|
599
|
+
return new Promise(function (reslove, reject) {
|
600
|
+
$1.ajax(
|
601
|
+
MiApiCloudClient.MiApiCloudClient_this.defaultPostConfig(MiApiCloudClient.MiApiCloudClient_this._apiHost + "/"+tableName+"/" + data.id,data)
|
602
|
+
).success(function (data, status, header) {
|
603
|
+
reslove(data)
|
604
|
+
}).fail(function (header, status, errorThrown) {
|
605
|
+
reject(errorThrown)
|
606
|
+
})
|
607
|
+
}
|
608
|
+
)
|
609
|
+
}
|
610
|
+
|
611
|
+
tclient.list = function (data,limit,skip,order,include,includefilter) {
|
612
|
+
MiApiCloudClient.begin({data,limit,skip,order,include,includefilter,"action":"list"})
|
613
|
+
return new Promise(function (reslove, reject) {
|
614
|
+
tclient.tableClient.query({
|
615
|
+
"filter": {
|
616
|
+
"where": data||{},
|
617
|
+
"limit": limit||500000,
|
618
|
+
"skip": skip||0,
|
619
|
+
"order": order||"createdAt ASC",
|
620
|
+
"include":include||{},
|
621
|
+
"includefilter":includefilter||{}
|
622
|
+
}
|
623
|
+
}, function (ret, err) {
|
624
|
+
reslove(ret)
|
625
|
+
})
|
626
|
+
}
|
627
|
+
)
|
628
|
+
}
|
629
|
+
|
630
|
+
tclient.count = function (data) {
|
631
|
+
MiApiCloudClient.begin({tableName,data,"action":"count"})
|
632
|
+
return new Promise(function (reslove, reject) {
|
633
|
+
tclient.tableClient.count({
|
634
|
+
"filter": {
|
635
|
+
"where": data||{}
|
636
|
+
}
|
637
|
+
}, function (ret, err) {
|
638
|
+
reslove(ret)
|
639
|
+
})
|
640
|
+
}
|
641
|
+
)
|
642
|
+
}
|
643
|
+
return tclient;
|
644
|
+
}
|
645
|
+
}
|
646
|
+
|
647
|
+
|
648
|
+
|
649
|
+
module.exports = MiApiCloudClient;
|
package/package.json
CHANGED