refactor: 统一响应格式和成功状态码
将接口响应格式中的 message 字段统一改为 msg 将成功状态码从 0 改为 1 更新相关文档和测试用例 禁用百度统计脚本 添加主题模式默认配置
Showing
11 changed files
with
52 additions
and
34 deletions
| 1 | +/* | ||
| 2 | + * @Date: 2025-11-17 19:45:50 | ||
| 3 | + * @LastEditors: hookehuyr hookehuyr@gmail.com | ||
| 4 | + * @LastEditTime: 2026-01-06 17:26:59 | ||
| 5 | + * @FilePath: /vben-admin/apps/backend-mock/utils/response.ts | ||
| 6 | + * @Description: 文件描述 | ||
| 7 | + */ | ||
| 1 | import type { EventHandlerRequest, H3Event } from 'h3'; | 8 | import type { EventHandlerRequest, H3Event } from 'h3'; |
| 2 | 9 | ||
| 3 | import { setResponseStatus } from 'h3'; | 10 | import { setResponseStatus } from 'h3'; |
| 4 | 11 | ||
| 5 | export function useResponseSuccess<T = any>(data: T) { | 12 | export function useResponseSuccess<T = any>(data: T) { |
| 6 | return { | 13 | return { |
| 7 | - code: 0, | 14 | + code: 1, |
| 8 | data, | 15 | data, |
| 9 | error: null, | 16 | error: null, |
| 10 | - message: 'ok', | 17 | + msg: 'ok', |
| 11 | }; | 18 | }; |
| 12 | } | 19 | } |
| 13 | 20 | ||
| ... | @@ -15,8 +22,9 @@ export function usePageResponseSuccess<T = any>( | ... | @@ -15,8 +22,9 @@ export function usePageResponseSuccess<T = any>( |
| 15 | page: number | string, | 22 | page: number | string, |
| 16 | pageSize: number | string, | 23 | pageSize: number | string, |
| 17 | list: T[], | 24 | list: T[], |
| 18 | - { message = 'ok' } = {}, | 25 | + { msg, message } = {} as { message?: string; msg?: string }, |
| 19 | ) { | 26 | ) { |
| 27 | + const finalMessage = msg ?? message ?? 'ok'; | ||
| 20 | const pageData = pagination( | 28 | const pageData = pagination( |
| 21 | Number.parseInt(`${page}`), | 29 | Number.parseInt(`${page}`), |
| 22 | Number.parseInt(`${pageSize}`), | 30 | Number.parseInt(`${pageSize}`), |
| ... | @@ -28,7 +36,7 @@ export function usePageResponseSuccess<T = any>( | ... | @@ -28,7 +36,7 @@ export function usePageResponseSuccess<T = any>( |
| 28 | items: pageData, | 36 | items: pageData, |
| 29 | total: list.length, | 37 | total: list.length, |
| 30 | }), | 38 | }), |
| 31 | - message, | 39 | + msg: finalMessage, |
| 32 | }; | 40 | }; |
| 33 | } | 41 | } |
| 34 | 42 | ||
| ... | @@ -37,7 +45,7 @@ export function useResponseError(message: string, error: any = null) { | ... | @@ -37,7 +45,7 @@ export function useResponseError(message: string, error: any = null) { |
| 37 | code: -1, | 45 | code: -1, |
| 38 | data: null, | 46 | data: null, |
| 39 | error, | 47 | error, |
| 40 | - message, | 48 | + msg: message, |
| 41 | }; | 49 | }; |
| 42 | } | 50 | } |
| 43 | 51 | ... | ... |
| 1 | # 应用标题 | 1 | # 应用标题 |
| 2 | -VITE_APP_TITLE=Vben Admin Ele | 2 | +VITE_APP_TITLE=Itomix Admin |
| 3 | 3 | ||
| 4 | # 应用命名空间,用于缓存、store等功能的前缀,确保隔离 | 4 | # 应用命名空间,用于缓存、store等功能的前缀,确保隔离 |
| 5 | -VITE_APP_NAMESPACE=vben-web-ele | 5 | +VITE_APP_NAMESPACE=itomix-web-ele |
| 6 | 6 | ||
| 7 | # 对store进行加密的密钥,在将store持久化到localStorage时会使用该密钥进行加密 | 7 | # 对store进行加密的密钥,在将store持久化到localStorage时会使用该密钥进行加密 |
| 8 | VITE_APP_STORE_SECURE_KEY=please-replace-me-with-your-own-key | 8 | VITE_APP_STORE_SECURE_KEY=please-replace-me-with-your-own-key | ... | ... |
| ... | @@ -17,14 +17,14 @@ | ... | @@ -17,14 +17,14 @@ |
| 17 | <script> | 17 | <script> |
| 18 | // 生产环境下注入百度统计 | 18 | // 生产环境下注入百度统计 |
| 19 | if (window._VBEN_ADMIN_PRO_APP_CONF_) { | 19 | if (window._VBEN_ADMIN_PRO_APP_CONF_) { |
| 20 | - var _hmt = _hmt || []; | 20 | + // var _hmt = _hmt || []; |
| 21 | - (function () { | 21 | + // (function () { |
| 22 | - var hm = document.createElement('script'); | 22 | + // var hm = document.createElement('script'); |
| 23 | - hm.src = | 23 | + // hm.src = |
| 24 | - 'https://hm.baidu.com/hm.js?97352b16ed2df8c3860cf5a1a65fb4dd'; | 24 | + // 'https://hm.baidu.com/hm.js?97352b16ed2df8c3860cf5a1a65fb4dd'; |
| 25 | - var s = document.getElementsByTagName('script')[0]; | 25 | + // var s = document.getElementsByTagName('script')[0]; |
| 26 | - s.parentNode.insertBefore(hm, s); | 26 | + // s.parentNode.insertBefore(hm, s); |
| 27 | - })(); | 27 | + // })(); |
| 28 | } | 28 | } |
| 29 | </script> | 29 | </script> |
| 30 | </head> | 30 | </head> | ... | ... |
| ... | @@ -76,7 +76,7 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { | ... | @@ -76,7 +76,7 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { |
| 76 | defaultResponseInterceptor({ | 76 | defaultResponseInterceptor({ |
| 77 | codeField: 'code', | 77 | codeField: 'code', |
| 78 | dataField: 'data', | 78 | dataField: 'data', |
| 79 | - successCode: 0, | 79 | + successCode: 1, |
| 80 | }), | 80 | }), |
| 81 | ); | 81 | ); |
| 82 | 82 | ||
| ... | @@ -95,9 +95,9 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { | ... | @@ -95,9 +95,9 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { |
| 95 | client.addResponseInterceptor( | 95 | client.addResponseInterceptor( |
| 96 | errorMessageResponseInterceptor((msg: string, error) => { | 96 | errorMessageResponseInterceptor((msg: string, error) => { |
| 97 | // 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg | 97 | // 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg |
| 98 | - // 当前mock接口返回的错误字段是 error 或者 message | 98 | + // 当前接口返回的错误字段是 error 或者 msg |
| 99 | const responseData = error?.response?.data ?? {}; | 99 | const responseData = error?.response?.data ?? {}; |
| 100 | - const errorMessage = responseData?.error ?? responseData?.message ?? ''; | 100 | + const errorMessage = responseData?.error ?? responseData?.msg ?? ''; |
| 101 | // 如果没有错误信息,则会根据状态码进行提示 | 101 | // 如果没有错误信息,则会根据状态码进行提示 |
| 102 | ElMessage.error(errorMessage || msg); | 102 | ElMessage.error(errorMessage || msg); |
| 103 | }), | 103 | }), | ... | ... |
| 1 | +/* | ||
| 2 | + * @Date: 2025-11-17 19:45:50 | ||
| 3 | + * @LastEditors: hookehuyr hookehuyr@gmail.com | ||
| 4 | + * @LastEditTime: 2026-01-06 17:14:40 | ||
| 5 | + * @FilePath: /vben-admin/apps/web-ele/src/preferences.ts | ||
| 6 | + * @Description: 文件描述 | ||
| 7 | + */ | ||
| 1 | import { defineOverridesPreferences } from '@vben/preferences'; | 8 | import { defineOverridesPreferences } from '@vben/preferences'; |
| 2 | 9 | ||
| 3 | /** | 10 | /** |
| ... | @@ -10,4 +17,7 @@ export const overridesPreferences = defineOverridesPreferences({ | ... | @@ -10,4 +17,7 @@ export const overridesPreferences = defineOverridesPreferences({ |
| 10 | app: { | 17 | app: { |
| 11 | name: import.meta.env.VITE_APP_TITLE, | 18 | name: import.meta.env.VITE_APP_TITLE, |
| 12 | }, | 19 | }, |
| 20 | + theme: { | ||
| 21 | + mode: 'light', | ||
| 22 | + }, | ||
| 13 | }); | 23 | }); | ... | ... |
| ... | @@ -235,7 +235,7 @@ function createRequestClient(baseURL: string) { | ... | @@ -235,7 +235,7 @@ function createRequestClient(baseURL: string) { |
| 235 | 235 | ||
| 236 | const { code, data } = responseData; | 236 | const { code, data } = responseData; |
| 237 | 237 | ||
| 238 | - if (status >= 200 && status < 400 && code === 0) { | 238 | + if (status >= 200 && status < 400 && code === 1) { |
| 239 | return data; | 239 | return data; |
| 240 | } | 240 | } |
| 241 | throw Object.assign({}, response, { response }); | 241 | throw Object.assign({}, response, { response }); |
| ... | @@ -257,9 +257,9 @@ function createRequestClient(baseURL: string) { | ... | @@ -257,9 +257,9 @@ function createRequestClient(baseURL: string) { |
| 257 | client.addResponseInterceptor( | 257 | client.addResponseInterceptor( |
| 258 | errorMessageResponseInterceptor((msg: string, error) => { | 258 | errorMessageResponseInterceptor((msg: string, error) => { |
| 259 | // 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg | 259 | // 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg |
| 260 | - // 当前mock接口返回的错误字段是 error 或者 message | 260 | + // 当前接口返回的错误字段是 error 或者 msg |
| 261 | const responseData = error?.response?.data ?? {}; | 261 | const responseData = error?.response?.data ?? {}; |
| 262 | - const errorMessage = responseData?.error ?? responseData?.message ?? ''; | 262 | + const errorMessage = responseData?.error ?? responseData?.msg ?? ''; |
| 263 | // 如果没有错误信息,则会根据状态码进行提示 | 263 | // 如果没有错误信息,则会根据状态码进行提示 |
| 264 | message.error(errorMessage || msg); | 264 | message.error(errorMessage || msg); |
| 265 | }), | 265 | }), | ... | ... |
| ... | @@ -269,7 +269,7 @@ function createRequestClient(baseURL: string) { | ... | @@ -269,7 +269,7 @@ function createRequestClient(baseURL: string) { |
| 269 | // 指定接口返回的数据中装载了主要数据的字段名 | 269 | // 指定接口返回的数据中装载了主要数据的字段名 |
| 270 | dataField: 'data', | 270 | dataField: 'data', |
| 271 | // 请求成功的 code 值,如果接口返回的 code 等于 successCode 则会认为是成功的请求 | 271 | // 请求成功的 code 值,如果接口返回的 code 等于 successCode 则会认为是成功的请求 |
| 272 | - successCode: 0, | 272 | + successCode: 1, |
| 273 | }), | 273 | }), |
| 274 | ); | 274 | ); |
| 275 | 275 | ||
| ... | @@ -288,9 +288,9 @@ function createRequestClient(baseURL: string) { | ... | @@ -288,9 +288,9 @@ function createRequestClient(baseURL: string) { |
| 288 | client.addResponseInterceptor( | 288 | client.addResponseInterceptor( |
| 289 | errorMessageResponseInterceptor((msg: string, error) => { | 289 | errorMessageResponseInterceptor((msg: string, error) => { |
| 290 | // 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg | 290 | // 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg |
| 291 | - // 当前mock接口返回的错误字段是 error 或者 message | 291 | + // 当前接口返回的错误字段是 error 或者 msg |
| 292 | const responseData = error?.response?.data ?? {}; | 292 | const responseData = error?.response?.data ?? {}; |
| 293 | - const errorMessage = responseData?.error ?? responseData?.message ?? ''; | 293 | + const errorMessage = responseData?.error ?? responseData?.msg ?? ''; |
| 294 | // 如果没有错误信息,则会根据状态码进行提示 | 294 | // 如果没有错误信息,则会根据状态码进行提示 |
| 295 | message.error(errorMessage || msg); | 295 | message.error(errorMessage || msg); |
| 296 | }), | 296 | }), | ... | ... |
| ... | @@ -127,12 +127,12 @@ outline: deep | ... | @@ -127,12 +127,12 @@ outline: deep |
| 127 | ```ts | 127 | ```ts |
| 128 | interface HttpResponse<T = any> { | 128 | interface HttpResponse<T = any> { |
| 129 | /** | 129 | /** |
| 130 | - * 0 表示成功 其他表示失败 | 130 | + * 1 表示成功 其他表示失败 |
| 131 | - * 0 means success, others means fail | 131 | + * 1 means success, others means fail |
| 132 | */ | 132 | */ |
| 133 | code: number; | 133 | code: number; |
| 134 | data: T; | 134 | data: T; |
| 135 | - message: string; | 135 | + msg: string; |
| 136 | } | 136 | } |
| 137 | ``` | 137 | ``` |
| 138 | 138 | ... | ... |
| ... | @@ -9,7 +9,7 @@ import axios from 'axios'; | ... | @@ -9,7 +9,7 @@ import axios from 'axios'; |
| 9 | export const defaultResponseInterceptor = ({ | 9 | export const defaultResponseInterceptor = ({ |
| 10 | codeField = 'code', | 10 | codeField = 'code', |
| 11 | dataField = 'data', | 11 | dataField = 'data', |
| 12 | - successCode = 0, | 12 | + successCode = 1, |
| 13 | }: { | 13 | }: { |
| 14 | /** 响应数据中代表访问结果的字段名 */ | 14 | /** 响应数据中代表访问结果的字段名 */ |
| 15 | codeField: string; | 15 | codeField: string; | ... | ... |
| ... | @@ -69,12 +69,12 @@ type MakeErrorMessageFn = (message: string, error: any) => void; | ... | @@ -69,12 +69,12 @@ type MakeErrorMessageFn = (message: string, error: any) => void; |
| 69 | 69 | ||
| 70 | interface HttpResponse<T = any> { | 70 | interface HttpResponse<T = any> { |
| 71 | /** | 71 | /** |
| 72 | - * 0 表示成功 其他表示失败 | 72 | + * 1 表示成功 其他表示失败 |
| 73 | - * 0 means success, others means fail | 73 | + * 1 means success, others means fail |
| 74 | */ | 74 | */ |
| 75 | code: number; | 75 | code: number; |
| 76 | data: T; | 76 | data: T; |
| 77 | - message: string; | 77 | + msg: string; |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | export type { | 80 | export type { | ... | ... |
| ... | @@ -90,7 +90,7 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { | ... | @@ -90,7 +90,7 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { |
| 90 | defaultResponseInterceptor({ | 90 | defaultResponseInterceptor({ |
| 91 | codeField: 'code', | 91 | codeField: 'code', |
| 92 | dataField: 'data', | 92 | dataField: 'data', |
| 93 | - successCode: 0, | 93 | + successCode: 1, |
| 94 | }), | 94 | }), |
| 95 | ); | 95 | ); |
| 96 | 96 | ||
| ... | @@ -109,9 +109,9 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { | ... | @@ -109,9 +109,9 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) { |
| 109 | client.addResponseInterceptor( | 109 | client.addResponseInterceptor( |
| 110 | errorMessageResponseInterceptor((msg: string, error) => { | 110 | errorMessageResponseInterceptor((msg: string, error) => { |
| 111 | // 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg | 111 | // 这里可以根据业务进行定制,你可以拿到 error 内的信息进行定制化处理,根据不同的 code 做不同的提示,而不是直接使用 message.error 提示 msg |
| 112 | - // 当前mock接口返回的错误字段是 error 或者 message | 112 | + // 当前接口返回的错误字段是 error 或者 msg |
| 113 | const responseData = error?.response?.data ?? {}; | 113 | const responseData = error?.response?.data ?? {}; |
| 114 | - const errorMessage = responseData?.error ?? responseData?.message ?? ''; | 114 | + const errorMessage = responseData?.error ?? responseData?.msg ?? ''; |
| 115 | // 如果没有错误信息,则会根据状态码进行提示 | 115 | // 如果没有错误信息,则会根据状态码进行提示 |
| 116 | message.error(errorMessage || msg); | 116 | message.error(errorMessage || msg); |
| 117 | }), | 117 | }), | ... | ... |
-
Please register or login to post a comment