@ysgroup/core 0.1.19 → 0.1.21

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": "@ysgroup/core",
3
- "version": "0.1.19",
3
+ "version": "0.1.21",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
package/src/index.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  /* @ysgroup/core —— 前端运行时:WS RPC 客户端 / 登录态 / 差量订阅 / REST。 */
2
2
 
3
+ // 放在最前:导入 core 时就从地址上取走 ys_code,早于业务仓 createRouter。
4
+ export { takeSsoCode } from "./sso";
5
+
3
6
  export {
4
7
  YsWsClient,
5
8
  YsRpcError,
package/src/sso.ts ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * 门户跳转码 ys_code:Auth 系统导航、拓扑或业务系统互跳打开子系统时带在地址上的一次性登录码。
3
+ *
4
+ * 不能等路由读:vue-router 首次导航是异步的,App 的 setup / onMounted 里 route.query 还是空的;
5
+ * hash 路由还会把创建时的地址(含 ?ys_code=)原样写回地址栏。所以本模块加载时(早于任何
6
+ * createRouter)直接从地址上取出码并立即删掉,之后由 App 调 takeSsoCode() 拿走去兑换。
7
+ * 同时支持 Auth「码放在」的两种位置:hash(/#/page?ys_code=…)和 search(/?ys_code=…#/page)。
8
+ */
9
+
10
+ const PARAM = "ys_code";
11
+
12
+ function stripParam(query: string): { code: string; rest: string } {
13
+ const params = new URLSearchParams(query);
14
+ const code = params.get(PARAM) ?? "";
15
+ params.delete(PARAM);
16
+ const rest = params.toString();
17
+ return { code, rest: rest ? `?${rest}` : "" };
18
+ }
19
+
20
+ /** 从当前地址取出 ys_code 并从地址栏删掉(不新增历史记录)。没有时返回空串。 */
21
+ function captureSsoCode(): string {
22
+ if (typeof location === "undefined") return "";
23
+ try {
24
+ const search = stripParam(location.search);
25
+ let hash = location.hash;
26
+ let hashCode = "";
27
+ const mark = hash.indexOf("?");
28
+ if (mark >= 0) {
29
+ const stripped = stripParam(hash.slice(mark + 1));
30
+ hashCode = stripped.code;
31
+ hash = hash.slice(0, mark) + stripped.rest;
32
+ }
33
+ const code = search.code || hashCode;
34
+ if (code && typeof history !== "undefined")
35
+ history.replaceState(
36
+ history.state,
37
+ "",
38
+ `${location.pathname}${search.rest}${hash}`,
39
+ );
40
+ return code.trim();
41
+ } catch {
42
+ return "";
43
+ }
44
+ }
45
+
46
+ let pending = captureSsoCode();
47
+
48
+ /**
49
+ * 取走本次页面打开时地址上的 ys_code(只返回一次,之后为空串)。
50
+ * 有码时 App 先 store.forceLogout() 清掉本页旧会话,再 store.loginByCode(code) 兑换。
51
+ */
52
+ export function takeSsoCode(): string {
53
+ const code = pending;
54
+ pending = "";
55
+ return code;
56
+ }
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("__connected", () => {
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;
@@ -253,7 +280,7 @@ export const useYsStore = defineStore("ys", () => {
253
280
  setSession(result);
254
281
  }
255
282
 
256
- /** 门户一次性 code 单点登录(子系统前端启动时检测 URL 上的 ?ys_code=)。 */
283
+ /** 门户一次性 code 单点登录。code 用 takeSsoCode() 取,不要读 route.query(首次导航前是空的)。 */
257
284
  async function loginByCode(code: string): Promise<void> {
258
285
  await waitUntilConnected();
259
286
  if (!ws) throw new Error("未连接");
@@ -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,