diff --git a/oaweb/composables/api/access.ts b/oaweb/composables/api/access.ts index 90d163e..17dfd7e 100644 --- a/oaweb/composables/api/access.ts +++ b/oaweb/composables/api/access.ts @@ -17,7 +17,7 @@ export interface ListQuery { updated_at?: Date } export function List(json: ListOpts, query: ListQuery) { - return webapi.Get<[models.Access]>(`/access`, { json, query }) + return webapi.Get(`/access`, { json, query }) } export interface PostOpts { diff --git a/oaweb/composables/api/app.ts b/oaweb/composables/api/app.ts index f61a6f1..8488392 100644 --- a/oaweb/composables/api/app.ts +++ b/oaweb/composables/api/app.ts @@ -7,7 +7,7 @@ import webapi from "./webapi" import * as models from "./models" export function Get(app_id: string) { - return webapi.Get(`/app/${app_id}`, { }) + return webapi.Get(`/app/${app_id}`, {}) } export interface PatchOpts { @@ -22,7 +22,7 @@ export function Patch(app_id: string, json: PatchOpts) { } export function Delete(app_id: string) { - return webapi.Delete(`/app/${app_id}`, { }) + return webapi.Delete(`/app/${app_id}`, {}) } export interface PostOpts { @@ -39,7 +39,7 @@ export interface ListOpts { name?: string } export function List(json: ListOpts) { - return webapi.Get<[models.App]>(`/app`, { json }) + return webapi.Get(`/app`, { json }) } export interface AppUserGetOpts { @@ -57,7 +57,7 @@ export function AppUserPatch(app_user_id: string, app_id: string, json: AppUserP } export function AppUserDelete(app_user_id: string, app_id: string) { - return webapi.Delete(`/app/${app_id}/app_user/${app_user_id}`, { }) + return webapi.Delete(`/app/${app_id}/app_user/${app_user_id}`, {}) } export interface AppUserListOpts { @@ -65,7 +65,7 @@ export interface AppUserListOpts { status?: string } export function AppUserList(app_id: string, json: AppUserListOpts) { - return webapi.Get<[models.AppUser]>(`/app/${app_id}/app_user`, { json }) + return webapi.Get(`/app/${app_id}/app_user`, { json }) } export interface AppUserPostOpts { @@ -81,7 +81,7 @@ export interface ResourceListQuery { updated_at?: Date } export function ResourceList(app_id: string, query: ResourceListQuery) { - return webapi.Get<[models.Resource]>(`/app/${app_id}/resource`, { query }) + return webapi.Get(`/app/${app_id}/resource`, { query }) } export interface ResourcePostOpts { @@ -100,10 +100,10 @@ export function ResourceDelete(app_id: string, json: ResourceDeleteOpts) { } export function ResourceGet(app_id: string) { - return webapi.Get(`/app/${app_id}/resource`, { }) + return webapi.Get(`/app/${app_id}/resource`, {}) } export function ResourcePatch(app_id: string) { - return webapi.Patch(`/app/${app_id}/resource`, { }) + return webapi.Patch(`/app/${app_id}/resource`, {}) } diff --git a/oaweb/composables/api/role.ts b/oaweb/composables/api/role.ts index fe5e874..fe85386 100644 --- a/oaweb/composables/api/role.ts +++ b/oaweb/composables/api/role.ts @@ -7,7 +7,7 @@ import webapi from "./webapi" import * as models from "./models" export function Get(role_id: string) { - return webapi.Get(`/role/${role_id}`, { }) + return webapi.Get(`/role/${role_id}`, {}) } export interface PatchOpts { @@ -20,7 +20,7 @@ export function Patch(role_id: string, json: PatchOpts) { } export function Delete(role_id: string) { - return webapi.Delete(`/role/${role_id}`, { }) + return webapi.Delete(`/role/${role_id}`, {}) } export interface PostOpts { @@ -36,6 +36,6 @@ export interface ListOpts { name?: string } export function List(json: ListOpts) { - return webapi.Get<[models.Role]>(`/role`, { json }) + return webapi.Get(`/role`, { json }) } diff --git a/oaweb/composables/api/token.ts b/oaweb/composables/api/token.ts index e787090..9c6c632 100644 --- a/oaweb/composables/api/token.ts +++ b/oaweb/composables/api/token.ts @@ -50,6 +50,6 @@ export interface ListOpts { app_id: string } export function List(json: ListOpts) { - return webapi.Get<[models.Token]>(`/token`, { json }) + return webapi.Get(`/token`, { json }) } diff --git a/oaweb/composables/api/user.ts b/oaweb/composables/api/user.ts index d672772..d598af7 100644 --- a/oaweb/composables/api/user.ts +++ b/oaweb/composables/api/user.ts @@ -7,7 +7,7 @@ import webapi from "./webapi" import * as models from "./models" export function Get(user_id: string) { - return webapi.Get(`/user/${user_id}`, { }) + return webapi.Get(`/user/${user_id}`, {}) } export interface PatchOpts { @@ -23,7 +23,7 @@ export function Patch(user_id: string, json: PatchOpts) { } export function Delete(user_id: string) { - return webapi.Delete(`/user/${user_id}`, { }) + return webapi.Delete(`/user/${user_id}`, {}) } export interface PostOpts { @@ -47,11 +47,11 @@ export interface ListOpts { status?: number } export function List(json: ListOpts) { - return webapi.Get<[models.User]>(`/user`, { json }) + return webapi.Get(`/user`, { json }) } export function UserRoleGet(user_role_id: string, user_id: string) { - return webapi.Get(`/user/${user_id}/user_role/${user_role_id}`, { }) + return webapi.Get(`/user/${user_id}/user_role/${user_role_id}`, {}) } export interface UserRolePatchOpts { @@ -82,6 +82,6 @@ export interface UserRoleListOpts { status?: string } export function UserRoleList(user_id: string, json: UserRoleListOpts) { - return webapi.Get<[models.UserRole]>(`/user/${user_id}/user_role`, { json }) + return webapi.Get(`/user/${user_id}/user_role`, { json }) } diff --git a/oaweb/composables/api/webapi.ts b/oaweb/composables/api/webapi.ts index 6e7c925..3f0c1c1 100644 --- a/oaweb/composables/api/webapi.ts +++ b/oaweb/composables/api/webapi.ts @@ -1,6 +1,6 @@ // // Copyright (C) 2024 veypi -// 2024-10-25 18:42:03 +// 2024-10-28 17:43:57 // Distributed under terms of the MIT license. // @@ -22,14 +22,45 @@ const proxy = axios.create({ }, }); - +export const token = { + value: '', + update: () => { + return new Promise((resolve) => { resolve(token.value) }) + }, + set_updator: (fn: () => Promise) => { + let locked = false + token.update = () => { + if (locked) { + return new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 1000) + }).then(() => { + return token.update() + }) + } + locked = true + return new Promise((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) => { - // 设置 token - const token = util.getToken() config.retryTimes = 3 // NOTE 添加自定义头部 - token && (config.headers.Authorization = `Bearer ${token}`) + token.value && (config.headers.Authorization = `Bearer ${token.value}`) // config.headers['auth_token'] = '' return config } @@ -64,12 +95,23 @@ const responseFailed = (error: AxiosError) => { needRetry = false } else if (response?.status == 401) { 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) { return Promise.reject(data || response) }; + return requestRetry(1000, response!) +} + +const requestRetry = (delay = 0, response: AxiosResponse) => { + const config = response?.config // @ts-ignore const { __retryCount = 0, retryDelay = 1000, retryTimes } = config; // 在请求对象上设置重试次数 @@ -79,18 +121,20 @@ const responseFailed = (error: AxiosError) => { if (__retryCount >= retryTimes) { return Promise.reject(response?.data || response?.headers.error) } + if (delay <= 0) { + return proxy.request(config as any) + } // 延时处理 - const delay = new Promise((resolve) => { + return new Promise((resolve) => { setTimeout(() => { resolve(); - }, retryDelay); - }); - // 重新发起请求 - return delay.then(function() { - return proxy.request(config as any); - }); + }, delay) + }).then(() => { + return proxy.request(config as any) + }) } + proxy.interceptors.response.use(responseSuccess, responseFailed) interface data {