tl 2 lat temu
rodzic
commit
1a8080474c
1 zmienionych plików z 58 dodań i 3 usunięć
  1. 58 3
      webutils/response.go

+ 58 - 3
webutils/response.go

@@ -1,11 +1,14 @@
 package webutils
 
 import (
+	"encoding/base64"
+	"encoding/json"
 	"net/http"
 
 	"github.com/gin-gonic/gin"
 	ut "github.com/go-playground/universal-translator"
 	"github.com/sirupsen/logrus"
+	"gogs.daxia.dev/huanan/pkg.daxia.dev/encrypt"
 )
 
 const (
@@ -57,7 +60,35 @@ func FailedResponse(c *gin.Context, msg string) {
 		Data: nil,
 	})
 
-	panic(kkerror.NewRespError("failed"))
+	panic(NewRespError("failed"))
+}
+
+func FailedResponseEncrypt(c *gin.Context, msg string, encKey []byte) {
+	if c == nil {
+		return
+	}
+
+	if encKey != nil {
+		respData, _ := json.Marshal(Response{
+			Code: CodeFailed,
+			Msg:  msg,
+			Data: nil,
+		})
+
+		encData, _ := encrypt.AesEncrypt(respData, encKey)
+		encDataStr := base64.StdEncoding.EncodeToString(encData)
+
+		c.Data(200, "application/json", []byte(encDataStr))
+		panic(NewRespError("failed"))
+	}
+
+	c.JSON(http.StatusOK, &Response{
+		Code: CodeFailed,
+		Msg:  msg,
+		Data: nil,
+	})
+
+	panic(NewRespError("failed"))
 }
 
 func FailedResponseErr(c *gin.Context, err error) {
@@ -76,7 +107,7 @@ func FailedResponseWithCode(c *gin.Context, code int, msg string) {
 		Data: nil,
 	})
 
-	panic(kkerror.NewRespError("failed"))
+	panic(NewRespError("failed"))
 }
 
 func SuccessResponse(c *gin.Context, data interface{}) {
@@ -86,5 +117,29 @@ func SuccessResponse(c *gin.Context, data interface{}) {
 		Data: data,
 	})
 
-	panic(kkerror.NewRespError("success"))
+	panic(NewRespError("success"))
+}
+
+func SuccessResponseEncrypt(c *gin.Context, data interface{}, encKey []byte) {
+	if encKey != nil {
+		respData, _ := json.Marshal(Response{
+			Code: CodeSuccess,
+			Msg:  "",
+			Data: data,
+		})
+
+		encData, _ := encrypt.AesEncrypt(respData, encKey)
+		encDataStr := base64.StdEncoding.EncodeToString(encData)
+
+		c.Data(200, "application/json", []byte(encDataStr))
+		panic(NewRespError("failed"))
+	}
+
+	c.JSON(http.StatusOK, &Response{
+		Code: CodeSuccess,
+		Msg:  "",
+		Data: data,
+	})
+
+	panic(NewRespError("success"))
 }