102 lines
2.4 KiB
Go
102 lines
2.4 KiB
Go
package service
|
||
|
||
import (
|
||
"fmt"
|
||
"reflect"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"sandc/pkg/utils"
|
||
|
||
"github.com/google/wire"
|
||
"github.com/shopspring/decimal"
|
||
)
|
||
|
||
// ProviderSet is service providers.
|
||
var ProviderSet = wire.NewSet(NewEonlineService)
|
||
|
||
// 定义加密key Secretkey
|
||
const Secretkey = "eonline~#*^%$@!~0702"
|
||
|
||
// validateSignRequest 验证参数请求
|
||
// Ts, Sign 参数必备
|
||
// signFixedParameters 签名固定参数
|
||
|
||
func (c *EonlineService) validateSignRequest(in any, signFixedParameters []string) error {
|
||
var (
|
||
// ts int64
|
||
sign string
|
||
// err error
|
||
)
|
||
defer func() {
|
||
if err := recover(); err != nil {
|
||
fmt.Println("validateRequest panic: ", err)
|
||
}
|
||
}()
|
||
|
||
v := reflect.ValueOf(in)
|
||
|
||
// 获取签名
|
||
if signValue, ok := v.FieldByName("Sign").Interface().(string); ok {
|
||
sign = signValue
|
||
}
|
||
if sign == "" {
|
||
return fmt.Errorf("Sign不能为空")
|
||
}
|
||
|
||
// // 获取时间戳,ts 字符串转换为int64
|
||
// if tsValue, ok := v.FieldByName("Ts").Interface().(string); ok {
|
||
// ts, err = strconv.ParseInt(tsValue, 10, 64)
|
||
// if err != nil {
|
||
// return fmt.Errorf("Ts转换失败")
|
||
// }
|
||
// }
|
||
// if ts == 0 {
|
||
// return fmt.Errorf("Ts不能为空")
|
||
// }
|
||
//
|
||
// currentTime := time.Now().Unix()
|
||
// // 比较时间戳是否超过60秒, QA环境不判断次时间
|
||
// if currentTime-ts > 60 || currentTime-ts < -60 {
|
||
// fmt.Println("ts invalid: ", ts, currentTime, currentTime-ts)
|
||
// if c.conf.Server.Env != "qa" {
|
||
// return fmt.Errorf("ts invalid")
|
||
// }
|
||
// }
|
||
secretStr := Secretkey
|
||
for _, sfp := range signFixedParameters {
|
||
fieldName := strings.ToLower(sfp)
|
||
tmpValue := ""
|
||
switch vtype := v.FieldByName(sfp).Interface().(type) {
|
||
case string:
|
||
tmpValue = vtype
|
||
case float32:
|
||
tmpValue = decimal.NewFromFloat32(vtype).String()
|
||
case float64:
|
||
tmpValue = decimal.NewFromFloat(vtype).String()
|
||
case int64:
|
||
tmpValue = decimal.NewFromInt(vtype).String()
|
||
case bool:
|
||
tmpValue = strconv.FormatBool(vtype)
|
||
case int32:
|
||
tmpValue = decimal.NewFromInt32(vtype).String()
|
||
default:
|
||
return fmt.Errorf("参数错误: %s", sfp)
|
||
}
|
||
|
||
secretStr += fmt.Sprintf("%s=%s", fieldName, tmpValue)
|
||
}
|
||
|
||
secretSignStr := utils.MD5Hex([]byte(secretStr))
|
||
if secretSignStr != sign {
|
||
fmt.Println("---------签名错误---------", time.Now().Format("2006-01-02 15:04:05"))
|
||
fmt.Println("加密前: ", secretStr)
|
||
fmt.Println("加密后: ", secretSignStr)
|
||
fmt.Println("sign: ", sign)
|
||
return fmt.Errorf("签名错误")
|
||
}
|
||
|
||
return nil
|
||
}
|