feat✨: 添加修改角色状态接口

This commit is contained in:
zhangwenjian committed 2021-06-09 21:31:31 +08:00
1 parent 91d902389a
commit 1b579fb814
5 files changed
+103 -19

No files matched your search

+52 -14
View File
@@ -1,6 +1,7 @@
package apis
import (
"fmt"
"github.com/gin-gonic/gin/binding"
"github.com/go-admin-team/go-admin-core/sdk"
"go-admin/app/admin/models"
@@ -63,8 +64,7 @@ func (e SysRole) GetPage(c *gin.Context) {
// @Description 获取JSON
// @Tags 角色/Role
// @Param roleId path string false "roleId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/role/{id} [get]
// @Security Bearer
func (e SysRole) Get(c *gin.Context) {
@@ -77,7 +77,7 @@ func (e SysRole) Get(c *gin.Context) {
Errors
if err != nil {
e.Logger.Error(err)
e.Error(500, err, err.Error())
e.Error(500, err, fmt.Sprintf(" %s ", err.Error()))
return
}
@@ -99,8 +99,7 @@ func (e SysRole) Get(c *gin.Context) {
// @Accept application/json
// @Product application/json
// @Param data body dto.SysRoleControl true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/role [post]
// @Security Bearer
func (e SysRole) Insert(c *gin.Context) {
@@ -144,8 +143,7 @@ func (e SysRole) Insert(c *gin.Context) {
// @Accept application/json
// @Product application/json
// @Param data body dto.SysRoleControl true "body"
// @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
// @Success 200 {string} string "{"code": -1, "message": "修改失败"}"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/role/{id} [put]
// @Security Bearer
func (e SysRole) Update(c *gin.Context) {
@@ -183,8 +181,7 @@ func (e SysRole) Update(c *gin.Context) {
// @Description 删除数据
// @Tags 角色/Role
// @Param data body dto.SysRoleById true "body"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/role [delete]
// @Security Bearer
func (e SysRole) Delete(c *gin.Context) {
@@ -197,25 +194,66 @@ func (e SysRole) Delete(c *gin.Context) {
Errors
if err != nil {
e.Logger.Error(err)
e.Error(500, err, err.Error())
e.Error(500, err, fmt.Sprintf("删除角色 %v 失败,\r\n失败信息 %s", req.Ids, err.Error()))
return
}
err = s.Remove(&req)
if err != nil {
e.Logger.Error(err)
e.Error(http.StatusInternalServerError, err, "")
e.Error(500, err, "")
return
}
_, err = global.LoadPolicy(c)
if err != nil {
e.Error(http.StatusInternalServerError, err, "")
e.Error(500, err, fmt.Sprintf("删除角色 %v 失败,失败信息 %s", req.Id, err.Error()))
return
}
e.OK(req.GetId(), "删除成功")
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, nil, binding.JSON).
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{}
@@ -241,4 +279,4 @@ func (e SysRole) Update2DataScope(c *gin.Context) {
return
}
e.OK(nil, "操作成功")
}
}
+2 -1
View File
@@ -23,5 +23,6 @@ func registerSysRoleRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle
r.PUT("/:id", api.Update)
r.DELETE("", api.Delete)
}
v1.PUT("/role-status", api.Update2Status)
v1.PUT("/roledatascope", api.Update2DataScope)
}
}
+19 -2
View File
@@ -34,7 +34,6 @@ func (m *SysRoleSearch) GetNeedSearch() interface{} {
return *m
}
// SysConfigControl 增、改使用的结构体
type SysRoleControl struct {
RoleId int `uri:"id" comment:"角色编码"` // 角色编码
RoleName string `form:"roleName" comment:"角色名称"` // 角色名称
@@ -80,7 +79,25 @@ func (s *SysRoleControl) GetId() interface{} {
return s.RoleId
}
// SysConfigById 获取单个或者删除的结构体
type UpdateStatusReq struct {
RoleId int `form:"roleId" comment:"角色编码"` // 角色编码
Status string `form:"status" comment:"状态"` // 状态
common.ControlBy
}
func (s *UpdateStatusReq) Generate(model *models.SysRole) {
if s.RoleId != 0 {
model.RoleId = s.RoleId
}
model.Status = s.Status
}
func (s *UpdateStatusReq) GetId() interface{} {
return s.RoleId
}
// SysRoleById 获取单个或者删除的结构体
type SysRoleById struct {
dto.ObjectById
}
+29 -1
View File
@@ -236,4 +236,32 @@ func (e *SysRole) UpdateDataScope(c *models.SysRole) (err error) {
}
}
return err
}
}
// UpdateStatus 修改SysRole对象status
func (e *SysRole) UpdateStatus(c *dto.UpdateStatusReq) error {
var err error
tx := e.Orm.Debug().Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
var model = models.SysRole{}
tx.First(&model, c.GetId())
c.Generate(&model)
db := tx.Session(&gorm.Session{FullSaveAssociations: true}).Debug().Save(&model)
if db.Error != nil {
e.Log.Errorf("db error:%s", err)
return err
}
if db.RowsAffected == 0 {
return errors.New("无权更新该数据")
}
return nil
}
+1 -1
View File
@@ -139,6 +139,7 @@ func LoginLogToDB(c *gin.Context, status string, msg string, username string) {
}
}
// LogOut
// @Summary 退出登录
// @Description 获取token
// LoginHandler can be used by clients to get a jwt token.
@@ -167,7 +168,6 @@ func Authorizator(data interface{}, c *gin.Context) bool {
c.Set("userId", u.UserId)
c.Set("userName", u.Username)
c.Set("dataScope", r.DataScope)
return true
}
return false