Files
easygoadmin/app/controller/role.go
yaoyilin 1b36bd8fbe feat: 初始化项目
初始化项目
2022-10-31 22:29:16 +08:00

205 lines
3.8 KiB
Go

/**
* 角色管理-控制器
* @author
* @since 2021/9/13
* @File : role
*/
package controller
import (
"easygoadmin/app/dto"
"easygoadmin/app/model"
"easygoadmin/app/service"
"easygoadmin/utils"
"easygoadmin/utils/common"
"easygoadmin/utils/gconv"
"easygoadmin/utils/response"
"github.com/gin-gonic/gin"
"net/http"
)
var Role = new(roleCtl)
type roleCtl struct{}
func (c *roleCtl) Index(ctx *gin.Context) {
// 渲染模板
response.BuildTpl(ctx, "role_index.html").WriteTpl()
}
func (c *roleCtl) List(ctx *gin.Context) {
// 绑定参数
var req *dto.RolePageReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用查询列表方法
list, count, err := service.Role.GetList(req)
if err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "查询成功",
Data: list,
Count: count,
})
}
func (c *roleCtl) Edit(ctx *gin.Context) {
// 查询记录
id := gconv.Int(ctx.Query("id"))
if id > 0 {
info := &model.Role{Id: id}
isOk, err := info.Get()
if !isOk || err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
}
// 渲染模板
response.BuildTpl(ctx, "role_edit.html").WriteTpl(gin.H{
"info": info,
})
} else {
// 渲染模板
response.BuildTpl(ctx, "role_edit.html").WriteTpl()
}
}
func (c *roleCtl) Add(ctx *gin.Context) {
// 参数绑定
var req *dto.RoleAddReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用添加方法
rows, err := service.Role.Add(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "添加成功",
})
}
func (c *roleCtl) Update(ctx *gin.Context) {
// 参数绑定
var req *dto.RoleUpdateReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用更新方法
rows, err := service.Role.Update(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "更新方法",
})
}
func (c *roleCtl) Delete(ctx *gin.Context) {
// 记录ID
ids := ctx.Param("ids")
if ids == "" {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: "记录ID不能为空",
})
return
}
// 调用删除方法
rows, err := service.Role.Delete(ids)
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "删除成功",
})
}
func (c *roleCtl) Status(ctx *gin.Context) {
// 参数绑定
var req *dto.RoleStatusReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用设置状态方法
rows, err := service.Role.Status(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "设置成功",
})
}
func (c *roleCtl) GetRoleList(ctx *gin.Context) {
// 获取角色列表
list := make([]model.Role, 0)
utils.XormDb.Where("status=1 and mark=1").Asc("sort").Find(&list)
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "查询成功",
Data: list,
})
}