ol-base-components 1.5.0 → 1.5.2

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.5.0",
3
+ "version": "1.5.2",
4
4
  "private": false,
5
5
  "main": "src/package/index.js",
6
6
  "scripts": {
@@ -63,7 +63,7 @@ function storeData(data) {
63
63
  }
64
64
 
65
65
  // 获取数据
66
- function getData() {
66
+ export function getData() {
67
67
  return openDatabase().then((db) => {
68
68
  return new Promise((resolve, reject) => {
69
69
  const transaction = db.transaction(STORE_NAME, "readonly");
@@ -104,17 +104,50 @@ function getLoginStatus() {
104
104
  return localStorage.getItem(LOGIN_STATUS_KEY) === "true";
105
105
  }
106
106
 
107
- export const SwaggerHandler = async (Vue, swaggerUrl) => {
108
- const client = await new SwaggerClient(swaggerUrl);
109
- console.log("client", client);
110
- Vue.prototype.$swagger = { specification: client.spec };
107
+ // 注册
108
+ const swaggerInstall = async (swaggerUrl) => {
109
+ if (!swaggerUrl) return Promise.reject();
110
+ // 检查登录状态
111
+ const isLoggedIn = getLoginStatus();
112
+
113
+ if (isLoggedIn) {
114
+ try {
115
+ const cachedData = await getData();
116
+ if (cachedData) {
117
+ consoleTooltip();
118
+ return Promise.resolve(cachedData);
119
+ }
120
+ } catch (error) {
121
+ console.error("获取缓存数据失败:", error);
122
+ }
123
+ } else {
124
+ // 用户未登录或缓存数据不存在,重新请求 Swagger 数据
125
+ try {
126
+ showLoading();
127
+ const client = await SwaggerClient(swaggerUrl);
128
+ const swaggerData = client.spec; // 获取 Swagger 数据
129
+ await storeData(swaggerData); // 缓存数据到 IndexedDB
130
+ storeLoginStatus(true); // 设置用户为已登录状态
131
+ hideLoading();
132
+ consoleTooltip();
133
+ return Promise.resolve(swaggerData); // 返回已解析的 Promise
134
+ } catch (error) {
135
+ hideLoading();
136
+ console.error("获取 Swagger 数据失败:", error);
137
+ return Promise.reject(error); // 返回拒绝的 Promise
138
+ }
139
+ }
140
+ return Promise.reject(new Error("Swagger URL is required.")); // 返回拒绝的 Promise
141
+ };
142
+ // 销毁
143
+ const swaggerUnload = async function () {
144
+ // 重置登录状态
145
+ storeLoginStatus(false); // 清除 localStorage 中的登录状态
146
+ await clearData(); // 清空 IndexedDB 中的缓存数据
111
147
  };
112
148
 
113
149
  const components = [OlTable, OlSearch, Dialog];
114
150
 
115
- const SWAGGER_DATA_KEY = "swaggerData"; // 存储 Swagger 数据的键
116
- let isLoggedIn = false; // 用于跟踪用户登录状态
117
-
118
151
  // 自定义加载指示器
119
152
  function showLoading() {
120
153
  // 创建样式
@@ -184,70 +217,11 @@ const install = async function (
184
217
  components.map((item) => {
185
218
  Vue.component(`ol-${item.name}`, item);
186
219
  });
187
- // 检查登录状态
188
- const isLoggedIn = getLoginStatus();
189
-
190
- // 如果 $swagger 已经存在,直接返回
191
- if (Vue.prototype.$swagger) {
192
- if (
193
- options.successSwaggerCallback &&
194
- typeof options.successSwaggerCallback === "function"
195
- ) {
196
- options.successSwaggerCallback();
197
- }
198
- return; // 数据已加载,直接返回
199
- }
200
-
201
- if (options && options.swaggerUrl) {
202
- // 检查 IndexedDB 中是否存在 Swagger 数据
203
- if (isLoggedIn) {
204
- try {
205
- const cachedData = await getData();
206
- if (cachedData) {
207
- Vue.prototype.$swagger = { specification: cachedData };
208
- if (
209
- options.successSwaggerCallback &&
210
- typeof options.successSwaggerCallback === "function"
211
- ) {
212
- options.successSwaggerCallback();
213
- }
214
- return; // 数据已加载,直接返回
215
- }
216
- } catch (error) {
217
- console.error("获取缓存数据失败:", error);
218
- }
219
- }
220
-
221
- // 用户未登录或缓存数据不存在,重新请求 Swagger 数据
222
- try {
223
- showLoading();
224
- const client = await SwaggerClient(options.swaggerUrl);
225
- const swaggerData = client.spec; // 获取 Swagger 数据
226
- await storeData(swaggerData); // 缓存数据到 IndexedDB
227
- Vue.prototype.$swagger = { specification: swaggerData };
228
- storeLoginStatus(true); // 设置用户为已登录状态
229
- if (
230
- options.successSwaggerCallback &&
231
- typeof options.successSwaggerCallback === "function"
232
- ) {
233
- options.successSwaggerCallback();
234
- hideLoading();
235
- }
236
- } catch (error) {
237
- console.error("获取 Swagger 数据失败:", error);
238
- }
239
- }
220
+ await swaggerInstall(Vue, options);
240
221
  consoleTooltip();
241
222
  };
242
223
 
243
- // 提供一个方法用于用户退出登录时调用
244
- export const OlLogout = async function () {
245
- // 重置登录状态
246
- storeLoginStatus(false); // 清除 localStorage 中的登录状态
247
- await clearData(); // 清空 IndexedDB 中的缓存数据
248
- Vue.prototype.$swagger = null; // 清空 Swagger 数据
249
- };
250
-
251
224
  // 判断是否引入文件
252
225
  export default install; //全局导入
253
226
  export { OlTable, OlSearch, Dialog }; //按需导入
227
+ export { swaggerInstall, swaggerUnload };
@@ -235,6 +235,7 @@
235
235
  <script>
236
236
  import printTemplate from "./printTable.vue";
237
237
  import nodata from "./nodata.jpg";
238
+ import { getData } from '../../index.js'
238
239
  export default {
239
240
  name: "table",
240
241
  components: {
@@ -403,37 +404,42 @@ export default {
403
404
  },
404
405
  methods: {
405
406
  init() {
406
- const swaggerColumns = this.$swagger.specification.paths[this.url].get.responses["200"].content['application/json'].schema.properties.items.items.properties
407
+ // IndexedDB 中获取 Swagger 数据
408
+ getData().then((swaggerData) => {
409
+ const swaggerColumns = swaggerData.paths[this.url].get.responses["200"].content['application/json'].schema.properties.items.items.properties;
407
410
 
408
- Object.keys(swaggerColumns).forEach(key => {
409
- const item = swaggerColumns[key]
410
- let tempItem = this.tableData.columns.find((e) => e.prop == key)
411
- if (tempItem) {
412
- tempItem = { ...item, ...tempItem }
413
- } else if (item.description) {
414
- this.tableData.columns.push({
415
- prop: key,
416
- label: item.description,
417
- show: true,
418
- sortable: false,
419
- attrs: {}
420
- })
421
- }
422
- })
423
-
424
- // 一定加上selection,通过show显示隐藏
425
- const itemSelection = this.tableData.columns.find((item) => item.type == "selection");
426
- const hasSelection = this.tableData.options.selection;
427
- if (itemSelection) {
428
- itemSelection.show = !!hasSelection;
429
- } else {
430
- this.tableData.columns.unshift({
431
- label: "",
432
- minWidth: "",
433
- type: "selection",
434
- show: !!hasSelection,
411
+ Object.keys(swaggerColumns).forEach(key => {
412
+ const item = swaggerColumns[key];
413
+ let tempItem = this.tableData.columns.find((e) => e.prop == key);
414
+ if (tempItem) {
415
+ tempItem = { ...item, ...tempItem };
416
+ } else if (item.description) {
417
+ this.tableData.columns.push({
418
+ prop: key,
419
+ label: item.description,
420
+ show: true,
421
+ sortable: false,
422
+ attrs: {}
423
+ });
424
+ }
435
425
  });
436
- }
426
+
427
+ // 一定加上selection,通过show显示隐藏
428
+ const itemSelection = this.tableData.columns.find((item) => item.type == "selection");
429
+ const hasSelection = this.tableData.options.selection;
430
+ if (itemSelection) {
431
+ itemSelection.show = !!hasSelection;
432
+ } else {
433
+ this.tableData.columns.unshift({
434
+ label: "",
435
+ minWidth: "",
436
+ type: "selection",
437
+ show: !!hasSelection,
438
+ });
439
+ }
440
+ }).catch((error) => {
441
+ console.error("获取 Swagger 数据失败:", error);
442
+ });
437
443
  },
438
444
  radioChange() {
439
445
  this.$emit("radioChange", this.twinPage);