You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-fly/middleware/jwt.go

45 lines
996 B

package middleware
import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/tools"
"time"
)
func JwtPageMiddleware(c *gin.Context){
//暂时不处理
//token := c.Query("token")
//userinfo := tools.ParseToken(token)
//if userinfo == nil {
// c.Redirect(302,"/login")
// c.Abort()
//}
}
func JwtApiMiddleware(c *gin.Context){
token := c.GetHeader("token")
userinfo := tools.ParseToken(token)
if userinfo == nil||userinfo["name"]==nil||userinfo["create_time"]==nil {
c.JSON(200, gin.H{
"code": 400,
"msg": "验证失败",
})
c.Abort()
return
}
createTime:=int64(userinfo["create_time"].(float64))
var expire int64=2*60*60
nowTime:=time.Now().Unix();
if (nowTime-createTime) >=expire{
c.JSON(200, gin.H{
"code": 401,
"msg": "token失效",
})
c.Abort()
}
c.Set("user",userinfo["name"])
if userinfo["type"]=="kefu"{
c.Set("kefu_id",userinfo["kefu_id"])
c.Set("kefu_name",userinfo["name"])
c.Set("role_id",userinfo["role_id"])
}
}