From d46cfd81802c71e0a783dbcf4e8f58d4078e1ded Mon Sep 17 00:00:00 2001 From: xxm Date: Tue, 11 Oct 2022 17:34:45 +0800 Subject: [PATCH] =?UTF-8?q?ref=20=E8=B0=83=E6=95=B4=E8=A1=A8=E5=8D=95?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=92=8Caxios=E5=8F=82=E6=95=B0=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/registerGlobComp.ts | 4 ++ src/enums/formTypeEnum.ts | 4 +- src/hooks/bootx/useFormEdit.ts | 18 +++---- src/utils/http/axios/index.ts | 37 +++++++------- src/views/modules/system/client/Client.api.ts | 40 ++++++++++++++- .../modules/system/client/ClientEdit.vue | 36 ++++++++----- .../modules/system/client/ClientList.vue | 50 ++++++++++++++++--- types/axios.d.ts | 3 +- 8 files changed, 137 insertions(+), 55 deletions(-) diff --git a/src/components/registerGlobComp.ts b/src/components/registerGlobComp.ts index 24ec894ec..960f603f2 100644 --- a/src/components/registerGlobComp.ts +++ b/src/components/registerGlobComp.ts @@ -5,7 +5,9 @@ import { Input, InputNumber, Empty, + Popconfirm, Select, + SelectOption, Switch, Tree, TreeSelect, @@ -45,7 +47,9 @@ export function registerGlobComp(app: App) { app.use(DatePicker) app.use(TimePicker) app.use(Empty) + app.use(Popconfirm) app.use(Select) + app.use(SelectOption) app.use(Switch) app.use(Tree) app.use(TreeSelect) diff --git a/src/enums/formTypeEnum.ts b/src/enums/formTypeEnum.ts index 5fe213332..6ebc9cf51 100644 --- a/src/enums/formTypeEnum.ts +++ b/src/enums/formTypeEnum.ts @@ -1,7 +1,7 @@ /** - * 表单类型 + * 表单编辑类型 */ -export enum FormType { +export enum FormEditType { Add, Edit, Show, diff --git a/src/hooks/bootx/useFormEdit.ts b/src/hooks/bootx/useFormEdit.ts index c16bbf20b..5f73f82fe 100644 --- a/src/hooks/bootx/useFormEdit.ts +++ b/src/hooks/bootx/useFormEdit.ts @@ -1,5 +1,5 @@ import { reactive, toRefs } from 'vue' -import { FormType } from "/@/enums/formTypeEnum"; +import { FormEditType } from '/@/enums/formTypeEnum' export default function () { const model = reactive({ @@ -18,23 +18,23 @@ export default function () { editable: false, addable: false, showable: false, - type: FormType.Add, + formEditType: FormEditType.Add, }) // 状态 - const { labelCol, wrapperCol, title, modalWidth, confirmLoading, visible, editable, addable, showable, type } = toRefs(model) + const { labelCol, wrapperCol, title, modalWidth, confirmLoading, visible, editable, addable, showable, formEditType } = toRefs(model) - function initFormModel(record, fromType: FormType, ...vars) { - type.value = fromType + function initFormModel(record, editType: FormEditType, ...vars) { + formEditType.value = editType visible.value = true - if (type.value === FormType.Add) { + if (formEditType.value === FormEditType.Add) { addable.value = true title.value = '新增' } - if (type.value === FormType.Edit) { + if (formEditType.value === FormEditType.Edit) { editable.value = true title.value = '修改' } - if (type.value === FormType.Show) { + if (formEditType.value === FormEditType.Show) { showable.value = true title.value = '查看' } @@ -64,7 +64,7 @@ export default function () { editable, addable, showable, - type, + formEditType, initFormModel, handleCancel, search, diff --git a/src/utils/http/axios/index.ts b/src/utils/http/axios/index.ts index 0f8d55454..cdfc68eba 100644 --- a/src/utils/http/axios/index.ts +++ b/src/utils/http/axios/index.ts @@ -33,29 +33,26 @@ const transform: AxiosTransform = { transformResponseHook: (res: AxiosResponse, options: RequestOptions) => { const { t } = useI18n() const { isTransformResponse, isReturnNativeResponse } = options - // 是否返回原生响应头 比如:需要获取响应头时使用该属性 - if (isReturnNativeResponse) { - return res - } + // 不进行任何处理,直接返回 // 用于页面代码可能需要直接获取code,data,message这些信息时开启 - if (!isTransformResponse) { - return res.data - } + // if (!isTransformResponse) { + // return res.data + // } // 错误的时候返回 // 获取请求头重的数据 const rawData = res.data if (!rawData) { // return '[HTTP] Request has no return value'; - throw new Error(t('sys.api.apiRequestFailed')) + throw new Error('请求出错,请稍候重试') } // 这里 code,data,message为 后台统一的字段,需要在 types.ts内修改为项目自己的接口返回格式 - const { code, data, message } = rawData + const { code, msg, traceId } = rawData // 这里逻辑可以根据项目进行修改 const hasSuccess = rawData && Reflect.has(rawData, 'code') && code === ResultEnum.SUCCESS if (hasSuccess) { - return data + return rawData } // 在此处根据自己项目的实际情况对不同的code执行不同的操作 @@ -63,14 +60,14 @@ const transform: AxiosTransform = { let timeoutMsg = '' switch (code) { case ResultEnum.TIMEOUT: - timeoutMsg = t('sys.api.timeoutMessage') + timeoutMsg = '登录超时,请重新登录!' const userStore = useUserStoreWithOut() userStore.setToken(undefined) userStore.logout(true) break default: - if (message) { - timeoutMsg = message + if (msg) { + timeoutMsg = msg } } @@ -81,8 +78,8 @@ const transform: AxiosTransform = { } else if (options.errorMessageMode === 'message') { createMessage.error(timeoutMsg) } - - throw new Error(timeoutMsg || t('sys.api.apiRequestFailed')) + console.error('TraceId:', traceId) + throw new Error(timeoutMsg || '请求出错,请稍候重试') }, // 请求之前处理config @@ -115,9 +112,9 @@ const transform: AxiosTransform = { config.data = data config.params = params } else { - // 非GET请求如果没有提供data,则将params视为data - config.data = params - config.params = undefined + // 非GET请求如果没有提供data,则将params视为data (去除, 非GET也会进行普通参数请求) + // config.data = params + // config.params = undefined } if (joinParamsToUrl) { config.url = setObjToUrlParams(config.url as string, Object.assign({}, config.params, config.data)) @@ -221,7 +218,7 @@ function createAxios(opt?: Partial) { joinPrefix: true, // 是否返回原生响应头 比如:需要获取响应头时使用该属性 isReturnNativeResponse: false, - // 需要对返回数据进行处理 + // 需要对返回数据进行处理 (无效) isTransformResponse: false, // post请求的时候添加参数到url joinParamsToUrl: false, @@ -240,7 +237,7 @@ function createAxios(opt?: Partial) { // 是否携带token withToken: true, retryRequest: { - isOpenRetry: true, + isOpenRetry: false, count: 5, waitTime: 100, }, diff --git a/src/views/modules/system/client/Client.api.ts b/src/views/modules/system/client/Client.api.ts index 46c258a59..ebccbde3a 100644 --- a/src/views/modules/system/client/Client.api.ts +++ b/src/views/modules/system/client/Client.api.ts @@ -25,7 +25,7 @@ export const get = (id) => { /** * 添加 */ -export const add = (obj) => { +export const add = (obj: Client) => { return defHttp.post({ url: '/client/add', data: obj, @@ -35,13 +35,49 @@ export const add = (obj) => { /** * 更新 */ -export const update = (obj) => { +export const update = (obj: Client) => { return defHttp.post({ url: '/client/update', data: obj, }) } +/** + * 删除 + */ +export const del = (id) => { + return defHttp.delete({ + url: '/client/delete', + params: { id }, + }) +} + +/** + * 查询全部 + */ +export const findAll = () => { + return defHttp.get>>({ + url: '/client/findAll', + }) +} + +/** + * 编码是否被使用 + */ +export const existsByCode = (code: string) => { + return defHttp.get>({ + url: '/client/existsByCode', + method: 'GET', + }) +} +export const existsByCodeNotId = (code: string, id: number) => { + return defHttp.get>({ + url: '/client/existsByCodeNotId', + method: 'GET', + params: { code, id }, + }) +} + /** * 实体类接口 */ diff --git a/src/views/modules/system/client/ClientEdit.vue b/src/views/modules/system/client/ClientEdit.vue index 9d0fecfcf..0cfff000b 100644 --- a/src/views/modules/system/client/ClientEdit.vue +++ b/src/views/modules/system/client/ClientEdit.vue @@ -56,11 +56,11 @@ diff --git a/types/axios.d.ts b/types/axios.d.ts index 38d7f8da5..6b3131feb 100644 --- a/types/axios.d.ts +++ b/types/axios.d.ts @@ -39,7 +39,8 @@ export interface RetryRequest { export interface Result { code: number type: 'success' | 'error' | 'warning' - message: string + msg: string + traceId: string | null | undefined data: T }