-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandleImportData.js
136 lines (127 loc) · 3.48 KB
/
HandleImportData.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
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
const _ = require('underscore');
const axios = require('axios');
const isNode = typeof global == 'object' && global.global === global;
async function handle(
res,
projectId,
selectCatid,
menuList,
basePath,
dataSync,
messageError,
messageSuccess,
callback,
token,
port
) {
const handleAddCat = async cats => {
let catsObj = {};
if (cats && Array.isArray(cats)) {
for (let i = 0; i < cats.length; i++) {
let cat = cats[i];
let findCat = _.find(menuList, menu => menu.name === cat.name);
catsObj[cat.name] = cat;
if (findCat) {
cat.id = findCat._id;
} else {
let apipath = '/api/interface/add_cat';
if (isNode) {
apipath = 'http://127.0.0.1:' + port + apipath;
}
let data = {
name: cat.name,
project_id: projectId,
desc: cat.desc,
token
};
let result = await axios.post(apipath, data);
if (result.data.errcode) {
messageError(result.data.errmsg);
callback({ showLoading: false });
return false;
}
cat.id = result.data.data._id;
}
}
}
return catsObj;
};
const handleAddInterface = async res => {
const cats = await handleAddCat(res.cats);
if (cats === false) {
return;
}
res = res.apis;
let len = res.length;
let count = 0;
let successNum = len;
let existNum = 0;
if (len === 0) {
messageError(`解析数据为空`);
callback({ showLoading: false });
return;
}
for (let index = 0; index < res.length; index++) {
let item = res[index];
let data = Object.assign(item, {
project_id: projectId,
catid: selectCatid
});
if (basePath) {
data.path =
data.path.indexOf(basePath) === 0 ? data.path.substr(basePath.length) : data.path;
}
if (
data.catname &&
cats[data.catname] &&
typeof cats[data.catname] === 'object' &&
cats[data.catname].id
) {
data.catid = cats[data.catname].id;
}
data.token = token;
if (dataSync !== 'normal') {
// 开启同步功能
count++;
let apipath = '/api/interface/save';
if (isNode) {
apipath = 'http://127.0.0.1:' + port + apipath;
}
data.dataSync = dataSync;
let result = await axios.post(apipath, data);
if (result.data.errcode) {
successNum--;
callback({ showLoading: false });
messageError(result.data.errmsg);
} else {
existNum = existNum + result.data.data.length;
}
} else {
// 未开启同步功能
count++;
let apipath = '/api/interface/add';
if (isNode) {
apipath = 'http://127.0.0.1:' + port + apipath;
}
let result = await axios.post(apipath, data);
if (result.data.errcode) {
successNum--;
if (result.data.errcode == 40022) {
existNum++;
}
if (result.data.errcode == 40033) {
callback({ showLoading: false });
messageError('没有权限');
break;
}
}
}
if (count === len) {
callback({ showLoading: false });
messageSuccess(`成功导入接口 ${successNum} 个, 已存在的接口 ${existNum} 个`);
}
}
};
return await handleAddInterface(res);
}
module.exports = handle;