@jx3box/jx3box-ui 2.4.15 → 2.4.16

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": "@jx3box/jx3box-ui",
3
- "version": "2.4.15",
3
+ "version": "2.4.16",
4
4
  "description": "JX3BOX Vue3 UI",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/service/cms.js CHANGED
@@ -227,6 +227,7 @@ export {
227
227
  getCollection,
228
228
  getTopicBucket,
229
229
  getConfig,
230
+ getConfigFromGlobal as getHeaderConfig,
230
231
  getUserMeta,
231
232
  setUserMeta,
232
233
  refreshAuth,
package/service/header.js CHANGED
@@ -3,45 +3,23 @@ import { $cms, $next } from "@jx3box/jx3box-common/js/api";
3
3
  import JX3BOX from "@jx3box/jx3box-common/data/jx3box.json";
4
4
 
5
5
  const GLOBAL_CONFIG_STORAGE_KEY = "jx3box:global-config";
6
- const GLOBAL_CONFIG_TTL = 6 * 60 * 60 * 1000;
7
- let globalConfigCache = null;
8
6
  let globalConfigPending = null;
9
- let globalConfigRefreshPending = null;
10
-
11
- function getGlobalConfigUrl() {
12
- const root = JX3BOX.__ossRoot || "https://cdn.jx3box.com/";
13
- return `${root.replace(/\/?$/, "/")}config/global.json`;
14
- }
15
-
16
- function getNow() {
17
- return Date.now ? Date.now() : new Date().getTime();
18
- }
19
7
 
20
8
  function readGlobalConfigStorage() {
21
- if (typeof sessionStorage === "undefined") return null;
22
9
  try {
23
- const raw = sessionStorage.getItem(GLOBAL_CONFIG_STORAGE_KEY);
24
- if (!raw) return null;
25
- const cache = JSON.parse(raw);
26
- if (!cache?.data || !cache?.time) return null;
27
- if (getNow() - cache.time > GLOBAL_CONFIG_TTL) return null;
28
- return cache.data;
10
+ const config = JSON.parse(sessionStorage.getItem(GLOBAL_CONFIG_STORAGE_KEY));
11
+ // 旧格式 { time, data } 属于原来的六小时缓存,不再复用。
12
+ return config && typeof config === "object" && !Array.isArray(config) && !config.time && !config.data
13
+ ? config
14
+ : null;
29
15
  } catch (e) {
30
16
  return null;
31
17
  }
32
18
  }
33
19
 
34
- function writeGlobalConfigStorage(data) {
35
- if (typeof sessionStorage === "undefined") return;
36
- try {
37
- sessionStorage.setItem(
38
- GLOBAL_CONFIG_STORAGE_KEY,
39
- JSON.stringify({
40
- time: getNow(),
41
- data,
42
- })
43
- );
44
- } catch (e) {}
20
+ function getGlobalConfigUrl() {
21
+ const root = JX3BOX.__ossRoot || "https://cdn.jx3box.com/";
22
+ return `${root.replace(/\/?$/, "/")}config/global.json`;
45
23
  }
46
24
 
47
25
  function getLetter() {
@@ -74,45 +52,26 @@ function getGames() {
74
52
  return axios.get(JX3BOX.__dataPath + "data/product/games.json");
75
53
  }
76
54
 
77
- // 获取全局配置
55
+ // 标签页会话内复用配置,不设置时间有效期;网络请求仍遵循浏览器 HTTP 缓存。
78
56
  function getGlobalConfig({ force = false } = {}) {
79
- if (force) {
80
- if (!globalConfigRefreshPending) {
81
- globalConfigRefreshPending = axios
82
- .get(getGlobalConfigUrl())
83
- .then((res) => {
84
- globalConfigCache = res.data || {};
85
- writeGlobalConfigStorage(globalConfigCache);
86
- return globalConfigCache;
87
- })
88
- .finally(() => {
89
- globalConfigRefreshPending = null;
90
- });
91
- }
92
- return globalConfigRefreshPending;
93
- }
94
-
95
- if (globalConfigCache) return Promise.resolve(globalConfigCache);
96
-
97
- const storageConfig = readGlobalConfigStorage();
98
- if (storageConfig) {
99
- globalConfigCache = storageConfig;
100
- return Promise.resolve(globalConfigCache);
57
+ if (!force) {
58
+ const config = readGlobalConfigStorage();
59
+ if (config) return Promise.resolve(config);
101
60
  }
102
-
103
61
  if (!globalConfigPending) {
104
62
  globalConfigPending = axios
105
63
  .get(getGlobalConfigUrl())
106
64
  .then((res) => {
107
- globalConfigCache = res.data || {};
108
- writeGlobalConfigStorage(globalConfigCache);
109
- return globalConfigCache;
65
+ const config = res.data || {};
66
+ try {
67
+ sessionStorage.setItem(GLOBAL_CONFIG_STORAGE_KEY, JSON.stringify(config));
68
+ } catch (e) {}
69
+ return config;
110
70
  })
111
71
  .finally(() => {
112
72
  globalConfigPending = null;
113
73
  });
114
74
  }
115
-
116
75
  return globalConfigPending;
117
76
  }
118
77
 
@@ -83,7 +83,7 @@ import miniprogram from "@jx3box/jx3box-common/data/miniprogram.json";
83
83
 
84
84
  // 数据
85
85
  import { getGlobalConfig } from "../service/header";
86
- import { getConfig, getMyAccountStatus } from "../service/cms";
86
+ import { getHeaderConfig as getConfig, getMyAccountStatus } from "../service/cms";
87
87
  import { clearActiveAuthToken, refreshTokenIfNeeded } from "./utils/auth-token-refresh";
88
88
  import {
89
89
  checkClientStatOnVisible,
@@ -49,7 +49,7 @@
49
49
  import { getMenu } from "../../service/header";
50
50
  import JX3BOX from "@jx3box/jx3box-common/data/jx3box.json";
51
51
  import i18nMixin from "../../i18n/mixin";
52
- import { getConfig } from "../../service/cms";
52
+ import { getHeaderConfig as getConfig } from "../../service/cms";
53
53
  // import manageIcon from "@/assets/img/components/common/header/manage.svg";
54
54
  const { __imgPath } = JX3BOX;
55
55
  const NOTICE_POP_KEY = "notice_pop";
@@ -177,7 +177,7 @@ export default {
177
177
  return {
178
178
  ...item,
179
179
  link: noticeUrlConfig?.val || item.link,
180
- meta: noticeConfig?.val || item.meta,
180
+ meta: noticeConfig?.val ?? "",
181
181
  };
182
182
  });
183
183
  },
@@ -23,7 +23,7 @@
23
23
  </template>
24
24
 
25
25
  <script>
26
- import { getConfig } from "../../service/cms";
26
+ import { getHeaderConfig as getConfig } from "../../service/cms";
27
27
  import i18nMixin from "../../i18n/mixin";
28
28
  // import shopIcon from "@/assets/img/components/common/header/gift.svg";
29
29
  export default {
@@ -6,7 +6,7 @@
6
6
  placement="bottom"
7
7
  popper-class="c-header-tooltip"
8
8
  >
9
- <a class="u-post u-vip" href="/vip/premium" @click="markPopRead">
9
+ <a class="u-post u-vip" href="/vip/premium" @click="markPopRead" @auxclick.middle="markPopRead">
10
10
  <i class="u-icon u-icon-msg">
11
11
  <i class="u-pop" style="display: none" v-show="pop"></i>
12
12
  <!-- <vipIcon class="u-add" /> -->
@@ -23,7 +23,7 @@
23
23
  </template>
24
24
 
25
25
  <script>
26
- import { getConfig } from "../../service/cms";
26
+ import { getHeaderConfig as getConfig } from "../../service/cms";
27
27
  import i18nMixin from "../../i18n/mixin";
28
28
  // import vipIcon from "@/assets/img/components/common/header/vip.svg";
29
29
  export default {
@@ -61,11 +61,24 @@ export default {
61
61
  },
62
62
  },
63
63
  mounted() {
64
+ window.addEventListener("storage", this.syncPopRead);
65
+ window.addEventListener("pageshow", this.syncPopRead);
66
+ window.addEventListener("focus", this.syncPopRead);
64
67
  if (this.configLoaded) {
65
68
  this.init();
66
69
  }
67
70
  },
71
+ beforeUnmount() {
72
+ window.removeEventListener("storage", this.syncPopRead);
73
+ window.removeEventListener("pageshow", this.syncPopRead);
74
+ window.removeEventListener("focus", this.syncPopRead);
75
+ },
68
76
  methods: {
77
+ syncPopRead() {
78
+ if (this.initialized) {
79
+ this.pop = this.shouldShowPop(this.popValue);
80
+ }
81
+ },
69
82
  async init() {
70
83
  if (this.initialized) return;
71
84
  /**
@@ -0,0 +1,62 @@
1
+ const assert = require("node:assert/strict");
2
+ const fs = require("node:fs");
3
+ const path = require("node:path");
4
+ const test = require("node:test");
5
+
6
+ function loadService(storage, request) {
7
+ const source = fs
8
+ .readFileSync(path.resolve(__dirname, "../service/header.js"), "utf8")
9
+ .replace(/^import .+;$/gm, "")
10
+ .replace(/export \{[^}]+\};/, "return { getGlobalConfig };");
11
+ return Function(
12
+ "axios",
13
+ "JX3BOX",
14
+ "sessionStorage",
15
+ source
16
+ )(
17
+ { get: request },
18
+ { __ossRoot: "https://cdn.jx3box.com/" },
19
+ { getItem: (key) => storage.get(key) ?? null, setItem: (key, value) => storage.set(key, value) }
20
+ );
21
+ }
22
+
23
+ test("one CDN request is shared and the session cache survives module remounts", async () => {
24
+ const storage = new Map();
25
+ let calls = 0;
26
+ const request = async (url) => {
27
+ assert.equal(url, "https://cdn.jx3box.com/config/global.json");
28
+ calls++;
29
+ return { data: { vip: "20260920", mall: "20260703", important_notice: "20260728" } };
30
+ };
31
+ const service = loadService(storage, request);
32
+ const [a, b] = await Promise.all([service.getGlobalConfig(), service.getGlobalConfig()]);
33
+ assert.deepEqual(a, b);
34
+ assert.equal(calls, 1);
35
+ assert.deepEqual(await loadService(storage, request).getGlobalConfig(), a);
36
+ assert.equal(calls, 1);
37
+ assert.deepEqual(JSON.parse(storage.get("jx3box:global-config")), a);
38
+ storage.clear();
39
+ await service.getGlobalConfig();
40
+ assert.equal(calls, 2);
41
+ });
42
+
43
+ test("old timed cache is replaced and explicit refresh updates the session", async () => {
44
+ const storage = new Map([["jx3box:global-config", JSON.stringify({ time: Date.now(), data: { vip: "old" } })]]);
45
+ let value = "new";
46
+ const service = loadService(storage, async () => ({ data: { vip: value } }));
47
+ assert.equal((await service.getGlobalConfig()).vip, "new");
48
+ value = "changed";
49
+ assert.equal((await service.getGlobalConfig()).vip, "new");
50
+ assert.equal((await service.getGlobalConfig({ force: true })).vip, "changed");
51
+ });
52
+
53
+ test("a failed CDN request can be retried", async () => {
54
+ let fail = true;
55
+ const service = loadService(new Map(), async () => {
56
+ if (fail) throw Error("offline");
57
+ return { data: { vip: "1" } };
58
+ });
59
+ await assert.rejects(service.getGlobalConfig(), /offline/);
60
+ fail = false;
61
+ assert.deepEqual(await service.getGlobalConfig(), { vip: "1" });
62
+ });
@@ -0,0 +1,71 @@
1
+ const assert = require("node:assert/strict");
2
+ const fs = require("node:fs");
3
+ const path = require("node:path");
4
+ const test = require("node:test");
5
+
6
+ function createComponent(readVersion = null) {
7
+ const source = fs.readFileSync(path.resolve(__dirname, "../src/header/vip.vue"), "utf8");
8
+ const script = source
9
+ .match(/<script>([\s\S]*?)<\/script>/)[1]
10
+ .replace(/^import .+;$/gm, "")
11
+ .replace("export default", "return");
12
+ const storage = new Map(readVersion == null ? [] : [["vip_pop", readVersion]]);
13
+ const component = Function(
14
+ "i18nMixin",
15
+ "localStorage",
16
+ script
17
+ )(
18
+ {},
19
+ {
20
+ getItem: (key) => storage.get(key) ?? null,
21
+ setItem: (key, value) => storage.set(key, String(value)),
22
+ }
23
+ );
24
+ const vm = { ...component.data(), configManaged: true };
25
+ for (const [key, method] of Object.entries(component.methods)) vm[key] = method.bind(vm);
26
+ return { vm, storage };
27
+ }
28
+
29
+ test("any changed reminder version is unread regardless of numerical order", async () => {
30
+ const { vm } = createComponent("20260920");
31
+ vm.config = { val: "20260703" };
32
+ await vm.init();
33
+ assert.equal(vm.pop, true);
34
+ assert.equal(vm.shouldShowPop("20260920"), false);
35
+ assert.equal(vm.shouldShowPop("20260921"), true);
36
+ });
37
+
38
+ test("first click persists the current version and hides the reminder", async () => {
39
+ const { vm, storage } = createComponent();
40
+ vm.config = { val: "20260920" };
41
+ await vm.init();
42
+ assert.equal(vm.pop, true);
43
+ vm.markPopRead();
44
+ assert.equal(vm.pop, false);
45
+ assert.equal(storage.get("vip_pop"), "20260920");
46
+ });
47
+
48
+ test("click acknowledges the displayed version regardless of numerical order", () => {
49
+ const { vm, storage } = createComponent("20260920");
50
+ vm.popValue = "20260703";
51
+ vm.pop = true;
52
+ vm.markPopRead();
53
+ assert.equal(storage.get("vip_pop"), "20260703");
54
+ assert.equal(vm.pop, false);
55
+ });
56
+
57
+ test("sync hides a reminder read in another page", () => {
58
+ const { vm, storage } = createComponent();
59
+ Object.assign(vm, { initialized: true, popValue: "20260920", pop: true });
60
+ storage.set("vip_pop", "20260920");
61
+ vm.syncPopRead();
62
+ assert.equal(vm.pop, false);
63
+ });
64
+
65
+ test("disabled or invalid config stays hidden and invalid read state can recover", () => {
66
+ const { vm } = createComponent("undefined");
67
+ for (const value of [null, undefined, "", "0", "invalid"]) {
68
+ assert.equal(vm.shouldShowPop(value), false);
69
+ }
70
+ assert.equal(vm.shouldShowPop("20260920"), true);
71
+ });