From 34adf896a389c4b93874635444702f23bf334257 Mon Sep 17 00:00:00 2001 From: mohuishou <1@lailin.xyz> Date: Sun, 28 Jan 2018 13:25:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B7=AF=E7=94=B1=EF=BC=8C?= =?UTF-8?q?=E6=9A=82=E6=97=B6=E5=85=B3=E9=97=ADtoken=E6=97=B6=E6=95=88?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/library/library.go | 9 +++++++-- main.go | 16 +++++++--------- middleware/jwt.go | 21 ++++++++++----------- route/routes.go | 1 + 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/api/library/library.go b/api/library/library.go index b8421ce..9d96c26 100644 --- a/api/library/library.go +++ b/api/library/library.go @@ -65,7 +65,12 @@ func Loan(ctx iris.Context) { func GetBook(ctx iris.Context) { uid := middleware.GetUserID(ctx) userLibrary := model.UserLibrary{} - model.DB().Where("user_id = ?", uid).Find(&userLibrary) + if err := model.DB().Where("user_id = ?", uid).Find(&userLibrary).Error; err != nil { + api.Error(ctx, 60401, "用户暂未绑定", map[string]interface{}{ + "verify": userLibrary.Verify, + }) + return + } lib, err := userLibrary.GetLibrary() if err != nil { api.Error(ctx, 60401, err.Error(), map[string]interface{}{ @@ -74,7 +79,7 @@ func GetBook(ctx iris.Context) { return } - isHistory := "1" + isHistory := ctx.URLParam("is_history") books := []sculibrary.LoanBook{} if isHistory == "1" { diff --git a/main.go b/main.go index cd9476f..01671ff 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,6 @@ import ( "github.com/mohuishou/scuplus-go/config" "github.com/mohuishou/scuplus-go/middleware" - "github.com/betacraft/yaag/irisyaag" - "github.com/betacraft/yaag/yaag" "github.com/kataras/iris" "github.com/mohuishou/scuplus-go/route" ) @@ -17,13 +15,13 @@ func main() { env := os.Getenv("SCUPLUS_ENV") if env == "test" { - yaag.Init(&yaag.Config{ // <- IMPORTANT, init the middleware. - On: true, - DocTitle: "SCUPLUS", - DocPath: "apidoc/apidoc.html", - BaseUrls: map[string]string{"Production": "", "Staging": ""}, - }) - app.Use(irisyaag.New()) // <- IMPORTANT, register the middleware. + // yaag.Init(&yaag.Config{ // <- IMPORTANT, init the middleware. + // On: true, + // DocTitle: "SCUPLUS", + // DocPath: "apidoc/apidoc.html", + // BaseUrls: map[string]string{"Production": "", "Staging": ""}, + // }) + // app.Use(irisyaag.New()) // <- IMPORTANT, register the middleware. } // 注册中间件 diff --git a/middleware/jwt.go b/middleware/jwt.go index 0221e8d..c98a983 100644 --- a/middleware/jwt.go +++ b/middleware/jwt.go @@ -1,7 +1,6 @@ package middleware import ( - "log" "time" "github.com/mohuishou/scuplus-go/config" @@ -49,16 +48,16 @@ func jwtMiddle(ctx iris.Context) { } // token 时效验证 - end, ok := token.Claims.(jwt.MapClaims)["end"].(float64) - if !ok || time.Now().Unix() > int64(end) { - log.Println("[Error]: 登录信息已失效", end) - ctx.JSON(map[string]interface{}{ - "status": 401, - "msg": "用户尚未登录,获取用户信息失败", - }) - ctx.StopExecution() - return - } + // end, ok := token.Claims.(jwt.MapClaims)["end"].(float64) + // if !ok || time.Now().Unix() > int64(end) { + // log.Println("[Error]: 登录信息已失效", end) + // ctx.JSON(map[string]interface{}{ + // "status": 401, + // "msg": "用户尚未登录,获取用户信息失败", + // }) + // ctx.StopExecution() + // return + // } // 设置用户id ctx.Values().Set("user_id", userID) diff --git a/route/routes.go b/route/routes.go index 5667efb..fb01fb7 100644 --- a/route/routes.go +++ b/route/routes.go @@ -26,4 +26,5 @@ func Routes(app *iris.Application) { app.Post("/library/search", library.Search) app.Get("/library/books", library.GetBook) app.Post("/library/loan", library.Loan) + app.Post("/library/bind", library.BindLibrary) }