138 lines
3.6 KiB
Go
138 lines
3.6 KiB
Go
|
package app
|
|||
|
|
|||
|
import (
|
|||
|
"encoding/json"
|
|||
|
"github.com/gin-gonic/gin"
|
|||
|
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
|||
|
"go-admin/app/admin/models"
|
|||
|
"go-admin/tools"
|
|||
|
"net/http"
|
|||
|
"strconv"
|
|||
|
)
|
|||
|
|
|||
|
// 失败数据处理
|
|||
|
func Error(c *gin.Context, code int, err error, msg string) {
|
|||
|
var res Response
|
|||
|
if err != nil {
|
|||
|
res.Msg = err.Error()
|
|||
|
}
|
|||
|
if msg != "" {
|
|||
|
res.Msg = msg
|
|||
|
}
|
|||
|
res.RequestId = tools.GenerateMsgIDFromContext(c)
|
|||
|
//traceId, _ := c.Get("trace")
|
|||
|
//res.RequestId = traceId.(string)
|
|||
|
|
|||
|
c.JSON(http.StatusOK, res.ReturnError(code))
|
|||
|
response, _ := json.Marshal(res)
|
|||
|
c.Set("response", string(response))
|
|||
|
}
|
|||
|
|
|||
|
// 通常成功数据处理
|
|||
|
func OK(c *gin.Context, data interface{}, msg string) {
|
|||
|
var res Response
|
|||
|
res.Data = data
|
|||
|
if msg != "" {
|
|||
|
res.Msg = msg
|
|||
|
}
|
|||
|
res.RequestId = tools.GenerateMsgIDFromContext(c)
|
|||
|
//traceId, _ := c.Get("trace")
|
|||
|
//res.RequestId = traceId.(string)
|
|||
|
|
|||
|
c.JSON(http.StatusOK, res.ReturnOK())
|
|||
|
response, _ := json.Marshal(res)
|
|||
|
c.Set("response", string(response))
|
|||
|
}
|
|||
|
|
|||
|
// MiGuOK 通常成功数据处理
|
|||
|
func MiGuOK(c *gin.Context, data interface{}) {
|
|||
|
// 从上下文中获取 requestId
|
|||
|
requestId := pkg.GenerateMsgIDFromContext(c)
|
|||
|
|
|||
|
// 根据具体的响应类型判断并添加 requestId
|
|||
|
switch v := data.(type) {
|
|||
|
case models.MiGuRsp:
|
|||
|
// 修改数据并添加 requestId
|
|||
|
v.RequestId = requestId
|
|||
|
c.JSON(http.StatusOK, v)
|
|||
|
case models.MiGuCheckRsp:
|
|||
|
// 修改数据并添加 requestId
|
|||
|
v.RequestId = requestId
|
|||
|
c.JSON(http.StatusOK, v)
|
|||
|
case models.CheckOrderResp:
|
|||
|
// 修改数据并添加 requestId
|
|||
|
v.RequestId = requestId
|
|||
|
c.JSON(http.StatusOK, v)
|
|||
|
default:
|
|||
|
// 如果不是预期的类型,则直接返回原始数据
|
|||
|
c.JSON(http.StatusOK, data)
|
|||
|
}
|
|||
|
|
|||
|
// 如果仍然需要记录 response 到上下文中
|
|||
|
response, _ := json.Marshal(data)
|
|||
|
c.Set("response", string(response))
|
|||
|
}
|
|||
|
|
|||
|
// MiGuError 失败数据处理,code 为 int 类型,内部转换为 string
|
|||
|
func MiGuError(c *gin.Context, code int, err error, msg string) {
|
|||
|
var res MiGuErrorResponse
|
|||
|
if err != nil {
|
|||
|
res.Msg = err.Error()
|
|||
|
}
|
|||
|
if msg != "" {
|
|||
|
res.Msg = msg
|
|||
|
}
|
|||
|
res.RequestId = tools.GenerateMsgIDFromContext(c) // 获取 requestId
|
|||
|
res.Code = strconv.Itoa(code) // 将 int 类型的 code 转为 string
|
|||
|
|
|||
|
// 返回 JSON 响应
|
|||
|
c.JSON(http.StatusOK, res)
|
|||
|
|
|||
|
// 如果仍然需要记录 response 到上下文中
|
|||
|
response, _ := json.Marshal(res)
|
|||
|
c.Set("response", string(response))
|
|||
|
}
|
|||
|
|
|||
|
// MiGuNoticeOK 通常成功数据处理
|
|||
|
func MiGuNoticeOK(c *gin.Context, data interface{}) {
|
|||
|
// 将传入的数据转换为字符串
|
|||
|
var responseString string
|
|||
|
|
|||
|
// 根据 data 的类型进行不同的处理
|
|||
|
switch v := data.(type) {
|
|||
|
case string:
|
|||
|
responseString = v // 如果 data 已经是字符串,直接赋值
|
|||
|
default:
|
|||
|
// 对于其他类型的数据,尝试将其转换为 JSON 字符串
|
|||
|
responseBytes, err := json.Marshal(data)
|
|||
|
if err != nil {
|
|||
|
// 如果转换失败,返回一个默认的错误信息
|
|||
|
c.String(http.StatusInternalServerError, "无法处理响应数据")
|
|||
|
return
|
|||
|
}
|
|||
|
responseString = string(responseBytes)
|
|||
|
}
|
|||
|
|
|||
|
c.Writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|||
|
// 直接返回字符串
|
|||
|
c.String(http.StatusOK, responseString)
|
|||
|
|
|||
|
// 如果仍然需要记录 response 到上下文中
|
|||
|
c.Set("response", responseString)
|
|||
|
}
|
|||
|
|
|||
|
// 分页数据处理
|
|||
|
func PageOK(c *gin.Context, result interface{}, count int, pageIndex int, pageSize int, msg string) {
|
|||
|
var res Page
|
|||
|
res.List = result
|
|||
|
res.Count = count
|
|||
|
res.PageIndex = pageIndex
|
|||
|
res.PageSize = pageSize
|
|||
|
OK(c, res, msg)
|
|||
|
}
|
|||
|
|
|||
|
// 兼容函数
|
|||
|
func Custum(c *gin.Context, data gin.H) {
|
|||
|
c.JSON(http.StatusOK, data)
|
|||
|
}
|