Commit 14853cbb authored by shajiaiming's avatar shajiaiming

fix

parent 849f944f
[app]
PageSize = 10
JwtSecret = 233
JwtSecret = bwallet
Timeout = 60 * 60 * 24
PrefixUrl = http://127.0.0.1:8983
RuntimeRootPath = runtime/
......
......@@ -8,6 +8,10 @@ type Auth struct {
Password string `json:"password"`
}
func (a Auth) TableName() string {
return "coin_user"
}
func CheckAuth(username, password string) (bool, error) {
var auth Auth
err := db.Select("id").Where(Auth{Username: username, Password: password}).First(&auth).Error
......
package models
import "github.com/jinzhu/gorm"
type ExploreAppCategory struct {
ID uint64 `gorm:"primary_key;auto_increment" json:"id"`
Name string `gorm:"size:255;not null;" json:"name"`
Sort uint32 `gorm:"not null;default:1" json:"sort"`
Limit uint32 `gorm:"not null;default:1" json:"limit"`
Style uint32 `gorm:"not null;default:1" json:"style"`
Status uint8 `gorm:"not null;default:1" json:"status"`
PlatformId uint64 `gorm:"not null;default:0" json:"platform_id"`
}
/**
* 通过id检查类别是否存在
* @param id
* @return
*/
func ExistCategoryById(id int) (bool, error) {
var category ExploreAppCategory
err := db.Select("id").Where("id = ?", id).First(&category).Error
if err != nil && err != gorm.ErrRecordNotFound {
return false, err
}
if category.ID > 0 {
return true, nil
}
return false, nil
}
/**
* 通过id获取指定类别信息
* @param id
* @return
*/
func GetCategory(id int) (*ExploreAppCategory, error) {
var category ExploreAppCategory
err := db.Where("id = ?", id).First(&category).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
err = db.Model(&category).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return &category, nil
}
......@@ -26,6 +26,15 @@ const (
ERROR_GET_ARTICLE_FAIL = 10018
ERROR_GEN_ARTICLE_POSTER_FAIL = 10019
ERROR_NOT_EXIST_CATEGORY = 10011
ERROR_CHECK_EXIST_CATEGORY_FAIL = 10012
ERROR_ADD_CATEGORY_FAIL = 10013
ERROR_DELETE_CATEGORY_FAIL = 10014
ERROR_EDIT_CATEGORY_FAIL = 10015
ERROR_COUNT_CATEGORY_FAIL = 10016
ERROR_GET_CATEGORIES_FAIL = 10017
ERROR_GET_CATEGORY_FAIL = 10018
ERROR_AUTH_CHECK_TOKEN_FAIL = 40001
ERROR_AUTH_CHECK_TOKEN_TIMEOUT = 40002
ERROR_AUTH_TOKEN = 40003
......
......@@ -10,6 +10,7 @@ import (
type App struct {
JwtSecret string
PageSize int
Timeout int
PrefixUrl string
RuntimeRootPath string
......
......@@ -3,6 +3,7 @@ package util
import (
"github.com/dgrijalva/jwt-go"
"time"
"bwallet/pkg/setting"
)
var jwtSecret []byte
......@@ -14,17 +15,19 @@ type Claims struct {
}
func GenerateToken(username, password string) (string, error) {
nowTime := time.Now()
expireTime := nowTime.Add(3 * time.Hour)
claims := Claims{
EncodeMD5(username),
EncodeMD5(password),
jwt.StandardClaims{
ExpiresAt: expireTime.Unix(),
//nowTime := time.Now()
//expireTime := nowTime.Add(3 * time.Hour)
expiresTime := time.Now().Unix() + int64(60 * 60 * 24)
claims := jwt.StandardClaims{
Audience: username,
ExpiresAt: expiresTime,
IssuedAt: time.Now().Unix(),
Issuer: "bwallet",
},
NotBefore: time.Now().Unix(),
Subject: "login",
}
var jwtSecret = []byte(setting.AppSetting.JwtSecret)
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
token, err := tokenClaims.SignedString(jwtSecret)
......@@ -32,13 +35,13 @@ func GenerateToken(username, password string) (string, error) {
return token, err
}
func ParseToken(token string)(*Claims, error){
tokenClaims,err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token)(interface{}, error){
func ParseToken(token string) (*Claims, error) {
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
return jwtSecret, nil
})
if tokenClaims != nil {
if claims, ok:= tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
return claims, nil
}
}
......
......@@ -15,7 +15,7 @@ type auth struct {
Password string `valid:"Required; MaxSize(50)"`
}
func GetAuth(c *gin.Context){
func GetAuth(c *gin.Context) {
appG := app.Gin{C: c}
valid := validation.Validation{}
......
package v1
import (
"github.com/gin-gonic/gin"
"bwallet/pkg/app"
"github.com/Unknwon/com"
"github.com/astaxie/beego/validation"
"net/http"
"bwallet/pkg/e"
"bwallet/service/explore_service"
)
/**
* 通过id获取指定类别信息
* @return
*/
func GetCategory(c *gin.Context) {
appG := app.Gin{C: c}
id := com.StrTo(c.Param("id")).MustInt()
valid := validation.Validation{}
valid.Min(id, 1, "id")
if valid.HasErrors() {
app.MarkErrors(valid.Errors)
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
return
}
categoryService := explore_service.Category{Id: id}
exists, err := categoryService.ExistById()
if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_CHECK_EXIST_CATEGORY_FAIL, nil)
return
}
if !exists {
appG.Response(http.StatusOK, e.ERROR_NOT_EXIST_CATEGORY, nil)
return
}
category, err := categoryService.Get()
if err != nil {
appG.Response(http.StatusInternalServerError, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil)
return
}
appG.Response(http.StatusOK, e.SUCCESS, category)
}
......@@ -24,28 +24,30 @@ func InitRouter() *gin.Engine {
r.POST("/auth", api.GetAuth)
r.POST("/upload", api.UploadImage)
apiv1 := r.Group("/api")
apiv1.Use(jwt.JWT())
api := r.Group("/api")
api.Use(jwt.JWT())
{
apiv1.GET("/coins", v1.GetCoins)
apiv1.POST("/coin", v1.AddCoin)
apiv1.GET("/coin/:id", v1.GetCoin)
apiv1.PUT("/coin/:id", v1.EditCoin)
apiv1.DELETE("/coin/:id", v1.DeleteCoin)
apiv1.GET("/get-platform", v1.GetCoinsPlatform)
apiv1.GET("/wallets", v1.GetWallets)
apiv1.POST("/wallet", v1.AddWallet)
apiv1.GET("/wallet/:id", v1.GetWallet)
apiv1.PUT("/wallet/:id", v1.EditWallet)
apiv1.DELETE("/wallet/:id", v1.DeleteWallet)
apiv1.GET("/coins-recommend", v1.GetCoinsRecommend)
apiv1.POST("/coin-recommend", v1.AddCoinRecommend)
apiv1.GET("/coin-recommend/:id", v1.GetCoinRecommend)
apiv1.PUT("/coin-recommend/:id", v1.EditCoinRecommend)
apiv1.DELETE("/coin-recommend/:id", v1.DeleteCoinRecommend)
api.GET("/explore-app/category-update/:id", v1.GetCategory)
api.GET("/coins", v1.GetCoins)
api.POST("/coin", v1.AddCoin)
api.GET("/coin/:id", v1.GetCoin)
api.PUT("/coin/:id", v1.EditCoin)
api.DELETE("/coin/:id", v1.DeleteCoin)
api.GET("/get-platform", v1.GetCoinsPlatform)
api.GET("/wallets", v1.GetWallets)
api.POST("/wallet", v1.AddWallet)
api.GET("/wallet/:id", v1.GetWallet)
api.PUT("/wallet/:id", v1.EditWallet)
api.DELETE("/wallet/:id", v1.DeleteWallet)
api.GET("/coins-recommend", v1.GetCoinsRecommend)
api.POST("/coin-recommend", v1.AddCoinRecommend)
api.GET("/coin-recommend/:id", v1.GetCoinRecommend)
api.PUT("/coin-recommend/:id", v1.EditCoinRecommend)
api.DELETE("/coin-recommend/:id", v1.DeleteCoinRecommend)
}
return r
......
package explore_service
import "bwallet/models"
type Category struct {
Id int
Name string
Sort int
Limit int
Style int
Status int
PlatformId int
}
func (c *Category) Get() (*models.ExploreAppCategory, error) {
category, err := models.GetCategory(c.Id)
if err != nil {
return nil, err
}
return category, nil
}
func (c *Category) ExistById() (bool, error) {
return models.ExistCategoryById(c.Id)
}
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