Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-csf-h5
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jianan
yd-csf-h5
Commits
66c94542
Commit
66c94542
authored
Oct 23, 2026
by
jianan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(types): 适配 axios 解包与 oidc-client-ts v3 Storage 接口
parent
3dfc8960
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
25 deletions
+29
-25
src/api/http.ts
+17
-23
src/utils/oidc.ts
+12
-2
No files found.
src/api/http.ts
View file @
66c94542
...
...
@@ -22,38 +22,32 @@ instance.interceptors.request.use(async (config) => {
// 防止多个并发请求同时 401 触发多次跳转
let
redirecting
=
false
function
redirectToLogin
():
void
{
if
(
redirecting
)
return
// 已在回调页则不再发起,避免循环
if
(
window
.
location
.
pathname
===
'/auth/callback'
)
return
redirecting
=
true
void
signinRedirect
(
window
.
location
.
pathname
+
window
.
location
.
search
)
}
instance
.
interceptors
.
response
.
use
(
(
response
)
=>
{
const
body
=
response
.
data
as
ApiResult
<
unknown
>
if
(
body
&&
body
.
code
!==
200
)
{
throw
new
Error
(
body
.
msg
||
'请求失败'
)
}
// 拦截器直接解包 data,业务侧只拿到 T
return
body
.
data
},
(
response
)
=>
response
,
(
error
)
=>
{
if
(
error
.
response
?.
status
===
401
)
{
// token 缺失/失效:重新走授权,IdP 有 SSO 会话时无感跳回
redirectToLogin
()
if
(
!
redirecting
&&
window
.
location
.
pathname
!==
'/auth/callback'
)
{
redirecting
=
true
void
signinRedirect
(
window
.
location
.
pathname
+
window
.
location
.
search
)
}
throw
new
Error
(
'未登录或登录已过期'
)
}
throw
new
Error
(
error
.
message
||
'网络请求失败'
)
}
)
// 拦截器已在运行时把响应解包为 data,这里用类型断言表达该事实
export
function
get
<
T
>
(
path
:
string
):
Promise
<
T
>
{
return
instance
.
get
(
path
)
as
unknown
as
Promise
<
T
>
// 解包 { code, msg, data }:非 200 抛错,成功只返回 data
function
unwrap
<
T
>
(
body
:
ApiResult
<
T
>
):
T
{
if
(
body
.
code
!==
200
)
throw
new
Error
(
body
.
msg
||
'请求失败'
)
return
body
.
data
}
export
async
function
get
<
T
>
(
path
:
string
):
Promise
<
T
>
{
const
res
=
await
instance
.
get
<
ApiResult
<
T
>>
(
path
)
return
unwrap
(
res
.
data
)
}
export
function
post
<
T
>
(
path
:
string
,
body
?:
unknown
):
Promise
<
T
>
{
return
instance
.
post
(
path
,
body
)
as
unknown
as
Promise
<
T
>
export
async
function
post
<
T
>
(
path
:
string
,
body
?:
unknown
):
Promise
<
T
>
{
const
res
=
await
instance
.
post
<
ApiResult
<
T
>>
(
path
,
body
)
return
unwrap
(
res
.
data
)
}
src/utils/oidc.ts
View file @
66c94542
import
{
UserManager
,
WebStorageStateStore
,
type
IStore
}
from
'oidc-client-ts'
import
{
UserManager
,
WebStorageStateStore
}
from
'oidc-client-ts'
/**
* 内存存储:登录后的 User(含 access_token)只存于此,不写入 localStorage/sessionStorage。
* 刷新页面或关闭标签即丢失,符合 D8「access_token 仅存内存」。
* 实现标准 Web Storage 接口(含 length/key/clear),以满足 oidc-client-ts v3 的存储类型。
*/
class
MemoryStore
implements
IStor
e
{
class
MemoryStore
implements
Storag
e
{
private
readonly
map
=
new
Map
<
string
,
string
>
()
get
length
():
number
{
return
this
.
map
.
size
}
getItem
(
key
:
string
):
string
|
null
{
return
this
.
map
.
has
(
key
)
?
this
.
map
.
get
(
key
)
!
:
null
}
...
...
@@ -15,6 +19,12 @@ class MemoryStore implements IStore {
removeItem
(
key
:
string
):
void
{
this
.
map
.
delete
(
key
)
}
clear
():
void
{
this
.
map
.
clear
()
}
key
(
index
:
number
):
string
|
null
{
return
Array
.
from
(
this
.
map
.
keys
())[
index
]
??
null
}
}
const
authority
=
import
.
meta
.
env
.
VITE_OIDC_AUTHORITY
??
'https://auth.anjibao.cn'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment