2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 06:47:20 +08:00

refactor: 接口信息字段调整为msg

This commit is contained in:
2025-05-16 00:29:00 +08:00
parent 1a821b361f
commit 04d286ca03
3 changed files with 25 additions and 25 deletions

View File

@@ -14,13 +14,13 @@ import (
// SuccessResponse 通用成功响应
type SuccessResponse struct {
Message string `json:"message"`
Data any `json:"data"`
Msg string `json:"msg"`
Data any `json:"data"`
}
// ErrorResponse 通用错误响应
type ErrorResponse struct {
Message string `json:"message"`
Msg string `json:"msg"`
}
// Success 响应成功
@@ -28,8 +28,8 @@ func Success(w http.ResponseWriter, data any) {
render := chix.NewRender(w)
defer render.Release()
render.JSON(&SuccessResponse{
Message: "success",
Data: data,
Msg: "success",
Data: data,
})
}
@@ -43,7 +43,7 @@ func Error(w http.ResponseWriter, code int, format string, args ...any) {
format = fmt.Sprintf(format, args...)
}
render.JSON(&ErrorResponse{
Message: format,
Msg: format,
})
}
@@ -54,7 +54,7 @@ func ErrorSystem(w http.ResponseWriter) {
render.Header(chix.HeaderContentType, chix.MIMEApplicationJSONCharsetUTF8) // must before Status()
render.Status(http.StatusInternalServerError)
render.JSON(&ErrorResponse{
Message: http.StatusText(http.StatusInternalServerError),
Msg: http.StatusText(http.StatusInternalServerError),
})
}

View File

@@ -1,27 +1,27 @@
import { useUserStore } from '@/store'
export function resolveResError(code: number | string | undefined, message = ''): string {
export function resolveResError(code: number | string | undefined, msg = ''): string {
switch (code) {
case 400:
case 422:
message = message ?? '请求参数错误'
msg = msg ?? '请求参数错误'
break
case 401:
message = message ?? '登录已过期'
msg = msg ?? '登录已过期'
useUserStore().logout()
break
case 403:
message = message ?? '没有权限'
msg = msg ?? '没有权限'
break
case 404:
message = message ?? '资源或接口不存在'
msg = msg ?? '资源或接口不存在'
break
case 500:
message = message ?? '服务器异常'
msg = msg ?? '服务器异常'
break
default:
message = message ?? `【${code}】: 未知异常!`
msg = msg ?? `【${code}】: 未知异常!`
break
}
return message
return msg
}

View File

@@ -18,45 +18,45 @@ export const http = createAlova({
if (ct && ct.includes('application/json')) {
json = typeof response.data === 'string' ? JSON.parse(response.data) : response.data
} else {
json = { code: response.status, message: response.data }
json = { code: response.status, msg: response.data }
}
} catch (error) {
json = { code: response.status, message: 'JSON 解析失败' }
json = { code: response.status, msg: 'failed to parse response' }
}
const { status, statusText } = response
const { meta } = method
if (status !== 200) {
const code = json?.code ?? status
const message = resolveResError(
const msg = resolveResError(
code,
(typeof json?.message === 'string' && json.message.trim()) || statusText
(typeof json?.msg === 'string' && json.msg.trim()) || statusText
)
const noAlert = meta?.noAlert
if (!noAlert) {
if (code === 422) {
window.$message.error(message)
window.$message.error(msg)
} else if (code !== 401) {
window.$dialog.error({
title: '错误',
content: message,
content: msg,
maskClosable: false
})
}
}
throw new Error(message)
throw new Error(msg)
}
return json.data
},
onError: (error: any, method: Method) => {
const { code, message } = error
const { code, msg } = error
const { meta } = method
const errorMessage = resolveResError(code, message)
const errorMsg = resolveResError(code, msg)
const noAlert = meta?.noAlert
if (!noAlert) {
window.$dialog.error({
title: '接口请求失败',
content: errorMessage,
content: errorMsg,
maskClosable: false
})
}