@@ -12,6 +12,26 @@ export const STORAGE_KEYS = {
1212
1313const LOCAL_STORAGE_KEY_PREFIX = `${ __NEXT_NAME__ } ${ __VERSION__ } :` ;
1414
15+ /**
16+ * js-cookie 统一包装
17+ * @description 全局 Cookie 读写统一走此对象,业务代码不直接 import 'js-cookie',
18+ * 便于后续统一调整 Cookie 属性(path、expires、secure 等)
19+ */
20+ export const Cookie = {
21+ // 写入 Cookie,options 透传 js-cookie 的属性配置
22+ set ( key : string , val : string , options ?: object ) {
23+ Cookies . set ( key , val , options ) ;
24+ } ,
25+ // 读取 Cookie,不存在时返回 undefined
26+ get ( key : string ) : string | undefined {
27+ return Cookies . get ( key ) ;
28+ } ,
29+ // 移除 Cookie
30+ remove ( key : string ) {
31+ Cookies . remove ( key ) ;
32+ } ,
33+ } ;
34+
1535/**
1636 * 判断 key 是否需要同步写入 Cookie
1737 * @description token、refresh_token、deptId 在写入 sessionStorage 的同时也需要写入 Cookie,
@@ -22,6 +42,17 @@ function isCookieBackedSessionKey(key: string) {
2242 return key === STORAGE_KEYS . TOKEN || key === STORAGE_KEYS . REFRESH_TOKEN || key === STORAGE_KEYS . DEPT_ID ;
2343}
2444
45+ /**
46+ * 为存储键统一追加应用前缀,localStorage 与 sessionStorage 共用
47+ * @description 已带前缀的 key 原样返回,避免重复拼接
48+ */
49+ function setStorageKey ( key : string ) {
50+ if ( key . startsWith ( LOCAL_STORAGE_KEY_PREFIX ) ) {
51+ return key ;
52+ }
53+ return `${ LOCAL_STORAGE_KEY_PREFIX } ${ key } ` ;
54+ }
55+
2556/**
2657 * 安全读取并解析 sessionStorage 中的 JSON 值
2758 * @description Session.get 和 cookie-backed key 的兜底逻辑共用此函数,
@@ -49,7 +80,7 @@ function getSessionStorageValue(key: string) {
4980export const Local = {
5081 // 查看 v2.4.3版本更新日志
5182 setKey ( key : string ) {
52- return ` ${ LOCAL_STORAGE_KEY_PREFIX } ${ key } ` ;
83+ return setStorageKey ( key ) ;
5384 } ,
5485 // 设置永久缓存
5586 set < T > ( key : string , val : T ) {
@@ -86,39 +117,43 @@ export const Local = {
86117 * @method clear 移除全部临时缓存
87118 */
88119export const Session = {
120+ // 与 Local 保持一致的键名前缀规则;Cookie 键保持原名,供网关和请求拦截器按原名读取
121+ setKey ( key : string ) {
122+ return setStorageKey ( key ) ;
123+ } ,
89124 // 设置临时缓存
90125 set ( key : string , val : any ) {
91126 if ( val === undefined || val === null ) {
92127 return ;
93128 }
94129 if ( isCookieBackedSessionKey ( key ) ) {
95- Cookies . set ( key , val ) ;
130+ Cookie . set ( key , val ) ;
96131 }
97- window . sessionStorage . setItem ( key , JSON . stringify ( val ) ) ;
132+ window . sessionStorage . setItem ( Session . setKey ( key ) , JSON . stringify ( val ) ) ;
98133 } ,
99134 // 获取临时缓存
100135 get ( key : string ) {
101- if ( isCookieBackedSessionKey ( key ) ) return Cookies . get ( key ) || getSessionStorageValue ( key ) ;
102- return getSessionStorageValue ( key ) ;
136+ if ( isCookieBackedSessionKey ( key ) ) return Cookie . get ( key ) || getSessionStorageValue ( Session . setKey ( key ) ) ;
137+ return getSessionStorageValue ( Session . setKey ( key ) ) ;
103138 } ,
104139 // 移除临时缓存
105140 remove ( key : string ) {
106- if ( isCookieBackedSessionKey ( key ) ) Cookies . remove ( key ) ;
107- window . sessionStorage . removeItem ( key ) ;
141+ if ( isCookieBackedSessionKey ( key ) ) Cookie . remove ( key ) ;
142+ window . sessionStorage . removeItem ( Session . setKey ( key ) ) ;
108143 } ,
109144 // 移除全部临时缓存
110- clear ( ) {
111- Cookies . remove ( STORAGE_KEYS . TOKEN ) ;
112- Cookies . remove ( STORAGE_KEYS . REFRESH_TOKEN ) ;
113- Cookies . remove ( STORAGE_KEYS . DEPT_ID ) ;
114- window . sessionStorage . clear ( ) ;
115- } ,
145+ clear ( ) {
146+ Cookie . remove ( STORAGE_KEYS . TOKEN ) ;
147+ Cookie . remove ( STORAGE_KEYS . REFRESH_TOKEN ) ;
148+ Cookie . remove ( STORAGE_KEYS . DEPT_ID ) ;
149+ window . sessionStorage . clear ( ) ;
150+ } ,
116151 // 获取当前存储的 token
117152 getToken ( ) {
118153 return this . get ( STORAGE_KEYS . TOKEN ) ;
119154 } ,
120- // 获取当前的部门
121- getDeptId ( ) {
155+ // 获取当前的部门
156+ getDeptId ( ) {
122157 return Local . get ( STORAGE_KEYS . DEPT_ID ) ? Local . get ( STORAGE_KEYS . DEPT_ID ) : '' ;
123158 } ,
124159} ;
0 commit comments