diff --git a/src/api/axios.ts b/src/api/axios.ts index 4c218427..2d44a6d4 100644 --- a/src/api/axios.ts +++ b/src/api/axios.ts @@ -1,4 +1,4 @@ -import axios, { AxiosResponse, AxiosRequestConfig } from 'axios' +import axios, { AxiosResponse, AxiosRequestConfig, Axios } from 'axios' import { ResultEnum, ModuleTypeEnum } from "@/enums/httpEnum" import { PageEnum, ErrorPageNameMap } from "@/enums/pageEnum" import { StorageEnum } from '@/enums/storageEnum' @@ -8,10 +8,20 @@ import { redirectErrorPage, getLocalStorage, routerTurnByName, isPreview } from import { fetchAllowList } from './axios.config' import includes from 'lodash/includes' +export interface MyResponseType { + code: ResultEnum + data: any + message: string +} + +export interface MyRequestInstance extends Axios { + (config: AxiosRequestConfig): Promise +} + const axiosInstance = axios.create({ baseURL: `${import.meta.env.PROD ? import.meta.env.VITE_PRO_PATH : ''}${axiosPre}`, timeout: ResultEnum.TIMEOUT, -}) +}) as unknown as MyRequestInstance axiosInstance.interceptors.request.use( (config: AxiosRequestConfig) => { diff --git a/src/hooks/useSystemInit.hook.ts b/src/hooks/useSystemInit.hook.ts index 54b61e6b..ee7dc7e1 100644 --- a/src/hooks/useSystemInit.hook.ts +++ b/src/hooks/useSystemInit.hook.ts @@ -10,8 +10,8 @@ export const useSystemInit = async () => { // 获取 OSS 信息的 url 地址,用来拼接展示图片的地址 const getOssUrl = async () => { - const res = await ossUrlApi({}) as unknown as MyResponseType - if (res.code === ResultEnum.SUCCESS) { + const res = await ossUrlApi({}) + if (res && res.code === ResultEnum.SUCCESS) { systemStore.setItem(SystemStoreEnum.FETCH_INFO, { OSSUrl: res.data?.bucketURL }) diff --git a/src/utils/router.ts b/src/utils/router.ts index 5f104f56..7c04b96f 100644 --- a/src/utils/router.ts +++ b/src/utils/router.ts @@ -106,8 +106,8 @@ export const reloadRoutePage = () => { */ export const logout = async () => { try { - const res = await logoutApi() as unknown as MyResponseType - if(res.code === ResultEnum.SUCCESS) { + const res = await logoutApi() + if(res && res.code === ResultEnum.SUCCESS) { window['$message'].success(window['$t']('global.logout_success')) clearCookie(RequestHttpHeaderEnum.COOKIE) clearLocalStorage(StorageEnum.GO_SYSTEM_STORE) diff --git a/src/views/chart/ContentConfigurations/components/CanvasPage/index.vue b/src/views/chart/ContentConfigurations/components/CanvasPage/index.vue index 908cc6b0..084b33ee 100644 --- a/src/views/chart/ContentConfigurations/components/CanvasPage/index.vue +++ b/src/views/chart/ContentConfigurations/components/CanvasPage/index.vue @@ -282,9 +282,9 @@ const customRequest = (options: UploadCustomRequestOptions) => { ) let uploadParams = new FormData() uploadParams.append('object', newNameFile) - const uploadRes = await uploadFile(uploadParams) as unknown as MyResponseType + const uploadRes = await uploadFile(uploadParams) - if(uploadRes.code === ResultEnum.SUCCESS) { + if(uploadRes && uploadRes.code === ResultEnum.SUCCESS) { chartEditStore.setEditCanvasConfig( EditCanvasConfigEnum.BACKGROUND_IMAGE, `${systemStore.getFetchInfo.OSSUrl}${uploadRes.data.fileName}?time=${new Date().getTime()}` diff --git a/src/views/chart/ContentHeader/headerRightBtn/index.vue b/src/views/chart/ContentHeader/headerRightBtn/index.vue index a24d2da2..635bc64e 100644 --- a/src/views/chart/ContentHeader/headerRightBtn/index.vue +++ b/src/views/chart/ContentHeader/headerRightBtn/index.vue @@ -152,9 +152,9 @@ const sendHandle = async () => { id: fetchRouteParamsLocation(), // 反过来 state: release.value ? -1 : 1, - })) as unknown as MyResponseType + })) - if (res.code === ResultEnum.SUCCESS) { + if (res && res.code === ResultEnum.SUCCESS) { modelShowHandle() if (!release.value) { copyPreviewPath('发布成功!已复制地址到剪贴板~', '发布成功!') diff --git a/src/views/chart/ContentHeader/headerTitle/index.vue b/src/views/chart/ContentHeader/headerTitle/index.vue index a0b056e3..9629a91b 100644 --- a/src/views/chart/ContentHeader/headerTitle/index.vue +++ b/src/views/chart/ContentHeader/headerTitle/index.vue @@ -67,8 +67,8 @@ const handleBlur = async () => { const res = (await updateProjectApi({ id: fetchRouteParamsLocation(), projectName: title.value - })) as unknown as MyResponseType - if (res.code === ResultEnum.SUCCESS) { + })) + if (res && res.code === ResultEnum.SUCCESS) { dataSyncUpdate() } else { httpErrorHandle() diff --git a/src/views/chart/hooks/useSync.hook.ts b/src/views/chart/hooks/useSync.hook.ts index 4372bf21..8db46c82 100644 --- a/src/views/chart/hooks/useSync.hook.ts +++ b/src/views/chart/hooks/useSync.hook.ts @@ -229,8 +229,8 @@ export const useSync = () => { chartEditStore.componentList = [] chartEditStore.setEditCanvas(EditCanvasTypeEnum.SAVE_STATUS, SyncEnum.START) try { - const res = await fetchProjectApi({ projectId: fetchRouteParamsLocation() }) as unknown as MyResponseType - if (res.code === ResultEnum.SUCCESS) { + const res = await fetchProjectApi({ projectId: fetchRouteParamsLocation() }) + if (res && res.code === ResultEnum.SUCCESS) { if (res.data) { updateStoreInfo(res.data) // 更新全局数据 @@ -278,9 +278,9 @@ export const useSync = () => { // 上传预览图 let uploadParams = new FormData() uploadParams.append('object', base64toFile(canvasImage.toDataURL(), `${fetchRouteParamsLocation()}_index_preview.png`)) - const uploadRes = await uploadFile(uploadParams) as unknown as MyResponseType + const uploadRes = await uploadFile(uploadParams) // 保存预览图 - if(uploadRes.code === ResultEnum.SUCCESS) { + if(uploadRes && uploadRes.code === ResultEnum.SUCCESS) { await updateProjectApi({ id: fetchRouteParamsLocation(), indexImage: `${systemStore.getFetchInfo.OSSUrl}${uploadRes.data.fileName}` @@ -295,9 +295,9 @@ export const useSync = () => { let params = new FormData() params.append('projectId', projectId) params.append('content', JSON.stringify(chartEditStore.getStorageInfo || {})) - const res= await saveProjectApi(params) as unknown as MyResponseType + const res= await saveProjectApi(params) - if (res.code === ResultEnum.SUCCESS) { + if (res && res.code === ResultEnum.SUCCESS) { // 成功状态 setTimeout(() => { chartEditStore.setEditCanvas(EditCanvasTypeEnum.SAVE_STATUS, SyncEnum.SUCCESS) diff --git a/src/views/login/index.vue b/src/views/login/index.vue index eec07dbf..ef95f3cb 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -207,8 +207,8 @@ const handleSubmit = async (e: Event) => { const res = await loginApi({ username, password - }) as unknown as MyResponseType - if(res.data) { + }) + if(res && res.data) { const { tokenValue, tokenName } = res.data.token const { nickname, username, id } = res.data.userinfo diff --git a/src/views/preview/utils/storage.ts b/src/views/preview/utils/storage.ts index 494315c7..f924d43a 100644 --- a/src/views/preview/utils/storage.ts +++ b/src/views/preview/utils/storage.ts @@ -18,8 +18,8 @@ export const getSessionStorageInfo = async () => { // 是否本地预览 if (!storageList || storageList.findIndex(e => e.id === id.toString()) === -1) { // 接口调用 - const res = await fetchProjectApi({ projectId: id }) as unknown as MyResponseType - if (res.code === ResultEnum.SUCCESS) { + const res = await fetchProjectApi({ projectId: id }) + if (res && res.code === ResultEnum.SUCCESS) { const { content, state } = res.data if (state === -1) { // 跳转未发布页 diff --git a/src/views/project/items/components/ProjectItemsList/hooks/useData.hook.ts b/src/views/project/items/components/ProjectItemsList/hooks/useData.hook.ts index 591e5ff1..53872321 100644 --- a/src/views/project/items/components/ProjectItemsList/hooks/useData.hook.ts +++ b/src/views/project/items/components/ProjectItemsList/hooks/useData.hook.ts @@ -90,8 +90,8 @@ export const useDataListInit = () => { id: id, // [-1未发布, 1发布] state: !release ? 1 : -1 - }) as unknown as MyResponseType - if (res.code === ResultEnum.SUCCESS) { + }) + if (res && res.code === ResultEnum.SUCCESS) { list.value = [] fetchList() // 发布 -> 未发布 diff --git a/src/views/project/layout/components/ProjectLayoutCreate/components/CreateModal/index.vue b/src/views/project/layout/components/ProjectLayoutCreate/components/CreateModal/index.vue index d1b48531..de622c4d 100644 --- a/src/views/project/layout/components/ProjectLayoutCreate/components/CreateModal/index.vue +++ b/src/views/project/layout/components/ProjectLayoutCreate/components/CreateModal/index.vue @@ -96,8 +96,8 @@ const btnHandle = async (key: string) => { remarks: null, // 图片地址 indexImage: null, - }) as unknown as MyResponseType - if(res.code === ResultEnum.SUCCESS) { + }) + if(res && res.code === ResultEnum.SUCCESS) { window['$message'].success(window['$t']('project.create_success')) const { id } = res.data