新增问诊订单详情。修改错误码格式
This commit is contained in:
@@ -128,7 +128,7 @@ func Auth() gin.HandlerFunc {
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "请求路径错误",
|
||||
"code": consts.SERVER_ERROR,
|
||||
"code": consts.ServerError,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
@@ -147,7 +147,7 @@ func Auth() gin.HandlerFunc {
|
||||
if len(adminApis) == 0 || err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "请求路径错误",
|
||||
"code": consts.SERVER_ERROR,
|
||||
"code": consts.ServerError,
|
||||
"data": "",
|
||||
})
|
||||
c.Abort()
|
||||
@@ -168,7 +168,7 @@ func Auth() gin.HandlerFunc {
|
||||
if adminRoleMenu == nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"message": "暂无权限",
|
||||
"code": consts.CLIENT_HTTP_UNAUTHORIZED,
|
||||
"code": consts.ClientHttpUnauthorized,
|
||||
"data": "",
|
||||
})
|
||||
c.Abort()
|
||||
@@ -197,7 +197,7 @@ func Auth() gin.HandlerFunc {
|
||||
if !hasPermission {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"message": "暂无权限",
|
||||
"code": consts.CLIENT_HTTP_UNAUTHORIZED,
|
||||
"code": consts.ClientHttpUnauthorized,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ func Jwt() gin.HandlerFunc {
|
||||
if authorization == "" || !strings.HasPrefix(authorization, "Bearer ") {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"message": "请求未授权",
|
||||
"code": consts.TOKEN_ERROR,
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
c.Abort()
|
||||
@@ -35,7 +35,7 @@ func Jwt() gin.HandlerFunc {
|
||||
if res != "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "token错误/过期",
|
||||
"code": consts.TOKEN_ERROR,
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
@@ -48,7 +48,7 @@ func Jwt() gin.HandlerFunc {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "token错误/过期",
|
||||
"code": consts.TOKEN_ERROR,
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
@@ -61,7 +61,7 @@ func Jwt() gin.HandlerFunc {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "token错误",
|
||||
"code": consts.TOKEN_ERROR,
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
@@ -73,7 +73,7 @@ func Jwt() gin.HandlerFunc {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "token错误",
|
||||
"code": consts.TOKEN_ERROR,
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
@@ -85,7 +85,7 @@ func Jwt() gin.HandlerFunc {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "token错误",
|
||||
"code": consts.TOKEN_ERROR,
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
@@ -97,7 +97,7 @@ func Jwt() gin.HandlerFunc {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "token错误",
|
||||
"code": consts.TOKEN_ERROR,
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
|
||||
@@ -19,11 +19,14 @@ func Logrus() gin.HandlerFunc {
|
||||
c.Next()
|
||||
|
||||
// 获取 请求 参数
|
||||
params, ok := c.Get("params")
|
||||
if !ok {
|
||||
params = ""
|
||||
} else {
|
||||
params = params.(map[string]interface{})
|
||||
params := make(map[string]string)
|
||||
|
||||
paramsRaw, ok := c.Get("params")
|
||||
if ok {
|
||||
requestParams, ok := paramsRaw.(map[string]string)
|
||||
if ok || len(requestParams) > 0 {
|
||||
params = requestParams
|
||||
}
|
||||
}
|
||||
|
||||
// 结束时间
|
||||
|
||||
@@ -1,62 +1,71 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"hospital-admin-api/consts"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// RequestParamsMiddleware 获取请求参数中间件
|
||||
func RequestParamsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
contentType := c.GetHeader("Content-Type")
|
||||
contentType := c.Request.Header.Get("Content-Type")
|
||||
|
||||
params := make(map[string]string)
|
||||
|
||||
// 判断请求参数类型
|
||||
switch contentType {
|
||||
case "application/json":
|
||||
// 解析 JSON 参数
|
||||
var params map[string]interface{}
|
||||
// 读取请求体数据
|
||||
data, err := c.GetRawData()
|
||||
// 解析 application/json 请求体
|
||||
bodyBytes, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read request body"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 解析 JSON 数据到 map[string]interface{}
|
||||
err = json.Unmarshal(data, ¶ms)
|
||||
// 创建新的请求对象,并设置请求体数据
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||
|
||||
var jsonParams map[string]interface{}
|
||||
err = json.Unmarshal(bodyBytes, &jsonParams)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "Invalid JSON data",
|
||||
"code": consts.HTTP_ERROR,
|
||||
"code": consts.HttpError,
|
||||
"data": "",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
for key, value := range jsonParams {
|
||||
params[key] = fmt.Sprintf("%v", value)
|
||||
}
|
||||
|
||||
// 存储参数到上下文
|
||||
c.Set("params", params)
|
||||
|
||||
case "application/form-data", "application/x-www-form-urlencoded":
|
||||
case "multipart/form-data", "application/form-data", "application/x-www-form-urlencoded":
|
||||
// 解析 Form 表单参数
|
||||
err := c.Request.ParseForm()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "Invalid form data",
|
||||
"code": consts.HTTP_ERROR,
|
||||
"code": consts.HttpError,
|
||||
"data": "",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 将参数转换为 map[string]interface{}
|
||||
params := make(map[string]interface{})
|
||||
for key, values := range c.Request.Form {
|
||||
if len(values) > 0 {
|
||||
params[key] = values[0]
|
||||
params[key] = fmt.Sprintf("%v", values[0])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,11 +76,9 @@ func RequestParamsMiddleware() gin.HandlerFunc {
|
||||
// 解析 URL 参数
|
||||
queryParams := c.Request.URL.Query()
|
||||
|
||||
// 将参数转换为 map[string]interface{}
|
||||
params := make(map[string]interface{})
|
||||
for key, values := range queryParams {
|
||||
if len(values) > 0 {
|
||||
params[key] = values[0]
|
||||
params[key] = fmt.Sprintf("%v", values[0])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user