sys_role_menu.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package model
  2. import (
  3. model2 "cold-delivery/common/model"
  4. "encoding/json"
  5. "errors"
  6. "gogs.baozhida.cn/zoie/OAuth-core/sdk"
  7. "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
  8. )
  9. type SysRoleMenu struct {
  10. model2.Model
  11. RoleKey string `json:"roleKey" gorm:"size:128;comment:角色编码"` // 角色编码
  12. MenuId int `json:"menuId" gorm:"size:128;comment:菜单id"` // 菜单id
  13. model2.ControlBy
  14. model2.ModelTime
  15. }
  16. func (SysRoleMenu) TableName() string {
  17. return "sys_role_menu"
  18. }
  19. func (e *SysRoleMenu) Generate() model2.ActiveRecord {
  20. o := *e
  21. return &o
  22. }
  23. func (e *SysRoleMenu) GetId() interface{} {
  24. return e.Id
  25. }
  26. func GetRoleMenuCacheKey(roleKey string) string {
  27. return "role_menu-" + roleKey
  28. }
  29. // 添加、修改、删除菜单时删除角色菜单缓存
  30. func DeleteAllRoleMenuCache() error {
  31. //准备db
  32. db := sdk.Runtime.GetDbByKey(config.ApplicationConfig.Host)
  33. if db == nil {
  34. return errors.New("db not exist")
  35. }
  36. var list []SysRole
  37. err := db.Model(SysRole{}).Find(&list).Error
  38. if err != nil {
  39. return err
  40. }
  41. for _, role := range list {
  42. _ = sdk.Runtime.GetCacheAdapter().Del(GetRoleMenuCacheKey(role.RoleKey) + "-0")
  43. _ = sdk.Runtime.GetCacheAdapter().Del(GetRoleMenuCacheKey(role.RoleKey) + "-1")
  44. }
  45. return nil
  46. }
  47. func DeleteRoleMenuCache(roleKey string) error {
  48. err := sdk.Runtime.GetCacheAdapter().Del(GetRoleMenuCacheKey(roleKey) + "-0")
  49. err = sdk.Runtime.GetCacheAdapter().Del(GetRoleMenuCacheKey(roleKey) + "-1")
  50. return err
  51. }
  52. func SetRoleMenuCache(roleKey string, menuList []SysMenu) error {
  53. s, err := json.Marshal(menuList)
  54. err = sdk.Runtime.GetCacheAdapter().Set(GetRoleMenuCacheKey(roleKey), s, 24*60*60)
  55. return err
  56. }
  57. func GetRoleMenuCache(roleKey string, menuList *[]SysMenu) error {
  58. s, err := sdk.Runtime.GetCacheAdapter().Get(GetRoleMenuCacheKey(roleKey))
  59. if err != nil {
  60. return err
  61. }
  62. err = json.Unmarshal([]byte(s), &menuList)
  63. return err
  64. }