bff config to nacos
CI / changes (push) Successful in 2s
CI / docker-bff (push) Successful in 3m7s
CI / docker-product (push) Has been skipped
CI / docker-admin (push) Successful in 3m26s

This commit is contained in:
2026-08-13 16:27:18 +08:00
parent 94e1c6f97d
commit 6c20427b0a
19 changed files with 458 additions and 169 deletions
+3 -45
View File
@@ -5,9 +5,8 @@ import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"math/rand"
"strings"
"time"
"pkg.local/utils"
)
var (
@@ -21,47 +20,6 @@ func Init(debug bool, key string) {
encryptKey = key
}
func GetConfigBool(key string) bool {
if key == "base.is_debug" {
return isDebug
}
return false
}
func GetConfigString(key string) string {
if key == "encrypt.encrypt_key" {
return encryptKey
}
return ""
}
func GetEncrypt(key string, nums ...int8) string {
original := GetConfigString(key)
str := original
//TODO 后台切换到新版时开启
//for _, num := range nums {
// str = str[num:] + str[:num]
//}
return str
}
// GetRandString 取得随机字符串:使用字符串拼接
func GetRandString(length int) string {
if length < 1 {
return ""
}
char := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
charArr := strings.Split(char, "")
charlen := len(charArr)
ran := rand.New(rand.NewSource(time.Now().Unix()))
var rchar string = ""
for i := 1; i <= length; i++ {
rchar = rchar + charArr[ran.Intn(charlen)]
}
return rchar
}
type Crypto struct{}
// EncryptKey 加密
@@ -76,7 +34,7 @@ func (c Crypto) EncryptKey(data []byte, key string) (map[string]string, Error) {
func (c Crypto) aesCBCEncryptKey(data []byte, key string) (map[string]string, Error) {
k := []byte(key)
iv := GetRandString(NumberSixteen)
iv := utils.GetRandString(NumberSixteen)
// 分组密钥
block, err := aes.NewCipher(k)
-2
View File
@@ -3,7 +3,6 @@ package response
import (
"bytes"
"encoding/json"
"log"
"net/http"
"strings"
@@ -35,7 +34,6 @@ func Wrap(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bw := &bodyWriter{ResponseWriter: w, status: http.StatusOK}
next(bw, r)
log.Printf("raw json: %s", bw.status)
ctrl := BaseController{}
if bw.status != http.StatusOK {
ctrl.Error(w, mapHTTPError(bw.status, bw.buf.String()))
+20 -26
View File
@@ -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)