权限初步模板

This commit is contained in:
pixelqm
2019-09-08 16:37:05 +08:00
parent 1b1ceefa27
commit 9f17f71ee2
13 changed files with 707 additions and 101 deletions

View File

@@ -0,0 +1,43 @@
package dbModel
import (
"github.com/jinzhu/gorm"
"main/controller/servers"
"main/init/qmsql"
"main/model/modelInterface"
)
type Api struct {
gorm.Model `json:"-"`
AuthorityId uint `json:"-"`
Path string `json:"path"`
Description string `json:"description"`
}
func (a *Api) CreateApi() (err error) {
err = qmsql.DEFAULTDB.Create(a).Error
return err
}
func (a *Api) DeleteApi() (err error) {
err = qmsql.DEFAULTDB.Where("id = ?", a.AuthorityId).Delete(a).Error
return err
}
func (a *Api) EditApi() (err error) {
err = qmsql.DEFAULTDB.Update(a).Error
return err
}
// 分页获取数据 需要分页实现这个接口即可
func (a *Api) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
// 封装分页方法 调用即可 传入 当前的结构体和分页信息
err, db, total := servers.PagingServer(a, info)
if err != nil {
return
} else {
var apiList []Api
err = db.Find(&apiList).Error
return err, apiList, total
}
}

View File

@@ -2,17 +2,29 @@ package dbModel
import (
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"main/init/qmsql"
)
type Authority struct {
gorm.Model `json:"-"`
AuthorityId uint `json:"authorityId"`
AuthorityId uint `json:"authorityId" gorm:"not null;unique"`
AuthorityName string `json:"authorityName"`
Menus []Menu `json:"_"`
}
// 创建角色
func (a *Authority) CreateAuthority() (err error, authority *Authority) {
err = qmsql.DEFAULTDB.Create(a).Error
return err, a
}
// 删除角色
func (a *Authority) DeleteAuthority() (err error) {
err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).Find(&User{}).Error
if err != nil {
err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).Delete(a).Error
} else {
err = errors.New("此角色有用户正在使用禁止删除")
}
return err
}

View File

@@ -23,6 +23,7 @@ type Meta struct {
Icon string `json:"icon"`
}
//获取动态路由树
func (m *Menu) GetMenuTree(authorityId float64) (err error, menus []Menu) {
err = qmsql.DEFAULTDB.Where("authority_id = ? AND parent_id = ?", authorityId, 0).Find(&menus).Error
for i := 0; i < len(menus); i++ {