Commit 27ceb447 authored by shajiaiming's avatar shajiaiming

fix

parent 77ce6f4a
......@@ -2,6 +2,7 @@ package models
import (
"bwallet/pkg/setting"
"bwallet/pkg/util"
"encoding/json"
"github.com/jinzhu/gorm"
)
......@@ -25,7 +26,7 @@ type Coin struct {
Decimals int `json:"decimals"` //币种精度
Address string `json:"address"` //合约地址
Treaty int `json:"treaty"` //币种类型
PlatformId int `json:"platform_id"`
PlatformId string `json:"platform_id"`
}
func (c Coin) TableName() string {
......@@ -58,7 +59,17 @@ func ExistCoinById(id int) (bool, error) {
*/
func GetCoinTotal(maps interface{}) (int, error) {
var count int
if err := db.Model(&Coin{}).Where(maps).Count(&count).Error; err != nil {
var platform_id int
term := make(map[string]interface{})
for key, val := range maps.(map[string]interface{}) {
if ("platform_id" == key) {
platform_id = util.ToInt(val)
} else {
term[key] = val
}
}
if err := db.Model(&Coin{}).Where("FIND_IN_SET(?, platform_id)", platform_id).Where(term).Count(&count).Error; err != nil {
return 0, err
}
......@@ -72,8 +83,18 @@ func GetCoinTotal(maps interface{}) (int, error) {
*/
func GetCoins(pageNum, pageSize int, maps interface{}) ([]*Coin, error) {
var coins []*Coin
var platform_id int
term := make(map[string]interface{})
for key, val := range maps.(map[string]interface{}) {
if ("platform_id" == key) {
platform_id = util.ToInt(val)
} else {
term[key] = val
}
}
err := db.Where("FIND_IN_SET(?, platform_id)", platform_id).Where(term).Offset(pageNum).Limit(pageSize).Find(&coins).Error
err := db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&coins).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
......@@ -138,7 +159,7 @@ func AddCoin(data map[string]interface{}) (error) {
CirculateCount: data["circulate_count"].(float64),
Decimals: data["decimals"].(int),
Address: data["address"].(string),
PlatformId: data["platform_id"].(int),
PlatformId: data["platform_id"].(string),
Treaty: data["treaty"].(int),
}
if err := db.Create(&coin).Error; err != nil {
......
package util
import "bwallet/pkg/setting"
import (
"bwallet/pkg/setting"
"strconv"
)
// Setup Initialize the util
func Setup() {
jwtSecret = []byte(setting.AppSetting.JwtSecret)
}
func ToInt(o interface{}) int {
if o == nil {
return 0
}
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
}
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ import (
"github.com/Unknwon/com"
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"
"strconv"
"strings"
)
......@@ -35,7 +36,7 @@ func GetCoin(c *gin.Context) {
}
coinService := coin_service.Coin{
Id: id,
PlatformId: platform_id,
PlatformId: strconv.Itoa(platform_id),
PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
}
......@@ -78,9 +79,9 @@ func GetCoins(c *gin.Context) {
chain = c.Query("chain")
}
var platform_id int
platform_id := ""
if arg := c.Query("platform_id"); arg != "" {
platform_id = com.StrTo(c.DefaultQuery("platform_id", "1")).MustInt()
platform_id = c.DefaultQuery("platform_id", "1")
}
if valid.HasErrors() {
......@@ -153,7 +154,7 @@ func AddCoin(c *gin.Context) {
Decimals: coin.Decimals,
Address: coin.Address,
Treaty: coin.Treaty,
PlatformId: coin.PlatformId,
PlatformId: strconv.Itoa(coin.PlatformId),
}
if err := coinService.Add(); err != nil {
......@@ -191,7 +192,7 @@ func EditCoin(c *gin.Context) {
CirculateCount: coin.CirculateCount,
Decimals: coin.Decimals,
Address: coin.Address,
PlatformId: coin.PlatformId,
PlatformId: strconv.Itoa(coin.PlatformId),
Treaty: coin.Treaty,
}
......@@ -210,11 +211,12 @@ func EditCoin(c *gin.Context) {
auth, _ := authService.GetUserInfo()
group := auth.Group
coin_model := coin_service.Coin{Id: coin.Id}
coin_exist, _ := coin_model.Get()
//coin_model := coin_service.Coin{Id: coin.Id}
//coin_exist, _ := coin_model.Get()
if ("administrator" != group) {
if (coin.PlatformId != auth.PlatformId) || (auth.PlatformId != coin_exist.PlatformId){
//if (coin.PlatformId != auth.PlatformId) || (auth.PlatformId != coin_exist.PlatformId){
if (coin.PlatformId != auth.PlatformId){
handler.SendResponse(c, errno.ErrAuthCoin, nil)
return
}
......
......@@ -21,7 +21,7 @@ type Coin struct {
CirculateCount float64
Decimals int
Address string
PlatformId int
PlatformId string
Treaty int
PageNum int
......@@ -192,7 +192,7 @@ func (c *Coin) getMaps() (map[string]interface{}) {
maps["platform"] = c.Platform
}
if c.PlatformId != 0 {
if c.PlatformId != "" {
maps["platform_id"] = c.PlatformId
}
......
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