Browse Source

2024-03-15

zoie 1 year ago
parent
commit
5d95904f3a

+ 3 - 2
app/admin/controller/sys_dept.go

@@ -11,6 +11,7 @@ import (
 	"gogs.baozhida.cn/zoie/OAuth-core/api"
 	"gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
 	_ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
+	"gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
 )
 
 type SysDeptController struct {
@@ -238,8 +239,8 @@ func (e SysDeptController) Enter(c *gin.Context) {
 		return
 	}
 
-	err = e.Cache.Set(model.GetEnterDeptCacheKey(user.GetUserId(c)), req.GetId(), -1)
-	err = e.Cache.Set(model.GetEnterDeptNameCacheKey(user.GetUserId(c)), object.DeptName, -1)
+	err = e.Cache.Set(model.GetEnterDeptCacheKey(c), req.GetId(), int(config.JwtConfig.Timeout))
+	err = e.Cache.Set(model.GetEnterDeptNameCacheKey(c), object.DeptName, int(config.JwtConfig.Timeout))
 	if err != nil {
 		e.Logger.Error(err)
 		e.Error(500, err, "进入失败")

+ 3 - 11
app/admin/controller/sys_menu.go

@@ -4,13 +4,13 @@ import (
 	"Medical_OAuth/app/admin/model"
 	"Medical_OAuth/app/admin/service"
 	"Medical_OAuth/app/admin/service/dto"
+	"Medical_OAuth/common/actions"
 	"encoding/json"
 	"github.com/gin-gonic/gin"
 	"github.com/gin-gonic/gin/binding"
 	"gogs.baozhida.cn/zoie/OAuth-core/api"
 	"gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
 	_ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
-	"strconv"
 )
 
 type SysMenuController struct {
@@ -224,16 +224,8 @@ func (e SysMenuController) List(c *gin.Context) {
 		e.Error(500, err, err.Error())
 		return
 	}
-	var deptId int
-	deptId = user.GetDeptId(c)
-	if deptId == 0 {
-		deptIdStr, err := e.Cache.Get(model.GetEnterDeptCacheKey(user.GetUserId(c)))
-		if err == nil {
-			deptId, _ = strconv.Atoi(deptIdStr)
-		}
-	}
-
-	if deptId == 0 {
+	p := actions.GetPermissionFromContext(c)
+	if p.DeptId == 0 {
 		data := make([]map[string]interface{}, 0)
 		err = json.Unmarshal([]byte(model.NoDeptMenu), &data)
 		if err != nil {

+ 2 - 2
app/admin/controller/sys_service.go

@@ -253,10 +253,10 @@ func (e SysService) UserInfo(c *gin.Context) {
 		DeptName:  user.GetDeptName(c),
 	}
 
-	if deptIdStr, err := e.Cache.Get(model.GetEnterDeptCacheKey(user.GetUserId(c))); err == nil {
+	if deptIdStr, err := e.Cache.Get(model.GetEnterDeptCacheKey(c)); err == nil {
 		info.DeptId, _ = strconv.Atoi(deptIdStr)
 	}
-	if deptNameStr, err := e.Cache.Get(model.GetEnterDeptNameCacheKey(user.GetUserId(c))); err == nil {
+	if deptNameStr, err := e.Cache.Get(model.GetEnterDeptNameCacheKey(c)); err == nil {
 		info.DeptName = deptNameStr
 	}
 	e.Logger.Debug("info", info)

+ 2 - 7
app/admin/controller/sys_user.go

@@ -19,7 +19,6 @@ import (
 	"gogs.baozhida.cn/zoie/OAuth-core/pkg/sms"
 	"golang.org/x/crypto/bcrypt"
 	"net/http"
-	"strconv"
 )
 
 // SysUser 定义用户控制器
@@ -128,12 +127,8 @@ func (e SysUser) Insert(c *gin.Context) {
 		e.Error(500, err, err.Error())
 		return
 	}
-	deptId := user.GetDeptId(c)
-
-	if deptIdStr, err := e.Cache.Get(model.GetEnterDeptCacheKey(user.GetUserId(c))); err == nil {
-		deptId, _ = strconv.Atoi(deptIdStr)
-	}
-	req.DeptId = deptId
+	p := actions.GetPermissionFromContext(c)
+	req.DeptId = p.DeptId
 	req.RoleId = 1
 	req.Status = "2"
 

+ 20 - 4
app/admin/model/sys_dept.go

@@ -4,7 +4,10 @@ import (
 	model2 "Medical_OAuth/common/model"
 	"errors"
 	"fmt"
+	"github.com/gin-gonic/gin"
+	"gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
 	"gorm.io/gorm"
+	"strings"
 )
 
 type SysDept struct {
@@ -39,9 +42,22 @@ func (e *SysDept) BeforeDelete(_ *gorm.DB) (err error) {
 	return
 }
 
-func GetEnterDeptCacheKey(userId int) string {
-	return fmt.Sprintf("enter-dept-%d", userId)
+func GetEnterDeptCacheKey(c *gin.Context) string {
+	userId := user.GetUserId(c)
+	tokenId := ""
+	list := strings.Split(c.Request.Header.Get("Authorization"), ".")
+	if len(list) > 0 {
+		tokenId = list[len(list)-1]
+	}
+
+	return fmt.Sprintf("enter-dept-%s-%d", tokenId, userId)
 }
-func GetEnterDeptNameCacheKey(userId int) string {
-	return fmt.Sprintf("enter-dept-name-%d", userId)
+func GetEnterDeptNameCacheKey(c *gin.Context) string {
+	userId := user.GetUserId(c)
+	tokenId := ""
+	list := strings.Split(c.Request.Header.Get("Authorization"), ".")
+	if len(list) > 0 {
+		tokenId = list[len(list)-1]
+	}
+	return fmt.Sprintf("enter-dept-name-%s-%d", tokenId, userId)
 }

+ 23 - 0
app/admin/model/sys_menu.go

@@ -220,6 +220,29 @@ var (
         }
       }
     ]
+  },
+  {
+    "path": "/medicine",
+    "name": "medicine",
+    "icon": "icon-banbenxinxi",
+    "component": "@/views/medicine/index.vue",
+    "redirect":  "/medicine/medicineManage",
+    "meta": {
+      "roles": false,
+      "title": "药品管理"
+    },
+    "children": [
+      {
+        "path": "/medicine/medicineManage",
+        "name": "medicineManage",
+        "icon": "icon-banbenxinxi",
+        "component": "@/views/medicine/medicineManage/index.vue",
+        "meta": {
+          "roles": false,
+          "title": "药品管理"
+        }
+      }
+    ]
   }
 ]
 `

+ 8 - 0
app/admin/service/dto/sys_dept.go

@@ -77,6 +77,14 @@ func (s *SysDeptGetReq) GetId() interface{} {
 	return s.Id
 }
 
+type SysDeptGetRootIDReq struct {
+	Id int `form:"id"`
+}
+
+func (s *SysDeptGetRootIDReq) GetId() interface{} {
+	return s.Id
+}
+
 type SysDeptDeleteReq struct {
 	Id int `json:"id"`
 }

+ 1 - 1
common/actions/permission.go

@@ -116,7 +116,7 @@ func getPermissionFromContext(c *gin.Context) *DataPermission {
 			p = pm.(*DataPermission)
 		}
 	}
-	if deptIdStr, err := sdk.Runtime.GetCacheAdapter().Get(model.GetEnterDeptCacheKey(user.GetUserId(c))); err == nil {
+	if deptIdStr, err := sdk.Runtime.GetCacheAdapter().Get(model.GetEnterDeptCacheKey(c)); err == nil {
 		p.DeptId, _ = strconv.Atoi(deptIdStr)
 	}
 	return p

+ 1 - 0
common/middleware/auth.go

@@ -34,6 +34,7 @@ func AuthInit() (*jwt.GinJWTMiddleware, error) {
 		TimeFunc:        time.Now,
 		SaveNewestToken: handler.SaveNewestToken,
 		GetNewestToken:  handler.GetNewestToken,
+		SetEnterDeptId:  handler.SetEnterDeptId,
 	})
 
 }

+ 30 - 2
common/middleware/handler/auth.go

@@ -7,16 +7,19 @@ import (
 	"errors"
 	"fmt"
 	"github.com/gin-gonic/gin"
+	"github.com/go-redis/redis/v7"
 	"github.com/mssola/user_agent"
 	"gogs.baozhida.cn/zoie/OAuth-core/api"
 	"gogs.baozhida.cn/zoie/OAuth-core/pkg"
 	jwt "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth"
 	"gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
 	"gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
+	"gogs.baozhida.cn/zoie/OAuth-core/pkg/utils"
 	"gogs.baozhida.cn/zoie/OAuth-core/sdk"
 	"gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
 	"gorm.io/gorm"
 	"net/http"
+	"strings"
 )
 
 func PayloadFunc(data interface{}) jwt.MapClaims {
@@ -36,6 +39,7 @@ func PayloadFunc(data interface{}) jwt.MapClaims {
 			jwt.SingleKey:    single,
 			jwt.DeptIdKey:    u.DeptId,
 			jwt.DeptNameKey:  d.DeptName,
+			jwt.RandomKey:    utils.GetRandString(8, "", 0),
 		}
 	}
 	return jwt.MapClaims{}
@@ -55,6 +59,7 @@ func IdentityHandler(c *gin.Context) interface{} {
 		"single":      claims["single"],
 		"DeptId":      claims["deptId"],
 		"DeptName":    claims["deptName"],
+		"RandomKey":   claims["randomKey"],
 	}
 }
 
@@ -115,8 +120,6 @@ func Authenticator(c *gin.Context) (interface{}, error) {
 	if err != nil {
 		return nil, err
 	}
-	err = sdk.Runtime.GetCacheAdapter().Del(model.GetEnterDeptCacheKey(u.Id))
-	err = sdk.Runtime.GetCacheAdapter().Del(model.GetEnterDeptNameCacheKey(u.Id))
 
 	return map[string]interface{}{"user": u, "role": role, "dept": dept, "single": single}, nil
 }
@@ -193,6 +196,7 @@ func Authorizator(data interface{}, c *gin.Context) bool {
 		c.Set("dataScope", r.DataScope)
 		c.Set("deptId", u.DeptId)
 		c.Set("deptName", d.DeptName)
+		c.Set("randomKey", utils.GetRandString(8, "", 0))
 		return true
 	}
 	return false
@@ -243,3 +247,27 @@ func GetSingleLogin(c *gin.Context) (bool, error) {
 	return false, nil
 
 }
+
+func SetEnterDeptId(c *gin.Context, newToken string, userId int64) error {
+	oldToken := ""
+	list := strings.Split(c.Request.Header.Get("Authorization"), ".")
+	if len(list) > 0 {
+		oldToken = list[len(list)-1]
+	}
+	list2 := strings.Split(newToken, ".")
+	newToken2 := list2[len(list2)-1]
+	deptIdStr, err := sdk.Runtime.GetCacheAdapter().Get(fmt.Sprintf("enter-dept-%s-%d", oldToken, userId))
+	if err == nil {
+		sdk.Runtime.GetCacheAdapter().Set(fmt.Sprintf("enter-dept-%s-%d", newToken2, userId), deptIdStr, int(config.JwtConfig.Timeout))
+		sdk.Runtime.GetCacheAdapter().Del(fmt.Sprintf("enter-dept-%s-%d", oldToken, userId))
+	}
+	deptName, err := sdk.Runtime.GetCacheAdapter().Get(fmt.Sprintf("enter-dept-name-%s-%d", oldToken, userId))
+	if err == nil {
+		sdk.Runtime.GetCacheAdapter().Set(fmt.Sprintf("enter-dept-name-%s-%d", newToken2, userId), deptName, int(config.JwtConfig.Timeout))
+		sdk.Runtime.GetCacheAdapter().Del(fmt.Sprintf("enter-dept-name-%s-%d", oldToken, userId))
+	}
+	if err == redis.Nil {
+		return nil
+	}
+	return err
+}

+ 4 - 3
conf/settings.yml

@@ -31,12 +31,13 @@ settings:
     driver: mysql
     # 数据库连接字符串 mysql 缺省信息 charset=utf8&parseTime=True&loc=Local&timeout=1000ms
 #    source: godown:Fp8stPSZ!W3@#G@tcp(192.168.0.88:3306)/godown?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
-    source: godown:wCHDXHL35kZGSD8rG@tcp(127.0.0.1:40306)/godown?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
+    # source: godown:wCHDXHL35kZGSD8rG@tcp(127.0.0.1:40306)/godown?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
+    source: medical_erp:XMzTDthD4By3ArjG@tcp(192.168.11.77:3306)/medical_erp?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
     host: localhost:6400
   cache:
     redis:
-#      addr: 192.168.0.33:6379
-      addr: 127.0.0.1:43379
+      addr: 192.168.11.77:6379
+      # addr: 127.0.0.1:43379
       password:
       db: 3
     # key存在即可

+ 1 - 1
setting.yml

@@ -30,7 +30,7 @@ settings:
     # 数据库类型 mysql
     driver: mysql
     # 数据库连接字符串 mysql 缺省信息 charset=utf8&parseTime=True&loc=Local&timeout=1000ms
-    source: medical_erp:XMzTDthD4By3ArjG_@tcp(192.168.11.77:3306)/medical_erp?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
+    source: medical_erp:XMzTDthD4By3ArjG@tcp(192.168.11.77:3306)/medical_erp?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
   #    host: localhost:8080
   #    host: 192.168.31.163:8080
   cache: