liuxiaolong
2019-10-26 eb2c3a8890a0e9695c0cd2bc3b3dc310c6fbcfa9
fix user timeout
2个文件已修改
23 ■■■■■ 已修改文件
controllers/user.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
middlewares/auth/auth.go 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/user.go
@@ -161,6 +161,7 @@
    var api dbapi.UserApi
    b, d := api.SaveAuth(paramBody)
    if b {
        auth.SetOutUser(userEditVo.Id)
        util.ResponseFormat(c,code.UpdateSuccess,d)
    } else {
        util.ResponseFormat(c,code.UpdateFail,"保存失败")
middlewares/auth/auth.go
@@ -4,6 +4,7 @@
    "github.com/gin-gonic/gin"
    "net/http"
    "strings"
    "sync"
    "webserver/extend/code"
    "webserver/extend/util"
)
@@ -25,6 +26,23 @@
    return &authDriver
}
var outUserM = make(map[string]string,0)
var lock sync.RWMutex
func SetOutUser(userId string) {
    lock.Lock()
    defer lock.Unlock()
    outUserM[userId] = userId
}
func removeOutUser(userId string) {
    lock.Lock()
    defer lock.Unlock()
    if _,ok := outUserM[userId];ok{
        delete(outUserM,userId)
    }
}
func AuthHandler() gin.HandlerFunc {
    return func(c *gin.Context) {
        urlPath := c.Request.URL.Path
@@ -35,6 +53,10 @@
                util.ResponseFormat(c,code.TokenNotFound,"尚未登录,请登录")
                c.Abort()
            }
            user := (*jwtDriver).User(c)
            loginM := user.(map[string]interface{})
            userId := loginM["id"].(string)
            removeOutUser(userId)
            c.Next()
        } else {
            c.Next()