/
res-plus.js
71 lines (64 loc) · 1.33 KB
/
res-plus.js
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
/**
* Created by likaituan on 03/03/2017.
*/
// 输出JSON字符串
exports.print = function(json) {
json.code *= 1;
json.success = json.code === 200;
json.message = json.message || '';
json.data = json.data || null;
if (Array.isArray(json.data) === false) {
let data1 = json.data || {};
let data2 = this.dataPlus || {};
let newData = Object.assign({}, data1, data2);
if (Object.keys(newData).length > 0) {
json.data = newData;
}
}
this.returnData = json;
this.dataPlus = {};
this.json(json);
};
// 返回成功数据
exports.okJson = function(val) {
let json = {
code: 200
};
if (typeof val === 'object') {
json.data = val;
}
else {
json.message = val || 'ok';
}
return json;
};
// 返回失败数据
exports.errJson = function (val) {
let json = {
code: -3
};
if (arguments.length === 2) {
json.code = arguments[0];
json.message = arguments[1];
}
else if (typeof val === "object") {
json = val;
}
else if (typeof val === "number") {
json.code = val;
}
else if (typeof val === 'string') {
json.message = val;
}
return json;
};
// 输出成功
exports.printOk = function() {
let json = exports.okJson(...arguments);
exports.print.call(this, json);
};
// 输出错误
exports.printErr = function() {
let json = exports.errJson(...arguments);
exports.print.call(this, json);
};