ol-base-components 2.0.4 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/package.json +5 -1
- package/src/App.vue +4 -1
- package/src/api/run.js +113 -0
- package/src/api/runApi.js +196 -0
- package/src/main.js +4 -5
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ol-base-components",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "src/package/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"run": "src/api/run.js",
|
|
8
|
+
"runApi": "src/api/runApi.js"
|
|
9
|
+
},
|
|
6
10
|
"scripts": {
|
|
7
11
|
"serve": "vue-cli-service serve --no-verify",
|
|
8
12
|
"build": "vue-cli-service build",
|
package/src/App.vue
CHANGED
|
@@ -7,11 +7,13 @@
|
|
|
7
7
|
@handleReset="handleReset"
|
|
8
8
|
url="/api/app/admission-info/paged-result"
|
|
9
9
|
/>
|
|
10
|
+
<!-- url="/api/app/admission-info/paged-result" -->
|
|
11
|
+
|
|
10
12
|
<ol-table
|
|
11
13
|
:paginations="paginations"
|
|
12
14
|
:empty-img="tableData.emptyImg"
|
|
13
15
|
:btnlist="[]"
|
|
14
|
-
url="/api/app/
|
|
16
|
+
url="/api/app/stock-out/send-pick-tray-back"
|
|
15
17
|
:table-data="tableData"
|
|
16
18
|
:multiple-selection="multipleSelection"
|
|
17
19
|
@SelectionChange="SelectionChange"
|
|
@@ -302,6 +304,7 @@ export default {
|
|
|
302
304
|
},
|
|
303
305
|
};
|
|
304
306
|
},
|
|
307
|
+
onMounted() { },
|
|
305
308
|
methods: {
|
|
306
309
|
SelectionChange(row) {
|
|
307
310
|
this.multipleSelection = row;
|
package/src/api/run.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
const http = require("http");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
const swaggerUrl = "http://220.179.249.140:20019/swagger/v1/swagger.json";
|
|
6
|
+
|
|
7
|
+
http
|
|
8
|
+
.get(swaggerUrl, (response) => {
|
|
9
|
+
let data = "";
|
|
10
|
+
|
|
11
|
+
// 监听数据事件
|
|
12
|
+
response.on("data", (chunk) => {
|
|
13
|
+
data += chunk;
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// 监听结束事件
|
|
17
|
+
response.on("end", () => {
|
|
18
|
+
const swaggerData = JSON.parse(data);
|
|
19
|
+
const apiEndpoints = generateApiModules(swaggerData);
|
|
20
|
+
|
|
21
|
+
// 输出到文件
|
|
22
|
+
const outputPath = path.join(__dirname, "swagger.js");
|
|
23
|
+
fs.writeFileSync(outputPath, apiEndpoints, "utf-8");
|
|
24
|
+
console.log(`API地址对象已生成并保存到 ${outputPath}`);
|
|
25
|
+
});
|
|
26
|
+
})
|
|
27
|
+
.on("error", (err) => {
|
|
28
|
+
console.error("获取swagger.json时出错:", err);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// url转成键名规则
|
|
32
|
+
function generateKeyName(url, method) {
|
|
33
|
+
// 移除前缀 "/api/app"
|
|
34
|
+
const cleanedUrl = url.replace(/\/api\/app/, "");
|
|
35
|
+
const arr = cleanedUrl.split("/");
|
|
36
|
+
|
|
37
|
+
// 处理 {xxx} 转换为 ByXxx
|
|
38
|
+
const processedArr = arr.map(
|
|
39
|
+
(item) =>
|
|
40
|
+
item
|
|
41
|
+
.replace(
|
|
42
|
+
/{(.*?)}/,
|
|
43
|
+
(_, param) => `By${param.charAt(0).toUpperCase() + param.slice(1)}`
|
|
44
|
+
) // 处理 {xxx}
|
|
45
|
+
.replace(/[-_]/g, "") // 去除 - 和 _
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// 删除第一个空项
|
|
49
|
+
if (processedArr[0] === "") {
|
|
50
|
+
processedArr.shift();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 去重和拼接相邻相同的项
|
|
54
|
+
const resultArr = [];
|
|
55
|
+
for (let i = 0; i < processedArr.length; i++) {
|
|
56
|
+
if (i === 0 || processedArr[i] !== processedArr[i - 1]) {
|
|
57
|
+
// 将每项首字母大写
|
|
58
|
+
const capitalizedItem =
|
|
59
|
+
processedArr[i].charAt(0).toUpperCase() + processedArr[i].slice(1);
|
|
60
|
+
resultArr.push(capitalizedItem);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const key = resultArr.join("");
|
|
64
|
+
return `${method.toLowerCase()}${key}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
//去掉url后面 {}
|
|
68
|
+
function removeCurlyBraces(url) {
|
|
69
|
+
// 使用正则表达式去掉 URL 中的 {xxx} 部分
|
|
70
|
+
return url.replace(/\/?{[^}]*}/g, "").replace(/\/+$/, ""); // 去掉 {xxx} 和结尾的斜杠
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 生成API模块
|
|
74
|
+
const generateApiModules = (swagger) => {
|
|
75
|
+
const { tags, paths } = swagger;
|
|
76
|
+
const apiModules = {};
|
|
77
|
+
// 初始化模块对象
|
|
78
|
+
tags.forEach((tag) => {
|
|
79
|
+
apiModules[tag.name] = {};
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// 遍历paths,将接口添加到相应的模块中
|
|
83
|
+
for (const [url, methods] of Object.entries(paths)) {
|
|
84
|
+
for (const [method, details] of Object.entries(methods)) {
|
|
85
|
+
// 获取接口的tags
|
|
86
|
+
const tags = details.tags || [];
|
|
87
|
+
// const summary = details.summary.replace(/\s+/g, "");
|
|
88
|
+
tags.forEach((tag) => {
|
|
89
|
+
const key = generateKeyName(url, method);
|
|
90
|
+
if (apiModules[tag]) {
|
|
91
|
+
const summary = details.summary || "";
|
|
92
|
+
apiModules[tag][key] = `${key}: "${removeCurlyBraces(
|
|
93
|
+
url
|
|
94
|
+
)}", //${method} ${summary}\n`;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// 生成最终的输出字符串
|
|
101
|
+
let output = "";
|
|
102
|
+
for (const [moduleName, methods] of Object.entries(apiModules)) {
|
|
103
|
+
const description = tags.find((e) => e.name === moduleName).description;
|
|
104
|
+
output += `// ${description}\n`;
|
|
105
|
+
output += `export const ${moduleName} = {\n`;
|
|
106
|
+
for (const method of Object.values(methods)) {
|
|
107
|
+
output += ` ${method}`;
|
|
108
|
+
}
|
|
109
|
+
output += `};\n\n`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return output;
|
|
113
|
+
};
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const SwaggerClient = require("swagger-client");
|
|
4
|
+
|
|
5
|
+
const swaggerUrl = "http://220.179.249.140:20019/swagger/v1/swagger.json";
|
|
6
|
+
|
|
7
|
+
SwaggerClient(swaggerUrl)
|
|
8
|
+
.then((client) => {
|
|
9
|
+
const swaggerData = client.spec; // 获取 Swagger 数据
|
|
10
|
+
|
|
11
|
+
const apiModules = generateApiModules(swaggerData);
|
|
12
|
+
// 创建文件夹
|
|
13
|
+
const modulesDir = path.join(__dirname, "./modules");
|
|
14
|
+
if (!fs.existsSync(modulesDir)) {
|
|
15
|
+
fs.mkdirSync(modulesDir);
|
|
16
|
+
console.log(`创建了文件夹: ${modulesDir}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
Object.keys(apiModules).forEach((fileName) => {
|
|
20
|
+
const outputPath = path.join(modulesDir, `${fileName}.js`);
|
|
21
|
+
fs.writeFileSync(outputPath, apiModules[fileName], "utf-8");
|
|
22
|
+
console.log(`API接口已生成并保存到 ${outputPath}`);
|
|
23
|
+
});
|
|
24
|
+
})
|
|
25
|
+
.catch((err) => {
|
|
26
|
+
console.error("获取 Swagger 数据时出错:", err);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// url转成键名规则
|
|
30
|
+
function generateKeyName(url, method) {
|
|
31
|
+
// 移除前缀 "/api/app"
|
|
32
|
+
const cleanedUrl = url.replace(/\/api\/app/, "");
|
|
33
|
+
const arr = cleanedUrl.split("/");
|
|
34
|
+
|
|
35
|
+
// 处理 {xxx} 转换为 ByXxx
|
|
36
|
+
const processedArr = arr.map(
|
|
37
|
+
(item) =>
|
|
38
|
+
item
|
|
39
|
+
.replace(
|
|
40
|
+
/{(.*?)}/,
|
|
41
|
+
(_, param) => `By${param.charAt(0).toUpperCase() + param.slice(1)}`
|
|
42
|
+
) // 处理 {xxx}
|
|
43
|
+
.replace(/[-_]/g, "") // 去除 - 和 _
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// 删除第一个空项
|
|
47
|
+
if (processedArr[0] === "") {
|
|
48
|
+
processedArr.shift();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 去重和拼接相邻相同的项
|
|
52
|
+
const resultArr = [];
|
|
53
|
+
for (let i = 0; i < processedArr.length; i++) {
|
|
54
|
+
if (i === 0 || processedArr[i] !== processedArr[i - 1]) {
|
|
55
|
+
// 将每项首字母大写
|
|
56
|
+
const capitalizedItem =
|
|
57
|
+
processedArr[i].charAt(0).toUpperCase() + processedArr[i].slice(1);
|
|
58
|
+
resultArr.push(capitalizedItem);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const key = resultArr.join("");
|
|
62
|
+
return `${method}${key}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// java数据类型转成js数据类型
|
|
66
|
+
const javaTypeToJsType = (javaType) => {
|
|
67
|
+
switch (javaType) {
|
|
68
|
+
case "integer":
|
|
69
|
+
return "number";
|
|
70
|
+
case "array":
|
|
71
|
+
return "Array";
|
|
72
|
+
case "object":
|
|
73
|
+
return "Object";
|
|
74
|
+
default:
|
|
75
|
+
return javaType;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// 根据parameters参数返回path对应的接口参数,path参数都是必填的
|
|
80
|
+
const getPathParameters = (parameters) => {
|
|
81
|
+
if (!parameters && !Array.isArray(parameters)) return [];
|
|
82
|
+
return parameters
|
|
83
|
+
.filter((param) => param.in === "path") // 过滤出路径参数
|
|
84
|
+
.map((param) => param.name); // 提取name属性
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const MethodEnum = {
|
|
88
|
+
get: "get",
|
|
89
|
+
post: "post",
|
|
90
|
+
put: "put",
|
|
91
|
+
delete: "del",
|
|
92
|
+
};
|
|
93
|
+
const generateApiModules = (swagger) => {
|
|
94
|
+
const { tags, paths } = swagger;
|
|
95
|
+
const apiModules = {};
|
|
96
|
+
// 初始化模块对象
|
|
97
|
+
tags.forEach((tag) => {
|
|
98
|
+
apiModules[
|
|
99
|
+
tag.name
|
|
100
|
+
] = `import { get, post, put, del } from "@/api/request/sendRuest"\n`;
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
for (const [url, methods] of Object.entries(paths)) {
|
|
104
|
+
for (const [method, details] of Object.entries(methods)) {
|
|
105
|
+
const tags = details.tags || [];
|
|
106
|
+
const parameters = details.parameters || [];
|
|
107
|
+
const requestBody = details.requestBody || {};
|
|
108
|
+
tags.forEach((tag) => {
|
|
109
|
+
if (Object.keys(apiModules).includes(tag)) {
|
|
110
|
+
// 生成 JSDoc 注释
|
|
111
|
+
let functionDoc = ``;
|
|
112
|
+
functionDoc += `/**\n`;
|
|
113
|
+
functionDoc += ` * ${details.summary || "无描述"}\n`;
|
|
114
|
+
|
|
115
|
+
let pathParameters = [];
|
|
116
|
+
let hasQuery = false;
|
|
117
|
+
let hasBody = false;
|
|
118
|
+
//parameters此参数
|
|
119
|
+
if (Object.keys(details).includes("parameters")) {
|
|
120
|
+
functionDoc += ` * @param {Object} params - 请求参数\n`;
|
|
121
|
+
parameters.forEach((param) => {
|
|
122
|
+
const paramType = param.schema
|
|
123
|
+
? javaTypeToJsType(param.schema.type)
|
|
124
|
+
: "any";
|
|
125
|
+
const paramName = param.name;
|
|
126
|
+
const temp = param.required
|
|
127
|
+
? `params.${paramName}`
|
|
128
|
+
: `[params.${paramName}]`;
|
|
129
|
+
functionDoc += ` * @param {${paramType}} ${temp} - ${
|
|
130
|
+
param.description || ""
|
|
131
|
+
}\n`;
|
|
132
|
+
});
|
|
133
|
+
pathParameters = getPathParameters(parameters);
|
|
134
|
+
// 是否有query参数
|
|
135
|
+
hasQuery = parameters.some((e) => e.in === "query");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 如果有requestBody
|
|
139
|
+
if (
|
|
140
|
+
Object.keys(requestBody).length > 0 &&
|
|
141
|
+
requestBody.content &&
|
|
142
|
+
requestBody.content["text/json"] &&
|
|
143
|
+
requestBody.content["text/json"].schema
|
|
144
|
+
) {
|
|
145
|
+
const schema = requestBody.content["text/json"].schema;
|
|
146
|
+
const { type, properties } = schema;
|
|
147
|
+
//目前只有这两种入参结构
|
|
148
|
+
if (type === "object") {
|
|
149
|
+
functionDoc += ` * @param {Object} body - 请求参数\n`;
|
|
150
|
+
|
|
151
|
+
Object.keys(properties).forEach((key) => {
|
|
152
|
+
// 是否必填
|
|
153
|
+
const isRequired =
|
|
154
|
+
schema.required &&
|
|
155
|
+
Array.isArray(schema.required) &&
|
|
156
|
+
schema.required.includes(key);
|
|
157
|
+
|
|
158
|
+
const temp = isRequired ? `body.${key}` : `[body.${key}]`;
|
|
159
|
+
functionDoc += ` * @param {${javaTypeToJsType(
|
|
160
|
+
properties[key].type
|
|
161
|
+
)}} ${temp} - ${properties[key].description || ""}\n`;
|
|
162
|
+
});
|
|
163
|
+
if (Object.keys(properties).length > 1) hasBody = true;
|
|
164
|
+
} else if (type === "array") {
|
|
165
|
+
// 公司入参是数组的swagger一定是接收id数组的
|
|
166
|
+
functionDoc += ` * @param {Array<string>} body - 数组类型的入参\n`;
|
|
167
|
+
hasBody = true;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
functionDoc += `*/\n`;
|
|
172
|
+
// 接口入参
|
|
173
|
+
let functionParams = [];
|
|
174
|
+
if (hasQuery) functionParams.push("params");
|
|
175
|
+
if (hasBody) functionParams.push("body");
|
|
176
|
+
functionParams = functionParams.concat(pathParameters).join(", ");
|
|
177
|
+
// 函数
|
|
178
|
+
functionDoc += `export const ${generateKeyName(
|
|
179
|
+
url,
|
|
180
|
+
method
|
|
181
|
+
)} = (${functionParams}) => {\n`;
|
|
182
|
+
|
|
183
|
+
functionDoc += ` return ${MethodEnum[method]}({\n`;
|
|
184
|
+
functionDoc += ` url: \`${url.replace(/{/g, "${")}\`,\n`;
|
|
185
|
+
if (hasQuery) functionDoc += ` params,\n`;
|
|
186
|
+
if (hasBody) functionDoc += ` data: body,\n`;
|
|
187
|
+
functionDoc += ` });\n`;
|
|
188
|
+
functionDoc += `};\n\n`;
|
|
189
|
+
apiModules[tag] += functionDoc;
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return apiModules;
|
|
196
|
+
};
|
package/src/main.js
CHANGED
|
@@ -2,14 +2,13 @@ import Vue from "vue";
|
|
|
2
2
|
import ElementUI from "element-ui";
|
|
3
3
|
import "element-ui/lib/theme-chalk/index.css";
|
|
4
4
|
import App from "./App.vue";
|
|
5
|
-
import OlCom from "@/package/index.js";
|
|
6
|
-
|
|
5
|
+
import OlCom, { swaggerInstall } from "@/package/index.js";
|
|
7
6
|
Vue.use(ElementUI);
|
|
8
7
|
|
|
9
8
|
Vue.use(OlCom);
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
swaggerInstall("http://220.179.249.140:20019/swagger/v1/swagger.json").then(
|
|
10
|
+
() => {}
|
|
11
|
+
);
|
|
13
12
|
new Vue({
|
|
14
13
|
render: (h) => h(App),
|
|
15
14
|
}).$mount("#app");
|