@ysgroup/core 0.1.20 → 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 +1 -1
- package/src/index.ts +3 -0
- package/src/sso.ts +56 -0
- package/src/store.ts +1 -1
package/package.json
CHANGED
package/src/index.ts
CHANGED
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
|
@@ -280,7 +280,7 @@ export const useYsStore = defineStore("ys", () => {
|
|
|
280
280
|
setSession(result);
|
|
281
281
|
}
|
|
282
282
|
|
|
283
|
-
/** 门户一次性 code
|
|
283
|
+
/** 门户一次性 code 单点登录。code 用 takeSsoCode() 取,不要读 route.query(首次导航前是空的)。 */
|
|
284
284
|
async function loginByCode(code: string): Promise<void> {
|
|
285
285
|
await waitUntilConnected();
|
|
286
286
|
if (!ws) throw new Error("未连接");
|