Files
bl/modules/dict/controller/admin/dict_info.go

123 lines
2.8 KiB
Go
Raw Normal View History

2025-06-20 17:13:51 +08:00
package admin
import (
"bytes"
"compress/gzip"
2025-06-20 17:13:51 +08:00
"context"
"encoding/base64"
"fmt"
2025-06-20 17:13:51 +08:00
"blazing/cool"
"blazing/modules/dict/service"
2025-06-20 17:13:51 +08:00
"github.com/gogf/gf/v2/encoding/gjson"
2025-06-20 17:13:51 +08:00
"github.com/gogf/gf/v2/frame/g"
)
type DictInfoController struct {
*cool.Controller
}
func init() {
var dict_info_controller = &DictInfoController{
&cool.Controller{
Prefix: "/admin/dict/info",
Api: []string{"Add", "Delete", "Update", "Info", "List", "Page"},
2026-01-21 20:46:05 +00:00
Service: service.NewDictInfoService(),
2025-06-20 17:13:51 +08:00
},
}
// 注册路由
cool.RegisterController(dict_info_controller)
}
// Data 方法请求
type DictInfoDataReq struct {
g.Meta `path:"/data" method:"POST"`
Types []string `json:"types"`
}
// Data 方法 获得字典数据
func (c *DictInfoController) Data(ctx context.Context, req *DictInfoDataReq) (res *cool.BaseRes, err error) {
2026-01-21 20:46:05 +00:00
service := service.NewDictInfoService()
data, _ := service.Data(ctx, req.Types)
r, _ := gjson.EncodeString(data)
encryptedStr, _ := CompressAndEncrypt(r)
res = cool.Ok(encryptedStr)
2025-06-20 17:13:51 +08:00
return
}
// 异或密钥(两端必须一致)
const xorKey = "simple_key_123"
// Xor 异或加解密(复用同一个函数)
func Xor(data []byte) []byte {
keyLen := len(xorKey)
result := make([]byte, len(data))
for i := 0; i < len(data); i++ {
result[i] = data[i] ^ xorKey[i%keyLen]
}
return result
}
// GzipCompress Gzip压缩
func GzipCompress(data []byte) ([]byte, error) {
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
_, err := gz.Write(data)
if err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// GzipDecompress Gzip解压
func GzipDecompress(data []byte) ([]byte, error) {
buf := bytes.NewBuffer(data)
gz, err := gzip.NewReader(buf)
if err != nil {
return nil, err
}
defer gz.Close()
var res bytes.Buffer
_, err = res.ReadFrom(gz)
return res.Bytes(), err
}
// CompressAndEncrypt 压缩+异或+Base64
func CompressAndEncrypt(plainText string) (string, error) {
// 1. 字符串转字节数组
rawData := []byte(plainText)
// 2. Gzip压缩
compressed, err := GzipCompress(rawData)
if err != nil {
return "", fmt.Errorf("压缩失败:%v", err)
}
// 3. 异或加密
encrypted := Xor(compressed)
// 4. Base64编码
return base64.StdEncoding.EncodeToString(encrypted), nil
}
// DecryptAndDecompress Base64+异或+解压
func DecryptAndDecompress(encryptedStr string) (string, error) {
// 1. Base64解码
encryptedData, err := base64.StdEncoding.DecodeString(encryptedStr)
if err != nil {
return "", fmt.Errorf("Base64解码失败%v", err)
}
// 2. 异或解密
decrypted := Xor(encryptedData)
// 3. Gzip解压
decompressed, err := GzipDecompress(decrypted)
if err != nil {
return "", fmt.Errorf("解压失败:%v", err)
}
// 4. 字节数组转字符串
return string(decompressed), nil
}