From 09f311dc5f41e944983e36dfce684fcf6eae5468 Mon Sep 17 00:00:00 2001 From: yxh Date: Sat, 22 Feb 2020 16:25:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=87=E7=AD=BE=E7=BC=93=E5=AD=98=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E6=8C=89=E6=A0=87=E7=AD=BE=E6=B8=85=E7=A9=BA?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/admin/auth.go | 38 +++-- app/service/admin/auth_service/auth_rule.go | 2 +- app/service/admin/user_service/user.go | 17 +- app/service/cache_service/cache.go | 169 ++++++++++++++++++++ test/demo2_test.go | 12 ++ 5 files changed, 214 insertions(+), 24 deletions(-) create mode 100644 app/service/cache_service/cache.go diff --git a/app/controller/admin/auth.go b/app/controller/admin/auth.go index 9c72ff6..629bd70 100644 --- a/app/controller/admin/auth.go +++ b/app/controller/admin/auth.go @@ -241,25 +241,16 @@ func (c *Auth) EditRole(r *ghttp.Request) { response.FailJson(true, r, "获取权限处理器失败") } gp := enforcer.GetFilteredNamedPolicy("p", 0, fmt.Sprintf("g_%d", id)) - g.Log().Debug(gp) - gpMap := map[int64]int64{} - for _, v := range gp { - gpMap[gconv.Int64(gstr.SubStr(v[1], 2))] = gconv.Int64(gstr.SubStr(v[1], 2)) - } - //关联选中的权限 - for k, v := range mList { - if _, has := gpMap[gconv.Int64(v["id"])]; has { - v["isChecked"] = true - } else { - v["isChecked"] = false - } - mList[k] = v + gpSlice := make([]int, len(gp)) + for k, v := range gp { + gpSlice[k] = gconv.Int(gstr.SubStr(v[1], 2)) } mList = utils.PushSonToParent(mList) res := g.Map{ - "parentList": pList, - "menuList": mList, - "role": role, + "parentList": pList, + "menuList": mList, + "role": role, + "checkedRules": gpSlice, } response.SusJson(true, r, "成功", res) } @@ -325,7 +316,7 @@ func (c *Auth) AddUser(r *ghttp.Request) { //修改管理员 func (c *Auth) EditUser(r *ghttp.Request) { - id := r.GetRequestInt64("id") + id := r.GetRequestInt("id") if r.Method == "POST" { requestData := r.GetFormMap() err := auth_service.EditUser(requestData) @@ -353,9 +344,16 @@ func (c *Auth) EditUser(r *ghttp.Request) { response.FailJson(true, r, "获取角色数据失败") } roleList = utils.ParentSonSort(roleList, 0, 0, "parent_id", "id", "flg", "name") + //获取已选择的角色信息 + checkedRoleIds, err := user_service.GetAdminRoleIds(id) + if err != nil { + g.Log().Error(err) + response.FailJson(true, r, "获取用户角色数据失败") + } res := g.Map{ - "roleList": roleList, - "userInfo": userEntity, + "roleList": roleList, + "userInfo": userEntity, + "checkedRoleIds": checkedRoleIds, } response.SusJson(true, r, "成功", res) } @@ -379,7 +377,7 @@ func (c *Auth) UserList(r *ghttp.Request) { users := make([]g.Map, len(userList)) for k, u := range userList { users[k] = gconv.Map(u) - roles, err := user_service.GetAdminRole(gconv.Int(u.Id)) + roles, err := user_service.GetAdminRole(u.Id) if err != nil { g.Log().Error(err) response.FailJson(true, r, "获取用户角色数据失败") diff --git a/app/service/admin/auth_service/auth_rule.go b/app/service/admin/auth_service/auth_rule.go index 18e3330..924f8f1 100644 --- a/app/service/admin/auth_service/auth_rule.go +++ b/app/service/admin/auth_service/auth_rule.go @@ -300,7 +300,7 @@ func AddUserRole(roleIds interface{}, userId int64) (err error) { } //修改用户角色信息 -func EditUserRole(roleIds interface{}, userId int64) (err error) { +func EditUserRole(roleIds interface{}, userId int) (err error) { enforcer, e := casbin_adapter_service.GetEnforcer() if e != nil { err = e diff --git a/app/service/admin/user_service/user.go b/app/service/admin/user_service/user.go index 644d959..026b1a2 100644 --- a/app/service/admin/user_service/user.go +++ b/app/service/admin/user_service/user.go @@ -48,6 +48,19 @@ func GetAdminList(where g.Map, page int) (total int, userList []*user.Entity, er //获取管理员的角色信息 func GetAdminRole(userId int) (roles []*role.Entity, err error) { + roleIds, err := GetAdminRoleIds(userId) + if err != nil { + return + } + if roleIds != nil { + //获取角色信息 + roles, err = role.Model.Where("id in(?)", roleIds).All() + } + return +} + +//获取管理员对应的角色ids +func GetAdminRoleIds(userId int) (roleIds []int, err error) { enforcer, e := casbin_adapter_service.GetEnforcer() if e != nil { err = e @@ -56,13 +69,11 @@ func GetAdminRole(userId int) (roles []*role.Entity, err error) { //查询关联角色规则 groupPolicy := enforcer.GetFilteredGroupingPolicy(0, fmt.Sprintf("u_%d", userId)) if len(groupPolicy) > 0 { - roleIds := make([]int, len(groupPolicy)) + roleIds = make([]int, len(groupPolicy)) //得到角色id的切片 for k, v := range groupPolicy { roleIds[k] = gconv.Int(gstr.SubStr(v[1], 2)) } - //获取角色信息 - roles, err = role.Model.Where("id in(?)", roleIds).All() } return } diff --git a/app/service/cache_service/cache.go b/app/service/cache_service/cache.go new file mode 100644 index 0000000..0d48462 --- /dev/null +++ b/app/service/cache_service/cache.go @@ -0,0 +1,169 @@ +package cache_service + +import ( + "fmt" + "github.com/gogf/gf/crypto/gmd5" + "github.com/gogf/gf/os/gcache" + "github.com/gogf/gf/util/gconv" + "reflect" + "time" +) + +type CacheTagService struct { + tagKey interface{} +} + +func New() *CacheTagService { + return &CacheTagService{} +} + +//设置tag缓存的keys +func (c *CacheTagService) cacheTagKey(key interface{}, tag interface{}) { + c.setTagKey(tag) + if c.tagKey != nil { + tagValue := []interface{}{key} + value := gcache.Get(c.tagKey) + if value != nil { + keyValue := gconv.SliceAny(value) + hasKey := false + for _, v := range keyValue { + if reflect.DeepEqual(key, v) { + hasKey = true + break + } + } + if !hasKey { + tagValue = append(tagValue, gconv.SliceAny(value)...) + } + } + gcache.Set(c.tagKey, tagValue, 0) + } +} + +//获取带标签的键名 +func (c *CacheTagService) setTagKey(tag interface{}) { + if tag != nil { + c.tagKey = interface{}(fmt.Sprintf("tag_%s", gmd5.MustEncryptString(gconv.String(tag)))) + } +} + +// Set sets cache with - pair, which is expired after . +// It does not expire if <= 0. +func (c *CacheTagService) Set(key interface{}, value interface{}, duration time.Duration, tag interface{}) { + c.cacheTagKey(key, tag) + gcache.Set(key, value, duration) +} + +// SetIfNotExist sets cache with - pair if does not exist in the cache, +// which is expired after . It does not expire if <= 0. +func (c *CacheTagService) SetIfNotExist(key interface{}, value interface{}, duration time.Duration, tag interface{}) bool { + c.cacheTagKey(key, tag) + return gcache.SetIfNotExist(key, value, duration) +} + +// Sets batch sets cache with tagKey-value pairs by , which is expired after . +// +// It does not expire if <= 0. +func (c *CacheTagService) Sets(data map[interface{}]interface{}, duration time.Duration, tag interface{}) { + if tag != nil { + for k, _ := range data { + c.cacheTagKey(k, tag) + } + gcache.Sets(data, duration) + } else { + gcache.Sets(data, duration) + } +} + +// Get returns the value of . +// It returns nil if it does not exist or its value is nil. +func (c *CacheTagService) Get(key interface{}) interface{} { + return gcache.Get(key) +} + +// GetOrSet returns the value of , +// or sets - pair and returns if does not exist in the cache. +// The tagKey-value pair expires after . +// +// It does not expire if <= 0. +func (c *CacheTagService) GetOrSet(key interface{}, value interface{}, duration time.Duration, tag interface{}) interface{} { + c.cacheTagKey(key, tag) + return gcache.GetOrSet(key, value, duration) +} + +// GetOrSetFunc returns the value of , or sets with result of function +// and returns its result if does not exist in the cache. The tagKey-value pair expires +// after . It does not expire if <= 0. +func (c *CacheTagService) GetOrSetFunc(key interface{}, f func() interface{}, duration time.Duration, tag interface{}) interface{} { + c.cacheTagKey(key, tag) + return gcache.GetOrSetFunc(key, f, duration) +} + +// GetOrSetFuncLock returns the value of , or sets with result of function +// and returns its result if does not exist in the cache. The tagKey-value pair expires +// after . It does not expire if <= 0. +// +// Note that the function is executed within writing mutex lock. +func (c *CacheTagService) GetOrSetFuncLock(key interface{}, f func() interface{}, duration time.Duration, tag interface{}) interface{} { + c.cacheTagKey(key, tag) + return gcache.GetOrSetFuncLock(key, f, duration) +} + +// Contains returns true if exists in the cache, or else returns false. +func (c *CacheTagService) Contains(key interface{}) bool { + return gcache.Contains(key) +} + +// Remove deletes the in the cache, and returns its value. +func (c *CacheTagService) Remove(key interface{}) interface{} { + return gcache.Remove(key) +} + +// Removes deletes in the cache. +func (c *CacheTagService) Removes(keys []interface{}) { + gcache.Removes(keys) +} + +// Remove deletes the in the cache, and returns its value. +func (c *CacheTagService) RemoveByTag(tag interface{}) { + c.setTagKey(tag) + //删除tagKey 对应的 key和值 + keys := c.Get(c.tagKey) + if keys != nil { + ks := gconv.SliceAny(keys) + c.Removes(ks) + } + c.Remove(c.tagKey) +} + +// Removes deletes in the cache. +func (c *CacheTagService) RemoveByTags(tag []interface{}) { + for _, v := range tag { + c.RemoveByTag(v) + } +} + +// Data returns a copy of all tagKey-value pairs in the cache as map type. +func (c *CacheTagService) Data() map[interface{}]interface{} { + return gcache.Data() +} + +// Keys returns all keys in the cache as slice. +func (c *CacheTagService) Keys() []interface{} { + return gcache.Keys() +} + +// KeyStrings returns all keys in the cache as string slice. +func (c *CacheTagService) KeyStrings() []string { + return gcache.KeyStrings() +} + +// Values returns all values in the cache as slice. +func (c *CacheTagService) Values() []interface{} { + return gcache.Values() +} + +// Size returns the size of the cache. +func (c *CacheTagService) Size() int { + return gcache.Size() +} diff --git a/test/demo2_test.go b/test/demo2_test.go index d2ad02d..275a5a1 100644 --- a/test/demo2_test.go +++ b/test/demo2_test.go @@ -1,6 +1,8 @@ package test import ( + "fmt" + "gfast/app/service/cache_service" "testing" ) @@ -9,5 +11,15 @@ func TestDemo2(t *testing.T) { } func test21(t *testing.T) { + cache_service.New().Set("wang", "老王", 0, "person") + cache_service.New().Set("zs", "张三", 0, "person") + cache_service.New().Set("ls", "李四", 0, "person") + cache_service.New().Set("dog", "狗狗", 0, "animal") + cache_service.New().Set("cat", "猫猫", 0, "animal") + fmt.Println(cache_service.New().Keys()) + cache_service.New().RemoveByTag("animal") //删除动物标签 + fmt.Println(cache_service.New().Keys()) + + fmt.Println(cache_service.New().Get("dog")) }