@@ -23,13 +23,25 @@ type State = {
23
23
const createCustomStorage = ( ) => {
24
24
const sessionKeys = [ 'source' , 'chatId' , 'appId' ] ;
25
25
26
+ // 从 URL 中获取 appId 作为存储键的一部分
27
+ const getStorageKey = ( name : string ) => {
28
+ let appId = '' ;
29
+ if ( typeof window !== 'undefined' ) {
30
+ const urlParams = new URLSearchParams ( window . location . search ) ;
31
+ appId = urlParams . get ( 'appId' ) || '' ;
32
+ }
33
+ return appId ? `${ name } _${ appId } ` : name ;
34
+ } ;
35
+
26
36
return {
27
37
getItem : ( name : string ) => {
28
- const sessionData = JSON . parse ( sessionStorage . getItem ( name ) || '{}' ) ;
29
- const localData = JSON . parse ( localStorage . getItem ( name ) || '{}' ) ;
38
+ const storageKey = getStorageKey ( name ) ;
39
+ const sessionData = JSON . parse ( sessionStorage . getItem ( storageKey ) || '{}' ) ;
40
+ const localData = JSON . parse ( localStorage . getItem ( storageKey ) || '{}' ) ;
30
41
return JSON . stringify ( { ...localData , ...sessionData } ) ;
31
42
} ,
32
43
setItem : ( name : string , value : string ) => {
44
+ const storageKey = getStorageKey ( name ) ;
33
45
const data = JSON . parse ( value ) ;
34
46
35
47
// 分离 session 和 local 数据
@@ -42,15 +54,16 @@ const createCustomStorage = () => {
42
54
43
55
// 分别存储
44
56
if ( Object . keys ( sessionData ) . length > 0 ) {
45
- sessionStorage . setItem ( name , JSON . stringify ( { state : sessionData , version : 0 } ) ) ;
57
+ sessionStorage . setItem ( storageKey , JSON . stringify ( { state : sessionData , version : 0 } ) ) ;
46
58
}
47
59
if ( Object . keys ( localData ) . length > 0 ) {
48
- localStorage . setItem ( name , JSON . stringify ( { state : localData , version : 0 } ) ) ;
60
+ localStorage . setItem ( storageKey , JSON . stringify ( { state : localData , version : 0 } ) ) ;
49
61
}
50
62
} ,
51
63
removeItem : ( name : string ) => {
52
- sessionStorage . removeItem ( name ) ;
53
- localStorage . removeItem ( name ) ;
64
+ const storageKey = getStorageKey ( name ) ;
65
+ sessionStorage . removeItem ( storageKey ) ;
66
+ localStorage . removeItem ( storageKey ) ;
54
67
}
55
68
} ;
56
69
} ;
0 commit comments