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.
179 lines
5.5 KiB
179 lines
5.5 KiB
package api
|
|
|
|
import (
|
|
"epur-pay/cache"
|
|
"epur-pay/model"
|
|
"epur-pay/pkg/dapi"
|
|
"epur-pay/pkg/utils"
|
|
)
|
|
|
|
type MenuResponse struct {
|
|
*dapi.ResponseCommon
|
|
Data []model.Menu `json:"data"` //数据列表
|
|
}
|
|
|
|
// @Summary 角色菜单列表
|
|
func GetMenuList(a *dapi.ApiBase) error {
|
|
// 查询菜单
|
|
menu := []model.Menu{}
|
|
utils.Error(a.Ts.Raw(`
|
|
select DISTINCT m.id,m.pid,m.title,m.icon,m.path,m.component,m.target,m.permission,m.type,m.status,m.hide,m.note,m.sort
|
|
from menu as m left join role_menu as rm on rm.menu_id = m.id
|
|
left join user_role as ur on ur.role_id = rm.role_id
|
|
where ur.uid = ? and m.type = 0 and m.status = 1 and m.is_delete = 0
|
|
order by m.pid asc, m.sort asc`, a.User.Uid).Scan(&menu).Error)
|
|
|
|
Response := MenuResponse{}
|
|
Response.Data = GetMenu(a.User.Detail.Token, menu, 0, nil)
|
|
Response.ResponseCommon = a.NewSuccessResponseCommon()
|
|
return a.ReturnSuccessCustomResponse(Response)
|
|
}
|
|
|
|
// 重装结构方式
|
|
func GetMenu(token string, menuList []model.Menu, pid int64, message map[string]int64) []model.Menu {
|
|
treeList := []model.Menu{}
|
|
for _, v := range menuList {
|
|
if v.Pid == pid {
|
|
child := GetMenu(token, menuList, v.Id, message)
|
|
v.Children = child
|
|
// 消息数量
|
|
if message != nil {
|
|
v.Meta.Badge = message[v.Permission]
|
|
}
|
|
treeList = append(treeList, v)
|
|
}
|
|
}
|
|
return treeList
|
|
}
|
|
|
|
type MenuPermissionList struct {
|
|
Permission string `json:"permission"`
|
|
}
|
|
|
|
type UserPermissionListResponse struct {
|
|
*dapi.ResponseCommon
|
|
Data struct {
|
|
Id int64 `json:"id"` // 用户ID
|
|
Nickname string `json:"nickname"` // 用户昵称
|
|
RealName string `json:"realName"` // 别名
|
|
Avatar string `json:"avatar"` // 头像
|
|
PermissionList []string `json:"permissionList"`
|
|
} `json:"data"` //数据列表
|
|
}
|
|
|
|
// @Summary 权限标识列表
|
|
func GetPermissionList(a *dapi.ApiBase) error {
|
|
Response := UserPermissionListResponse{}
|
|
|
|
permission := []MenuPermissionList{}
|
|
utils.Error(a.Ts.Raw(`select m.permission from menu as m left join role_menu as rm on rm.menu_id = m.id
|
|
left join user_role as ur on ur.role_id = rm.role_id
|
|
where ur.uid = ? and m.type = 1 and m.status = 1 and m.is_delete = 0`,
|
|
a.User.Uid).Scan(&permission).Error)
|
|
|
|
var permissionList []string
|
|
for _, v := range permission {
|
|
permissionList = append(permissionList, v.Permission)
|
|
}
|
|
|
|
Response.Data.Id = a.User.Uid
|
|
Response.Data.Nickname = a.User.Detail.Nickname
|
|
Response.Data.RealName = a.User.Detail.Nickname
|
|
//Response.Avatar = a.User.Detail.Avatar
|
|
Response.Data.PermissionList = permissionList
|
|
|
|
Response.ResponseCommon = a.NewSuccessResponseCommon()
|
|
return a.ReturnSuccessCustomResponse(Response)
|
|
}
|
|
|
|
type GetPermissionMenuParams struct {
|
|
MenuId int64 `json:"menuId"` //菜单集合
|
|
}
|
|
|
|
type MenuAllResponse struct {
|
|
*dapi.ResponseCommon
|
|
Data []model.Menu `json:"data"` //数据列表
|
|
}
|
|
|
|
// @Summary 获取所有菜单
|
|
func GetAllMenuList(a *dapi.ApiBase) error {
|
|
Response := MenuResponse{}
|
|
|
|
utils.Error(a.Ts.Raw(
|
|
`select m.* from menu as m where m.is_delete = 0 order by m.pid asc, m.sort asc`).Scan(&Response.Data).Error)
|
|
|
|
Response.ResponseCommon = a.NewSuccessResponseCommon()
|
|
return a.ReturnSuccessCustomResponse(Response)
|
|
}
|
|
|
|
type EditMenuResponse struct {
|
|
Id int64 `json:"id,omitempty"` //菜单ID
|
|
Pid int64 `json:"pid"` //菜单父级ID
|
|
Title string `json:"title"` //菜单名称
|
|
Icon string `json:"icon"` //菜单图标
|
|
Path string `json:"path"` //菜单路径
|
|
Component string `json:"component"` //菜单组件
|
|
Target int64 `json:"target"` //打开方式 0-组件 1-内链 2-外链
|
|
Permission string `json:"permission"` //权限标识
|
|
Type int `json:"type"` //类型 0-菜单 1-节点
|
|
Status int `json:"status"` //状态 1-正常 2-禁用
|
|
Hide int `json:"hide"` //类型 0-显示 1-隐藏
|
|
Note string `json:"note"` //备注
|
|
Sort int `json:"sort"` //排序
|
|
}
|
|
|
|
// @Summary 编辑菜单
|
|
func EditMenuInfo(a *dapi.ApiBase, data *EditMenuResponse) error {
|
|
if data.Id > 0 {
|
|
utils.Error(a.Ts.Model(&model.Menu{}).Where("id", data.Id).Updates(map[string]interface{}{
|
|
"pid": data.Pid,
|
|
"status": 1,
|
|
"title": data.Title,
|
|
"type": data.Type,
|
|
"permission": data.Permission,
|
|
"hide": data.Hide,
|
|
"sort": data.Sort,
|
|
"target": data.Target,
|
|
"path": data.Path,
|
|
"component": data.Component,
|
|
"icon": data.Icon,
|
|
}).Error)
|
|
// 这里要刷新下权限
|
|
cache.Global.Caches.RolePermission = cache.Global.Caches.RolePermission.Refresh()
|
|
} else {
|
|
menu := &model.Menu{}
|
|
menu.Pid = data.Pid
|
|
menu.Title = data.Title
|
|
menu.Type = data.Type
|
|
menu.Permission = data.Permission
|
|
menu.Hide = data.Hide
|
|
menu.Sort = data.Sort
|
|
menu.Target = data.Target
|
|
menu.Path = data.Path
|
|
menu.Component = data.Component
|
|
menu.Icon = data.Icon
|
|
menu.Status = 1
|
|
// 如果是菜单的时候, 需要判断菜单权限表示是否重复
|
|
if data.Type == 0 {
|
|
if len(data.Permission) <= 0 {
|
|
return a.ReturnPublicErrorResponse(a.Translate("AT021"))
|
|
}
|
|
|
|
var total int64
|
|
utils.Error(a.Ts.Table(model.Menu{}.TableName()).Where(`permission = ?`, data.Permission).Count(&total).Error)
|
|
if total > 0 {
|
|
return a.ReturnPublicErrorResponse(a.Translate("AT000"))
|
|
}
|
|
}
|
|
|
|
utils.Error(a.Ts.Create(&menu).Error)
|
|
}
|
|
|
|
return a.ReturnSuccessResponse()
|
|
}
|
|
|
|
type CommonIdsResponse struct {
|
|
Id []interface{} `json:"id"`
|
|
Status string `json:"status"`
|
|
}
|