diff --git a/src/api/index.ts b/src/api/index.ts index e633c87..a3ad168 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,191 +1,5 @@ -import type { AxiosProgressEvent, GenericAbortSignal } from 'axios' import { post } from '@/utils/request' -export function fetchChatAPI( - prompt: string, - options?: { conversationId?: string; parentMessageId?: string }, - signal?: GenericAbortSignal, -) { - return post({ - url: '/chat', - data: { prompt, options }, - signal, - }) -} - -export function fetchChatConfig() { - return post({ - url: '/config', - }) -} - -export function fetchChatAPIProcess( - params: { - aiChatDialogId: number - prompt: string - options?: { conversationId?: string; parentMessageId?: string } - signal?: GenericAbortSignal - - onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void }, -) { - // const settingStore = useSettingStore() - // const authStore = useAuthStore() - const data: Record = { - prompt: params.prompt, - options: params.options, - aiChatDialogId: params.aiChatDialogId, - } - - // if (authStore.isChatGPTAPI) { - // data = { - // ...data, - // systemMessage: settingStore.systemMessage, - // temperature: settingStore.temperature, - // top_p: settingStore.top_p, - // } - // } - return post({ - url: '/chatGpt/chatCompletion', - data, - signal: params.signal, - onDownloadProgress: params.onDownloadProgress, - }) -} - -export function againFetchChatAPIProcess( - params: { - aiChatDialogId: number - prompt?: string - options?: { conversationId?: string; parentMessageId?: string } - signal?: GenericAbortSignal - id?: number // 记录id - - onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void }, -) { - const data: Record = { - prompt: params.prompt, - options: params.options, - aiChatDialogId: params.aiChatDialogId, - id: params.id, - } - - return post({ - url: '/chatGpt/againChatCompletion', - data, - signal: params.signal, - onDownloadProgress: params.onDownloadProgress, - }) -} - -export function fetchSession() { - return post({ - url: '/chatGpt/session', - }) -} - -export function fetchVerify(token: string) { - return post({ - url: '/verify', - data: { token }, - }) -} - -// 获取对话列表 -export function chatDialogGetList(page: number, limit: number, keyword?: string) { - return post({ - url: '/aiChatDialog/getList', - data: { page, limit, keyword }, - }) -} - -// 新建对话 -export function chatDialogAdd(title: string, aiRoleId: number) { - return post({ - url: '/aiChatDialog/add', - data: { title, aiRoleId }, - }) -} - -// 修改 -export function chatDialogUpdate(aiChatDialogId: number, title: string) { - return post({ - url: '/aiChatDialog/update', - data: { aiChatDialogId, title }, - }) -} - -// 删除对话 -export function chatDialogDelete(aiChatDialogId: number) { - return post({ - url: '/aiChatDialog/delete', - data: { aiChatDialogId }, - }) -} - -export function chatDialogGetInfo(aiChatDialogId: number) { - return post({ - url: '/aiChatDialog/getInfo', - data: { aiChatDialogId }, - }) -} - -// 获取某对话框聊天记录 -export function chatRecordGetList(aiChatDialogId: number) { - return post({ - url: '/aiChatRecord/getList', - data: { aiChatDialogId }, - }) -} - -// export function chatRecordAddOne(data: ChatRecord.AddOneRequest) { -// return post({ -// url: '/aiChatRecord/addOne', -// data, -// }) -// } - -export function chatRecordDelete(aiChatDialogId: number, recordId: number) { - return post({ - url: '/aiChatRecord/delete', - data: { aiChatDialogId, id: recordId }, - }) -} - -export function chatRoleGetSystemList(data: Common.ListRequest) { - return post({ - url: '/aiChatRole/getSystemList', - data, - }) -} - -export function chatRoleGetMyCreateList(data: Common.ListRequest) { - return post({ - url: '/aiChatRole/getMyCreateList', - data, - }) -} - -export function chatRoleGetInfo(aiRoleId: number) { - return post({ - url: '/aiChatRole/getInfo', - data: { aiRoleId }, - }) -} - -// export function chatRoleEdit(roleInfo: ChatRole.RoleInfo) { -// return post({ -// url: '/aiChatRole/edit', -// data: roleInfo, -// }) -// } - -export function chatRoleEditDeletes(aiRoleIds: number[]) { - return post({ - url: '/aiChatRole/deletes', - data: { aiRoleIds }, - }) -} - // 登录相关 export function login(data: Login.LoginReqest) { diff --git a/src/components/apps/Users/index.vue b/src/components/apps/Users/index.vue index 015f6ef..35ae0fb 100644 --- a/src/components/apps/Users/index.vue +++ b/src/components/apps/Users/index.vue @@ -97,8 +97,6 @@ const createColumns = ({ setPublicVisitUser(row.id as number).then(({ code }) => { if (code === 0) publicVisitUserId.value = row.id as number - else if (code === 1111) - message.error('用户不存在,请刷新后再试') }) } break diff --git a/src/locales/en-US.json b/src/locales/en-US.json index e2da894..6877a02 100644 --- a/src/locales/en-US.json +++ b/src/locales/en-US.json @@ -19,6 +19,19 @@ "api": { "loginExpires": "Login status has expired, please login again" }, + "apiErrorCode": { + "1000": "Not logged in", + "1003": "wrong user name or password", + "1004": "Account has been deactivated or inactivated", + "1005": "Currently no permission to operate", + "1006": "Account does not exist", + "1200": "database error", + "1201": "Please keep at least one", + "1202": "Data record not found", + "1300": "Upload failed", + "1301": "Unsupported file format", + "1400": "Parameter format error" + }, "appLauncher": { "title": "System Applications & Settings" }, @@ -140,6 +153,7 @@ "inputPlaceholderByText": "Please enter {text}", "language": "Language", "leastOne": "Please keep at least one", + "networkError": "Network error, please try again later", "nikeName": "Nickname", "no": "No", "noData": "No data available", diff --git a/src/locales/zh-CN.json b/src/locales/zh-CN.json index 814e430..f101652 100644 --- a/src/locales/zh-CN.json +++ b/src/locales/zh-CN.json @@ -19,6 +19,19 @@ "api": { "loginExpires": "登录状态已过期,请重新登录" }, + "apiErrorCode": { + "1000": "未登录", + "1003": "用户名或密码错误", + "1004": "账号已停用或未激活", + "1005": "当前无权限操作", + "1006": "账号不存在", + "1200": "数据库出错", + "1201": "请至少保留一个", + "1202": "未找到数据记录", + "1300": "上传失败", + "1301": "不被支持的文件格式", + "1400": "参数格式错误" + }, "appLauncher": { "title": "系统应用 & 设置" }, @@ -140,6 +153,7 @@ "inputPlaceholderByText": "请输入{text}", "language": "语言", "leastOne": "请至少保留一项", + "networkError": "网络错误,请稍后重试", "nikeName": "昵称", "no": "否", "noData": "暂无数据", diff --git a/src/store/modules/app/index.ts b/src/store/modules/app/index.ts index 4efbdfd..87e4a2b 100644 --- a/src/store/modules/app/index.ts +++ b/src/store/modules/app/index.ts @@ -2,6 +2,7 @@ import { defineStore } from 'pinia' import type { AppState, Language, Theme } from './helper' import { defaultSetting, getLocalSetting, removeLocalState, setLocalSetting } from './helper' import { store } from '@/store' +import { useTheme } from '@/hooks/useTheme' export const useAppStore = defineStore('app-store', { state: (): AppState => getLocalSetting(), @@ -23,6 +24,22 @@ export const useAppStore = defineStore('app-store', { } }, + getTheme() { + const { theme } = useTheme() + return theme + + // const appStore = useAppStore() + // console.log('主题', appStore.theme) + // if (appStore.theme === 'auto') + // return (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) ? darkTheme : lightTheme + + // else if (appStore.theme === 'light') + // return lightTheme + + // else + // return darkTheme + }, + recordState() { setLocalSetting(this.$state) }, diff --git a/src/utils/cmn/index.ts b/src/utils/cmn/index.ts index bd6d259..516a15c 100644 --- a/src/utils/cmn/index.ts +++ b/src/utils/cmn/index.ts @@ -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' diff --git a/src/utils/request/apiMessage.ts b/src/utils/request/apiMessage.ts new file mode 100644 index 0000000..543c00c --- /dev/null +++ b/src/utils/request/apiMessage.ts @@ -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(() => ({ + 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 + } +} diff --git a/src/utils/request/index.ts b/src/utils/request/index.ts index 5a61c17..989fc17 100644 --- a/src/utils/request/index.ts +++ b/src/utils/request/index.ts @@ -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( const authStore = useAuthStore() const appStore = useAppStore() const successHandler = (res: AxiosResponse>) => { - 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( } 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) => { afterRequest?.() - // message.error('网络错误,请稍后重试', { - // duration: 50000, - // closable: true, - // }) + message.error(t('common.networkError'), { + duration: 50000, + closable: true, + }) throw new Error(error?.msg || 'Error') } diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 9e71269..675abd3 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -19,8 +19,6 @@ const languageValue = ref(appStore.language) // const isShowCaptcha = ref(false) // const isShowRegister = ref(false) -const captchaRef = ref() - const form = ref({ username: '', password: '', @@ -28,21 +26,27 @@ const form = ref({ const loginPost = async () => { loading.value = true - const res = await login(form.value) + try { + const res = await login(form.value) + if (res.code === 0) { + authStore.setToken(res.data.token) + authStore.setUserInfo(res.data) - if (res.code === 0) { - authStore.setToken(res.data.token) - authStore.setUserInfo(res.data) - - setTimeout(() => { - ms.success(`Hi ${res.data.name},${t('login.welcomeMessage')}`) + setTimeout(() => { + ms.success(`Hi ${res.data.name},${t('login.welcomeMessage')}`) + loading.value = false + router.push({ path: '/' }) + }, 500) + } + else { loading.value = false - router.push({ path: '/' }) - }, 500) + // captchaRef.value.refresh() + } } - else { + catch (error) { loading.value = false - captchaRef.value.refresh() + // 请检查网络或者服务器错误 + console.log(error) } }