temp
This commit is contained in:
parent
f649d32c19
commit
8c09120db6
2
aes.go
2
aes.go
@ -48,7 +48,7 @@ type Data struct {
|
||||
|
||||
func main() {
|
||||
// 定义 AES 密钥
|
||||
key, _ := hex.DecodeString("958de6826370b57d9ae93b88e5009e26")
|
||||
key, _ := hex.DecodeString("85af347e771bd631bf734068ad5798cc")
|
||||
|
||||
var data interface{}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -43,6 +43,4 @@ func (l *GetProductByIdLogic) GetProductById(req *types.GetProductByIdReq) (resp
|
||||
ProductPrice: productResp.ProductPrice,
|
||||
},
|
||||
}, nil
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user