diff --git a/db/db_sys_cfg.go b/db/db_sys_cfg.go deleted file mode 100644 index 48975a6..0000000 --- a/db/db_sys_cfg.go +++ /dev/null @@ -1,50 +0,0 @@ -package db - -import ( - "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/db/model" - "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md" - cache "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils/cache" - "fmt" - "xorm.io/xorm" -) - -// 获取一条记录 -func SysCfgGetOne(Db *xorm.Engine, key string) (*model.SysCfg, error) { - var cfgList model.SysCfg - if has, err := Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false { - return nil, err - } - return &cfgList, nil -} - -//单条记录获取DB -func SysCfgGetWithDb(eg *xorm.Engine, masterId string, HKey string) string { - cacheKey := fmt.Sprintf(md.AppCfgCacheKey, masterId, HKey[0:1]) - get, err := cache.HGetString(cacheKey, HKey) - if err != nil || get == "" { - cfg, err := SysCfgGetOne(eg, HKey) - if err != nil || cfg == nil { - return "" - } - - // key是否存在 - cacheKeyExist := false - if cache.Exists(cacheKey) { - cacheKeyExist = true - } - - // 设置缓存 - _, err = cache.HSet(cacheKey, HKey, cfg.Val) - if err != nil { - return "" - } - if !cacheKeyExist { // 如果是首次设置 设置过期时间 - _, err := cache.Expire(cacheKey, md.CfgCacheTime) - if err != nil { - return "" - } - } - return cfg.Val - } - return get -} diff --git a/go.mod b/go.mod index 19f6dc3..9c4ad4e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git go 1.15 require ( - code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git v1.1.4 github.com/go-sql-driver/mysql v1.6.0 github.com/iGoogle-ink/gopay v1.5.36 xorm.io/builder v0.3.10 // indirect diff --git a/lib/local_alipay/api.go b/lib/local_alipay/api.go index b5caad8..ad353bd 100644 --- a/lib/local_alipay/api.go +++ b/lib/local_alipay/api.go @@ -2,7 +2,6 @@ package local_alipay import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md" - zhios_tool_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils/logx" "fmt" "github.com/iGoogle-ink/gopay" "github.com/iGoogle-ink/gopay/alipay" @@ -86,7 +85,7 @@ func TradeAppPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS str //手机APP支付参数请求 payParam, err := client.TradeAppPay(body) if err != nil { - return "", zhios_tool_logx.Warn(err) + return "", err } return payParam, nil } @@ -165,7 +164,7 @@ func TradeWapPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS, pa //手机网站支付请求 payUrl, err := client.TradeWapPay(body) if err != nil { - return "", zhios_tool_logx.Warn(err) + return "", err } return payUrl, nil @@ -238,7 +237,7 @@ func TradeCreate(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS str // 2、client.SystemOauthToken() 返回取值:aliRsp.SystemOauthTokenResponse.UserId buyer_id, err := client.SystemOauthToken(body) if err != nil { - return nil, zhios_tool_logx.Warn(err) + return nil, err } body.Set("buyer_id", buyer_id) body.Set("out_trade_no", orderID) @@ -247,10 +246,9 @@ func TradeCreate(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS str aliRsp, err := client.TradeCreate(body) if err != nil { - return nil, zhios_tool_logx.Warn(err) + return nil, err } - zhios_tool_logx.Warn("aliRsp:", *aliRsp) - zhios_tool_logx.Warn("aliRsp.TradeNo:", aliRsp.Response.TradeNo) + return aliRsp, nil } diff --git a/pay/pay_by_own.go b/pay/pay_by_own.go index aa8b3ef..5ef648d 100644 --- a/pay/pay_by_own.go +++ b/pay/pay_by_own.go @@ -3,9 +3,9 @@ package pay import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/lib/local_alipay" "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md" - zhios_tool_e "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/e" - zhios_tool_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" + zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils" "fmt" + "github.com/pkg/errors" ) //自有支付 支付宝 @@ -19,7 +19,7 @@ func Alipay(args map[string]string) (string, error) { WxAppletFilepathUrl: args["wx_applet_filepath_url"], } if args["private_key"] == "" || args["app_id"] == "" { - return "", zhios_tool_e.NewErr(400, "请在后台正确配置支付宝") + return "", errors.New("请在后台正确配置支付宝") } var param interface{} var err error @@ -31,13 +31,13 @@ func Alipay(args map[string]string) (string, error) { case md.PLATFORM_ANDROID, md.PLATFORM_IOS: param, err = local_alipay.TradeAppPay(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], paySet) default: - return "", zhios_tool_e.NewErr(400, "支付平台不存在") + return "", errors.New("支付平台不存在") } if err != nil { fmt.Println("支付宝错误日志") fmt.Println(param) fmt.Println(err) - return "", zhios_tool_e.NewErr(400, "支付宝订单创建失败") + return "", errors.New("支付宝订单创建失败") } - return zhios_tool_utils.AnyToString(param), nil + return zhios_pay_utils.AnyToString(param), nil } diff --git a/pay/pay_config.go b/pay/pay_config.go index f254cb4..75561ee 100644 --- a/pay/pay_config.go +++ b/pay/pay_config.go @@ -3,7 +3,7 @@ package pay import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/db" "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/db/model" - zhios_tool_e "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/e" + "errors" ) //支付渠道获取 @@ -14,10 +14,10 @@ func JudgePayChannel(mid, paySceneIdentifier string) (model.PayChannelMaster, er return payChannelMaster, err } if !res { - return payChannelMaster, zhios_tool_e.NewErr(400, "查询数据失败") + return payChannelMaster, errors.New("查询数据失败") } if payChannelMaster.ChannelId == 0 { - return payChannelMaster, zhios_tool_e.NewErr(400, "请联系服务商绑定支付渠道") + return payChannelMaster, errors.New("请联系服务商绑定支付渠道") } return payChannelMaster, nil } diff --git a/utils/convert.go b/utils/convert.go new file mode 100644 index 0000000..ded0d90 --- /dev/null +++ b/utils/convert.go @@ -0,0 +1,366 @@ +package zhios_pay_utils + +import ( + "encoding/binary" + "encoding/json" + "fmt" + "math" + "strconv" + "strings" +) + +func ToString(raw interface{}, e error) (res string) { + if e != nil { + return "" + } + return AnyToString(raw) +} + +func ToInt64(raw interface{}, e error) int64 { + if e != nil { + return 0 + } + return AnyToInt64(raw) +} + +func AnyToBool(raw interface{}) bool { + switch i := raw.(type) { + case float32, float64, int, int64, uint, uint8, uint16, uint32, uint64, int8, int16, int32: + return i != 0 + case []byte: + return i != nil + case string: + if i == "false" { + return false + } + return i != "" + case error: + return false + case nil: + return true + } + val := fmt.Sprint(raw) + val = strings.TrimLeft(val, "&") + if strings.TrimLeft(val, "{}") == "" { + return false + } + if strings.TrimLeft(val, "[]") == "" { + return false + } + // ptr type + b, err := json.Marshal(raw) + if err != nil { + return false + } + if strings.TrimLeft(string(b), "\"\"") == "" { + return false + } + if strings.TrimLeft(string(b), "{}") == "" { + return false + } + return true +} + +func AnyToInt64(raw interface{}) int64 { + switch i := raw.(type) { + case string: + res, _ := strconv.ParseInt(i, 10, 64) + return res + case []byte: + return BytesToInt64(i) + case int: + return int64(i) + case int64: + return i + case uint: + return int64(i) + case uint8: + return int64(i) + case uint16: + return int64(i) + case uint32: + return int64(i) + case uint64: + return int64(i) + case int8: + return int64(i) + case int16: + return int64(i) + case int32: + return int64(i) + case float32: + return int64(i) + case float64: + return int64(i) + case error: + return 0 + case bool: + if i { + return 1 + } + return 0 + } + return 0 +} + +func AnyToString(raw interface{}) string { + switch i := raw.(type) { + case []byte: + return string(i) + case int: + return strconv.FormatInt(int64(i), 10) + case int64: + return strconv.FormatInt(i, 10) + case float32: + return Float64ToStr(float64(i)) + case float64: + return Float64ToStr(i) + case uint: + return strconv.FormatInt(int64(i), 10) + case uint8: + return strconv.FormatInt(int64(i), 10) + case uint16: + return strconv.FormatInt(int64(i), 10) + case uint32: + return strconv.FormatInt(int64(i), 10) + case uint64: + return strconv.FormatInt(int64(i), 10) + case int8: + return strconv.FormatInt(int64(i), 10) + case int16: + return strconv.FormatInt(int64(i), 10) + case int32: + return strconv.FormatInt(int64(i), 10) + case string: + return i + case error: + return i.Error() + case bool: + return strconv.FormatBool(i) + } + return fmt.Sprintf("%#v", raw) +} + +func AnyToFloat64(raw interface{}) float64 { + switch i := raw.(type) { + case []byte: + f, _ := strconv.ParseFloat(string(i), 64) + return f + case int: + return float64(i) + case int64: + return float64(i) + case float32: + return float64(i) + case float64: + return i + case uint: + return float64(i) + case uint8: + return float64(i) + case uint16: + return float64(i) + case uint32: + return float64(i) + case uint64: + return float64(i) + case int8: + return float64(i) + case int16: + return float64(i) + case int32: + return float64(i) + case string: + f, _ := strconv.ParseFloat(i, 64) + return f + case bool: + if i { + return 1 + } + } + return 0 +} + +func ToByte(raw interface{}, e error) []byte { + if e != nil { + return []byte{} + } + switch i := raw.(type) { + case string: + return []byte(i) + case int: + return Int64ToBytes(int64(i)) + case int64: + return Int64ToBytes(i) + case float32: + return Float32ToByte(i) + case float64: + return Float64ToByte(i) + case uint: + return Int64ToBytes(int64(i)) + case uint8: + return Int64ToBytes(int64(i)) + case uint16: + return Int64ToBytes(int64(i)) + case uint32: + return Int64ToBytes(int64(i)) + case uint64: + return Int64ToBytes(int64(i)) + case int8: + return Int64ToBytes(int64(i)) + case int16: + return Int64ToBytes(int64(i)) + case int32: + return Int64ToBytes(int64(i)) + case []byte: + return i + case error: + return []byte(i.Error()) + case bool: + if i { + return []byte("true") + } + return []byte("false") + } + return []byte(fmt.Sprintf("%#v", raw)) +} + +func Int64ToBytes(i int64) []byte { + var buf = make([]byte, 8) + binary.BigEndian.PutUint64(buf, uint64(i)) + return buf +} + +func BytesToInt64(buf []byte) int64 { + return int64(binary.BigEndian.Uint64(buf)) +} + +func StrToInt(s string) int { + res, _ := strconv.Atoi(s) + return res +} + +func StrToInt64(s string) int64 { + res, _ := strconv.ParseInt(s, 10, 64) + return res +} + +func Float32ToByte(float float32) []byte { + bits := math.Float32bits(float) + bytes := make([]byte, 4) + binary.LittleEndian.PutUint32(bytes, bits) + + return bytes +} + +func ByteToFloat32(bytes []byte) float32 { + bits := binary.LittleEndian.Uint32(bytes) + return math.Float32frombits(bits) +} + +func Float64ToByte(float float64) []byte { + bits := math.Float64bits(float) + bytes := make([]byte, 8) + binary.LittleEndian.PutUint64(bytes, bits) + return bytes +} + +func ByteToFloat64(bytes []byte) float64 { + bits := binary.LittleEndian.Uint64(bytes) + return math.Float64frombits(bits) +} + +func Float64ToStr(f float64) string { + return strconv.FormatFloat(f, 'f', 2, 64) +} +func Float64ToStrPrec1(f float64) string { + return strconv.FormatFloat(f, 'f', 1, 64) +} +func Float64ToStrByPrec(f float64, prec int) string { + return strconv.FormatFloat(f, 'f', prec, 64) +} + +func Float32ToStr(f float32) string { + return Float64ToStr(float64(f)) +} + +func StrToFloat64(s string) float64 { + res, err := strconv.ParseFloat(s, 64) + if err != nil { + return 0 + } + return res +} +func StrToFormat(s string, prec int) string { + ex := strings.Split(s, ".") + if len(ex) == 2 { + if StrToFloat64(ex[1]) == 0 { //小数点后面为空就是不要小数点了 + return ex[0] + } + //看取多少位 + str := ex[1] + str1 := str + if prec < len(str) { + str1 = str[0:prec] + } else { + for i := 0; i < prec-len(str); i++ { + str1 += "0" + } + } + if prec > 0 { + return ex[0] + "." + str1 + } else { + return ex[0] + } + } + return s +} + +func StrToFloat32(s string) float32 { + res, err := strconv.ParseFloat(s, 32) + if err != nil { + return 0 + } + return float32(res) +} + +func StrToBool(s string) bool { + b, _ := strconv.ParseBool(s) + return b +} + +func BoolToStr(b bool) string { + if b { + return "true" + } + return "false" +} + +func FloatToInt64(f float64) int64 { + return int64(f) +} + +func IntToStr(i int) string { + return strconv.Itoa(i) +} + +func Int64ToStr(i int64) string { + return strconv.FormatInt(i, 10) +} + +func IntToFloat64(i int) float64 { + s := strconv.Itoa(i) + res, err := strconv.ParseFloat(s, 64) + if err != nil { + return 0 + } + return res +} +func Int64ToFloat64(i int64) float64 { + s := strconv.FormatInt(i, 10) + res, err := strconv.ParseFloat(s, 64) + if err != nil { + return 0 + } + return res +}