service.js
3.29 KB
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
import { GET, POST } from '../../../libs/request';
import { API_HOST } from '../../../libs/config';
import Humps from './humps.js';
import iconfSwitch from '../../../libs/switch';
import md5 from '../../../vendors/md5';
//注:g_sourceApiArry与g_toJavaApiArry的length要相等,且一一对应
const g_sourceApiArry = [ '/list', '/content', '/list/recommend', '/code/recent', '/list/mine', '/code/gain', '/code/mine' ];
const g_toJavaApiArry = [ 'app.yoluck.activityList', 'app.yoluck.getContent', 'app.yoluck.recommendList', 'app.yoluck.recent', 'app.yoluck.participationList', 'app.yoluck.getCode', 'app.yoluck.userCode' ];
const app = getApp();
class Service {
constructor(url = '') {
this.url = url;
}
_get(path, data) {
let method = path ? this.url + path : API_HOST;
let pathIndex = g_sourceApiArry.indexOf(path);
let executeReq = () => {
//适配服务器更换Java接口,开关控制是否切换
if (app.globalData.switch_javaApiEnable && pathIndex >= 0 && pathIndex < g_toJavaApiArry.length) {
method = API_HOST;
data.method = g_toJavaApiArry[pathIndex];
}
return GET(method, data).then(result => {
//适配服务器更换Java接口,开关控制是否切换
if (app.globalData.switch_javaApiEnable && pathIndex >= 0) {
if (result.data.list) {
let newList = Humps.decamelizeKeys(Object.values(result.data.list));
result.data.list = newList;
} else {
let newData = Humps.decamelizeKeys(result.data);
result.data = newData;
}
}
return result
});
};
if (!app.globalData.hasSetSwitch) {
iconfSwitch().then(data => {
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> _get switch');
app.globalData.hasSetSwitch = true;
if (data['disableJavaApi'] && md5('disableJavaApi') === data['disableJavaApi']) { // TODO 键值确认
app.globalData.switch_javaApiEnable = false;
}
return executeReq();
});
} else {
return executeReq();
}
}
_post(path, data) {
let method = path ? this.url + path : API_HOST;
let pathIndex = g_sourceApiArry.indexOf(path);
let executeReq = () => {
//适配服务器更换Java接口,开关控制是否切换
if (app.globalData.switch_javaApiEnable && pathIndex >= 0 && pathIndex < g_toJavaApiArry.length) {
method = API_HOST;
data.method = g_toJavaApiArry[pathIndex];
}
return POST(method, data).then(result => {
//适配服务器更换Java接口,开关控制是否切换
if (app.globalData.switch_javaApiEnable && pathIndex >= 0) {
let newData = Humps.decamelizeKeys(result.data);
result.data = newData;
}
return result;
});
}
if (!app.globalData.hasSetSwitch) {
iconfSwitch().then(data => {
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> _post switch');
app.globalData.hasSetSwitch = true;
if (data['disableJavaApi'] && md5('disableJavaApi') === data['disableJavaApi']) { // TODO 键值确认
app.globalData.switch_javaApiEnable = false;
}
return executeReq();
});
} else {
return executeReq();
}
}
}
export default Service;