sys_role_menu.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package model
  2. import (
  3. model2 "Medical_OAuth/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))
  43. }
  44. return nil
  45. }
  46. func DeleteRoleMenuCache(roleKey string) error {
  47. return sdk.Runtime.GetCacheAdapter().Del(GetRoleMenuCacheKey(roleKey))
  48. }
  49. func SetRoleMenuCache(roleKey string, menuList []SysMenu) error {
  50. s, err := json.Marshal(menuList)
  51. err = sdk.Runtime.GetCacheAdapter().Set(GetRoleMenuCacheKey(roleKey), s, 24*60*60)
  52. return err
  53. }
  54. func GetRoleMenuCache(roleKey string, menuList *[]SysMenu) error {
  55. s, err := sdk.Runtime.GetCacheAdapter().Get(GetRoleMenuCacheKey(roleKey))
  56. if err != nil {
  57. return err
  58. }
  59. err = json.Unmarshal([]byte(s), &menuList)
  60. return err
  61. }