整改API接口错误码的返回
This commit is contained in:
parent
0e414cabf4
commit
cc62f3cb60
186
src/api/index.ts
186
src/api/index.ts
@ -1,191 +1,5 @@
|
|||||||
import type { AxiosProgressEvent, GenericAbortSignal } from 'axios'
|
|
||||||
import { post } from '@/utils/request'
|
import { post } from '@/utils/request'
|
||||||
|
|
||||||
export function fetchChatAPI<T = any>(
|
|
||||||
prompt: string,
|
|
||||||
options?: { conversationId?: string; parentMessageId?: string },
|
|
||||||
signal?: GenericAbortSignal,
|
|
||||||
) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/chat',
|
|
||||||
data: { prompt, options },
|
|
||||||
signal,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function fetchChatConfig<T = any>() {
|
|
||||||
return post<T>({
|
|
||||||
url: '/config',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function fetchChatAPIProcess<T = any>(
|
|
||||||
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<string, any> = {
|
|
||||||
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<T>({
|
|
||||||
url: '/chatGpt/chatCompletion',
|
|
||||||
data,
|
|
||||||
signal: params.signal,
|
|
||||||
onDownloadProgress: params.onDownloadProgress,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function againFetchChatAPIProcess<T = any>(
|
|
||||||
params: {
|
|
||||||
aiChatDialogId: number
|
|
||||||
prompt?: string
|
|
||||||
options?: { conversationId?: string; parentMessageId?: string }
|
|
||||||
signal?: GenericAbortSignal
|
|
||||||
id?: number // 记录id
|
|
||||||
|
|
||||||
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void },
|
|
||||||
) {
|
|
||||||
const data: Record<string, any> = {
|
|
||||||
prompt: params.prompt,
|
|
||||||
options: params.options,
|
|
||||||
aiChatDialogId: params.aiChatDialogId,
|
|
||||||
id: params.id,
|
|
||||||
}
|
|
||||||
|
|
||||||
return post<T>({
|
|
||||||
url: '/chatGpt/againChatCompletion',
|
|
||||||
data,
|
|
||||||
signal: params.signal,
|
|
||||||
onDownloadProgress: params.onDownloadProgress,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function fetchSession<T>() {
|
|
||||||
return post<T>({
|
|
||||||
url: '/chatGpt/session',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function fetchVerify<T>(token: string) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/verify',
|
|
||||||
data: { token },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取对话列表
|
|
||||||
export function chatDialogGetList<T>(page: number, limit: number, keyword?: string) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatDialog/getList',
|
|
||||||
data: { page, limit, keyword },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 新建对话
|
|
||||||
export function chatDialogAdd<T>(title: string, aiRoleId: number) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatDialog/add',
|
|
||||||
data: { title, aiRoleId },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 修改
|
|
||||||
export function chatDialogUpdate<T>(aiChatDialogId: number, title: string) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatDialog/update',
|
|
||||||
data: { aiChatDialogId, title },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除对话
|
|
||||||
export function chatDialogDelete<T>(aiChatDialogId: number) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatDialog/delete',
|
|
||||||
data: { aiChatDialogId },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function chatDialogGetInfo<T>(aiChatDialogId: number) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatDialog/getInfo',
|
|
||||||
data: { aiChatDialogId },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取某对话框聊天记录
|
|
||||||
export function chatRecordGetList<T>(aiChatDialogId: number) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatRecord/getList',
|
|
||||||
data: { aiChatDialogId },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// export function chatRecordAddOne<T>(data: ChatRecord.AddOneRequest) {
|
|
||||||
// return post<T>({
|
|
||||||
// url: '/aiChatRecord/addOne',
|
|
||||||
// data,
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
|
|
||||||
export function chatRecordDelete<T>(aiChatDialogId: number, recordId: number) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatRecord/delete',
|
|
||||||
data: { aiChatDialogId, id: recordId },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function chatRoleGetSystemList<T>(data: Common.ListRequest) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatRole/getSystemList',
|
|
||||||
data,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function chatRoleGetMyCreateList<T>(data: Common.ListRequest) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatRole/getMyCreateList',
|
|
||||||
data,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function chatRoleGetInfo<T>(aiRoleId: number) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatRole/getInfo',
|
|
||||||
data: { aiRoleId },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// export function chatRoleEdit<T>(roleInfo: ChatRole.RoleInfo) {
|
|
||||||
// return post<T>({
|
|
||||||
// url: '/aiChatRole/edit',
|
|
||||||
// data: roleInfo,
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
|
|
||||||
export function chatRoleEditDeletes<T>(aiRoleIds: number[]) {
|
|
||||||
return post<T>({
|
|
||||||
url: '/aiChatRole/deletes',
|
|
||||||
data: { aiRoleIds },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 登录相关
|
// 登录相关
|
||||||
|
|
||||||
export function login<T>(data: Login.LoginReqest) {
|
export function login<T>(data: Login.LoginReqest) {
|
||||||
|
@ -97,8 +97,6 @@ const createColumns = ({
|
|||||||
setPublicVisitUser(row.id as number).then(({ code }) => {
|
setPublicVisitUser(row.id as number).then(({ code }) => {
|
||||||
if (code === 0)
|
if (code === 0)
|
||||||
publicVisitUserId.value = row.id as number
|
publicVisitUserId.value = row.id as number
|
||||||
else if (code === 1111)
|
|
||||||
message.error('用户不存在,请刷新后再试')
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
@ -19,6 +19,19 @@
|
|||||||
"api": {
|
"api": {
|
||||||
"loginExpires": "Login status has expired, please login again"
|
"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": {
|
"appLauncher": {
|
||||||
"title": "System Applications & Settings"
|
"title": "System Applications & Settings"
|
||||||
},
|
},
|
||||||
@ -140,6 +153,7 @@
|
|||||||
"inputPlaceholderByText": "Please enter {text}",
|
"inputPlaceholderByText": "Please enter {text}",
|
||||||
"language": "Language",
|
"language": "Language",
|
||||||
"leastOne": "Please keep at least one",
|
"leastOne": "Please keep at least one",
|
||||||
|
"networkError": "Network error, please try again later",
|
||||||
"nikeName": "Nickname",
|
"nikeName": "Nickname",
|
||||||
"no": "No",
|
"no": "No",
|
||||||
"noData": "No data available",
|
"noData": "No data available",
|
||||||
|
@ -19,6 +19,19 @@
|
|||||||
"api": {
|
"api": {
|
||||||
"loginExpires": "登录状态已过期,请重新登录"
|
"loginExpires": "登录状态已过期,请重新登录"
|
||||||
},
|
},
|
||||||
|
"apiErrorCode": {
|
||||||
|
"1000": "未登录",
|
||||||
|
"1003": "用户名或密码错误",
|
||||||
|
"1004": "账号已停用或未激活",
|
||||||
|
"1005": "当前无权限操作",
|
||||||
|
"1006": "账号不存在",
|
||||||
|
"1200": "数据库出错",
|
||||||
|
"1201": "请至少保留一个",
|
||||||
|
"1202": "未找到数据记录",
|
||||||
|
"1300": "上传失败",
|
||||||
|
"1301": "不被支持的文件格式",
|
||||||
|
"1400": "参数格式错误"
|
||||||
|
},
|
||||||
"appLauncher": {
|
"appLauncher": {
|
||||||
"title": "系统应用 & 设置"
|
"title": "系统应用 & 设置"
|
||||||
},
|
},
|
||||||
@ -140,6 +153,7 @@
|
|||||||
"inputPlaceholderByText": "请输入{text}",
|
"inputPlaceholderByText": "请输入{text}",
|
||||||
"language": "语言",
|
"language": "语言",
|
||||||
"leastOne": "请至少保留一项",
|
"leastOne": "请至少保留一项",
|
||||||
|
"networkError": "网络错误,请稍后重试",
|
||||||
"nikeName": "昵称",
|
"nikeName": "昵称",
|
||||||
"no": "否",
|
"no": "否",
|
||||||
"noData": "暂无数据",
|
"noData": "暂无数据",
|
||||||
|
@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
|
|||||||
import type { AppState, Language, Theme } from './helper'
|
import type { AppState, Language, Theme } from './helper'
|
||||||
import { defaultSetting, getLocalSetting, removeLocalState, setLocalSetting } from './helper'
|
import { defaultSetting, getLocalSetting, removeLocalState, setLocalSetting } from './helper'
|
||||||
import { store } from '@/store'
|
import { store } from '@/store'
|
||||||
|
import { useTheme } from '@/hooks/useTheme'
|
||||||
|
|
||||||
export const useAppStore = defineStore('app-store', {
|
export const useAppStore = defineStore('app-store', {
|
||||||
state: (): AppState => getLocalSetting(),
|
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() {
|
recordState() {
|
||||||
setLocalSetting(this.$state)
|
setLocalSetting(this.$state)
|
||||||
},
|
},
|
||||||
|
@ -11,8 +11,7 @@ const noticeStore = useNoticeStore()
|
|||||||
const userStore = useUserStore()
|
const userStore = useUserStore()
|
||||||
const authStore = useAuthStore()
|
const authStore = useAuthStore()
|
||||||
|
|
||||||
const { notification } = createDiscreteApi(['notification'])
|
const { notification, message } = createDiscreteApi(['notification', 'message'])
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成指定时间格式
|
* 生成指定时间格式
|
||||||
* @param format 时间格式 默认:'YYYY-MM-DD HH:mm:ss'
|
* @param format 时间格式 默认:'YYYY-MM-DD HH:mm:ss'
|
||||||
|
31
src/utils/request/apiMessage.ts
Normal file
31
src/utils/request/apiMessage.ts
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,10 @@
|
|||||||
import type { AxiosProgressEvent, AxiosResponse, GenericAbortSignal } from 'axios'
|
import type { AxiosProgressEvent, AxiosResponse, GenericAbortSignal } from 'axios'
|
||||||
import { createDiscreteApi } from 'naive-ui'
|
|
||||||
import request from './axios'
|
import request from './axios'
|
||||||
|
import { apiRespErrMsg } from './apiMessage'
|
||||||
import { t } from '@/locales'
|
import { t } from '@/locales'
|
||||||
import { useAppStore, useAuthStore } from '@/store'
|
import { useAppStore, useAuthStore } from '@/store'
|
||||||
import { router } from '@/router'
|
import { router } from '@/router'
|
||||||
|
|
||||||
const { message } = createDiscreteApi(['message'])
|
|
||||||
let loginMessageShow = false
|
let loginMessageShow = false
|
||||||
export interface HttpOption {
|
export interface HttpOption {
|
||||||
url: string
|
url: string
|
||||||
@ -32,7 +31,7 @@ function http<T = any>(
|
|||||||
const authStore = useAuthStore()
|
const authStore = useAuthStore()
|
||||||
const appStore = useAppStore()
|
const appStore = useAppStore()
|
||||||
const successHandler = (res: AxiosResponse<Response<T>>) => {
|
const successHandler = (res: AxiosResponse<Response<T>>) => {
|
||||||
if (res.data.code === 0 || typeof res.data === 'string')
|
if (res.data.code === 0)
|
||||||
return res.data
|
return res.data
|
||||||
|
|
||||||
if (res.data.code === 1001) {
|
if (res.data.code === 1001) {
|
||||||
@ -64,25 +63,24 @@ function http<T = any>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (res.data.code === -1) {
|
if (res.data.code === -1) {
|
||||||
message.warning(res.data.msg)
|
// message.warning(res.data.msg)
|
||||||
// router.push({ path: '/login' })
|
// router.push({ path: '/login' })
|
||||||
// authStore.removeToken()
|
// authStore.removeToken()
|
||||||
return res.data
|
return res.data
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证码相关错误
|
if (!apiRespErrMsg(res.data))
|
||||||
if (res.data.code > 1100 && res.data.code < 1200)
|
return Promise.reject(res.data)
|
||||||
|
else
|
||||||
return res.data
|
return res.data
|
||||||
|
|
||||||
return Promise.reject(res.data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const failHandler = (error: Response<Error>) => {
|
const failHandler = (error: Response<Error>) => {
|
||||||
afterRequest?.()
|
afterRequest?.()
|
||||||
// message.error('网络错误,请稍后重试', {
|
message.error(t('common.networkError'), {
|
||||||
// duration: 50000,
|
duration: 50000,
|
||||||
// closable: true,
|
closable: true,
|
||||||
// })
|
})
|
||||||
throw new Error(error?.msg || 'Error')
|
throw new Error(error?.msg || 'Error')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,8 +19,6 @@ const languageValue = ref<Language>(appStore.language)
|
|||||||
// const isShowCaptcha = ref<boolean>(false)
|
// const isShowCaptcha = ref<boolean>(false)
|
||||||
// const isShowRegister = ref<boolean>(false)
|
// const isShowRegister = ref<boolean>(false)
|
||||||
|
|
||||||
const captchaRef = ref()
|
|
||||||
|
|
||||||
const form = ref<Login.LoginReqest>({
|
const form = ref<Login.LoginReqest>({
|
||||||
username: '',
|
username: '',
|
||||||
password: '',
|
password: '',
|
||||||
@ -28,21 +26,27 @@ const form = ref<Login.LoginReqest>({
|
|||||||
|
|
||||||
const loginPost = async () => {
|
const loginPost = async () => {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
const res = await login<Login.LoginResponse>(form.value)
|
try {
|
||||||
|
const res = await login<Login.LoginResponse>(form.value)
|
||||||
|
if (res.code === 0) {
|
||||||
|
authStore.setToken(res.data.token)
|
||||||
|
authStore.setUserInfo(res.data)
|
||||||
|
|
||||||
if (res.code === 0) {
|
setTimeout(() => {
|
||||||
authStore.setToken(res.data.token)
|
ms.success(`Hi ${res.data.name},${t('login.welcomeMessage')}`)
|
||||||
authStore.setUserInfo(res.data)
|
loading.value = false
|
||||||
|
router.push({ path: '/' })
|
||||||
setTimeout(() => {
|
}, 500)
|
||||||
ms.success(`Hi ${res.data.name},${t('login.welcomeMessage')}`)
|
}
|
||||||
|
else {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
router.push({ path: '/' })
|
// captchaRef.value.refresh()
|
||||||
}, 500)
|
}
|
||||||
}
|
}
|
||||||
else {
|
catch (error) {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
captchaRef.value.refresh()
|
// 请检查网络或者服务器错误
|
||||||
|
console.log(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user