-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyapi.js
213 lines (203 loc) · 4.85 KB
/
yapi.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
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
const Flow = require("./workflow");
const flow = new Flow();
const login = () => {
return new Promise(async (resolve, reject) => {
let email = await flow.getItem('email');
let password = await flow.getItem('password');
if (!email) {
reject(new Error("请先输入email"));
return;
}
if (!password) {
reject(new Error("请先输入password"));
return;
}
let url = await flow.getItem('url');
flow.post(`${url}/api/user/login_by_ldap`, {
email,
password
}).then((res) => {
let cookies = res.headers['set-cookie'];
let arr = [];
if (cookies && cookies.length > 0) {
cookies.forEach((item) => {
arr.push(item.split(";")[0]);
});
let Cookie = arr.join("; ");
flow.setHeader({ Cookie });
resolve();
}
}).catch((err) => {
reject(err);
});
});
}
const getProject = (url, id) => {
return new Promise(async (resolve, reject) => {
let body = await flow.get(`${url}/api/project/list`, {
group_id: id,
page: 1,
limit: 1000
});
body = JSON.parse(body.data);
let list = body.data.list;
list = list.filter(item => item.add_time);
resolve(list);
});
}
const getInterface = (url, project_id) => {
return new Promise(async (resolve, reject) => {
let body = await flow.get(`${url}/api/interface/list`, {
page: 1,
limit: 1000,
project_id: project_id
});
body = JSON.parse(body.data);
let list = body.data.list;
setTimeout(() => {
resolve(list);
}, 20);
});
}
const getGroup = (url) => {
return new Promise(async (resolve, reject) => {
let body = await flow.get(`${url}/api/group/list`);
body = JSON.parse(body.data);
if (body.errcode === 0) {
let projects = body.data;
let promiseArr = [];
projects.forEach((item) => {
promiseArr.push(getProject(url, item._id));
});
Promise.all(promiseArr).then((data) => {
let arr = [];
data.forEach((item) => {
item.forEach((it) => {
arr.push(it);
});
});
resolve(arr);
});
} else {
reject(new Error(body.errmsg));
}
});
}
const getAllInterface = (url) => {
return new Promise(async (resolve, reject) => {
let list = await getGroup(url);
let data = [];
for (var i=4; i<list.length; i++) {
data.push(await getInterface(url, list[i]._id));
}
let arr = [];
data.forEach((item) => {
item.forEach((it) => {
arr.push(it);
});
});
lists = arr.map((item) => {
return {
title: item.path,
subtitle: item.title,
arg: `${url}/project/${item.project_id}/interface/api/${item._id}`
};
});
resolve(lists);
});
}
const filterLists = (lists) => {
let query = flow.input || "";
query = query.toLowerCase();
lists = lists.filter((list) => {
let title = list.title.toLowerCase();
let subtitle = list.subtitle.toLowerCase();
return title.indexOf(query) > -1 || subtitle.indexOf(query) > -1;
});
lists = lists.map((list) => {
list.icon = flow.icon("url.png");
return list;
});
if (lists.length === 0) {
lists = [{
title: '暂无数据',
icon: flow.icon("tip.png")
}];
}
if (!query) {
lists = lists.splice(0, 10);
}
return lists;
}
const fetchList = async () => {
let url = await flow.getItem("url");
if (!url) {
flow.log([{
title: "暂无数据:缺少项目地址",
icon: flow.icon("error.png")
}]);
return;
}
let email = await flow.getItem("email");
if (!email) {
flow.log([{
title: "暂无数据:缺少email",
icon: flow.icon("error.png")
}]);
return;
}
let password = await flow.getItem("password");
if (!password) {
flow.log([{
title: "暂无数据:缺少password",
icon: flow.icon("error.png")
}]);
return;
}
try {
try {
await login();
} catch(err) {
console.error(err);
}
let lists = await getAllInterface(url);
flow.log(filterLists(lists));
flow.setItem("lists", lists);
} catch(err) {
flow.log([{
title: err.message
}]);
}
}
const showList = async () => {
let lists = await flow.getItem("lists");
if (lists) {
flow.log(filterLists(lists));
} else {
fetchList();
}
}
const run = () => {
if (flow.argv.list) {
showList();
}
if (flow.argv.email || flow.argv.password || flow.argv.url) {
flow.log([{
title: flow.input,
arg: flow.input
}]);
}
if (flow.argv.addEmail) {
flow.setItem("email", flow.input);
}
if (flow.argv.addPassword) {
flow.setItem("password", flow.input);
}
if (flow.argv.addUrl) {
flow.setItem("url", flow.input);
}
if (flow.argv.update) {
fetchList();
}
}
run();