@ysgroup/core 0.1.19 → 0.1.20
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 +1 -1
- package/src/store.ts +32 -2
package/package.json
CHANGED
package/src/store.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* @ysgroup/core —— 登录态 + 差量订阅缓存(Pinia store)。 */
|
|
2
2
|
import { defineStore } from "pinia";
|
|
3
|
-
import { ref, computed } from "vue";
|
|
3
|
+
import { ref, computed, watchEffect } from "vue";
|
|
4
4
|
import { sealLoginPassword } from "./password";
|
|
5
5
|
import { hasNewVersion } from "./version";
|
|
6
6
|
import { YsRpcError, YsWsClient, type YsDisconnectInfo } from "./ws";
|
|
@@ -33,6 +33,8 @@ interface LoginResult {
|
|
|
33
33
|
const LS_ACCESS = "ys_access_token";
|
|
34
34
|
const LS_REFRESH = "ys_refresh_token";
|
|
35
35
|
const LS_USER = "ys_session_user";
|
|
36
|
+
/** 按应用标识缓存 Auth 下发的应用别名,刷新页面时先显示上次的名称,不闪应用标识。 */
|
|
37
|
+
const LS_APP_CNAME = "ys_app_cname:";
|
|
36
38
|
|
|
37
39
|
/**
|
|
38
40
|
* Auth 明确拒绝续期的回执码:刷新令牌无效或过期、用户已停用、应用授权已撤销、刷新令牌已被使用、禁止登录。
|
|
@@ -86,6 +88,18 @@ export const useYsStore = defineStore("ys", () => {
|
|
|
86
88
|
/** 服务端已部署了新版本前端。 */
|
|
87
89
|
const updateAvailable = ref(false);
|
|
88
90
|
const appName = ref("");
|
|
91
|
+
/** Auth 应用管理里的「应用别名」,连接握手(hello.app_cname)和 app_profile 事件下发。 */
|
|
92
|
+
const appCname = ref("");
|
|
93
|
+
/**
|
|
94
|
+
* 系统名称:登录页、页头左上角、浏览器标签页统一用它,子系统不得自己写死或改写。
|
|
95
|
+
* 优先 Auth 下发的应用别名,还没拿到时用上次缓存,再没有才用应用标识兜底。
|
|
96
|
+
*/
|
|
97
|
+
const systemName = computed(() => appCname.value || appName.value);
|
|
98
|
+
// 浏览器标签页标题固定为系统名称,由这里统一设置;子系统不要再改 document.title。
|
|
99
|
+
watchEffect(() => {
|
|
100
|
+
if (systemName.value && typeof document !== "undefined")
|
|
101
|
+
document.title = systemName.value;
|
|
102
|
+
});
|
|
89
103
|
const sessionMessage = ref("");
|
|
90
104
|
/** 差量订阅的表数据:table -> Map<id, doc> */
|
|
91
105
|
const tables = ref<Record<string, Map<string, Record<string, unknown>>>>({});
|
|
@@ -107,15 +121,28 @@ export const useYsStore = defineStore("ys", () => {
|
|
|
107
121
|
return Array.from(tables.value[table]?.values() ?? []);
|
|
108
122
|
}
|
|
109
123
|
|
|
124
|
+
function applyAppCname(data: unknown): void {
|
|
125
|
+
const raw = (data as { app_cname?: unknown } | null)?.app_cname;
|
|
126
|
+
if (typeof raw !== "string") return;
|
|
127
|
+
const cname = raw.trim();
|
|
128
|
+
// 空串表示子系统还没连上 Auth(或别名没配),保留已知名称。
|
|
129
|
+
if (!cname || !appName.value) return;
|
|
130
|
+
appCname.value = cname;
|
|
131
|
+
localStorage.setItem(LS_APP_CNAME + appName.value, cname);
|
|
132
|
+
}
|
|
133
|
+
|
|
110
134
|
function connect(wsUrl: string, app: string, options: YsConnectOptions = {}) {
|
|
111
135
|
appName.value = app;
|
|
136
|
+
appCname.value = localStorage.getItem(LS_APP_CNAME + app) ?? "";
|
|
112
137
|
ws = new YsWsClient(wsUrl, {
|
|
113
138
|
callTimeoutMs: options.callTimeoutMs,
|
|
114
139
|
offlineWaitMs: options.offlineWaitMs,
|
|
115
140
|
});
|
|
116
141
|
disconnectedAt.value = Date.now();
|
|
117
142
|
let everConnected = false;
|
|
118
|
-
ws.on("
|
|
143
|
+
ws.on("app_profile", applyAppCname);
|
|
144
|
+
ws.on("__connected", (data) => {
|
|
145
|
+
applyAppCname(data);
|
|
119
146
|
connected.value = true;
|
|
120
147
|
disconnectedAt.value = 0;
|
|
121
148
|
restarting.value = false;
|
|
@@ -490,6 +517,9 @@ export const useYsStore = defineStore("ys", () => {
|
|
|
490
517
|
}
|
|
491
518
|
|
|
492
519
|
return {
|
|
520
|
+
appName,
|
|
521
|
+
appCname,
|
|
522
|
+
systemName,
|
|
493
523
|
user,
|
|
494
524
|
accessToken,
|
|
495
525
|
refreshToken,
|