Commit 4a26235b authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/optimize' into 'develop'

Feature/optimize See merge request !21
parents ed6563a8 b4ab5e9e
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/xinliangnote/go-util/aes" "github.com/xinliangnote/go-util/aes"
"net/url" "net/url"
"sort" "sort"
...@@ -39,14 +40,45 @@ func Aes() gin.HandlerFunc { ...@@ -39,14 +40,45 @@ func Aes() gin.HandlerFunc {
} }
} }
type Proof struct {
Ak string `json:"ak"`
Sn string `json:"sn"`
Ts string `json:"ts"`
Debug string `json:"debug"`
}
// 验证签名 // 验证签名
func verifySign(c *gin.Context) (map[string]string, error) { func verifySign(c *gin.Context) (map[string]string, error) {
var params = make(map[string]interface{})
if c.Request.Method == "POST" || c.Request.Method == "PUT" {
c.ShouldBindBodyWith(&params, binding.JSON)
//var bodyBytes []byte
//if c.Request.Body != nil {
// bodyBytes, _ = ioutil.ReadAll(c.Request.Body)
//}
//// 把刚刚读出来的再写进去
//c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
//json.Unmarshal(bodyBytes, &params)
//fmt.Println(params)
}
if "GET" == c.Request.Method {
keys := c.Request.URL.Query()
for k, v := range keys {
params[k] = v[0]
}
}
_ = c.Request.ParseForm() _ = c.Request.ParseForm()
req := c.Request.Form req := c.Request.Form
debug := strings.Join(c.Request.Form["debug"], "") for key, value := range params {
ak := strings.Join(c.Request.Form["ak"], "") req.Set(key, util.ToString(value))
sn := strings.Join(c.Request.Form["sn"], "") }
ts := strings.Join(c.Request.Form["ts"], "")
debug := strings.Join(req["debug"], "")
ak := strings.Join(req["ak"], "")
sn := strings.Join(req["sn"], "")
ts := strings.Join(req["ts"], "")
// 验证来源 // 验证来源
value, ok := config.ApiAuthConfig[ak] value, ok := config.ApiAuthConfig[ak]
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"net/url" "net/url"
"sort" "sort"
"strconv" "strconv"
...@@ -39,12 +40,36 @@ func Md5() gin.HandlerFunc { ...@@ -39,12 +40,36 @@ func Md5() gin.HandlerFunc {
// 验证签名 // 验证签名
func verifySign(c *gin.Context) (map[string]string, error) { func verifySign(c *gin.Context) (map[string]string, error) {
var params = make(map[string]interface{})
if c.Request.Method == "POST" || c.Request.Method == "PUT" {
c.ShouldBindBodyWith(&params, binding.JSON)
//var bodyBytes []byte
//if c.Request.Body != nil {
// bodyBytes, _ = ioutil.ReadAll(c.Request.Body)
//}
//// 把刚刚读出来的再写进去
//c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
//json.Unmarshal(bodyBytes, &params)
//fmt.Println(params)
}
if "GET" == c.Request.Method {
keys := c.Request.URL.Query()
for k, v := range keys {
params[k] = v[0]
}
}
_ = c.Request.ParseForm() _ = c.Request.ParseForm()
req := c.Request.Form req := c.Request.Form
debug := strings.Join(c.Request.Form["debug"], "") for key, value := range params {
ak := strings.Join(c.Request.Form["ak"], "") req.Set(key, util.ToString(value))
sn := strings.Join(c.Request.Form["sn"], "") }
ts := strings.Join(c.Request.Form["ts"], "")
debug := strings.Join(req["debug"], "")
ak := strings.Join(req["ak"], "")
sn := strings.Join(req["sn"], "")
ts := strings.Join(req["ts"], "")
// 验证来源 // 验证来源
value, ok := config.ApiAuthConfig[ak] value, ok := config.ApiAuthConfig[ak]
...@@ -66,9 +91,9 @@ func verifySign(c *gin.Context) (map[string]string, error) { ...@@ -66,9 +91,9 @@ func verifySign(c *gin.Context) (map[string]string, error) {
// 验证过期时间 // 验证过期时间
timestamp := time.Now().Unix() timestamp := time.Now().Unix()
exp, _ := strconv.ParseInt(config.AppSignExpiry, 10, 64) exp, _ := strconv.ParseInt(config.AppSignExpiry, 10, 64)
tsInt, _ := strconv.ParseInt(ts, 10, 64) tsInt, _ := strconv.ParseInt(ts, 10, 64)
if tsInt > timestamp || timestamp - tsInt >= exp { if tsInt > timestamp || timestamp-tsInt >= exp {
return nil, errors.New("ts Error") return nil, errors.New("ts Error")
} }
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/xinliangnote/go-util/rsa" "github.com/xinliangnote/go-util/rsa"
"net/url" "net/url"
"sort" "sort"
...@@ -41,12 +42,36 @@ func Rsa() gin.HandlerFunc { ...@@ -41,12 +42,36 @@ func Rsa() gin.HandlerFunc {
// 验证签名 // 验证签名
func verifySign(c *gin.Context) (map[string]string, error) { func verifySign(c *gin.Context) (map[string]string, error) {
var params = make(map[string]interface{})
if c.Request.Method == "POST" || c.Request.Method == "PUT" {
c.ShouldBindBodyWith(&params, binding.JSON)
//var bodyBytes []byte
//if c.Request.Body != nil {
// bodyBytes, _ = ioutil.ReadAll(c.Request.Body)
//}
//// 把刚刚读出来的再写进去
//c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
//json.Unmarshal(bodyBytes, &params)
//fmt.Println(params)
}
if "GET" == c.Request.Method {
keys := c.Request.URL.Query()
for k, v := range keys {
params[k] = v[0]
}
}
_ = c.Request.ParseForm() _ = c.Request.ParseForm()
req := c.Request.Form req := c.Request.Form
debug := strings.Join(c.Request.Form["debug"], "") for key, value := range params {
ak := strings.Join(c.Request.Form["ak"], "") req.Set(key, util.ToString(value))
sn := strings.Join(c.Request.Form["sn"], "") }
ts := strings.Join(c.Request.Form["ts"], "")
debug := strings.Join(req["debug"], "")
ak := strings.Join(req["ak"], "")
sn := strings.Join(req["sn"], "")
ts := strings.Join(req["ts"], "")
// 验证来源 // 验证来源
value, ok := config.ApiAuthConfig[ak] value, ok := config.ApiAuthConfig[ak]
......
...@@ -15,6 +15,7 @@ type Fee struct { ...@@ -15,6 +15,7 @@ type Fee struct {
Fees float32 `json:"fees"` Fees float32 `json:"fees"`
TransactionFees float32 `json:"transaction_fees"` TransactionFees float32 `json:"transaction_fees"`
CoinName string `json:"coin_name"` CoinName string `json:"coin_name"`
TransferType uint8 `json:"transfer_type"`
CoinInfo *Coin `gorm:"foreignkey:Cid" json:"coin,omitempty"` CoinInfo *Coin `gorm:"foreignkey:Cid" json:"coin,omitempty"`
} }
...@@ -76,9 +77,10 @@ func GetFees(pageNum, pageSize int, maps interface{}) ([]*Fee, error) { ...@@ -76,9 +77,10 @@ func GetFees(pageNum, pageSize int, maps interface{}) ([]*Fee, error) {
func AddFee(data map[string]interface{}) (error) { func AddFee(data map[string]interface{}) (error) {
fee := Fee{ fee := Fee{
Cid: data["cid"].(int), Cid: data["cid"].(int),
PlatformId: data["platform_id"].(int), PlatformId: data["platform_id"].(int),
CoinName: data["coin_name"].(string), CoinName: data["coin_name"].(string),
TransferType: data["transfer_type"].(uint8),
} }
if err := db.Create(&fee).Error; err != nil { if err := db.Create(&fee).Error; err != nil {
return err return err
......
package util
import (
"encoding/binary"
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
"strings"
)
// ToString returns the string result converted by src.
func ToString(src interface{}) string {
switch v := src.(type) {
case int, int8, int16, int32, int64:
return strconv.FormatInt(ToInt64(v), 10)
case uint, uint8, uint16, uint32, uint64, uintptr:
return strconv.FormatUint(ToUint64(v), 10)
case float32, float64, complex64, complex128:
return strconv.FormatFloat(ToFloat64(v), 'f', -1, 64)
case string:
return v
case []byte:
return string(v)
case []rune:
return string(v)
case bool:
return strconv.FormatBool(v)
default:
return fmt.Sprint(v)
}
}
// ToBool returns the bool result converted by src.
func ToBool(src interface{}) bool {
switch v := src.(type) {
case int, int8, int16, int32, int64:
return ToInt64(v) > 0
case uint, uint8, uint16, uint32, uint64, uintptr:
return ToUint64(v) > 0
case float32, float64, complex64, complex128:
return ToFloat64(v) > 0
case bool:
return v
case string, []byte, []rune:
result, _ := strconv.ParseBool(ToString(v))
return result
default:
return false
}
}
// ToInt returns the int result converted by src.
func ToInt(src interface{}) int {
return int(ToInt64(src))
}
// ToInt32 returns the int32 result converted by src.
func ToInt32(src interface{}) int32 {
return int32(ToInt64(src))
}
// ToInt64 returns the int64 result converted by src.
func ToInt64(src interface{}) int64 {
switch v := src.(type) {
case int:
return int64(v)
case int8:
return int64(v)
case int16:
return int64(v)
case int32:
return int64(v)
case int64:
return v
case uint:
return int64(v)
case uint8:
return int64(v)
case uint16:
return int64(v)
case uint32:
return int64(v)
case uint64:
return int64(v)
case uintptr:
return int64(v)
case float32:
return int64(v)
case float64:
return int64(v)
case complex64:
return int64(real(v))
case complex128:
return int64(real(v))
case bool:
if v {
return 1
}
return 0
case string:
v = strings.TrimSpace(v)
index := strings.Index(v, ".")
if index != -1 {
v = v[:index]
}
result, _ := strconv.ParseInt(v, 10, 64)
return result
case []byte:
return BytesToInt64(v)
default:
return 0
}
}
// ToUint returns the uint result converted by src.
func ToUint(src interface{}) uint {
return uint(ToUint64(src))
}
// ToUint32 returns the uint32 result converted by src.
func ToUint32(src interface{}) uint32 {
return uint32(ToUint64(src))
}
// ToUint64 returns the uint64 result converted by src.
func ToUint64(src interface{}) uint64 {
switch v := src.(type) {
case int:
return uint64(v)
case int8:
return uint64(v)
case int16:
return uint64(v)
case int32:
return uint64(v)
case int64:
return uint64(v)
case uint:
return uint64(v)
case uint8:
return uint64(v)
case uint16:
return uint64(v)
case uint32:
return uint64(v)
case uint64:
return v
case uintptr:
return uint64(v)
case float32:
return uint64(v)
case float64:
return uint64(v)
case complex64:
return uint64(real(v))
case complex128:
return uint64(real(v))
case bool:
if v {
return 1
}
return 0
case string:
v = strings.TrimSpace(v)
index := strings.Index(v, ".")
if index != -1 {
v = v[:index]
}
result, _ := strconv.ParseUint(v, 10, 64)
return result
case []byte:
return BytesToUint64(v)
default:
return 0
}
}
// ToFloat returns the float64 result converted by src.
func ToFloat(src interface{}) float64 {
return ToFloat64(src)
}
// ToFloat32 returns the float32 result converted by src.
func ToFloat32(src interface{}) float32 {
return float32(ToFloat64(src))
}
// ToFloat64 returns the float64 result converted by src.
func ToFloat64(src interface{}) float64 {
switch v := src.(type) {
case int, int8, int16, int32, int64:
return float64(ToInt64(v))
case uint, uint8, uint16, uint32, uint64, uintptr:
return float64(ToUint64(v))
case float32:
return float64(v)
case float64:
return v
case complex64:
return float64(real(v))
case complex128:
return real(v)
case bool:
if v {
return 1
}
return 0
case string:
v = strings.TrimSpace(v)
result, _ := strconv.ParseFloat(v, 64)
return result
case []byte:
return BytesToFloat64(v)
default:
return 0
}
}
// BytesToInt64 returns the int64 result converted by byte slice bytes.
func BytesToInt64(bytes []byte) int64 {
return int64(BytesToUint64(bytes))
}
// Int64ToBytes returns the byte slice result converted by int64 i.
func Int64ToBytes(i int64) []byte {
return Uint64ToBytes(uint64(i))
}
// BytesToUint64 returns the uint64 result converted by byte slice bytes.
func BytesToUint64(bytes []byte) uint64 {
return binary.BigEndian.Uint64(bytes)
}
// Uint64ToBytes returns the byte slice result converted by uint64 i.
func Uint64ToBytes(i uint64) []byte {
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, i)
return bytes
}
// BytesToFloat64 returns the float64 result converted by byte slice bytes.
func BytesToFloat64(bytes []byte) float64 {
bits := binary.BigEndian.Uint64(bytes)
return math.Float64frombits(bits)
}
// Float64ToBytes returns the byte slice result converted by float64 f.
func Float64ToBytes(f float64) []byte {
bits := math.Float64bits(f)
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, bits)
return bytes
}
// isStruct reports whether i is struct.
func isStruct(i interface{}) (reflect.Value, bool) {
v := reflect.ValueOf(i)
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return reflect.Value{}, false
}
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return reflect.Value{}, false
}
return v, true
}
// getStructFieldName returns the struct field name.
func getStructFieldName(sf reflect.StructField) string {
name := strings.SplitN(sf.Tag.Get("json"), ",", 2)[0]
if name == "-" {
return ""
} else if name == "" {
return sf.Name
}
return name
}
// StructToInterfaceMap returns the map[string]interface{} result converted by struct s.
func StructToInterfaceMap(s interface{}, ignoreZeroValue ...bool) map[string]interface{} {
m := make(map[string]interface{})
v, flag := isStruct(s)
if !flag {
return m
}
ignore := false
if len(ignoreZeroValue) != 0 {
ignore = ignoreZeroValue[0]
}
t := v.Type()
for i := 0; i < t.NumField(); i++ {
f := v.Field(i)
name := getStructFieldName(t.Field(i))
if name == "" || (ignore && f.IsZero()) {
continue
}
if f.Kind() == reflect.Ptr && !f.IsNil() {
f = f.Elem()
}
m[name] = f.Interface()
}
return m
}
// StructToStringMap returns the map[string]string result converted by struct s.
func StructToStringMap(s interface{}, ignoreZeroValue ...bool) map[string]string {
m := make(map[string]string)
v, flag := isStruct(s)
if !flag {
return m
}
ignore := false
if len(ignoreZeroValue) != 0 {
ignore = ignoreZeroValue[0]
}
t := v.Type()
for i := 0; i < t.NumField(); i++ {
f := v.Field(i)
name := getStructFieldName(t.Field(i))
if name == "" || (ignore && f.IsZero()) {
continue
}
if f.Kind() == reflect.Ptr && !f.IsNil() {
f = f.Elem()
}
m[name] = ToString(f.Interface())
}
return m
}
// ObjToMapString 将struct结构体转换成map
func ObjToMapString(obj interface{}) map[string]string {
m := make(map[string]string)
elem := reflect.ValueOf(obj).Elem()
relType := elem.Type()
for i := 0; i < relType.NumField(); i++ {
m[relType.Field(i).Name] = ToString(elem.Field(i).Interface())
}
return m
}
// JsonToMapString 将json结构体转换成map
func JsonToMapString(in interface{}) map[string]string {
m := make(map[string]interface{})
j, _ := json.Marshal(in)
var s string
decoder := json.NewDecoder(strings.NewReader(string(j)))
decoder.UseNumber()
decoder.Decode(&m)
//json.Unmarshal(j, &m)
ret := make(map[string]string)
for k, v := range m {
s = fmt.Sprintf("%v", v)
if s == "" {
continue
}
ret[k] = s
}
return ret
}
...@@ -18,28 +18,6 @@ func Setup() { ...@@ -18,28 +18,6 @@ func Setup() {
jwtSecret = []byte(setting.AppSetting.JwtSecret) jwtSecret = []byte(setting.AppSetting.JwtSecret)
} }
func ToInt(o interface{}) int {
if o == nil {
return 1
}
switch t := o.(type) {
case float64:
return int(t)
case int:
return int(t)
case int64:
return int(t)
case string:
tInt, err := strconv.Atoi(t)
if err != nil {
return -1
}
return tInt
default:
return -1
}
}
// IPString2Long 把ip字符串转为数值 // IPString2Long 把ip字符串转为数值
func IPString2Long(ip string) (uint, error) { func IPString2Long(ip string) (uint, error) {
b := net.ParseIP(ip).To4() b := net.ParseIP(ip).To4()
......
...@@ -18,24 +18,32 @@ func GetTransactionFee(c *gin.Context) { ...@@ -18,24 +18,32 @@ func GetTransactionFee(c *gin.Context) {
name := c.DefaultQuery("name", "") name := c.DefaultQuery("name", "")
valid.Required(name, "name").Message("币种不能为空") valid.Required(name, "name").Message("币种不能为空")
transfer_type := com.StrTo(c.DefaultQuery("transfer_type", "1")).MustUint8()
if valid.HasErrors() { if valid.HasErrors() {
handler.SendResponse(c, valid.Errors[0], nil) handler.SendResponse(c, valid.Errors[0], nil)
return return
} }
feeService := fee_service.Fee{ feeService := fee_service.Fee{
PlatformId: platform_id, PlatformId: platform_id,
CoinName: name, CoinName: name,
PageNum: util.GetPage(c), TransferType: transfer_type,
PageSize: util.GetLimit(c), PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
} }
total_fee, err_fee := feeService.Count() total_fee, err_fee := feeService.Count()
if err_fee != nil || 0 == total_fee { if err_fee != nil {
handler.SendResponse(c, errno.ErrCountCoin, nil) handler.SendResponse(c, errno.ErrCountCoin, nil)
return return
} }
if 0 == total_fee {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
fee, err := feeService.Search() fee, err := feeService.Search()
if err != nil { if err != nil {
handler.SendResponse(c, errno.ErrCountCoin, nil) handler.SendResponse(c, errno.ErrCountCoin, nil)
......
...@@ -37,7 +37,7 @@ func GetTransactionCoins(c *gin.Context) { ...@@ -37,7 +37,7 @@ func GetTransactionCoins(c *gin.Context) {
} }
vals := []string{} vals := []string{}
for _,value := range fees { for _, value := range fees {
if "" == value.CoinName { if "" == value.CoinName {
continue continue
} }
...@@ -61,10 +61,13 @@ func GetTransactionFees(c *gin.Context) { ...@@ -61,10 +61,13 @@ func GetTransactionFees(c *gin.Context) {
} }
} }
transfer_type := com.StrTo(c.DefaultQuery("transfer_type", "1")).MustUint8()
feeService := fee_service.Fee{ feeService := fee_service.Fee{
PlatformId: platform_id, PlatformId: platform_id,
PageNum: util.GetPage(c), TransferType: transfer_type,
PageSize: util.GetLimit(c), PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
} }
total, err := feeService.Count() total, err := feeService.Count()
......
...@@ -121,24 +121,28 @@ func AddRecommendCoin(c *gin.Context) { ...@@ -121,24 +121,28 @@ func AddRecommendCoin(c *gin.Context) {
recommendCoinService.Add() recommendCoinService.Add()
if 2 == recommend_coin.Type { if 2 == recommend_coin.Type {
feeValidate := fee_service.Fee{ for i := 1; i < 3; i++ {
PlatformId: platform_id, feeValidate := fee_service.Fee{
Cid: coin_id, PlatformId: platform_id,
Cid: coin_id,
TransferType: uint8(i),
}
total_fee, err := feeValidate.Count()
if err != nil || total_fee > 0 {
continue
}
coin, _ := coinService.Get()
feeService := fee_service.Fee{
Cid: coin_id,
PlatformId: platform_id,
CoinName: coin.Name,
TransferType: uint8(i),
}
feeService.Add()
} }
total_fee, err := feeValidate.Count()
if err != nil || total_fee > 0 {
continue
}
coin, _ := coinService.Get()
feeService := fee_service.Fee{
Cid: coin_id,
PlatformId: platform_id,
CoinName: coin.Name,
}
feeService.Add()
} }
} }
......
...@@ -12,6 +12,7 @@ type Fee struct { ...@@ -12,6 +12,7 @@ type Fee struct {
Fees float32 Fees float32
TransactionFees float32 TransactionFees float32
CoinName string CoinName string
TransferType uint8
PageNum int PageNum int
PageSize int PageSize int
...@@ -40,9 +41,10 @@ func (c *Fee) GetAll() ([]*models.Fee, error) { ...@@ -40,9 +41,10 @@ func (c *Fee) GetAll() ([]*models.Fee, error) {
func (c *Fee) Add() error { func (c *Fee) Add() error {
fee := map[string]interface{}{ fee := map[string]interface{}{
"cid": c.Cid, "cid": c.Cid,
"platform_id": c.PlatformId, "platform_id": c.PlatformId,
"coin_name": c.CoinName, "coin_name": c.CoinName,
"transfer_type": c.TransferType,
} }
if err := models.AddFee(fee); err != nil { if err := models.AddFee(fee); err != nil {
...@@ -117,5 +119,9 @@ func (c *Fee) getMaps() (map[string]interface{}) { ...@@ -117,5 +119,9 @@ func (c *Fee) getMaps() (map[string]interface{}) {
maps["coin_name"] = c.CoinName maps["coin_name"] = c.CoinName
} }
if c.TransferType != 0 {
maps["transfer_type"] = c.TransferType
}
return maps return maps
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment