Files
web_hgdjc_server/web/api/web_acc/role.go
T
2026-11-07 10:44:09 +08:00

401 lines
9.7 KiB
Go

package web_acc
import (
"videoweb/pub/common"
"videoweb/pub/common/pageopt"
"videoweb/pub/common/srvdef"
"videoweb/pub/models/mwacc"
"videoweb/pub/pubdef"
"videoweb/web/service/srv_mwacc"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
)
// @Tags 角色
// @Summary 角色-列表
// @Description 角色-列表
// @Accept json
// @Produce json
// @Param param body pubdef.Page true "参数列表"
// @Success 200 {object} MwRoleListResp "成功"
// @Failure 400 {string} string "获取失败的返回结果"
// @Security ApiKeyAuth
// @Router /api/web/role/list [GET]
func RoleGetList(c *gin.Context) {
var req pubdef.Page
if err := c.ShouldBindQuery(&req); err != nil {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
if req.PageNum <= 0 {
req.PageNum = 1
}
if req.PageSize <= 0 {
req.PageSize = 10
}
skip := (req.PageNum - 1) * req.PageSize
limit := req.PageSize
// 获取分页信息
var mats []pageopt.Matcher
dbRoleRec, err := mwacc.GetRoleList(c, skip, limit, mats...)
if err != nil {
common.ServerJSON(c, srvdef.Failure, "")
return
}
// 获取总条数
roleCount, err := mwacc.RoleCountByFilter(c, bson.M{})
if err != nil {
common.ServerJSON(c, srvdef.Failure, "")
return
}
webRoleRec := make([]*MwRoleRec, 0)
for _, v := range dbRoleRec {
webRoleRec = append(webRoleRec, &MwRoleRec{
Id: v.Id,
Name: v.Name,
Desc: v.Desc,
IsEnb: v.IsEnb,
ApiMenus: v.ApiMenus[:],
CreatedAt: v.CreatedAt,
UpdatedAt: v.UpdatedAt,
})
}
// 返回正确信息
common.ServerJSON(c, srvdef.Success, MwRoleListResp{TotalCount: roleCount, RoleRecs: webRoleRec})
return
}
// @Tags 角色
// @Summary 角色-添加
// @Description 角色-添加
// @Accept json
// @Produce json
// @Param param body MwRoleAddReq true "参数列表"
// @Success 200 {object} MwRoleAddResp "成功"
// @Failure 400 {string} string "获取失败的返回结果"
// @Security ApiKeyAuth
// @Router /api/web/role/add [POST]
func RoleAdd(c *gin.Context) {
var req MwRoleAddReq
if err := c.ShouldBindJSON(&req); err != nil {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
ok, _ := mwacc.RoleAdd(c, req.Name, req.Desc, req.IsEnb)
if !ok {
common.ServerJSON(c, srvdef.Failure, "")
return
}
// 返回正确信息
common.ServerJSON(c, srvdef.Success, MwRoleAddResp{Result: true})
return
}
// @Tags 角色
// @Summary 角色-删除
// @Description 角色-删除
// @Accept json
// @Produce json
// @Param param body MwRoleDelReq true "参数列表"
// @Success 200 {object} MwRoleDelResp "成功"
// @Failure 400 {string} string "获取失败的返回结果"
// @Security ApiKeyAuth
// @Router /api/web/role/del [POST]
func RoleDel(c *gin.Context) {
var req MwRoleDelReq
if err := c.ShouldBindJSON(&req); err != nil {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
ok, err := srv_mwacc.RoleDel(c, req.Id)
if !ok {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
common.ServerJSON(c, srvdef.Success, MwRoleDelResp{Result: true})
return
}
// @Tags 角色
// @Summary 角色-获取信息
// @Description 角色-获取信息
// @Accept json
// @Produce json
// @Param param body MwRoleGetInfoReq true "参数列表"
// @Success 200 {object} MwRoleGetInfoResp "成功"
// @Failure 400 {string} string "获取失败的返回结果"
// @Security ApiKeyAuth
// @Router /api/web/role/getinfo [GET]
func RoleGetInfo(c *gin.Context) {
var req MwRoleGetInfoReq
if err := c.ShouldBindQuery(&req); err != nil {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
roleInfo, err := srv_mwacc.RoleGetInfo(c, req.Id)
if err != nil {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
resq := &MwRoleGetInfoResp{
RoleRec: &MwRoleRec{
Id: roleInfo.Id,
Name: roleInfo.Name,
Desc: roleInfo.Desc,
IsEnb: roleInfo.IsEnb,
ApiMenus: roleInfo.ApiMenus[:],
CreatedAt: roleInfo.CreatedAt,
UpdatedAt: roleInfo.UpdatedAt,
},
}
common.ServerJSON(c, srvdef.Success, resq)
return
}
// @Tags 角色
// @Summary 角色-更新
// @Description 角色-更新
// @Accept json
// @Produce json
// @Param param body MwRoleUpReq true "参数列表"
// @Success 200 {object} MwRoleUpResp "成功"
// @Failure 400 {string} string "获取失败的返回结果"
// @Security ApiKeyAuth
// @Router /api/web/role/up [POST]
func RoleUp(c *gin.Context) {
var req MwRoleUpReq
if err := c.ShouldBindJSON(&req); err != nil {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
// RoleUp
_, err := srv_mwacc.RoleUp(c, req.Id, req.Name, req.Desc, req.IsEnb)
if err != nil {
common.ServerJSON(c, srvdef.Failure, MwRoleUpResp{Result: false})
return
}
common.ServerJSON(c, srvdef.Success, MwRoleUpResp{Result: true})
return
}
// @Tags 角色
// @Summary 角色-使能
// @Description 角色-使能
// @Accept json
// @Produce json
// @Param param body MwRoleEnableReq true "参数列表"
// @Success 200 {object} MwRoleEnableResp "成功"
// @Failure 400 {string} string "获取失败的返回结果"
// @Security ApiKeyAuth
// @Router /api/web/role/enable [POST]
func RoleEnable(c *gin.Context) {
var req MwRoleEnableReq
if err := c.ShouldBindJSON(&req); err != nil {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
_, err := srv_mwacc.RoleEnable(c, req.Id, req.IsEnb)
if err != nil {
common.ServerJSON(c, err.Code, err.Msg)
return
}
common.ServerJSON(c, srvdef.Success, MwRoleEnableResp{Result: true})
return
}
// @Tags 角色
// @Summary 角色-下拉列表
// @Description 角色-下拉列表
// @Accept json
// @Produce json
// @Success 200 {object} MwRoleDropResp "成功"
// @Failure 400 {string} string "获取失败的返回结果"
// @Security ApiKeyAuth
// @Router /api/web/role/drop [POST]
func RoleGetDrop(c *gin.Context) {
data, _ := mwacc.RoleDrop(c)
roleDropArr := make([]*MwRoleDrop, 0)
for _, v := range data {
roleDropArr = append(roleDropArr, &MwRoleDrop{Id: v.Id, Name: v.Name})
}
common.ServerJSON(c, srvdef.Success, MwRoleDropResp{RoleDrop: roleDropArr})
return
}
func DbRouterToWebRouter(dbRouter *mwacc.Router) (webRouter *Router) {
if dbRouter == nil {
return
}
webRouter = &Router{
Path: dbRouter.Path,
Component: dbRouter.Component,
Redirect: dbRouter.Redirect,
Name: dbRouter.Name,
IsShow: dbRouter.IsShow,
Hidden: dbRouter.Hidden,
Meta: &Router_Meta{
Title: dbRouter.Meta.Title,
Icon: dbRouter.Meta.Icon,
NoCache: dbRouter.Meta.NoCache,
},
Children: make([]*Router, 0),
ApiUrl: make([]*Router_ApiUrl, 0),
}
for _, v := range dbRouter.Children {
webRouter.Children = append(webRouter.Children, DbRouterToWebRouter(&v))
}
for _, v := range dbRouter.ApiUrl {
webRouter.ApiUrl = append(webRouter.ApiUrl, &Router_ApiUrl{
Url: v.Url[:],
Desc: v.Desc,
Status: v.Status,
Method: v.Method[:],
})
}
return
}
// @Tags 角色
// @Summary 角色-获取权限
// @Description 角色-获取权限
// @Accept json
// @Produce json
// @Param param body MwRoleGetAuthReq true "参数列表"
// @Success 200 {object} MwRoleGetAuthResp "成功"
// @Failure 400 {string} string "获取失败的返回结果"
// @Security ApiKeyAuth
// @Router /api/web/role/getauth [GET]
func RoleGetAuth(c *gin.Context) {
var req MwRoleGetAuthReq
if err := c.ShouldBindQuery(&req); err != nil {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
roleInfo, err := srv_mwacc.RoleGetInfo(c, req.Id)
if err != nil {
common.ServerJSON(c, srvdef.Failure, err.Error())
return
}
if roleInfo == nil {
common.ServerJSON(c, srvdef.Failure, "没找到角色")
return
}
if roleInfo.Id == 0 {
common.ServerJSON(c, srvdef.Failure, "没找到角色2")
return
}
resq := &MwRoleGetAuthResp{
Id: req.Id,
Router: make([]*Router, 0),
}
for _, v := range roleInfo.Router {
resq.Router = append(resq.Router, DbRouterToWebRouter(&v))
}
common.ServerJSON(c, srvdef.Success, resq)
return
}
// 网站Router转数据库Router
func WebRouterToDbRouter(webRouter *Router) (dbRouter *mwacc.Router) {
if webRouter == nil {
return
}
dbRouter = &mwacc.Router{
Path: webRouter.Path,
Component: webRouter.Component,
Redirect: webRouter.Redirect,
Name: webRouter.Name,
IsShow: webRouter.IsShow,
Hidden: webRouter.Hidden,
Meta: mwacc.Meta{
Title: webRouter.Meta.Title,
Icon: webRouter.Meta.Icon,
NoCache: webRouter.Meta.NoCache,
},
Children: make([]mwacc.Router, 0),
ApiUrl: make([]mwacc.Apiurl, 0),
}
for _, v := range webRouter.Children {
dbRouter.Children = append(dbRouter.Children, *WebRouterToDbRouter(v))
}
for _, v := range webRouter.ApiUrl {
dbRouter.ApiUrl = append(dbRouter.ApiUrl, mwacc.Apiurl{
Url: v.Url[:],
Desc: v.Desc,
Status: v.Status,
Method: v.Method[:],
})
}
return
}
// @Tags 角色
// @Summary 角色-更新权限
// @Description 角色-更新权限
// @Accept json
// @Produce json
// @Param param body MwRoleUpAuthReq true "参数列表"
// @Success 200 {object} MwRoleUpAuthResp "成功"
// @Failure 400 {string} string "获取失败的返回结果"
// @Security ApiKeyAuth
// @Router /api/web/role/upauth [GET]
func RoleUpAuth(c *gin.Context) {
var req MwRoleUpAuthReq
if err := c.ShouldBindJSON(&req); err != nil {
common.ServerJSON(c, srvdef.ErrParamError, err.Error())
return
}
dataSave := make([]*mwacc.Router, 0)
for _, v := range req.Router {
dataSave = append(dataSave, WebRouterToDbRouter(v))
}
_, err := srv_mwacc.UpAuth(c, req.Id, dataSave)
if err != nil {
common.ServerJSON(c, srvdef.Failure, MwRoleUpAuthResp{Result: false})
return
}
// auth.ClearRoleMap()
common.ServerJSON(c, srvdef.Success, MwRoleUpAuthResp{Result: true})
return
}