Commit ed8e93c5 authored by shajiaiming's avatar shajiaiming

error handle

parent 68d46c04
...@@ -3,15 +3,15 @@ package auth ...@@ -3,15 +3,15 @@ package auth
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"bwallet/pkg/e" "bwallet/pkg/e"
"net/http"
"bwallet/models" "bwallet/models"
"fmt" "fmt"
"bwallet/pkg/handler"
"bwallet/pkg/errno"
) )
func AUTH() gin.HandlerFunc { func AUTH() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
var code int var code int
var data interface{}
code = e.SUCCESS code = e.SUCCESS
token := c.Request.Header.Get("Token") token := c.Request.Header.Get("Token")
...@@ -26,12 +26,7 @@ func AUTH() gin.HandlerFunc { ...@@ -26,12 +26,7 @@ func AUTH() gin.HandlerFunc {
} }
if code != e.SUCCESS { if code != e.SUCCESS {
c.JSON(http.StatusUnauthorized, gin.H{ handler.SendResponse(c, errno.ErrUserTokenIncorrect, "Token鉴权失败")
"code": code,
"msg": e.GetMsg(code),
"data": data,
})
c.Abort() c.Abort()
return return
} }
......
...@@ -14,7 +14,6 @@ type RecommendCoin struct { ...@@ -14,7 +14,6 @@ type RecommendCoin struct {
PlatformId int `json:"platform_id"` PlatformId int `json:"platform_id"`
Sort int `json:"sort"` Sort int `json:"sort"`
Type int `json:"type"` Type int `json:"type"`
Chain string `json:"chain"`
CoinInfo *Coin `gorm:"foreignkey:Cid"` CoinInfo *Coin `gorm:"foreignkey:Cid"`
} }
...@@ -77,7 +76,6 @@ func AddRecommendCoin(data map[string]interface{}) (error) { ...@@ -77,7 +76,6 @@ func AddRecommendCoin(data map[string]interface{}) (error) {
PlatformId: data["platform_id"].(int), PlatformId: data["platform_id"].(int),
Sort: data["sort"].(int), Sort: data["sort"].(int),
Type: data["type"].(int), Type: data["type"].(int),
Chain: data["chain"].(string),
} }
if err := db.Create(&coinRecommend).Error; err != nil { if err := db.Create(&coinRecommend).Error; err != nil {
return err return err
......
...@@ -67,8 +67,29 @@ var ( ...@@ -67,8 +67,29 @@ var (
ErrDatabase = &Errno{Code: 20002, Message: "Database error."} ErrDatabase = &Errno{Code: 20002, Message: "Database error."}
// coin errors // coin errors
ErrCoinNotFound = &Errno{Code: 20101, Message: "The coin was not found."} ErrCoinNotFound = &Errno{Code: 20101, Message: "The coin was not found."}
ErrCountCoin = &Errno{Code: 20102, Message: "The coins statistic error."} ErrCountCoin = &Errno{Code: 20102, Message: "The coins statistic error."}
ErrUserNotFound = &Errno{Code: 20101, Message: "The user was not found."} ErrAddCoin = &Errno{Code: 20101, Message: "The coin add error."}
ErrPasswordIncorrect = &Errno{Code: 20102, Message: "The password was incorrect."} ErrUpdateCoin = &Errno{Code: 20102, Message: "The coin update error."}
ErrDeleteCoin = &Errno{Code: 20102, Message: "The coin delete error."}
// wallet errors
ErrWalletNotFound = &Errno{Code: 20101, Message: "The wallet was not found."}
ErrCountWallet = &Errno{Code: 20102, Message: "The wallets statistic error."}
ErrAddWallet = &Errno{Code: 20101, Message: "The wallet add error."}
ErrUpdateWallet = &Errno{Code: 20102, Message: "The wallet update error."}
ErrDeleteWallet = &Errno{Code: 20102, Message: "The wallet delete error."}
// chain errors
ErrChainNotFound = &Errno{Code: 20101, Message: "The chain was not found."}
ErrCountChain = &Errno{Code: 20102, Message: "The chain statistic error."}
ErrAddChain = &Errno{Code: 20101, Message: "The chain add error."}
ErrUpdateChain = &Errno{Code: 20102, Message: "The chain update error."}
ErrDeleteChain = &Errno{Code: 20102, Message: "The chain delete error."}
// user errors
ErrUserTokenIncorrect = &Errno{Code: 40001, Message: "The user token was incorrect."}
ErrUserAuthIncorrect = &Errno{Code: 40001, Message: "The user auth was incorrect."}
ErrUserNotFound = &Errno{Code: 20101, Message: "The user was not found."}
ErrPasswordIncorrect = &Errno{Code: 20102, Message: "The password was incorrect."}
) )
...@@ -14,12 +14,20 @@ type Response struct { ...@@ -14,12 +14,20 @@ type Response struct {
} }
func SendResponse(c *gin.Context, err error, data interface{}) { func SendResponse(c *gin.Context, err error, data interface{}) {
code, msg := errno.DecodeErr(err) if errno.ErrBind == err {
code, _ := errno.DecodeErr(err)
// always return http.StatusOK msg := data.(string)
c.JSON(http.StatusOK, Response{ c.JSON(http.StatusOK, Response{
Code: code, Code: code,
Msg: msg, Msg: msg,
Data: data, Data: nil,
}) })
} else {
code, msg := errno.DecodeErr(err)
c.JSON(http.StatusOK, Response{
Code: code,
Msg: msg,
Data: data,
})
}
} }
...@@ -2,72 +2,67 @@ package v1 ...@@ -2,72 +2,67 @@ package v1
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"bwallet/pkg/app"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/astaxie/beego/validation" "github.com/astaxie/beego/validation"
"net/http"
"bwallet/pkg/e"
"bwallet/service/chain_service" "bwallet/service/chain_service"
"bwallet/pkg/util" "bwallet/pkg/util"
"bwallet/service/auth_service" "bwallet/service/auth_service"
"bwallet/pkg/handler"
"bwallet/pkg/errno"
"bwallet/validate"
"gopkg.in/go-playground/validator.v8"
) )
type AddChainForm struct {
Platform string `form:"platform" valid:"Required;MinSize(2);MaxSize(50)"`
Address string `form:"address" valid:"Required;MinSize(2);MaxSize(50)"`
PrivateKey string `form:"private_key" valid:"Required;MinSize(10);MaxSize(100)"`
Execer string `form:"execer" valid:"Required;MinSize(5);MaxSize(50)"`
Token string `form:"token" valid:"Required;MinSize(2);MaxSize(10)"`
Fee float32 `form:"fee"`
BrowerUrl string `form:"brower_url" valid:"Required;MinSize(5);MaxSize(50)"`
}
type EditChainForm struct {
ID int `form:"id" valid:"Required;Min(1)"`
Platform string `form:"platform" valid:"Required;MinSize(2);MaxSize(50)"`
Address string `form:"address" valid:"Required;MinSize(2);MaxSize(50)"`
PrivateKey string `form:"private_key" valid:"Required;MinSize(10);MaxSize(100)"`
Execer string `form:"execer" valid:"Required;MinSize(5);MaxSize(50)"`
Token string `form:"token" valid:"Required;MinSize(2);MaxSize(10)"`
Fee float32 `form:"fee"`
BrowerUrl string `form:"brower_url" valid:"Required;MinSize(5);MaxSize(50)"`
}
func GetChain(c *gin.Context) { func GetChain(c *gin.Context) {
appG := app.Gin{c} token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo()
group := auth.Group
if ("administrator" != group) {
handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return
}
id := com.StrTo(c.Param("id")).MustInt() id := com.StrTo(c.Param("id")).MustInt()
valid := validation.Validation{} valid := validation.Validation{}
valid.Min(id, 1, "id").Message("ID必须大于0") valid.Min(id, 1, "id").Message("ID必须大于0")
if valid.HasErrors() { if valid.HasErrors() {
app.MarkErrors(valid.Errors) handler.SendResponse(c, errno.ErrValidation, nil)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
return return
} }
chainService := chain_service.Chain{ID: id} chainService := chain_service.Chain{ID: id}
exists, err := chainService.ExistById() exists, err := chainService.ExistById()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_CHAIN, nil) handler.SendResponse(c, errno.ErrChainNotFound, nil)
return return
} }
if (!exists) { if (!exists) {
appG.Response(http.StatusOK, e.ERROR_EXIST_CHAIN, nil) handler.SendResponse(c, errno.ErrChainNotFound, nil)
return return
} }
chain, err := chainService.Get() chain, err := chainService.Get()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_GET_CHAIN_FAIL, nil) handler.SendResponse(c, errno.ErrChainNotFound, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, chain) handler.SendResponse(c, nil, chain)
} }
func GetChains(c *gin.Context) { func GetChains(c *gin.Context) {
appG := app.Gin{C: c} token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo()
group := auth.Group
if ("administrator" != group) {
handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return
}
valid := validation.Validation{} valid := validation.Validation{}
platform := "" platform := ""
...@@ -76,8 +71,7 @@ func GetChains(c *gin.Context) { ...@@ -76,8 +71,7 @@ func GetChains(c *gin.Context) {
} }
if valid.HasErrors() { if valid.HasErrors() {
app.MarkErrors(valid.Errors) handler.SendResponse(c, errno.ErrValidation, nil)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return return
} }
...@@ -89,13 +83,13 @@ func GetChains(c *gin.Context) { ...@@ -89,13 +83,13 @@ func GetChains(c *gin.Context) {
total, err := chainService.Count() total, err := chainService.Count()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_CHAIN_FAIL, nil) handler.SendResponse(c, errno.ErrCountChain, nil)
return return
} }
chains, err := chainService.GetAll() chains, err := chainService.GetAll()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_GET_CHAIN_FAIL, nil) handler.SendResponse(c, errno.InternalServerError, nil)
return return
} }
...@@ -103,134 +97,128 @@ func GetChains(c *gin.Context) { ...@@ -103,134 +97,128 @@ func GetChains(c *gin.Context) {
data["items"] = chains data["items"] = chains
data["total"] = total data["total"] = total
appG.Response(http.StatusOK, e.SUCCESS, data) handler.SendResponse(c, nil, data)
} }
func AddChain(c *gin.Context) { func AddChain(c *gin.Context) {
var (
appG = app.Gin{C: c}
form AddChainForm
)
token := c.Request.Header.Get("Token") token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token} authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo() auth, _ := authService.GetUserInfo()
group := auth.Group group := auth.Group
if ("administrator" != group) { if ("administrator" != group) {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil) handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return return
} }
httpCode, errCode := app.BindAndValid(c, &form) var addChainRequest validate.AddChainForm
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil) if err := c.ShouldBind(&addChainRequest); err != nil {
var msg = addChainRequest.GetError(err.(validator.ValidationErrors))
handler.SendResponse(c, errno.ErrBind, msg)
return return
} }
chainService := chain_service.Chain{ chainService := chain_service.Chain{
Platform: form.Platform, Platform: addChainRequest.Platform,
Address: form.Address, Address: addChainRequest.Address,
PrivateKey: form.PrivateKey, PrivateKey: addChainRequest.PrivateKey,
Execer: form.Execer, Execer: addChainRequest.Execer,
Token: form.Token, Token: addChainRequest.Token,
Fee: form.Fee, Fee: addChainRequest.Fee,
BrowerUrl: form.BrowerUrl, BrowerUrl: addChainRequest.BrowerUrl,
} }
if err := chainService.Add(); err != nil { if err := chainService.Add(); err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_ADD_WALLET_FAIL, nil) handler.SendResponse(c, errno.ErrAddChain, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, nil) handler.SendResponse(c, nil, nil)
} }
func EditChain(c *gin.Context) { func EditChain(c *gin.Context) {
var (
appG = app.Gin{C: c}
form = EditChainForm{ID: com.StrTo(c.Param("id")).MustInt()}
)
httpCode, errCode := app.BindAndValid(c, &form)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}
cbainService := chain_service.Chain{
ID: form.ID,
Platform: form.Platform,
Address: form.Address,
PrivateKey: form.PrivateKey,
Execer: form.Execer,
Token: form.Token,
Fee: form.Fee,
BrowerUrl: form.BrowerUrl,
}
token := c.Request.Header.Get("Token") token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token} authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo() auth, _ := authService.GetUserInfo()
group := auth.Group group := auth.Group
if ("administrator" != group && auth.PlatformId != form.ID) { if ("administrator" != group) {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil) handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return return
} }
exists, err := cbainService.ExistById()
var editChainRequest validate.EditChainForm
if err := c.ShouldBind(&editChainRequest); err != nil {
var msg = editChainRequest.GetError(err.(validator.ValidationErrors))
handler.SendResponse(c, errno.ErrBind, msg)
return
}
chainService := chain_service.Chain{
ID: editChainRequest.Id,
Platform: editChainRequest.Platform,
Address: editChainRequest.Address,
PrivateKey: editChainRequest.PrivateKey,
Execer: editChainRequest.Execer,
Token: editChainRequest.Token,
Fee: editChainRequest.Fee,
BrowerUrl: editChainRequest.BrowerUrl,
}
exists, err := chainService.ExistById()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_WALLET, nil) handler.SendResponse(c, errno.ErrChainNotFound, nil)
return return
} }
if !exists { if !exists {
appG.Response(http.StatusOK, e.ERROR_EXIST_WALLET, nil) handler.SendResponse(c, errno.ErrChainNotFound, nil)
return return
} }
if err := cbainService.Edit(); err != nil { if err := chainService.Edit(); err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_UPDATE_WALLET_FAIL, nil) handler.SendResponse(c, errno.ErrUpdateChain, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, nil) handler.SendResponse(c, nil, nil)
} }
func DeleteChain(c *gin.Context) { func DeleteChain(c *gin.Context) {
appG := app.Gin{C: c}
valid := validation.Validation{}
id := com.StrTo(c.Param("id")).MustInt()
valid.Min(id, 1, "id").Message("ID必须大于0")
if valid.HasErrors() {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
return
}
token := c.Request.Header.Get("Token") token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token} authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo() auth, _ := authService.GetUserInfo()
group := auth.Group group := auth.Group
if ("administrator" != group) { if ("administrator" != group) {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil) handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return
}
valid := validation.Validation{}
id := com.StrTo(c.Param("id")).MustInt()
valid.Min(id, 1, "id").Message("ID必须大于0")
if valid.HasErrors() {
handler.SendResponse(c, errno.ErrValidation, nil)
return return
} }
chainService := chain_service.Chain{ID: id} chainService := chain_service.Chain{ID: id}
exists, err := chainService.ExistById() exists, err := chainService.ExistById()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_WALLET, nil) handler.SendResponse(c, errno.ErrValidation, nil)
return return
} }
if !exists { if !exists {
appG.Response(http.StatusOK, e.ERROR_EXIST_WALLET, nil) handler.SendResponse(c, errno.ErrValidation, nil)
return return
} }
err = chainService.Delete() err = chainService.Delete()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_WALLET_FAIL, nil) handler.SendResponse(c, errno.ErrDeleteChain, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, nil) handler.SendResponse(c, nil, nil)
} }
This diff is collapsed.
...@@ -4,50 +4,16 @@ import ( ...@@ -4,50 +4,16 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/astaxie/beego/validation" "github.com/astaxie/beego/validation"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"net/http"
"bwallet/pkg/app"
"bwallet/pkg/e"
"bwallet/pkg/util" "bwallet/pkg/util"
"bwallet/service/recommend_coin_service" "bwallet/service/recommend_coin_service"
"bwallet/service/coin_service"
"bwallet/service/auth_service" "bwallet/service/auth_service"
"bwallet/validate"
"gopkg.in/go-playground/validator.v8"
"bwallet/pkg/handler"
"bwallet/pkg/errno"
) )
type AddRecommendCoinForm struct {
Cid int `form:"cid" valid:"Required;Min(1)"`
Recommend int `form:"recommend" valid:"Range(1,2)"`
PlatformId int `form:"platform_id" valid:"Required;Min(1)"`
Sort int `form:"sort" valid:"Required;Min(1)"`
Type int `form:"type" valid:"Range(1,2)"`
Chain string `form:"chain" valid:"MaxSize(100)"`
}
type EditRecommendCoinForm struct {
Id int `form:"id" valid:"Required;Min(1)"`
Sort int `form:"sort" valid:"Required;Min(1)"`
}
func (cr *AddRecommendCoinForm) Valid(v *validation.Validation) {
coinService := coin_service.Coin{Id: cr.Cid}
exists, _ := coinService.ExistById()
if (!exists) {
v.SetError("Cid", "所推荐币种不存在")
}
recommendCoinService := recommend_coin_service.RecommendCoin{
Cid: cr.Cid,
PlatformId: cr.PlatformId,
Type: cr.Type,
}
total, _ := recommendCoinService.Count()
if total > 0 {
v.SetError("Cid", "推荐币种已经存在")
}
}
func GetRecommendCoins(c *gin.Context) { func GetRecommendCoins(c *gin.Context) {
appG := app.Gin{C: c}
valid := validation.Validation{}
token := c.Request.Header.Get("Token") token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token} authService := auth_service.Auth{Token: token}
...@@ -62,12 +28,6 @@ func GetRecommendCoins(c *gin.Context) { ...@@ -62,12 +28,6 @@ func GetRecommendCoins(c *gin.Context) {
} }
} }
if valid.HasErrors() {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}
recommendCoinService := recommend_coin_service.RecommendCoin{ recommendCoinService := recommend_coin_service.RecommendCoin{
PlatformId: platform_id, PlatformId: platform_id,
PageNum: util.GetPage(c), PageNum: util.GetPage(c),
...@@ -76,13 +36,13 @@ func GetRecommendCoins(c *gin.Context) { ...@@ -76,13 +36,13 @@ func GetRecommendCoins(c *gin.Context) {
total, err := recommendCoinService.Count() total, err := recommendCoinService.Count()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_RECOMMEND_COIN_FAIL, nil) handler.SendResponse(c, errno.ErrCountCoin, nil)
return return
} }
coinRecommends, err := recommendCoinService.GetAll() coinRecommends, err := recommendCoinService.GetAll()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_GET_RECOMMEND_COIN_FAIL, nil) handler.SendResponse(c, errno.InternalServerError, nil)
return return
} }
...@@ -90,18 +50,15 @@ func GetRecommendCoins(c *gin.Context) { ...@@ -90,18 +50,15 @@ func GetRecommendCoins(c *gin.Context) {
data["items"] = coinRecommends data["items"] = coinRecommends
data["total"] = total data["total"] = total
appG.Response(http.StatusOK, e.SUCCESS, data) handler.SendResponse(c, nil, data)
} }
func AddRecommendCoin(c *gin.Context) { func AddRecommendCoin(c *gin.Context) {
var ( var addRecommendCoinRequest validate.AddRecommendCoinForm
appG = app.Gin{C: c}
form AddRecommendCoinForm if err := c.ShouldBind(&addRecommendCoinRequest); err != nil {
) var msg = addRecommendCoinRequest.GetError(err.(validator.ValidationErrors))
handler.SendResponse(c, errno.ErrBind, msg)
httpCode, errCode := app.BindAndValid(c, &form)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return return
} }
...@@ -113,49 +70,45 @@ func AddRecommendCoin(c *gin.Context) { ...@@ -113,49 +70,45 @@ func AddRecommendCoin(c *gin.Context) {
platform_id = auth.PlatformId platform_id = auth.PlatformId
if ("administrator" == group) { if ("administrator" == group) {
platform_id = form.PlatformId platform_id = addRecommendCoinRequest.PlatformId
} }
recommendCoinService := recommend_coin_service.RecommendCoin{ recommendCoinService := recommend_coin_service.RecommendCoin{
Cid: form.Cid, Cid: addRecommendCoinRequest.Cid,
Recommend: form.Recommend, Recommend: addRecommendCoinRequest.Recommend,
PlatformId: platform_id, PlatformId: platform_id,
Sort: form.Sort, Sort: addRecommendCoinRequest.Sort,
Type: form.Type, Type: addRecommendCoinRequest.Type,
Chain: form.Chain,
} }
if err := recommendCoinService.Add(); err != nil { if err := recommendCoinService.Add(); err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_ADD_RECOMMEND_COIN_FAIL, nil) handler.SendResponse(c, errno.ErrAddCoin, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, nil) handler.SendResponse(c, nil, nil)
} }
func EditRecommendCoin(c *gin.Context) { func EditRecommendCoin(c *gin.Context) {
var ( var editRecommendCoinRequest validate.EditRecommendCoinForm
appG = app.Gin{C: c}
form = EditRecommendCoinForm{Id: com.StrTo(c.Param("id")).MustInt()} if err := c.ShouldBind(&editRecommendCoinRequest); err != nil {
) var msg = editRecommendCoinRequest.GetError(err.(validator.ValidationErrors))
handler.SendResponse(c, errno.ErrBind, msg)
httpCode, errCode := app.BindAndValid(c, &form)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return return
} }
recommendCoinService := recommend_coin_service.RecommendCoin{ recommendCoinService := recommend_coin_service.RecommendCoin{
Id: form.Id, Id: editRecommendCoinRequest.Id,
Sort: form.Sort, Sort: editRecommendCoinRequest.Sort,
} }
exists, err := recommendCoinService.ExistById() exists, err := recommendCoinService.ExistById()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_RECOMMEND_COIN, nil) handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return return
} }
if !exists { if !exists {
appG.Response(http.StatusOK, e.ERROR_EXIST_RECOMMEND_COIN, nil) handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return return
} }
...@@ -164,43 +117,41 @@ func EditRecommendCoin(c *gin.Context) { ...@@ -164,43 +117,41 @@ func EditRecommendCoin(c *gin.Context) {
auth, _ := authService.GetUserInfo() auth, _ := authService.GetUserInfo()
group := auth.Group group := auth.Group
if ("administrator" != group) { if ("administrator" != group) {
recommend_coin_model := recommend_coin_service.RecommendCoin{Id: form.Id} recommend_coin_model := recommend_coin_service.RecommendCoin{Id: editRecommendCoinRequest.Id}
recommend_coin, _ := recommend_coin_model.Get() recommend_coin, _ := recommend_coin_model.Get()
if recommend_coin.PlatformId != auth.PlatformId { if recommend_coin.PlatformId != auth.PlatformId {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil) handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return return
} }
} }
if err := recommendCoinService.Edit(); err != nil { if err := recommendCoinService.Edit(); err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_UPDATE_RECOMMEND_COIN_FAIL, nil) handler.SendResponse(c, errno.ErrUpdateCoin, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, nil) handler.SendResponse(c, nil, nil)
} }
func DeleteRecommendCoin(c *gin.Context) { func DeleteRecommendCoin(c *gin.Context) {
appG := app.Gin{C: c}
valid := validation.Validation{} valid := validation.Validation{}
id := com.StrTo(c.Param("id")).MustInt() id := com.StrTo(c.Param("id")).MustInt()
valid.Min(id, 1, "id").Message("ID必须大于0") valid.Min(id, 1, "id").Message("ID必须大于0")
if valid.HasErrors() { if valid.HasErrors() {
app.MarkErrors(valid.Errors) handler.SendResponse(c, errno.ErrValidation, nil)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
return return
} }
recommendCoinService := recommend_coin_service.RecommendCoin{Id: id} recommendCoinService := recommend_coin_service.RecommendCoin{Id: id}
exists, err := recommendCoinService.ExistById() exists, err := recommendCoinService.ExistById()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_RECOMMEND_COIN, nil) handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return return
} }
if !exists { if !exists {
appG.Response(http.StatusOK, e.ERROR_EXIST_RECOMMEND_COIN, nil) handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return return
} }
...@@ -211,15 +162,15 @@ func DeleteRecommendCoin(c *gin.Context) { ...@@ -211,15 +162,15 @@ func DeleteRecommendCoin(c *gin.Context) {
if ("administrator" != group) { if ("administrator" != group) {
recommend_coin, _ := recommendCoinService.Get() recommend_coin, _ := recommendCoinService.Get()
if recommend_coin.PlatformId != auth.PlatformId { if recommend_coin.PlatformId != auth.PlatformId {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil) handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return return
} }
} }
err = recommendCoinService.Delete() err = recommendCoinService.Delete()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_RECOMMEND_COIN_FAIL, nil) handler.SendResponse(c, errno.ErrDeleteCoin, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, nil) handler.SendResponse(c, nil, nil)
} }
...@@ -4,42 +4,22 @@ import ( ...@@ -4,42 +4,22 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/astaxie/beego/validation" "github.com/astaxie/beego/validation"
"github.com/Unknwon/com" "github.com/Unknwon/com"
"net/http"
"bwallet/pkg/app"
"bwallet/pkg/e"
"bwallet/service/wallet_service" "bwallet/service/wallet_service"
"bwallet/pkg/util" "bwallet/pkg/util"
"bwallet/service/auth_service" "bwallet/service/auth_service"
"bwallet/pkg/handler"
"bwallet/pkg/errno"
"bwallet/validate"
"gopkg.in/go-playground/validator.v8"
) )
type AddWalletForm struct {
Name string `form:"name" valid:"Required;MinSize(2);MaxSize(50)"`
DownloadUrl string `form:"download_url"`
Introduce string `form:"introduce" valid:"MaxSize(500)"`
ChainId int `form:"chain_id" gorm:"default:'0'"`
IssueCharge float32 `form:"issue_charge" gorm:"default:'0.00'"`
ChargeUnit string `form:"charge_unit" gorm:"default:'BTY'"`
}
type EditWalletForm struct {
Id int `form:"id" valid:"Required;Min(1)"`
Name string `form:"name" valid:"Required;MinSize(2);MaxSize(50)"`
DownloadUrl string `form:"download_url"`
Introduce string `form:"introduce" valid:"MaxSize(500)"`
ChainId int `form:"chain_id" gorm:"default:'0'"`
IssueCharge float32 `form:"issue_charge" gorm:"default:'0.00'"`
ChargeUnit string `form:"charge_unit" gorm:"default:'BTY'"`
}
func GetWallet(c *gin.Context) { func GetWallet(c *gin.Context) {
appG := app.Gin{c}
id := com.StrTo(c.Param("id")).MustInt() id := com.StrTo(c.Param("id")).MustInt()
valid := validation.Validation{} valid := validation.Validation{}
valid.Min(id, 1, "id").Message("ID必须大于0") valid.Min(id, 1, "id").Message("ID必须大于0")
if valid.HasErrors() { if valid.HasErrors() {
app.MarkErrors(valid.Errors) handler.SendResponse(c, errno.ErrValidation, nil)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
return return
} }
...@@ -50,7 +30,7 @@ func GetWallet(c *gin.Context) { ...@@ -50,7 +30,7 @@ func GetWallet(c *gin.Context) {
var platform_id int var platform_id int
platform_id = id platform_id = id
if ("administrator" != group && id != auth.PlatformId) { if ("administrator" != group && id != auth.PlatformId) {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil) handler.SendResponse(c, errno.ErrUserTokenIncorrect, nil)
return return
} }
...@@ -58,26 +38,25 @@ func GetWallet(c *gin.Context) { ...@@ -58,26 +38,25 @@ func GetWallet(c *gin.Context) {
exists, err := walletService.ExistById() exists, err := walletService.ExistById()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_WALLET, nil) handler.SendResponse(c, errno.ErrWalletNotFound, nil)
return return
} }
if (!exists) { if (!exists) {
appG.Response(http.StatusOK, e.ERROR_EXIST_WALLET, nil) handler.SendResponse(c, errno.ErrWalletNotFound, nil)
return return
} }
wallet, err := walletService.Get() wallet, err := walletService.Get()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_GET_WALLET_FAIL, nil) handler.SendResponse(c, errno.ErrWalletNotFound, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, wallet) handler.SendResponse(c, nil, wallet)
} }
func GetWallets(c *gin.Context) { func GetWallets(c *gin.Context) {
appG := app.Gin{C: c}
valid := validation.Validation{} valid := validation.Validation{}
name := "" name := ""
...@@ -86,8 +65,7 @@ func GetWallets(c *gin.Context) { ...@@ -86,8 +65,7 @@ func GetWallets(c *gin.Context) {
} }
if valid.HasErrors() { if valid.HasErrors() {
app.MarkErrors(valid.Errors) handler.SendResponse(c, errno.ErrValidation, nil)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return return
} }
...@@ -116,13 +94,13 @@ func GetWallets(c *gin.Context) { ...@@ -116,13 +94,13 @@ func GetWallets(c *gin.Context) {
total, err := walletService.Count() total, err := walletService.Count()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_WALLET_FAIL, nil) handler.SendResponse(c, errno.ErrCountWallet, nil)
return return
} }
wallets, err := walletService.GetAll() wallets, err := walletService.GetAll()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_GET_WALLET_FAIL, nil) handler.SendResponse(c, errno.InternalServerError, nil)
return return
} }
...@@ -130,100 +108,96 @@ func GetWallets(c *gin.Context) { ...@@ -130,100 +108,96 @@ func GetWallets(c *gin.Context) {
data["items"] = wallets data["items"] = wallets
data["total"] = total data["total"] = total
appG.Response(http.StatusOK, e.SUCCESS, data) handler.SendResponse(c, nil, data)
} }
func AddWallet(c *gin.Context) { func AddWallet(c *gin.Context) {
var ( var addWalletRequest validate.AddWalletForm
appG = app.Gin{C: c}
form AddWalletForm if err := c.ShouldBind(&addWalletRequest); err != nil {
) var msg = addWalletRequest.GetError(err.(validator.ValidationErrors))
handler.SendResponse(c, errno.ErrBind, msg)
return
}
token := c.Request.Header.Get("Token") token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token} authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo() auth, _ := authService.GetUserInfo()
group := auth.Group group := auth.Group
if ("administrator" != group) { if ("administrator" != group) {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil) handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return return
} }
httpCode, errCode := app.BindAndValid(c, &form)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return
}
walletService := wallet_service.Wallet{ walletService := wallet_service.Wallet{
Name: form.Name, Name: addWalletRequest.Name,
DownloadUrl: form.DownloadUrl, DownloadUrl: addWalletRequest.DownloadUrl,
Introduce: form.Introduce, Introduce: addWalletRequest.Introduce,
ChainId: form.ChainId, ChainId: addWalletRequest.ChainId,
IssueCharge: form.IssueCharge, IssueCharge: addWalletRequest.IssueCharge,
ChargeUnit: form.ChargeUnit, ChargeUnit: addWalletRequest.ChargeUnit,
} }
if err := walletService.Add(); err != nil { if err := walletService.Add(); err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_ADD_WALLET_FAIL, nil) handler.SendResponse(c, errno.ErrAddWallet, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, nil) handler.SendResponse(c, nil, nil)
} }
func EditWallet(c *gin.Context) { func EditWallet(c *gin.Context) {
var ( var editWalletRequest validate.EditWalletForm
appG = app.Gin{C: c}
form = EditWalletForm{Id: com.StrTo(c.Param("id")).MustInt()} if err := c.ShouldBind(&editWalletRequest); err != nil {
) var msg = editWalletRequest.GetError(err.(validator.ValidationErrors))
handler.SendResponse(c, errno.ErrBind, msg)
httpCode, errCode := app.BindAndValid(c, &form)
if errCode != e.SUCCESS {
appG.Response(httpCode, errCode, nil)
return return
} }
walletService := wallet_service.Wallet{ walletService := wallet_service.Wallet{
Id: form.Id, Id: editWalletRequest.Id,
Name: form.Name, Name: editWalletRequest.Name,
DownloadUrl: form.DownloadUrl, DownloadUrl: editWalletRequest.DownloadUrl,
Introduce: form.Introduce, Introduce: editWalletRequest.Introduce,
ChainId: editWalletRequest.ChainId,
IssueCharge: editWalletRequest.IssueCharge,
ChargeUnit: editWalletRequest.ChargeUnit,
} }
token := c.Request.Header.Get("Token") token := c.Request.Header.Get("Token")
authService := auth_service.Auth{Token: token} authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo() auth, _ := authService.GetUserInfo()
group := auth.Group group := auth.Group
if ("administrator" != group && auth.PlatformId != form.Id) { if ("administrator" != group && auth.PlatformId != editWalletRequest.Id) {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil) handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return return
} }
exists, err := walletService.ExistById() exists, err := walletService.ExistById()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_WALLET, nil) handler.SendResponse(c, errno.ErrWalletNotFound, nil)
return return
} }
if !exists { if !exists {
appG.Response(http.StatusOK, e.ERROR_EXIST_WALLET, nil) handler.SendResponse(c, errno.ErrWalletNotFound, nil)
return return
} }
if err := walletService.Edit(); err != nil { if err := walletService.Edit(); err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_UPDATE_WALLET_FAIL, nil) handler.SendResponse(c, errno.ErrUpdateWallet, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, nil) handler.SendResponse(c, nil, nil)
} }
func DeleteWallet(c *gin.Context) { func DeleteWallet(c *gin.Context) {
appG := app.Gin{C: c}
valid := validation.Validation{} valid := validation.Validation{}
id := com.StrTo(c.Param("id")).MustInt() id := com.StrTo(c.Param("id")).MustInt()
valid.Min(id, 1, "id").Message("ID必须大于0") valid.Min(id, 1, "id").Message("ID必须大于0")
if valid.HasErrors() { if valid.HasErrors() {
app.MarkErrors(valid.Errors) handler.SendResponse(c, errno.ErrValidation, nil)
appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
return return
} }
...@@ -232,27 +206,27 @@ func DeleteWallet(c *gin.Context) { ...@@ -232,27 +206,27 @@ func DeleteWallet(c *gin.Context) {
auth, _ := authService.GetUserInfo() auth, _ := authService.GetUserInfo()
group := auth.Group group := auth.Group
if ("administrator" != group && auth.PlatformId != id) { if ("administrator" != group && auth.PlatformId != id) {
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil) handler.SendResponse(c, errno.ErrUserAuthIncorrect, nil)
return return
} }
walletService := wallet_service.Wallet{Id: id} walletService := wallet_service.Wallet{Id: id}
exists, err := walletService.ExistById() exists, err := walletService.ExistById()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_WALLET, nil) handler.SendResponse(c, errno.ErrValidation, nil)
return return
} }
if !exists { if !exists {
appG.Response(http.StatusOK, e.ERROR_EXIST_WALLET, nil) handler.SendResponse(c, errno.ErrValidation, nil)
return return
} }
err = walletService.Delete() err = walletService.Delete()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_WALLET_FAIL, nil) handler.SendResponse(c, errno.ErrDeleteWallet, nil)
return return
} }
appG.Response(http.StatusOK, e.SUCCESS, nil) handler.SendResponse(c, nil, nil)
} }
...@@ -36,12 +36,12 @@ func InitRouter() *gin.Engine { ...@@ -36,12 +36,12 @@ func InitRouter() *gin.Engine {
api.DELETE("/coin/:id", v1.DeleteCoin) api.DELETE("/coin/:id", v1.DeleteCoin)
api.GET("/primary-chains", v1.GetPrimaryChains) api.GET("/primary-chains", v1.GetPrimaryChains)
api.GET("/types", v1.GetTypes) api.GET("/types", v1.GetTypes)
api.GET("/get-platform", v1.GetCoinsPlatform) //api.GET("/get-platform", v1.GetCoinsPlatform)
api.GET("/wallets", v1.GetWallets) api.GET("/wallets", v1.GetWallets)
api.POST("/wallet", v1.AddWallet) api.POST("/wallet", v1.AddWallet)
api.GET("/wallet/:id", v1.GetWallet) api.GET("/wallet/:id", v1.GetWallet)
api.PUT("/wallet/:id", v1.EditWallet) api.PUT("/wallet", v1.EditWallet)
api.DELETE("/wallet/:id", v1.DeleteWallet) api.DELETE("/wallet/:id", v1.DeleteWallet)
api.GET("/chains", v1.GetChains) api.GET("/chains", v1.GetChains)
......
...@@ -11,7 +11,6 @@ type RecommendCoin struct { ...@@ -11,7 +11,6 @@ type RecommendCoin struct {
PlatformId int PlatformId int
Sort int Sort int
Type int Type int
Chain string
PageNum int PageNum int
PageSize int PageSize int
...@@ -45,7 +44,6 @@ func (c *RecommendCoin) Add() error { ...@@ -45,7 +44,6 @@ func (c *RecommendCoin) Add() error {
"platform_id": c.PlatformId, "platform_id": c.PlatformId,
"sort": c.Sort, "sort": c.Sort,
"type": c.Type, "type": c.Type,
"chain": c.Chain,
} }
if err := models.AddRecommendCoin(coinRecommend); err != nil { if err := models.AddRecommendCoin(coinRecommend); err != nil {
......
...@@ -2,6 +2,7 @@ package wallet_service ...@@ -2,6 +2,7 @@ package wallet_service
import ( import (
"bwallet/models" "bwallet/models"
"fmt"
) )
type Wallet struct { type Wallet struct {
...@@ -60,10 +61,14 @@ func (w *Wallet) Edit() error { ...@@ -60,10 +61,14 @@ func (w *Wallet) Edit() error {
"name": w.Name, "name": w.Name,
"download_url": w.DownloadUrl, "download_url": w.DownloadUrl,
"introduce": w.Introduce, "introduce": w.Introduce,
"chain_id": w.ChainId,
"issue_charge": w.IssueCharge,
"charge_unit": w.ChargeUnit,
}) })
} }
func (w *Wallet) ExistById() (bool, error) { func (w *Wallet) ExistById() (bool, error) {
fmt.Println(w.Id)
return models.ExistWalletById(w.Id) return models.ExistWalletById(w.Id)
} }
......
No preview for this file type
package validate
import "gopkg.in/go-playground/validator.v8"
type AddChainForm struct {
Platform string `form:"platform" json:"platform" binding:"required"`
Address string `form:"address" json:"address" binding:"required"`
PrivateKey string `form:"private_key" json:"private_key" binding:"required"`
Execer string `form:"execer" json:"execer" binding:"required"`
Token string `form:"token" json:"token" binding:"required"`
Fee float32 `form:"fee"json:"fee" binding:"required"`
BrowerUrl string `form:"brower_url"json:"brower_url" binding:"required"`
}
type EditChainForm struct {
Id int `form:"id" json:"id" binding:"required"`
Platform string `form:"platform" json:"platform" binding:"required"`
Address string `form:"address" json:"address" binding:"required"`
PrivateKey string `form:"private_key" json:"private_key" binding:"required"`
Execer string `form:"execer" json:"execer" binding:"required"`
Token string `form:"token" json:"token" binding:"required"`
Fee float32 `form:"fee"json:"fee" binding:"required"`
BrowerUrl string `form:"brower_url"json:"brower_url" binding:"required"`
}
func (r *AddChainForm) GetError(err validator.ValidationErrors) string {
if val, exist := err["AddChainForm.Platform"]; exist {
if val.Field == "Platform" {
switch val.Tag {
case "required":
return "平行链名称不能为空"
}
}
}
if val, exist := err["AddChainForm.Address"]; exist {
if val.Field == "Address" {
switch val.Tag {
case "required":
return "代扣地址不能为空"
}
}
}
if val, exist := err["AddChainForm.PrivateKey"]; exist {
if val.Field == "PrivateKey" {
switch val.Tag {
case "required":
return "代扣私钥不能为空"
}
}
}
if val, exist := err["AddChainForm.Execer"]; exist {
if val.Field == "Execer" {
switch val.Tag {
case "required":
return "执行器不能为空"
}
}
}
if val, exist := err["AddChainForm.Token"]; exist {
if val.Field == "Token" {
switch val.Tag {
case "required":
return "基础代币不能为空"
}
}
}
if val, exist := err["AddChainForm.Fee"]; exist {
if val.Field == "Fee" {
switch val.Tag {
case "required":
return "手续费(基础代币)不能为空"
}
}
}
if val, exist := err["AddChainForm.BrowerUrl"]; exist {
if val.Field == "BrowerUrl" {
switch val.Tag {
case "required":
return "浏览器链接不能为空"
}
}
}
return "参数错误"
}
func (r *EditChainForm) GetError(err validator.ValidationErrors) string {
if val, exist := err["EditChainForm.Id"]; exist {
if val.Field == "Id" {
switch val.Tag {
case "required":
return "平行链Id不能为空"
}
}
}
if val, exist := err["EditChainForm.Platform"]; exist {
if val.Field == "Platform" {
switch val.Tag {
case "required":
return "平行链名称不能为空"
}
}
}
if val, exist := err["EditChainForm.Address"]; exist {
if val.Field == "Address" {
switch val.Tag {
case "required":
return "代扣地址不能为空"
}
}
}
if val, exist := err["EditChainForm.PrivateKey"]; exist {
if val.Field == "PrivateKey" {
switch val.Tag {
case "required":
return "代扣私钥不能为空"
}
}
}
if val, exist := err["EditChainForm.Execer"]; exist {
if val.Field == "Execer" {
switch val.Tag {
case "required":
return "执行器不能为空"
}
}
}
if val, exist := err["EditChainForm.Token"]; exist {
if val.Field == "Token" {
switch val.Tag {
case "required":
return "基础代币不能为空"
}
}
}
if val, exist := err["EditChainForm.Fee"]; exist {
if val.Field == "Fee" {
switch val.Tag {
case "required":
return "手续费(基础代币)不能为空"
}
}
}
if val, exist := err["EditChainForm.BrowerUrl"]; exist {
if val.Field == "BrowerUrl" {
switch val.Tag {
case "required":
return "浏览器链接不能为空"
}
}
}
return "参数错误"
}
This diff is collapsed.
package validate
import (
"gopkg.in/go-playground/validator.v8"
"bwallet/service/coin_service"
"bwallet/service/recommend_coin_service"
"github.com/astaxie/beego/validation"
)
type AddRecommendCoinForm struct {
Cid int `form:"cid" json:"sid" binding:"required"`
Recommend int `form:"recommend" json:"recommend" binding:"required"`
PlatformId int `form:"platform_id" json:"platform_id" binding:"required"`
Sort int `form:"sort" json:"sort" binding:"required"`
Type int `form:"type" json:"type" binding:"required"`
}
type EditRecommendCoinForm struct {
Id int `form:"id" json:"id" binding:"required"`
Sort int `form:"sort" json:"sort" binding:"required"`
}
func (r *AddRecommendCoinForm) GetError(err validator.ValidationErrors) string {
if val, exist := err["AddRecommendCoinForm.Cid"]; exist {
if val.Field == "Cid" {
switch val.Tag {
case "required":
return "币种Id不能为空"
}
}
}
if val, exist := err["AddRecommendCoinForm.Recommend"]; exist {
if val.Field == "Recommend" {
switch val.Tag {
case "required":
return "推介种类不能为空"
}
}
}
if val, exist := err["AddRecommendCoinForm.PlatformId"]; exist {
if val.Field == "PlatformId" {
switch val.Tag {
case "required":
return "币种推介平台id不能为空"
}
}
}
if val, exist := err["AddRecommendCoinForm.Sort"]; exist {
if val.Field == "Sort" {
switch val.Tag {
case "required":
return "推荐顺序不能为空"
}
}
}
if val, exist := err["AddRecommendCoinForm.Type"]; exist {
if val.Field == "Type" {
switch val.Tag {
case "required":
return "推荐类型不能为空"
}
}
}
return "参数错误"
}
func (r *EditRecommendCoinForm) GetError(err validator.ValidationErrors) string {
if val, exist := err["EditRecommendCoinForm.Id"]; exist {
if val.Field == "Id" {
switch val.Tag {
case "required":
return "币种Id不能为空"
}
}
}
if val, exist := err["EditRecommendCoinForm.Sort"]; exist {
if val.Field == "Sort" {
switch val.Tag {
case "required":
return "推荐排序不能为空"
}
}
}
return "参数错误"
}
func (cr *AddRecommendCoinForm) Valid(v *validation.Validation) {
coinService := coin_service.Coin{Id: cr.Cid}
exists, _ := coinService.ExistById()
if (!exists) {
v.SetError("Cid", "所推荐币种不存在")
}
recommendCoinService := recommend_coin_service.RecommendCoin{
Cid: cr.Cid,
PlatformId: cr.PlatformId,
Type: cr.Type,
}
total, _ := recommendCoinService.Count()
if total > 0 {
v.SetError("Cid", "推荐币种已经存在")
}
}
package validate
import "gopkg.in/go-playground/validator.v8"
type AddWalletForm struct {
Name string `form:"name" json:"name" binding:"required"`
DownloadUrl string `form:"download_url" json:"download_url" binding:"required"`
Introduce string `form:"introduce" json:"introduce" binding:"required"`
ChainId int `form:"chain_id" json:"chain_id" binding:"required"`
IssueCharge float32 `form:"issue_charge" json:"issue_charge" binding:"required"`
ChargeUnit string `form:"charge_unit"json:"charge_unit" binding:"required"`
}
type EditWalletForm struct {
Id int `form:"id" json:"id" binding:"required"`
Name string `form:"name" json:"name" binding:"required"`
DownloadUrl string `form:"download_url" json:"download_url" binding:"required"`
Introduce string `form:"introduce" json:"introduce" binding:"required"`
ChainId int `form:"chain_id" json:"chain_id" binding:"required"`
IssueCharge float32 `form:"issue_charge" json:"issue_charge" binding:"required"`
ChargeUnit string `form:"charge_unit"json:"charge_unit" binding:"required"`
}
func (r *AddWalletForm) GetError(err validator.ValidationErrors) string {
if val, exist := err["AddWalletForm.Name"]; exist {
if val.Field == "Name" {
switch val.Tag {
case "required":
return "钱包名称不能为空"
}
}
}
if val, exist := err["AddWalletForm.DownloadUrl"]; exist {
if val.Field == "DownloadUrl" {
switch val.Tag {
case "required":
return "钱包下载地址不能为空"
}
}
}
if val, exist := err["AddWalletForm.Introduce"]; exist {
if val.Field == "Introduce" {
switch val.Tag {
case "required":
return "钱包介绍不能为空"
}
}
}
if val, exist := err["AddWalletForm.IssueCharge"]; exist {
if val.Field == "IssueCharge" {
switch val.Tag {
case "required":
return "钱包手续费不能为空"
}
}
}
if val, exist := err["AddWalletForm.ChargeUnit"]; exist {
if val.Field == "ChargeUnit" {
switch val.Tag {
case "required":
return "钱包手续费单位不能为空"
}
}
}
if val, exist := err["AddWalletForm.ChainId"]; exist {
if val.Field == "ChainId" {
switch val.Tag {
case "required":
return "钱包所属不能为空"
}
}
}
return "参数错误"
}
func (r *EditWalletForm) GetError(err validator.ValidationErrors) string {
if val, exist := err["EditWalletForm.Id"]; exist {
if val.Field == "Id" {
switch val.Tag {
case "required":
return "钱包Id不能为空"
}
}
}
if val, exist := err["EditWalletForm.Name"]; exist {
if val.Field == "Name" {
switch val.Tag {
case "required":
return "钱包名称不能为空"
}
}
}
if val, exist := err["EditWalletForm.DownloadUrl"]; exist {
if val.Field == "DownloadUrl" {
switch val.Tag {
case "required":
return "钱包下载地址不能为空"
}
}
}
if val, exist := err["EditWalletForm.Introduce"]; exist {
if val.Field == "Introduce" {
switch val.Tag {
case "required":
return "钱包介绍不能为空"
}
}
}
if val, exist := err["EditWalletForm.IssueCharge"]; exist {
if val.Field == "IssueCharge" {
switch val.Tag {
case "required":
return "钱包手续费不能为空"
}
}
}
if val, exist := err["EditWalletForm.ChargeUnit"]; exist {
if val.Field == "ChargeUnit" {
switch val.Tag {
case "required":
return "钱包手续费单位不能为空"
}
}
}
if val, exist := err["EditWalletForm.ChainId"]; exist {
if val.Field == "ChainId" {
switch val.Tag {
case "required":
return "钱包所属不能为空"
}
}
}
return "参数错误"
}
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