:oaweb model

v3
veypi 4 weeks ago
parent 90a1f1d3a3
commit 58f5d57b77

@ -17,7 +17,7 @@ export interface ListQuery {
updated_at?: Date updated_at?: Date
} }
export function List(json: ListOpts, query: ListQuery) { export function List(json: ListOpts, query: ListQuery) {
return webapi.Get<[models.Access]>(`/access`, { json, query }) return webapi.Get<models.Access[]>(`/access`, { json, query })
} }
export interface PostOpts { export interface PostOpts {

@ -39,7 +39,7 @@ export interface ListOpts {
name?: string name?: string
} }
export function List(json: ListOpts) { export function List(json: ListOpts) {
return webapi.Get<[models.App]>(`/app`, { json }) return webapi.Get<models.App[]>(`/app`, { json })
} }
export interface AppUserGetOpts { export interface AppUserGetOpts {
@ -65,7 +65,7 @@ export interface AppUserListOpts {
status?: string status?: string
} }
export function AppUserList(app_id: string, json: AppUserListOpts) { export function AppUserList(app_id: string, json: AppUserListOpts) {
return webapi.Get<[models.AppUser]>(`/app/${app_id}/app_user`, { json }) return webapi.Get<models.AppUser[]>(`/app/${app_id}/app_user`, { json })
} }
export interface AppUserPostOpts { export interface AppUserPostOpts {
@ -81,7 +81,7 @@ export interface ResourceListQuery {
updated_at?: Date updated_at?: Date
} }
export function ResourceList(app_id: string, query: ResourceListQuery) { export function ResourceList(app_id: string, query: ResourceListQuery) {
return webapi.Get<[models.Resource]>(`/app/${app_id}/resource`, { query }) return webapi.Get<models.Resource[]>(`/app/${app_id}/resource`, { query })
} }
export interface ResourcePostOpts { export interface ResourcePostOpts {

@ -36,6 +36,6 @@ export interface ListOpts {
name?: string name?: string
} }
export function List(json: ListOpts) { export function List(json: ListOpts) {
return webapi.Get<[models.Role]>(`/role`, { json }) return webapi.Get<models.Role[]>(`/role`, { json })
} }

@ -50,6 +50,6 @@ export interface ListOpts {
app_id: string app_id: string
} }
export function List(json: ListOpts) { export function List(json: ListOpts) {
return webapi.Get<[models.Token]>(`/token`, { json }) return webapi.Get<models.Token[]>(`/token`, { json })
} }

@ -47,7 +47,7 @@ export interface ListOpts {
status?: number status?: number
} }
export function List(json: ListOpts) { export function List(json: ListOpts) {
return webapi.Get<[models.User]>(`/user`, { json }) return webapi.Get<models.User[]>(`/user`, { json })
} }
export function UserRoleGet(user_role_id: string, user_id: string) { export function UserRoleGet(user_role_id: string, user_id: string) {
@ -82,6 +82,6 @@ export interface UserRoleListOpts {
status?: string status?: string
} }
export function UserRoleList(user_id: string, json: UserRoleListOpts) { export function UserRoleList(user_id: string, json: UserRoleListOpts) {
return webapi.Get<[models.UserRole]>(`/user/${user_id}/user_role`, { json }) return webapi.Get<models.UserRole[]>(`/user/${user_id}/user_role`, { json })
} }

@ -1,6 +1,6 @@
// //
// Copyright (C) 2024 veypi <i@veypi.com> // Copyright (C) 2024 veypi <i@veypi.com>
// 2024-10-25 18:42:03 // 2024-10-28 17:43:57
// Distributed under terms of the MIT license. // Distributed under terms of the MIT license.
// //
@ -22,14 +22,45 @@ const proxy = axios.create({
}, },
}); });
export const token = {
value: '',
update: () => {
return new Promise<string>((resolve) => { resolve(token.value) })
},
set_updator: (fn: () => Promise<string>) => {
let locked = false
token.update = () => {
if (locked) {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, 1000)
}).then(() => {
return token.update()
})
}
locked = true
return new Promise<string>((resolve, reject) => {
if (token.value) {
locked = false
resolve(token.value)
return
}
fn().then((e) => {
token.value = e
resolve(e)
}).catch(() => {
reject()
}).finally(() => { locked = false })
})
}
}
}
// 请求拦截 // 请求拦截
const beforeRequest = (config: any) => { const beforeRequest = (config: any) => {
// 设置 token
const token = util.getToken()
config.retryTimes = 3 config.retryTimes = 3
// NOTE 添加自定义头部 // NOTE 添加自定义头部
token && (config.headers.Authorization = `Bearer ${token}`) token.value && (config.headers.Authorization = `Bearer ${token.value}`)
// config.headers['auth_token'] = '' // config.headers['auth_token'] = ''
return config return config
} }
@ -64,12 +95,23 @@ const responseFailed = (error: AxiosError) => {
needRetry = false needRetry = false
} else if (response?.status == 401) { } else if (response?.status == 401) {
needRetry = false needRetry = false
if (data.code === 40103) { // AuthNotFound = New(40100, "auth not found")
// AuthExpired = New(40102, "auth expired")
if (data.code === 40102 || data.code === 40100) {
token.value = ''
return token.update().then(() => {
return requestRetry(1000, response!)
})
} }
} }
if (!needRetry) { if (!needRetry) {
return Promise.reject(data || response) return Promise.reject(data || response)
}; };
return requestRetry(1000, response!)
}
const requestRetry = (delay = 0, response: AxiosResponse) => {
const config = response?.config
// @ts-ignore // @ts-ignore
const { __retryCount = 0, retryDelay = 1000, retryTimes } = config; const { __retryCount = 0, retryDelay = 1000, retryTimes } = config;
// 在请求对象上设置重试次数 // 在请求对象上设置重试次数
@ -79,18 +121,20 @@ const responseFailed = (error: AxiosError) => {
if (__retryCount >= retryTimes) { if (__retryCount >= retryTimes) {
return Promise.reject(response?.data || response?.headers.error) return Promise.reject(response?.data || response?.headers.error)
} }
if (delay <= 0) {
return proxy.request(config as any)
}
// 延时处理 // 延时处理
const delay = new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
setTimeout(() => { setTimeout(() => {
resolve(); resolve();
}, retryDelay); }, delay)
}); }).then(() => {
// 重新发起请求 return proxy.request(config as any)
return delay.then(function() { })
return proxy.request(config as any);
});
} }
proxy.interceptors.response.use(responseSuccess, responseFailed) proxy.interceptors.response.use(responseSuccess, responseFailed)
interface data { interface data {

Loading…
Cancel
Save