添加文件: xy_sh/internal/biz/biz.go
This commit is contained in:
parent
f8a5e274ee
commit
4dc65eab11
|
|
@ -0,0 +1,65 @@
|
|||
package biz
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"xy_sh/internal/entities"
|
||||
"xy_sh/pkg/crypto"
|
||||
)
|
||||
|
||||
// Biz 业务逻辑层
|
||||
type Biz struct {
|
||||
SM4Key []byte
|
||||
}
|
||||
|
||||
// NewBiz 创建业务逻辑实例
|
||||
func NewBiz(sm4Key []byte) *Biz {
|
||||
return &Biz{SM4Key: sm4Key}
|
||||
}
|
||||
|
||||
// DecryptRequest 解密请求体中的 encryptedData 并解析为指定结构体
|
||||
func (b *Biz) DecryptRequest(encryptedData string, target interface{}) error {
|
||||
plaintext, err := crypto.SM4CBCDecrypt(encryptedData, b.SM4Key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("SM4解密失败: %v", err)
|
||||
}
|
||||
if err := json.Unmarshal(plaintext, target); err != nil {
|
||||
return fmt.Errorf("JSON解析失败: %v", err)
|
||||
}
|
||||
log.Printf("[业务] 解密请求数据: %s", string(plaintext))
|
||||
return nil
|
||||
}
|
||||
|
||||
// EncryptResponse 加密响应业务数据
|
||||
func (b *Biz) EncryptResponse(data interface{}) (string, error) {
|
||||
jsonBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("JSON序列化失败: %v", err)
|
||||
}
|
||||
encrypted, err := crypto.SM4CBCEncrypt(jsonBytes, b.SM4Key)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("SM4加密失败: %v", err)
|
||||
}
|
||||
log.Printf("[业务] 加密响应数据: %s", string(jsonBytes))
|
||||
return encrypted, nil
|
||||
}
|
||||
|
||||
// CreateOrder 卡券/直充权益下单业务逻辑
|
||||
// TODO: 根据实际业务需求实现
|
||||
func (b *Biz) CreateOrder(req *entities.OrderCreateRequest) (*entities.OrderCreateResponse, error) {
|
||||
log.Printf("[业务] 下单请求: actCode=%s, goodsCode=%s, actOrderNum=%s, account=%s",
|
||||
req.ActCode, req.GoodsCode, req.ActOrderNum, req.Account)
|
||||
|
||||
// TODO: 调用供应商实际接口或数据库操作
|
||||
// 以下为模拟实现
|
||||
resp := &entities.OrderCreateResponse{
|
||||
OrderNo: fmt.Sprintf("HM%d%s", crypto.GenerateTimestampMillis(), "27073398f3772c00"),
|
||||
Status: 0,
|
||||
ExpireTime: "2025-09-12 00:00:00",
|
||||
}
|
||||
|
||||
log.Printf("[业务] 下单成功: orderNo=%s", resp.OrderNo)
|
||||
return resp, nil
|
||||
}
|
||||
Loading…
Reference in New Issue