bff config to nacos
This commit is contained in:
@@ -2,37 +2,14 @@ package response
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"pkg.local/utils"
|
||||
)
|
||||
|
||||
type BaseController struct{}
|
||||
|
||||
// Ok 成功输出结果 加密
|
||||
func (b *BaseController) Ok(w http.ResponseWriter, data any) {
|
||||
isDebug := GetConfigBool("base.is_debug")
|
||||
if !isDebug {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
b.Fail(w)
|
||||
return
|
||||
}
|
||||
encryptKey := GetEncrypt("encrypt.encrypt_key", 15, 28, 18)
|
||||
res, enErr := Crypto{}.EncryptKey(jsonData, encryptKey)
|
||||
if enErr != nil {
|
||||
b.Fail(w)
|
||||
return
|
||||
}
|
||||
b.Out(w, res, OK)
|
||||
return
|
||||
}
|
||||
b.Out(w, data, OK)
|
||||
}
|
||||
|
||||
// Ok2 成功输出结果 不加密
|
||||
func (b *BaseController) Ok2(w http.ResponseWriter, data any) {
|
||||
b.Out(w, data, OK)
|
||||
}
|
||||
|
||||
// Fail 基础错误的输出结果
|
||||
func (b *BaseController) Fail(w http.ResponseWriter) {
|
||||
b.Out(w, nil, Fail)
|
||||
@@ -54,6 +31,23 @@ func (b *BaseController) Out(w http.ResponseWriter, data any, cm Error) {
|
||||
}
|
||||
|
||||
func (b *BaseController) OutPut(w http.ResponseWriter, code int32, data any, msg string) {
|
||||
if code == utils.Ok.Code && !utils.GetConfigBool("encrypt.debug") {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
msg = utils.ErrorEncryptAesError.Msg
|
||||
code = utils.ErrorEncryptAesError.Code
|
||||
} else {
|
||||
res, enErr := utils.Crypto{}.Encrypt(jsonData)
|
||||
fmt.Printf("res: %v\n", res)
|
||||
if enErr != nil {
|
||||
msg = utils.ErrorEncryptAesError.Msg
|
||||
code = utils.ErrorEncryptAesError.Code
|
||||
} else {
|
||||
data = res
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 复制一份,避免并发请求改到包级 OK/Fail 单例
|
||||
out := NewError(int(code), msg).WithData(data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user