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.

36 lines
958 B

package dbModel
import (
"github.com/jinzhu/gorm"
"main/init/qmsql"
)
type ApiAuthority struct {
gorm.Model
AuthorityId string
Authority Authority `gorm:"ForeignKey:AuthorityId;AssociationForeignKey:AuthorityId"` //其实没有关联的必要
ApiId uint
Api Api
}
//创建角色api关联关系
func (a *ApiAuthority) SetAuthAndApi(authId string, apisid []uint) (err error) {
err = qmsql.DEFAULTDB.Where("authority_id = ?", authId).Unscoped().Delete(&ApiAuthority{}).Error
for _, v := range apisid {
err = qmsql.DEFAULTDB.Create(&ApiAuthority{AuthorityId: authId, ApiId: v}).Error
if err != nil {
return err
}
}
return nil
}
// 获取角色api关联关系
func (a *ApiAuthority) GetAuthAndApi(authId string) (err error,apiIds []uint) {
var apis []ApiAuthority
err = qmsql.DEFAULTDB.Where("authority_id = ?", authId).Find(&apis).Error
for _, v := range apis {
apiIds = append(apiIds,v.ApiId)
}
return nil,apiIds
}