zwpanel/src/utils/request/apiMessage.ts
2024-01-15 15:45:03 +08:00

32 lines
1.1 KiB
TypeScript

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,
}))
export 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
}
}