ol-base-components 2.0.5 → 2.1.1

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