Commit 4fbd6acd authored by shajiaiming's avatar shajiaiming

merge master

parents f76d3676 ce6b1492
package auth
import (
"github.com/gin-gonic/gin"
"bwallet/pkg/e"
"bwallet/models"
"net/http"
)
func AUTH() gin.HandlerFunc {
return func(c *gin.Context) {
var code int
var data interface{}
code = e.SUCCESS
token := c.Request.Header.Get("Token")
if token == "" {
code = e.INVALID_PARAMS
} else {
_, err := models.CheckToken(token)
if err != nil {
code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
}
}
if code != e.SUCCESS {
c.JSON(http.StatusUnauthorized, gin.H{
"code": code,
"msg": e.GetMsg(code),
"data": data,
})
c.Abort()
return
}
c.Next()
}
}
package auth
import (
"bwallet/pkg/errno"
"bwallet/pkg/handler"
"bwallet/pkg/util"
"github.com/gin-gonic/gin"
"strings"
)
func AUTH() gin.HandlerFunc {
return func(ctx *gin.Context) {
token := ctx.Request.Header.Get("Token")
user, _ := util.ParseToken(token)
if !strings.Contains(strings.ToLower(user.UserInfo.Username), "kefu") {
return
}
handlerName := strings.Split(ctx.HandlerName(), ".")[1]
handleAlowed := []string{"GetCoinChains", "GetUserChains", "AddOperationLog"}
_, handle := util.Contains(handleAlowed, handlerName)
if !handle {
handler.SendResponse(ctx, errno.PermissionDenied, nil)
ctx.Abort()
return
}
ctx.Next()
}
}
package e
type HandleAllowedDesc map[int32][]string
var HandleAllowed = HandleAllowedDesc{
}
...@@ -3,6 +3,7 @@ package util ...@@ -3,6 +3,7 @@ package util
import ( import (
"bwallet/pkg/setting" "bwallet/pkg/setting"
"crypto/md5" "crypto/md5"
"encoding/binary"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
...@@ -109,3 +110,28 @@ func Contains(slice []string, val string) (int, bool) { ...@@ -109,3 +110,28 @@ func Contains(slice []string, val string) (int, bool) {
} }
return -1, false return -1, false
} }
func FormatFloat(num float64, decimal int) string {
// 默认乘1
d := float64(1)
if decimal > 0 {
// 10的N次方
d = math.Pow10(decimal)
}
// math.trunc作用就是返回浮点数的整数部分
// 再除回去,小数点后无效的0也就不存在了
return strconv.FormatFloat(math.Trunc(num*d)/d, 'f', -1, 64)
}
func Float64FromBytes(bytes []byte) float64 {
bits := binary.LittleEndian.Uint64(bytes)
float := math.Float64frombits(bits)
return float
}
func Float64Bytes(float float64) []byte {
bits := math.Float64bits(float)
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, bits)
return bytes
}
...@@ -146,7 +146,7 @@ func AddCoin(c *gin.Context) { ...@@ -146,7 +146,7 @@ func AddCoin(c *gin.Context) {
Decimals: coin.Decimals, Decimals: coin.Decimals,
Address: coin.Address, Address: coin.Address,
Treaty: coin.Treaty, Treaty: coin.Treaty,
PlatformId: "[1]", PlatformId: "[]",
} }
if err := coinService.Add(); err != nil { if err := coinService.Add(); err != nil {
......
...@@ -122,7 +122,11 @@ func AddWalletCoinRelationCoinRelation(c *gin.Context) { ...@@ -122,7 +122,11 @@ func AddWalletCoinRelationCoinRelation(c *gin.Context) {
} }
} }
if is_exist == false { if is_exist == false {
if "[]" == coin_exist.PlatformId {
coinService.PlatformId = strings.Replace(coin_exist.PlatformId, "]", util.ToString(coin_relation.PlatformId), -1) + "]"
} else {
coinService.PlatformId = strings.Replace(coin_exist.PlatformId, "]", ",", -1) + strconv.Itoa(coin_relation.PlatformId) + "]" coinService.PlatformId = strings.Replace(coin_exist.PlatformId, "]", ",", -1) + strconv.Itoa(coin_relation.PlatformId) + "]"
}
coinService.UpdateCoinRelation() coinService.UpdateCoinRelation()
} }
} }
......
package routers package routers
import ( import (
"bwallet/middleware/auth"
"bwallet/middleware/jwt" "bwallet/middleware/jwt"
"bwallet/middleware/log" "bwallet/middleware/log"
"bwallet/pkg/e" "bwallet/pkg/e"
...@@ -52,7 +53,7 @@ func InitRouter() *gin.Engine { ...@@ -52,7 +53,7 @@ func InitRouter() *gin.Engine {
api := r.Group("/api") api := r.Group("/api")
api.Use(jwt.JWT()) //.Use(auth.Casbin()) api.Use(jwt.JWT()).Use(auth.AUTH())
api.POST("/log", backend.AddOperationLog) api.POST("/log", backend.AddOperationLog)
api.GET("/logs", backend.GetOperationLogs) api.GET("/logs", backend.GetOperationLogs)
api.Use(log.LogMiddleware(e.HandleTableName)) api.Use(log.LogMiddleware(e.HandleTableName))
......
...@@ -36,44 +36,53 @@ func GetTransactionGas(name string) (map[string]interface{}, error) { ...@@ -36,44 +36,53 @@ func GetTransactionGas(name string) (map[string]interface{}, error) {
if "ETC" == strings.ToUpper(name) || "HT" == strings.ToUpper(name) { if "ETC" == strings.ToUpper(name) || "HT" == strings.ToUpper(name) {
fee, _ := gredis.HashGet("ETC_GAS", "gasPrice", 3) fee, _ := gredis.HashGet("ETC_GAS", "gasPrice", 3)
low := util.FormatFloat(util.ToFloat64(string(fee))/1000000000*30000*0.000000001*30*1.1, 8)
average := util.FormatFloat(util.ToFloat64(string(fee))/1000000000*30000*0.000000001*30*1.2, 8)
high := util.FormatFloat(util.ToFloat64(string(fee))/1000000000*30000*0.000000001*30*1.3, 8)
data["name"] = strings.ToUpper(name) data["name"] = strings.ToUpper(name)
data["low"] = util.ToFloat32(string(fee)) / 1000000000 * 30000 * 0.000000001 * 30 * (1 + 0.1) data["low"] = low
data["average"] = util.ToFloat32(string(fee)) / 1000000000 * 30000 * 0.000000001 * 30 * (1 + 0.2) data["average"] = average
high := util.ToFloat32(string(fee)) / 1000000000 * 30000 * 0.000000001 * 30 * (1 + 0.3)
data["high"] = high data["high"] = high
if high > 0.001 { if util.ToFloat64(high) > 0.001 {
data["high"] = 0.001 data["high"] = "0.001"
} }
} else if "ETH" == strings.ToUpper(name) { } else if "ETH" == strings.ToUpper(name) {
fee, _ := gredis.HashGet(strings.ToUpper(name)+"_GAS", "gasPrice", 3) fee, _ := gredis.HashGet(strings.ToUpper(name)+"_GAS", "gasPrice", 3)
low := util.FormatFloat(util.ToFloat64(string(fee))/1000000000*30000*0.000000001*1.1, 8)
average := util.FormatFloat(util.ToFloat64(string(fee))/1000000000*30000*0.000000001*1.2, 8)
high := util.FormatFloat(util.ToFloat64(string(fee))/1000000000*30000*0.000000001*1.3, 8)
data["name"] = strings.ToUpper(name) data["name"] = strings.ToUpper(name)
data["low"] = util.ToFloat32(string(fee)) / 1000000000 * 30000 * 0.000000001 * (1 + 0.1) data["low"] = low
data["average"] = util.ToFloat32(string(fee)) / 1000000000 * 30000 * 0.000000001 * (1 + 0.2) data["average"] = average
high := util.ToFloat32(string(fee)) / 1000000000 * 30000 * 0.000000001 * (1 + 0.3)
data["high"] = high data["high"] = high
if high > 0.1 { if util.ToFloat64(high) > 0.1 {
data["high"] = 0.1 data["high"] = "0.1"
} }
} else { } else {
fee, _ := gredis.HashGet(strings.ToUpper(name)+"_GAS", "txFee", 3) fee, _ := gredis.HashGet(strings.ToUpper(name)+"_GAS", "txFee", 3)
low := util.FormatFloat(util.ToFloat64(string(fee))*1.1, 8)
average := util.FormatFloat(util.ToFloat64(string(fee))*1.2, 8)
high := util.FormatFloat(util.ToFloat64(string(fee))*1.3, 8)
data["name"] = strings.ToUpper(name) data["name"] = strings.ToUpper(name)
data["low"] = util.ToFloat32(string(fee)) * (1 + 0.1) data["low"] = low
data["average"] = util.ToFloat32(string(fee)) * (1 + 0.2) data["average"] = average
high := util.ToFloat32(string(fee)) * (1 + 0.3)
data["high"] = high data["high"] = high
if "BTC" == strings.ToUpper(name) || "BCH" == strings.ToUpper(name) || "LTC" == strings.ToUpper(name) || "ZEC" == strings.ToUpper(name) || "DCR" == strings.ToUpper(name) { if "BTC" == strings.ToUpper(name) || "BCH" == strings.ToUpper(name) || "LTC" == strings.ToUpper(name) || "ZEC" == strings.ToUpper(name) || "DCR" == strings.ToUpper(name) {
if high > 0.001 { if util.ToFloat64(high) > 0.001 {
data["high"] = 0.001 data["high"] = "0.001"
} }
} }
if "NEO" == strings.ToUpper(name) || "TRX" == strings.ToUpper(name) || "BTY" == strings.ToUpper(name) { if "NEO" == strings.ToUpper(name) || "TRX" == strings.ToUpper(name) || "BTY" == strings.ToUpper(name) {
if high > 1 { if util.ToFloat64(high) > 1 {
data["high"] = 1 data["high"] = "1"
} }
} }
if "ATOM" == strings.ToUpper(name) { if "ATOM" == strings.ToUpper(name) {
if high > 0.01 { if util.ToFloat64(high) > 0.01 {
data["high"] = 0.01 data["high"] = "0.01"
} }
} }
} }
......
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