Commit cbe425aa authored by shajiaiming's avatar shajiaiming

权限

parent 032b5b1d
[app] [app]
PageSize = 10 PageSize = 50
JwtSecret = bwallet JwtSecret = bwallet
Timeout = 60 * 60 * 24 Timeout = 60 * 60 * 24
PrefixUrl = http://127.0.0.1:8983 PrefixUrl = http://127.0.0.1:8983
......
package auth
import (
"github.com/gin-gonic/gin"
"bwallet/pkg/e"
"net/http"
"bwallet/models"
"fmt"
)
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)
fmt.Println(err)
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 models package models
import "github.com/jinzhu/gorm" import (
"github.com/jinzhu/gorm"
)
type Auth struct { type Auth struct {
ID int `gorm:"primary_key" json:"id"` ID int `gorm:"primary_key" json:"id"`
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
Group string `json:"group"`
PlatformId int `json:"platform_id"`
} }
func (a Auth) TableName() string { func (a Auth) TableName() string {
...@@ -25,3 +29,22 @@ func CheckAuth(username, password string) (bool, error) { ...@@ -25,3 +29,22 @@ func CheckAuth(username, password string) (bool, error) {
return false, nil return false, nil
} }
func CheckToken(token string) (bool, error) {
var auth Auth
err := db.Select("id").Where("access_token = ?", token).First(&auth).Error
if err != nil {
return false, err
}
return true, nil
}
func GetUserInfo (token string) (*Auth, error){
var auth Auth
if err := db.Where("access_token = ?", token).First(&auth).Error; err != nil {
return nil, err
}
return &auth, nil
}
...@@ -8,28 +8,34 @@ type Coin struct { ...@@ -8,28 +8,34 @@ type Coin struct {
Model Model
Id int `json:"primary_key,omitempty"` Id int `json:"primary_key,omitempty"`
Sid string `json:"sid,omitempty"` Sid string `json:"sid,omitempty"` //全称
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"` //简称
OptionalName string `json:"optional_name,omitempty"` Nickname string `json:"nickname,omitempty"` //全称
Nickname string `json:"nickname,omitempty"` Icon string `json:"icon,omitempty"` //币种图标
Icon string `json:"icon,omitempty"` Introduce string `json:"introduce,omitempty"` //币种介绍
Introduce string `json:"introduce,omitempty"` Official string `json:"official,omitempty"` //官网
Official string `json:"official,omitempty"` Paper string `json:"paper,omitempty"` //白皮书
Paper string `json:"paper,omitempty"` Platform string `json:"platform,omitempty"` //平台
Exchange int `json:"exchange,omitempty"` Chain string `json:"chain,omitempty"` //主链
Platform string `json:"platform,omitempty"` Release string `json:"release,omitempty"` //发行时间
Chain string `json:"chain,omitempty"` AreaSearch string `json:"area_search,omitempty"` //区块查询
Release string `json:"release,omitempty"` PublishCount float64 `json:"publish_count,omitempty"` //发行总量
Price float64 `json:"price,omitempty"` CirculateCount float64 `json:"circulate_count,omitempty"` //流通总量
AreaSearch string `json:"area_search,omitempty"` Decimals int `json:"decimals,omitempty"` //币种精度
PublishCount float64 `json:"publish_count,omitempty"` Address string `json:"address,omitempty"` //合约地址
CirculateCount float64 `json:"circulate_count,omitempty"` Treaty int `json:"treaty,omitempty"` //币种类型
Decimals int `json:"decimals,omitempty"` PlatformId int `json:"platform_id,omitempty"`
Address string `json:"address,omitempty"`
PlatformId string `json:"platform_id,omitempty"` //OptionalName string `json:"optional_name,omitempty"`
Treaty int `json:"treaty,omitempty"` //Exchange int `json:"exchange,omitempty"`
//Price float64 `json:"price,omitempty"`
} }
func (c Coin) TableName() string {
return "coins"
}
/** /**
* 通过id检查币种是否存在 * 通过id检查币种是否存在
* @param id * @param id
...@@ -70,6 +76,7 @@ func GetCoinTotal(maps interface{}) (int, error) { ...@@ -70,6 +76,7 @@ func GetCoinTotal(maps interface{}) (int, error) {
*/ */
func GetCoins(pageNum, pageSize int, maps interface{}) ([]*Coin, error) { func GetCoins(pageNum, pageSize int, maps interface{}) ([]*Coin, error) {
var coins []*Coin var coins []*Coin
err := db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&coins).Error err := db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&coins).Error
if err != nil && err != gorm.ErrRecordNotFound { if err != nil && err != gorm.ErrRecordNotFound {
return nil, err return nil, err
...@@ -107,24 +114,25 @@ func AddCoin(data map[string]interface{}) (error) { ...@@ -107,24 +114,25 @@ func AddCoin(data map[string]interface{}) (error) {
coin := Coin{ coin := Coin{
Sid: data["sid"].(string), Sid: data["sid"].(string),
Name: data["name"].(string), Name: data["name"].(string),
OptionalName: data["optional_name"].(string),
Nickname: data["nickname"].(string), Nickname: data["nickname"].(string),
Icon: data["icon"].(string), Icon: data["icon"].(string),
Introduce: data["introduce"].(string), Introduce: data["introduce"].(string),
Official: data["official"].(string), Official: data["official"].(string),
Paper: data["paper"].(string), Paper: data["paper"].(string),
Exchange: data["exchange"].(int),
Platform: data["platform"].(string), Platform: data["platform"].(string),
Chain: data["chain"].(string), Chain: data["chain"].(string),
Release: data["release"].(string), Release: data["release"].(string),
Price: data["price"].(float64),
AreaSearch: data["area_search"].(string), AreaSearch: data["area_search"].(string),
PublishCount: data["publish_count"].(float64), PublishCount: data["publish_count"].(float64),
CirculateCount: data["circulate_count"].(float64), CirculateCount: data["circulate_count"].(float64),
Decimals: data["decimals"].(int), Decimals: data["decimals"].(int),
Address: data["address"].(string), Address: data["address"].(string),
PlatformId: data["platform_id"].(string), PlatformId: data["platform_id"].(int),
Treaty: data["treaty"].(int), Treaty: data["treaty"].(int),
//OptionalName: data["optional_name"].(string),
//Exchange: data["exchange"].(int),
//Price: data["price"].(float64),
} }
if err := db.Create(&coin).Error; err != nil { if err := db.Create(&coin).Error; err != nil {
return err return err
......
...@@ -11,8 +11,13 @@ import ( ...@@ -11,8 +11,13 @@ import (
func GetPage(c *gin.Context) int { func GetPage(c *gin.Context) int {
result := 0 result := 0
page := com.StrTo(c.Query("page")).MustInt() page := com.StrTo(c.Query("page")).MustInt()
size := com.StrTo(c.DefaultQuery("size", "0")).MustInt()
if size == 0 {
size = setting.AppSetting.PageSize
}
if page > 0 { if page > 0 {
result = (page - 1) * setting.AppSetting.PageSize result = (page - 1) * size
} }
return result return result
...@@ -20,7 +25,7 @@ func GetPage(c *gin.Context) int { ...@@ -20,7 +25,7 @@ func GetPage(c *gin.Context) int {
// GetLimit get page parameters // GetLimit get page parameters
func GetLimit(c *gin.Context) int { func GetLimit(c *gin.Context) int {
result := 10 result := setting.AppSetting.PageSize
size := com.StrTo(c.Query("size")).MustInt() size := com.StrTo(c.Query("size")).MustInt()
if size > 0 { if size > 0 {
result = size result = size
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"bwallet/pkg/e" "bwallet/pkg/e"
"bwallet/service/coin_service" "bwallet/service/coin_service"
"bwallet/pkg/util" "bwallet/pkg/util"
"bwallet/service/auth_service"
) )
type AddCoinForm struct { type AddCoinForm struct {
...@@ -30,8 +31,8 @@ type AddCoinForm struct { ...@@ -30,8 +31,8 @@ type AddCoinForm struct {
CirculateCount float64 `form:"circulate_count"` CirculateCount float64 `form:"circulate_count"`
Decimals int `form:"decimals" valid:"Max(10)"` Decimals int `form:"decimals" valid:"Max(10)"`
Address string `form:"address" valid:"MaxSize(50)"` Address string `form:"address" valid:"MaxSize(50)"`
PlatformId string `form:"platform_id" valid:"MaxSize(10)"`
Treaty int `form:"treaty" valid:"Range(1,2)"` Treaty int `form:"treaty" valid:"Range(1,2)"`
PlatformId int `form:"platform_id" valid:"Required;Min(1)"`
} }
type EditCoinForm struct { type EditCoinForm struct {
...@@ -54,8 +55,8 @@ type EditCoinForm struct { ...@@ -54,8 +55,8 @@ type EditCoinForm struct {
CirculateCount float64 `form:"circulate_count"` CirculateCount float64 `form:"circulate_count"`
Decimals int `form:"decimals" valid:"Max(10)"` Decimals int `form:"decimals" valid:"Max(10)"`
Address string `form:"address" valid:"MaxSize(50)"` Address string `form:"address" valid:"MaxSize(50)"`
PlatformId string `form:"platform_id" valid:"MaxSize(10)"`
Treaty int `form:"treaty" valid:"Range(1,2)"` Treaty int `form:"treaty" valid:"Range(1,2)"`
PlatformId int `form:"platform_id" valid:"Required;Min(1)"`
} }
func GetCoin(c *gin.Context) { func GetCoin(c *gin.Context) {
...@@ -70,20 +71,24 @@ func GetCoin(c *gin.Context) { ...@@ -70,20 +71,24 @@ func GetCoin(c *gin.Context) {
return return
} }
coinService := coin_service.Coin{Id: id} token := c.Request.Header.Get("Token")
exists, err := coinService.ExistById() authService := auth_service.Auth{Token: token}
auth, _ := authService.GetUserInfo()
if err != nil { group := auth.Group
appG.Response(http.StatusInternalServerError, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil) var platform_id int
return if ("administrator" == group) {
platform_id = com.StrTo(c.DefaultQuery("platform_id", "1")).MustInt()
} else {
platform_id = auth.PlatformId
} }
coinService := coin_service.Coin{
if (!exists) { Id: id,
appG.Response(http.StatusOK, e.ERROR_NOT_EXIST_ARTICLE, nil) PlatformId: platform_id,
return PageNum: util.GetPage(c),
PageSize: util.GetLimit(c),
} }
coin, err := coinService.Get() coin, err := coinService.GetAll()
if err != nil { if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_GET_ARTICLE_FAIL, nil) appG.Response(http.StatusInternalServerError, e.ERROR_GET_ARTICLE_FAIL, nil)
return return
......
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"bwallet/pkg/qrcode" "bwallet/pkg/qrcode"
"bwallet/routers/api" "bwallet/routers/api"
"bwallet/routers/api/v1" "bwallet/routers/api/v1"
"bwallet/middleware/jwt" "bwallet/middleware/auth"
) )
func InitRouter() *gin.Engine { func InitRouter() *gin.Engine {
...@@ -25,7 +25,7 @@ func InitRouter() *gin.Engine { ...@@ -25,7 +25,7 @@ func InitRouter() *gin.Engine {
r.POST("/upload", api.UploadImage) r.POST("/upload", api.UploadImage)
api := r.Group("/api") api := r.Group("/api")
api.Use(jwt.JWT()) api.Use(auth.AUTH())
{ {
api.GET("/explore-app/category-update/:id", v1.GetCategory) api.GET("/explore-app/category-update/:id", v1.GetCategory)
...@@ -33,7 +33,7 @@ func InitRouter() *gin.Engine { ...@@ -33,7 +33,7 @@ func InitRouter() *gin.Engine {
api.GET("/coins", v1.GetCoins) api.GET("/coins", v1.GetCoins)
api.POST("/coin", v1.AddCoin) api.POST("/coin", v1.AddCoin)
api.GET("/coin/:id", v1.GetCoin) api.GET("/coin/:id", v1.GetCoin)
api.PUT("/coin/:id", v1.EditCoin) api.PUT("/coin", v1.EditCoin)
api.DELETE("/coin/:id", v1.DeleteCoin) api.DELETE("/coin/:id", v1.DeleteCoin)
api.GET("/get-platform", v1.GetCoinsPlatform) api.GET("/get-platform", v1.GetCoinsPlatform)
......
...@@ -5,8 +5,18 @@ import "bwallet/models" ...@@ -5,8 +5,18 @@ import "bwallet/models"
type Auth struct { type Auth struct {
Username string Username string
Password string Password string
Token string
} }
func (a *Auth) Check() (bool, error){ func (a *Auth) Check() (bool, error) {
return models.CheckAuth(a.Username, a.Password) return models.CheckAuth(a.Username, a.Password)
} }
func (a *Auth) GetUserInfo() (*models.Auth, error) {
auth, err := models.GetUserInfo(a.Token)
if err != nil {
return nil, err
}
return auth, nil
}
...@@ -25,7 +25,7 @@ type Coin struct { ...@@ -25,7 +25,7 @@ type Coin struct {
CirculateCount float64 CirculateCount float64
Decimals int Decimals int
Address string Address string
PlatformId string PlatformId int
Treaty int Treaty int
PageNum int PageNum int
...@@ -44,7 +44,7 @@ func (c *Coin) Get() (*models.Coin, error) { ...@@ -44,7 +44,7 @@ func (c *Coin) Get() (*models.Coin, error) {
} }
var m map[string]interface{} var m map[string]interface{}
if err := json.Unmarshal([]byte(coin.Nickname), &m);err == nil { if err := json.Unmarshal([]byte(coin.Nickname), &m); err == nil {
coin.Nickname = m["zh-CN"].(string) coin.Nickname = m["zh-CN"].(string)
} }
return coin, nil return coin, nil
...@@ -65,7 +65,7 @@ func (c *Coin) GetAll() ([]*models.Coin, error) { ...@@ -65,7 +65,7 @@ func (c *Coin) GetAll() ([]*models.Coin, error) {
var m map[string]interface{} var m map[string]interface{}
for _, value := range coins { for _, value := range coins {
if err := json.Unmarshal([]byte(value.Nickname), &m);err == nil { if err := json.Unmarshal([]byte(value.Nickname), &m); err == nil {
value.Nickname = m["zh-CN"].(string) value.Nickname = m["zh-CN"].(string)
} }
} }
...@@ -180,6 +180,10 @@ func (c *Coin) GetCoinsPlatform() ([]*models.Coin, error) { ...@@ -180,6 +180,10 @@ func (c *Coin) GetCoinsPlatform() ([]*models.Coin, error) {
func (c *Coin) getMaps() (map[string]interface{}) { func (c *Coin) getMaps() (map[string]interface{}) {
maps := make(map[string]interface{}) maps := make(map[string]interface{})
if c.Id != 0 {
maps["id"] = c.Id
}
if c.Name != "" { if c.Name != "" {
maps["name"] = c.Name maps["name"] = c.Name
} }
...@@ -190,5 +194,9 @@ func (c *Coin) getMaps() (map[string]interface{}) { ...@@ -190,5 +194,9 @@ func (c *Coin) getMaps() (map[string]interface{}) {
maps["platform"] = c.Platform maps["platform"] = c.Platform
} }
if c.PlatformId != 0 {
maps["platform_id"] = c.PlatformId
}
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