diff --git a/aes.go b/aes.go index b4eaad1..6eb7f78 100644 --- a/aes.go +++ b/aes.go @@ -48,7 +48,7 @@ type Data struct { func main() { // 定义 AES 密钥 - key, _ := hex.DecodeString("958de6826370b57d9ae93b88e5009e26") + key, _ := hex.DecodeString("85af347e771bd631bf734068ad5798cc") var data interface{} diff --git a/apps/api/internal/handler/IVYZ/ivyz5733handler.go b/apps/api/internal/handler/IVYZ/ivyz5733handler.go index 68553bd..5f11d5b 100644 --- a/apps/api/internal/handler/IVYZ/ivyz5733handler.go +++ b/apps/api/internal/handler/IVYZ/ivyz5733handler.go @@ -2,29 +2,28 @@ package IVYZ import ( "net/http" + "tianyuan-api/pkg/response" "github.com/zeromicro/go-zero/rest/httpx" "tianyuan-api/apps/api/internal/logic/IVYZ" "tianyuan-api/apps/api/internal/svc" "tianyuan-api/apps/api/internal/types" - - xhttp "github.com/zeromicro/x/http" ) func IVYZ5733Handler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.Request if err := httpx.Parse(r, &req); err != nil { - xhttp.JsonBaseResponseCtx(r.Context(), w, err) + response.Fail(r.Context(), w, err) return } l := IVYZ.NewIVYZ5733Logic(r.Context(), svcCtx) resp, err := l.IVYZ5733(&req) if err != nil { - xhttp.JsonBaseResponseCtx(r.Context(), w, err) + response.Fail(r.Context(), w, err) } else { - xhttp.JsonBaseResponseCtx(r.Context(), w, resp) + response.Success(r.Context(), w, resp) } } } diff --git a/apps/api/internal/logic/IVYZ/ivyz5733logic.go b/apps/api/internal/logic/IVYZ/ivyz5733logic.go index f420579..445f190 100644 --- a/apps/api/internal/logic/IVYZ/ivyz5733logic.go +++ b/apps/api/internal/logic/IVYZ/ivyz5733logic.go @@ -4,11 +4,10 @@ import ( "context" "encoding/hex" "errors" - "tianyuan-api/apps/api/internal/validator" - "tianyuan-api/pkg/crypto" - "tianyuan-api/apps/api/internal/svc" "tianyuan-api/apps/api/internal/types" + "tianyuan-api/apps/api/internal/validator" + "tianyuan-api/pkg/crypto" "github.com/zeromicro/go-zero/core/logx" ) @@ -27,6 +26,12 @@ func NewIVYZ5733Logic(ctx context.Context, svcCtx *svc.ServiceContext) *IVYZ5733 } } +type CustomResponse struct { + Code int `json:"code"` + Msg string `json:"msg"` + Data interface{} `json:"data,omitempty"` // `omitempty` 使得当 Data 为空时字段不返回 +} + func (l *IVYZ5733Logic) IVYZ5733(req *types.Request) (resp *types.Response, err error) { //userId, ok := l.ctx.Value("userId").(int64) //if !ok { @@ -36,7 +41,6 @@ func (l *IVYZ5733Logic) IVYZ5733(req *types.Request) (resp *types.Response, err if !ok { return &types.Response{}, errors.New("系统错误,请联系管理员") } - // 1、解密 key, err := hex.DecodeString(secretKey) decryptData, err := crypto.AesDecrypt(req.Data, key) @@ -46,7 +50,6 @@ func (l *IVYZ5733Logic) IVYZ5733(req *types.Request) (resp *types.Response, err // 2、校验 var data validator.IVYZ5733Request - if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil { return nil, validatorErr } diff --git a/apps/api/internal/middleware/apiauthinterceptormiddleware.go b/apps/api/internal/middleware/apiauthinterceptormiddleware.go index 5e65ec2..b4f75d6 100644 --- a/apps/api/internal/middleware/apiauthinterceptormiddleware.go +++ b/apps/api/internal/middleware/apiauthinterceptormiddleware.go @@ -7,13 +7,13 @@ import ( "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/redis" xhttp "github.com/zeromicro/x/http" + "net" + "net/http" + "strings" "tianyuan-api/apps/sentinel/client/secret" "tianyuan-api/apps/sentinel/client/userproduct" "tianyuan-api/apps/sentinel/client/whitelist" "tianyuan-api/apps/sentinel/sentinel" - "net" - "net/http" - "strings" ) type ApiAuthInterceptorMiddleware struct { diff --git a/apps/gateway/internal/logic/product/getproductbyidlogic.go b/apps/gateway/internal/logic/product/getproductbyidlogic.go index 6c6482f..ff46b6a 100644 --- a/apps/gateway/internal/logic/product/getproductbyidlogic.go +++ b/apps/gateway/internal/logic/product/getproductbyidlogic.go @@ -43,6 +43,4 @@ func (l *GetProductByIdLogic) GetProductById(req *types.GetProductByIdReq) (resp ProductPrice: productResp.ProductPrice, }, }, nil - - return } diff --git a/pkg/crypto/generate.go b/pkg/crypto/generate.go index e4d7437..2d91c6d 100644 --- a/pkg/crypto/generate.go +++ b/pkg/crypto/generate.go @@ -4,6 +4,9 @@ import ( "crypto/rand" "encoding/hex" "io" + mathrand "math/rand" + "strconv" + "time" ) // 生成AES-128密钥的函数,符合市面规范 @@ -29,3 +32,32 @@ func GenerateSecretId() (string, error) { // 将字节数组转换为16进制字符串 return hex.EncodeToString(bytes), nil } + +// GenerateTransactionID 生成16位数的交易单号 +func GenerateTransactionID() string { + length := 16 + // 获取当前时间戳 + timestamp := time.Now().UnixNano() + + // 转换为字符串 + timeStr := strconv.FormatInt(timestamp, 10) + + // 生成随机数 + mathrand.Seed(time.Now().UnixNano()) + randomPart := strconv.Itoa(mathrand.Intn(1000000)) + + // 组合时间戳和随机数 + combined := timeStr + randomPart + + // 如果长度超出指定值,则截断;如果不够,则填充随机字符 + if len(combined) >= length { + return combined[:length] + } + + // 如果长度不够,填充0 + for len(combined) < length { + combined += strconv.Itoa(mathrand.Intn(10)) // 填充随机数 + } + + return combined +} diff --git a/pkg/response/response.go b/pkg/response/response.go index fc591ee..e642816 100644 --- a/pkg/response/response.go +++ b/pkg/response/response.go @@ -1,6 +1,7 @@ package response import ( + "context" "github.com/zeromicro/go-zero/rest/httpx" "net/http" ) @@ -11,59 +12,42 @@ type Response struct { Data interface{} `json:"data,omitempty"` Message string `json:"message"` } - -// 定义分页响应结构 -type PageResult struct { - List interface{} `json:"list"` - Total int64 `json:"total"` - Page int `json:"page"` - PageSize int `json:"pageSize"` +type ResponseWithTransactionID struct { + Response + TransactionID string `json:"transaction_id"` } // 响应成功 -func Success(w http.ResponseWriter, data interface{}) { - result := Response{ - Code: http.StatusOK, - Data: data, - Message: "操作成功", +func Success(ctx context.Context, w http.ResponseWriter, data interface{}) { + // 从上下文中获取 TransactionID + transactionID := ctx.Value("TransactionID") + + // 判断是否存在 TransactionID + if transactionID != nil { + result := ResponseWithTransactionID{ + Response: Response{ + Code: http.StatusOK, + Data: data, + Message: "success", + }, + TransactionID: transactionID.(string), // 将 TransactionID 添加到响应 + } + httpx.OkJsonCtx(ctx, w, result) // 返回带有 TransactionID 的响应 + } else { + result := Response{ + Code: http.StatusOK, + Data: data, + Message: "success", + } + httpx.OkJsonCtx(ctx, w, result) // 返回没有 TransactionID 的响应 } - httpx.OkJson(w, result) } // 响应失败 -func Fail(w http.ResponseWriter, code int, message string) { +func Fail(ctx context.Context, w http.ResponseWriter, err error) { result := Response{ - Code: code, - Message: message, + Code: -1, + Message: err.Error(), } - httpx.WriteJson(w, code, result) -} - -// 无权限 -func Unauthorized(w http.ResponseWriter, message string) { - result := Response{ - Code: http.StatusUnauthorized, - Message: message, - } - httpx.WriteJson(w, http.StatusUnauthorized, result) -} - -// 响应分页数据 -func Page(w http.ResponseWriter, list interface{}, total int64, page int, pageSize int) { - result := Response{ - Code: http.StatusOK, - Data: PageResult{List: list, Total: total, Page: page, PageSize: pageSize}, - Message: "查询成功", - } - httpx.OkJson(w, result) -} - -// 自定义错误响应 -func CustomError(w http.ResponseWriter, code int, message string, data interface{}) { - result := Response{ - Code: code, - Data: data, - Message: message, - } - httpx.WriteJson(w, code, result) + httpx.OkJsonCtx(ctx, w, result) }