ol-base-components 1.2.5 → 1.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ol-base-components",
3
- "version": "1.2.5",
3
+ "version": "1.3.1",
4
4
  "private": false,
5
5
  "main": "src/package/index.js",
6
6
  "scripts": {
@@ -24,6 +24,86 @@ ${cyan}感谢使用我们的组件库,期待你的精彩应用!${reset}
24
24
  `);
25
25
  };
26
26
 
27
+ const DB_NAME = "SwaggerDB";
28
+ const DB_VERSION = 1;
29
+ const STORE_NAME = "swaggerDataStore";
30
+ const LOGIN_STATUS_KEY = "isLoggedIn"; // 存储登录状态的键
31
+
32
+ // 打开数据库
33
+ function openDatabase() {
34
+ return new Promise((resolve, reject) => {
35
+ const request = indexedDB.open(DB_NAME, DB_VERSION);
36
+
37
+ request.onupgradeneeded = (event) => {
38
+ const db = event.target.result;
39
+ db.createObjectStore(STORE_NAME);
40
+ };
41
+
42
+ request.onsuccess = (event) => {
43
+ resolve(event.target.result);
44
+ };
45
+
46
+ request.onerror = (event) => {
47
+ reject(event.target.error);
48
+ };
49
+ });
50
+ }
51
+
52
+ // 存储数据
53
+ function storeData(data) {
54
+ return openDatabase().then((db) => {
55
+ return new Promise((resolve, reject) => {
56
+ const transaction = db.transaction(STORE_NAME, "readwrite");
57
+ const store = transaction.objectStore(STORE_NAME);
58
+ store.put(data, "swaggerData"); // 使用 'swaggerData' 作为键
59
+ transaction.oncomplete = () => resolve();
60
+ transaction.onerror = (event) => reject(event.target.error);
61
+ });
62
+ });
63
+ }
64
+
65
+ // 获取数据
66
+ function getData() {
67
+ return openDatabase().then((db) => {
68
+ return new Promise((resolve, reject) => {
69
+ const transaction = db.transaction(STORE_NAME, "readonly");
70
+ const store = transaction.objectStore(STORE_NAME);
71
+ const request = store.get("swaggerData");
72
+
73
+ request.onsuccess = (event) => {
74
+ resolve(event.target.result);
75
+ };
76
+
77
+ request.onerror = (event) => {
78
+ reject(event.target.error);
79
+ };
80
+ });
81
+ });
82
+ }
83
+
84
+ // 清除数据
85
+ function clearData() {
86
+ return openDatabase().then((db) => {
87
+ return new Promise((resolve, reject) => {
88
+ const transaction = db.transaction(STORE_NAME, "readwrite");
89
+ const store = transaction.objectStore(STORE_NAME);
90
+ store.delete("swaggerData"); // 删除存储的数据
91
+ transaction.oncomplete = () => resolve();
92
+ transaction.onerror = (event) => reject(event.target.error);
93
+ });
94
+ });
95
+ }
96
+
97
+ // 存储登录状态
98
+ function storeLoginStatus(isLoggedIn) {
99
+ localStorage.setItem(LOGIN_STATUS_KEY, isLoggedIn);
100
+ }
101
+
102
+ // 获取登录状态
103
+ function getLoginStatus() {
104
+ return localStorage.getItem(LOGIN_STATUS_KEY) === "true";
105
+ }
106
+
27
107
  export const SwaggerHandler = async (Vue, swaggerUrl) => {
28
108
  const client = await new SwaggerClient(swaggerUrl);
29
109
  console.log("client", client);
@@ -32,10 +112,14 @@ export const SwaggerHandler = async (Vue, swaggerUrl) => {
32
112
 
33
113
  const components = [OlTable, OlSearch, Dialog];
34
114
 
115
+ const SWAGGER_DATA_KEY = "swaggerData"; // 存储 Swagger 数据的键
116
+ let isLoggedIn = false; // 用于跟踪用户登录状态
117
+
35
118
  const install = async function (
36
119
  Vue,
37
120
  options = {
38
121
  swaggerUrl: "",
122
+ successSwaggerCallback: null,
39
123
  }
40
124
  ) {
41
125
  // 遍历所有组件
@@ -43,14 +127,68 @@ const install = async function (
43
127
  Vue.component(`ol-${item.name}`, item);
44
128
  });
45
129
 
130
+ // 检查登录状态
131
+ const isLoggedIn = getLoginStatus();
132
+
133
+ // 如果 $swagger 已经存在,直接返回
134
+ if (Vue.prototype.$swagger) {
135
+ if (
136
+ options.successSwaggerCallback &&
137
+ typeof options.successSwaggerCallback === "function"
138
+ ) {
139
+ options.successSwaggerCallback();
140
+ }
141
+ return; // 数据已加载,直接返回
142
+ }
143
+
46
144
  if (options && options.swaggerUrl) {
47
- // "http://220.179.249.140:20019/swagger/v1/swagger.json"
48
- const client = await SwaggerClient(options.swaggerUrl);
49
- Vue.prototype.$swagger = { specification: client.spec };
145
+ // 检查 IndexedDB 中是否存在 Swagger 数据
146
+ if (isLoggedIn) {
147
+ try {
148
+ const cachedData = await getData();
149
+ if (cachedData) {
150
+ Vue.prototype.$swagger = { specification: cachedData };
151
+ if (
152
+ options.successSwaggerCallback &&
153
+ typeof options.successSwaggerCallback === "function"
154
+ ) {
155
+ options.successSwaggerCallback();
156
+ }
157
+ return; // 数据已加载,直接返回
158
+ }
159
+ } catch (error) {
160
+ console.error("获取缓存数据失败:", error);
161
+ }
162
+ }
163
+
164
+ // 用户未登录或缓存数据不存在,重新请求 Swagger 数据
165
+ try {
166
+ const client = await SwaggerClient(options.swaggerUrl);
167
+ const swaggerData = client.spec; // 获取 Swagger 数据
168
+ await storeData(swaggerData); // 缓存数据到 IndexedDB
169
+ Vue.prototype.$swagger = { specification: swaggerData };
170
+ storeLoginStatus(true); // 设置用户为已登录状态
171
+ if (
172
+ options.successSwaggerCallback &&
173
+ typeof options.successSwaggerCallback === "function"
174
+ ) {
175
+ options.successSwaggerCallback();
176
+ }
177
+ } catch (error) {
178
+ console.error("获取 Swagger 数据失败:", error);
179
+ }
50
180
  }
51
181
  consoleTooltip();
52
182
  };
53
183
 
184
+ // 提供一个方法用于用户退出登录时调用
185
+ export const OlLogout = async function () {
186
+ // 重置登录状态
187
+ storeLoginStatus(false); // 清除 localStorage 中的登录状态
188
+ await clearData(); // 清空 IndexedDB 中的缓存数据
189
+ Vue.prototype.$swagger = null; // 清空 Swagger 数据
190
+ };
191
+
54
192
  // 判断是否引入文件
55
193
  export default install; //全局导入
56
194
  export { OlTable, OlSearch, Dialog }; //按需导入
@@ -361,75 +361,8 @@ export default {
361
361
  return this.tableData.columns.filter((column) =>
362
362
  Object.keys(column).includes("show") ? column.show : true
363
363
  );
364
-
365
- // const swaggerColumns = this.$swagger.specification.paths[this.url].get.responses["200"].content['application/json'].schema.properties.items.items.properties
366
- // swaggerColumns.forEach(item => {
367
- // const tempItem = this.tableData.columns.find((e) => e.prop == item.prop)
368
- // tempItem ? tempItem = { ...item, ...tempItem } : this.tableData.columns.push(item)
369
- // })
370
-
371
- // // 一定加上selection,通过show显示隐藏
372
- // const itemSelection = this.tableData.columns.find((item) => item.type == "selection");
373
- // const hasSelection = this.tableData.options.selection;
374
- // if (itemSelection) {
375
- // itemSelection.show = !!hasSelection;
376
- // } else {
377
- // this.tableData.columns.unshift({
378
- // label: "",
379
- // minWidth: "",
380
- // type: "selection",
381
- // show: !!hasSelection,
382
- // });
383
- // }
384
-
385
- // return this.tableData.columns.filter((column) =>
386
- // Object.keys(column).includes("show") ? column.show : true
387
- // );
388
364
  },
389
- // bindTableColumns1() {
390
- // // 读取接口和类型获取表头数据
391
- // if (!this.$swagger) return this.tableData.columns.filter((column) =>
392
- // Object.keys(column).includes("show") ? column.show : true
393
- // );
394
- // const tableColumns = this.$swagger.specification.paths[this.url].get.responses["200"].content['application/json'].schema.properties.items.items.properties
395
- // let swaggerColumns = Object.keys(tableColumns).reduce((acc, key) => {
396
- // const column = tableColumns[key];
397
- // if (column.description) {
398
- // acc.push({
399
- // prop: key,
400
- // label: column.description,
401
- // minWidth: column.minWidth || "150px",
402
- // sortable: true,
403
- // show: true,
404
- // attrs: {},
405
- // })
406
- // }
407
- // return acc;
408
- // }, []);
409
365
 
410
- // this.tableData.columns.filter((column) =>
411
- // Object.keys(column).includes("show") ? column.show : true
412
- // );
413
-
414
- // this.tableData.columns.forEach(dataColumn => {
415
- // const tempIndex = swaggerColumns.findIndex((item) => item.prop == dataColumn.prop)
416
- // if (tempIndex == -1) {
417
- // swaggerColumns.push(dataColumn)
418
- // } else {
419
- // Object.assign(swaggerColumns[tempIndex], dataColumn)
420
- // }
421
- // })
422
-
423
- // // 如果options.selection有多选框直接加
424
- // swaggerColumns.unshift({
425
- // label: "",
426
- // minWidth: "",
427
- // type: "selection",
428
- // show: true,
429
- // });
430
- // console.log('表头数据', swaggerColumns)
431
- // return swaggerColumns
432
- // },
433
366
  checkedTableList: {
434
367
  get() {
435
368
  // 返回选中的列名