app
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"git.rosy.net.cn/jx-print/globals"
|
||||
"git.rosy.net.cn/jx-print/model"
|
||||
"git.rosy.net.cn/jx-print/utils"
|
||||
"github.com/dchest/captcha"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -22,35 +23,43 @@ func init() {
|
||||
globals.SugarLogger.Debugf("test init ...")
|
||||
}
|
||||
|
||||
func checkToken(c *gin.Context) (token string, b bool) {
|
||||
var (
|
||||
cookie *http.Cookie
|
||||
err error
|
||||
)
|
||||
//验证token
|
||||
//v2下的接口需要验证,v1不需要
|
||||
if !strings.Contains(c.Request.URL.String(), "v1") {
|
||||
if cookie, err = c.Request.Cookie("token"); err != nil {
|
||||
c.JSON(http.StatusOK, &CallBack{
|
||||
Desc: err.Error(),
|
||||
Code: model.ErrCodeNormal,
|
||||
})
|
||||
return "", false
|
||||
} else {
|
||||
token = cookie.Value
|
||||
}
|
||||
if user := utils.GetKet(token); user == nil {
|
||||
err = fmt.Errorf("token过期或无效,请重新登录!")
|
||||
c.JSON(http.StatusOK, &CallBack{
|
||||
Desc: err.Error(),
|
||||
Code: model.ErrCodeToken,
|
||||
})
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
return token, true
|
||||
}
|
||||
|
||||
func callFunc(c *gin.Context, worker func() (retVal interface{}, errCode string, err error)) bool {
|
||||
var (
|
||||
err error
|
||||
callBack = &CallBack{
|
||||
Code: model.ErrCodeNormal,
|
||||
}
|
||||
token string
|
||||
cookie *http.Cookie
|
||||
result interface{}
|
||||
)
|
||||
//验证token
|
||||
//v2下的接口需要验证,v1不需要
|
||||
if !strings.Contains(c.Request.URL.String(), "v1") {
|
||||
if cookie, err = c.Request.Cookie("token"); err != nil {
|
||||
callBack.Desc = err.Error()
|
||||
c.JSON(http.StatusOK, callBack)
|
||||
return false
|
||||
} else {
|
||||
token = cookie.Value
|
||||
}
|
||||
fmt.Println(token)
|
||||
//if user := putils.GetKet(token); user == nil {
|
||||
// err = fmt.Errorf("token过期或无效,请重新登录!")
|
||||
// callBack.Desc = err.Error()
|
||||
// callBack.Code = model.ErrCodeToken
|
||||
// c.JSON(http.StatusOK, callBack)
|
||||
// return false
|
||||
//}
|
||||
}
|
||||
if result, _, err = worker(); err == nil {
|
||||
if result != nil {
|
||||
if data, err := json.Marshal(&result); err == nil {
|
||||
|
||||
Reference in New Issue
Block a user