[优化] response 结构调整

This commit is contained in:
ryan
2026-06-05 11:32:10 +08:00
parent a5257be319
commit 4dc4c745c8
9 changed files with 164 additions and 96 deletions
+8 -36
View File
@@ -4,63 +4,35 @@ import (
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"openflare/common/response"
)
const invalidParamsMessage = "参数错误"
func respondSuccess(c *gin.Context, data any) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": data,
})
response.RespondSuccess(c, data)
}
func respondSuccessWithExtras(c *gin.Context, data any, extras gin.H) {
payload := gin.H{
"success": true,
"message": "",
"data": data,
}
for key, value := range extras {
payload[key] = value
}
c.JSON(http.StatusOK, payload)
response.RespondSuccessWithExtras(c, data, extras)
}
func respondSuccessMessage(c *gin.Context, message string) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": message,
})
response.RespondSuccessMessage(c, message)
}
func respondFailure(c *gin.Context, message string) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": message,
})
response.RespondFailure(c, message)
}
func respondBadRequest(c *gin.Context, message string) {
if message == "" {
message = invalidParamsMessage
}
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": message,
})
response.RespondBadRequest(c, message)
}
func respondUnauthorized(c *gin.Context, message string) {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": message,
})
response.RespondUnauthorized(c, message)
}
func decodeJSONBody(body io.Reader, target any) error {