123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- package apis
- import (
- "fmt"
- "go-admin/common/global"
- "net/http"
- "github.com/gin-gonic/gin/binding"
- "github.com/go-admin-team/go-admin-core/sdk"
- "go-admin/app/admin/models"
- "github.com/gin-gonic/gin"
- "github.com/go-admin-team/go-admin-core/sdk/api"
- "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
- _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
- "go-admin/app/admin/service"
- "go-admin/app/admin/service/dto"
- )
- type SysRole struct {
- api.Api
- }
- // GetPage
- // @Summary 角色列表数据
- // @Description Get JSON
- // @Tags 角色/Role
- // @Param roleName query string false "roleName"
- // @Param status query string false "status"
- // @Param roleKey query string false "roleKey"
- // @Param pageSize query int false "页条数"
- // @Param pageIndex query int false "页码"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/role [get]
- // @Security Bearer
- func (e SysRole) GetPage(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleGetPageReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.Form).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- list := make([]models.SysRole, 0)
- var count int64
- err = s.GetPage(&req, &list, &count)
- if err != nil {
- e.Error(500, err, "查询失败")
- return
- }
- e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
- }
- // Get
- // @Summary 获取Role数据
- // @Description 获取JSON
- // @Tags 角色/Role
- // @Param roleId path string false "roleId"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/role/{id} [get]
- // @Security Bearer
- func (e SysRole) Get(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleGetReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, nil).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, fmt.Sprintf(" %s ", err.Error()))
- return
- }
- var object models.SysRole
- err = s.Get(&req, &object)
- if err != nil {
- e.Error(http.StatusUnprocessableEntity, err, "查询失败")
- return
- }
- e.OK(object, "查询成功")
- }
- // Insert
- // @Summary 创建角色
- // @Description 获取JSON
- // @Tags 角色/Role
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.SysRoleInsertReq true "data"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/role [post]
- // @Security Bearer
- func (e SysRole) Insert(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleInsertReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.JSON).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- // 设置创建人
- req.CreateBy = user.GetUserId(c)
- if req.Status == "" {
- req.Status = "2"
- }
- cb := sdk.Runtime.GetCasbinKey(c.Request.Host)
- err = s.Insert(&req, cb)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, "创建失败,"+err.Error())
- return
- }
- _, err = global.LoadPolicy(c)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, "创建失败,"+err.Error())
- return
- }
- e.OK(req.GetId(), "创建成功")
- }
- // Update 修改用户角色
- // @Summary 修改用户角色
- // @Description 获取JSON
- // @Tags 角色/Role
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.SysRoleUpdateReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/role/{id} [put]
- // @Security Bearer
- func (e SysRole) Update(c *gin.Context) {
- s := service.SysRole{}
- req := dto.SysRoleUpdateReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, nil, binding.JSON).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- cb := sdk.Runtime.GetCasbinKey(c.Request.Host)
- req.SetUpdateBy(user.GetUserId(c))
- err = s.Update(&req, cb)
- if err != nil {
- e.Logger.Error(err)
- return
- }
- _, err = global.LoadPolicy(c)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, "更新失败,"+err.Error())
- return
- }
- e.OK(req.GetId(), "更新成功")
- }
- // Delete
- // @Summary 删除用户角色
- // @Description 删除数据
- // @Tags 角色/Role
- // @Param data body dto.SysRoleDeleteReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/role [delete]
- // @Security Bearer
- func (e SysRole) Delete(c *gin.Context) {
- s := new(service.SysRole)
- req := dto.SysRoleDeleteReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.JSON).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, fmt.Sprintf("删除角色 %v 失败,\r\n失败信息 %s", req.Ids, err.Error()))
- return
- }
- cb := sdk.Runtime.GetCasbinKey(c.Request.Host)
- err = s.Remove(&req, cb)
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, "")
- return
- }
- e.OK(req.GetId(), fmt.Sprintf("删除角色角色 %v 状态成功!", req.GetId()))
- }
- // Update2Status 修改用户角色状态
- // @Summary 修改用户角色
- // @Description 获取JSON
- // @Tags 角色/Role
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.UpdateStatusReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/role-status/{id} [put]
- // @Security Bearer
- func (e SysRole) Update2Status(c *gin.Context) {
- s := service.SysRole{}
- req := dto.UpdateStatusReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.JSON, nil).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, fmt.Sprintf("更新角色状态失败,失败原因:%s ", err.Error()))
- return
- }
- req.SetUpdateBy(user.GetUserId(c))
- err = s.UpdateStatus(&req)
- if err != nil {
- e.Error(500, err, fmt.Sprintf("更新角色状态失败,失败原因:%s ", err.Error()))
- return
- }
- e.OK(req.GetId(), fmt.Sprintf("更新角色 %v 状态成功!", req.GetId()))
- }
- // Update2DataScope 更新角色数据权限
- // @Summary 更新角色数据权限
- // @Description 获取JSON
- // @Tags 角色/Role
- // @Accept application/json
- // @Product application/json
- // @Param data body dto.RoleDataScopeReq true "body"
- // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
- // @Router /api/v1/role-status/{id} [put]
- // @Security Bearer
- func (e SysRole) Update2DataScope(c *gin.Context) {
- s := service.SysRole{}
- req := dto.RoleDataScopeReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.JSON, nil).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- data := &models.SysRole{
- RoleId: req.RoleId,
- DataScope: req.DataScope,
- DeptIds: req.DeptIds,
- }
- data.UpdateBy = user.GetUserId(c)
- err = s.UpdateDataScope(&req).Error
- if err != nil {
- e.Error(500, err, fmt.Sprintf("更新角色数据权限失败!错误详情:%s", err.Error()))
- return
- }
- e.OK(nil, "操作成功")
- }
|