整改API接口错误码的返回
This commit is contained in:
@@ -11,8 +11,7 @@ const noticeStore = useNoticeStore()
|
||||
const userStore = useUserStore()
|
||||
const authStore = useAuthStore()
|
||||
|
||||
const { notification } = createDiscreteApi(['notification'])
|
||||
|
||||
const { notification, message } = createDiscreteApi(['notification', 'message'])
|
||||
/**
|
||||
* 生成指定时间格式
|
||||
* @param format 时间格式 默认:'YYYY-MM-DD HH:mm:ss'
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { ConfigProviderProps } from 'naive-ui'
|
||||
import { createDiscreteApi, darkTheme, lightTheme, useOsTheme } from 'naive-ui'
|
||||
import { computed, ref } from 'vue'
|
||||
import type { Response } from './index'
|
||||
import { t } from '@/locales'
|
||||
import { useAppStore } from '@/store'
|
||||
|
||||
const themeRef = ref<'light' | 'dark'>('light')
|
||||
const configProviderPropsRef = computed<ConfigProviderProps>(() => ({
|
||||
theme: themeRef.value === 'light' ? lightTheme : darkTheme,
|
||||
}))
|
||||
const { message } = createDiscreteApi(['message'], { configProviderProps: configProviderPropsRef })
|
||||
|
||||
export function apiRespErrMsg(res: Response): boolean {
|
||||
const appStore = useAppStore()
|
||||
const osTheme = useOsTheme()
|
||||
if (appStore.theme === 'auto')
|
||||
themeRef.value = osTheme.value as 'dark' | 'light'
|
||||
else
|
||||
themeRef.value = appStore.theme as 'dark' | 'light'
|
||||
|
||||
const apiErrorCodeName = `apiErrorCode.${res.code}`
|
||||
const getI18nValue = t(apiErrorCodeName)
|
||||
if (apiErrorCodeName === getI18nValue) {
|
||||
return false
|
||||
}
|
||||
else {
|
||||
message.error(t(`apiErrorCode.${res.code}`))
|
||||
return true
|
||||
}
|
||||
}
|
||||
+10
-12
@@ -1,11 +1,10 @@
|
||||
import type { AxiosProgressEvent, AxiosResponse, GenericAbortSignal } from 'axios'
|
||||
import { createDiscreteApi } from 'naive-ui'
|
||||
import request from './axios'
|
||||
import { apiRespErrMsg } from './apiMessage'
|
||||
import { t } from '@/locales'
|
||||
import { useAppStore, useAuthStore } from '@/store'
|
||||
import { router } from '@/router'
|
||||
|
||||
const { message } = createDiscreteApi(['message'])
|
||||
let loginMessageShow = false
|
||||
export interface HttpOption {
|
||||
url: string
|
||||
@@ -32,7 +31,7 @@ function http<T = any>(
|
||||
const authStore = useAuthStore()
|
||||
const appStore = useAppStore()
|
||||
const successHandler = (res: AxiosResponse<Response<T>>) => {
|
||||
if (res.data.code === 0 || typeof res.data === 'string')
|
||||
if (res.data.code === 0)
|
||||
return res.data
|
||||
|
||||
if (res.data.code === 1001) {
|
||||
@@ -64,25 +63,24 @@ function http<T = any>(
|
||||
}
|
||||
|
||||
if (res.data.code === -1) {
|
||||
message.warning(res.data.msg)
|
||||
// message.warning(res.data.msg)
|
||||
// router.push({ path: '/login' })
|
||||
// authStore.removeToken()
|
||||
return res.data
|
||||
}
|
||||
|
||||
// 验证码相关错误
|
||||
if (res.data.code > 1100 && res.data.code < 1200)
|
||||
if (!apiRespErrMsg(res.data))
|
||||
return Promise.reject(res.data)
|
||||
else
|
||||
return res.data
|
||||
|
||||
return Promise.reject(res.data)
|
||||
}
|
||||
|
||||
const failHandler = (error: Response<Error>) => {
|
||||
afterRequest?.()
|
||||
// message.error('网络错误,请稍后重试', {
|
||||
// duration: 50000,
|
||||
// closable: true,
|
||||
// })
|
||||
message.error(t('common.networkError'), {
|
||||
duration: 50000,
|
||||
closable: true,
|
||||
})
|
||||
throw new Error(error?.msg || 'Error')
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user