You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
958 B

  1. package dbModel
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "main/init/qmsql"
  5. )
  6. type ApiAuthority struct {
  7. gorm.Model
  8. AuthorityId string
  9. Authority Authority `gorm:"ForeignKey:AuthorityId;AssociationForeignKey:AuthorityId"` //其实没有关联的必要
  10. ApiId uint
  11. Api Api
  12. }
  13. //创建角色api关联关系
  14. func (a *ApiAuthority) SetAuthAndApi(authId string, apisid []uint) (err error) {
  15. err = qmsql.DEFAULTDB.Where("authority_id = ?", authId).Unscoped().Delete(&ApiAuthority{}).Error
  16. for _, v := range apisid {
  17. err = qmsql.DEFAULTDB.Create(&ApiAuthority{AuthorityId: authId, ApiId: v}).Error
  18. if err != nil {
  19. return err
  20. }
  21. }
  22. return nil
  23. }
  24. // 获取角色api关联关系
  25. func (a *ApiAuthority) GetAuthAndApi(authId string) (err error,apiIds []uint) {
  26. var apis []ApiAuthority
  27. err = qmsql.DEFAULTDB.Where("authority_id = ?", authId).Find(&apis).Error
  28. for _, v := range apis {
  29. apiIds = append(apiIds,v.ApiId)
  30. }
  31. return nil,apiIds
  32. }