Browse Source

登录模块修改

zhangjinkun 5 năm trước cách đây
mục cha
commit
1ee5cc58e5
3 tập tin đã thay đổi với 23 bổ sung16 xóa
  1. 1 0
      src/jy/admin/admin.go
  2. 21 0
      src/jy/front/front.go
  3. 1 16
      src/jy/router/router.go

+ 1 - 0
src/jy/admin/admin.go

@@ -2,6 +2,7 @@ package admin
 
 import (
 	"html/template"
+	. "jy/front"
 	. "jy/mongodbutil"
 	. "jy/router"
 	qu "qfw/util"

+ 21 - 0
src/jy/front/front.go

@@ -85,3 +85,24 @@ func GetUserMenu(role string) []map[string]interface{} {
 	}
 	return list
 }
+
+func AuthMiddleWare() gin.HandlerFunc {
+	return func(c *gin.Context) {
+		session := sessions.Default(c)
+		user := session.Get("user")
+		if user != nil {
+			if u, ok := user.(*map[string]interface{}); ok {
+				if len(UserMenu[qu.ObjToString((*u)["email"])]) < 1 {
+					UserMenu[qu.ObjToString((*u)["email"])] = GetUserMenu(qu.ObjToString((*u)["role"]))
+				}
+			}
+			c.Next()
+			return
+		}
+		c.JSON(http.StatusUnauthorized, gin.H{
+			"error": "Unauthorized",
+		})
+		c.Abort()
+		return
+	}
+}

+ 1 - 16
src/jy/router/router.go

@@ -2,11 +2,11 @@ package router
 
 import (
 	"encoding/gob"
-	"net/http"
 	"time"
 
 	"github.com/gin-contrib/sessions"
 	"github.com/gin-contrib/sessions/cookie"
+
 	//"github.com/gin-contrib/sessions/memstore"
 	"github.com/gin-gonic/gin"
 	"gopkg.in/mgo.v2/bson"
@@ -22,18 +22,3 @@ func init() {
 	gob.Register(&time.Time{})
 	gob.Register(bson.ObjectIdHex("518781f5904a2ea61544f852"))
 }
-
-func AuthMiddleWare() gin.HandlerFunc {
-	return func(c *gin.Context) {
-		session := sessions.Default(c)
-		if session.Get("user") != nil {
-			c.Next()
-			return
-		}
-		c.JSON(http.StatusUnauthorized, gin.H{
-			"error": "Unauthorized",
-		})
-		c.Abort()
-		return
-	}
-}