增加获取路由树功能

This commit is contained in:
pixelqm
2019-09-08 11:13:43 +08:00
parent 5fee629f66
commit 1b1ceefa27
19 changed files with 636 additions and 150 deletions

View File

@@ -0,0 +1,18 @@
package dbModel
import (
"github.com/jinzhu/gorm"
"main/init/qmsql"
)
type Authority struct {
gorm.Model `json:"-"`
AuthorityId uint `json:"authorityId"`
AuthorityName string `json:"authorityName"`
Menus []Menu `json:"_"`
}
func (a *Authority) CreateAuthority() (err error, authority *Authority) {
err = qmsql.DEFAULTDB.Create(a).Error
return err, a
}

View File

@@ -0,0 +1,40 @@
package dbModel
import (
"github.com/jinzhu/gorm"
"main/init/qmsql"
)
type Menu struct {
gorm.Model `json:"-"`
MenuLevel uint `json:"-"`
AuthorityId uint `json:"-"`
ParentId uint `json:"parentId"`
Path string `json:"path"`
Name string `json:"name"`
Hidden bool `json:"hidden"`
Component string `json:"component"`
Meta `json:"meta"`
Children []Menu `json:"children"`
}
type Meta struct {
Title string `json:"title"`
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++ {
err = getChildrenList(&menus[i])
}
return err, menus
}
func getChildrenList(menu *Menu) (err error) {
err = qmsql.DEFAULTDB.Where("authority_id = ? AND parent_id = ?", menu.AuthorityId, menu.ID).Find(&menu.Children).Error
for i := 0; i < len(menu.Children); i++ {
err = getChildrenList(&menu.Children[i])
}
return err
}

View File

@@ -4,17 +4,21 @@ import (
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"main/controller/servers"
"main/init/qmsql"
"main/model/modelInterface"
"main/tools"
)
type User struct {
gorm.Model `json:"-"`
UUID uuid.UUID `json:"uuid"`
UserName string `json:"userName"`
PassWord string `json:"passWord"`
NickName string `json:"nickName" gorm:"default:'QMPlusUser'"`
HeaderImg string `json:"headerImg" gorm:"default:'http://www.henrongyi.top/avatar/lufu.jpg'"`
gorm.Model `json:"-"`
UUID uuid.UUID `json:"uuid"`
UserName string `json:"userName"`
PassWord string `json:"passWord"`
NickName string `json:"nickName" gorm:"default:'QMPlusUser'"`
HeaderImg string `json:"headerImg" gorm:"default:'http://www.henrongyi.top/avatar/lufu.jpg'"`
Authority Authority `json:"authority" form:"ForeignKey:AuthorityId;AssociationForeignKey:AuthorityId"`
AuthorityId float64 `json:"authorityId" gorm:"default:888"`
//Propertie // 多余属性自行添加
//PropertieId uint // 自动关联 Propertie 的Id 附加属性过多 建议创建一对一关系
}
@@ -60,6 +64,7 @@ func (u *User) Login() (err error, userInter *User) {
var user User
u.PassWord = tools.MD5V(u.PassWord)
err = qmsql.DEFAULTDB.Where("user_name = ? AND pass_word = ?", u.UserName, u.PassWord).First(&user).Error
err = qmsql.DEFAULTDB.Model(&user).Related(&user.Authority).Error
return err, &user
}
@@ -69,3 +74,16 @@ func (u *User) UploadHeaderImg(userName string, filePath string) (err error, use
err = qmsql.DEFAULTDB.Where("user_name = ?", userName).First(&user).Update("header_img", filePath).First(&user).Error
return err, &user
}
// 分页获取数据 需要分页实现这个接口即可
func (u *User) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
// 封装分页方法 调用即可 传入 当前的结构体和分页信息
err, db, total := servers.PagingServer(u, info)
if err != nil {
return
} else {
var userList []User
err = db.Find(&userList).Error
return err, userList, total
}
}

View File

@@ -2,9 +2,13 @@ package modelInterface
// 因为我也不确定项目要不要多人维护 所以定义了CURD接口 作为接口参考
// 由于很多接口使用Restful模式 暂时不用泛型 有需要可以iss提供示例
type CURD interface {
Create() (error, CURD)
Updata() (error, CURD)
Read() (error, CURD)
Delete() (error, CURD)
type PageInfo struct {
Page int
PageSize int
}
//分页接口
type Paging interface {
GetInfoList(PageInfo) (err error, list interface{}, total int)
}