sys_authority.go 5.8 KB
Newer Older
Mr.奇淼('s avatar
Mr.奇淼( 已提交
1
package v1
Mr.奇淼('s avatar
Mr.奇淼( 已提交
2 3 4

import (
	"fmt"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
5 6
	"gin-vue-admin/global/response"
	"gin-vue-admin/model"
7
	"gin-vue-admin/model/request"
8
	resp "gin-vue-admin/model/response"
9
	"gin-vue-admin/service"
10
	"gin-vue-admin/utils"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
11 12 13 14 15 16 17 18
	"github.com/gin-gonic/gin"
)

// @Tags authority
// @Summary 创建角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
19
// @Param data body model.SysAuthority true "创建角色"
20
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
21 22
// @Router /authority/createAuthority [post]
func CreateAuthority(c *gin.Context) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
23
	var auth model.SysAuthority
24
	_ = c.ShouldBindJSON(&auth)
25
	AuthorityVerify := utils.Rules{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
26 27
		"AuthorityId":   {utils.NotEmpty()},
		"AuthorityName": {utils.NotEmpty()},
28 29 30
		"ParentId":      {utils.NotEmpty()},
	}
	AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
31
	if AuthorityVerifyErr != nil {
32 33 34
		response.FailWithMessage(AuthorityVerifyErr.Error(), c)
		return
	}
35
	err, authBack := service.CreateAuthority(auth)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
36
	if err != nil {
37
		response.FailWithMessage(fmt.Sprintf("创建失败,%v", err), c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
38
	} else {
39
		response.OkWithData(resp.SysAuthorityResponse{Authority: authBack}, c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
40 41 42
	}
}

43 44 45 46 47 48 49 50 51 52 53
// @Tags authority
// @Summary 拷贝角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body response.SysAuthorityCopyResponse true "拷贝角色"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"拷贝成功"}"
// @Router /authority/copyAuthority [post]
func CopyAuthority(c *gin.Context) {
	var copyInfo resp.SysAuthorityCopyResponse
	_ = c.ShouldBindJSON(&copyInfo)
54
	OldAuthorityVerify := utils.Rules{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
55
		"OldAuthorityId": {utils.NotEmpty()},
56 57
	}
	OldAuthorityVerifyErr := utils.Verify(copyInfo, OldAuthorityVerify)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
58
	if OldAuthorityVerifyErr != nil {
59 60 61 62
		response.FailWithMessage(OldAuthorityVerifyErr.Error(), c)
		return
	}
	AuthorityVerify := utils.Rules{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
63 64
		"AuthorityId":   {utils.NotEmpty()},
		"AuthorityName": {utils.NotEmpty()},
65 66 67
		"ParentId":      {utils.NotEmpty()},
	}
	AuthorityVerifyErr := utils.Verify(copyInfo.Authority, AuthorityVerify)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
68
	if AuthorityVerifyErr != nil {
69 70 71
		response.FailWithMessage(AuthorityVerifyErr.Error(), c)
		return
	}
72 73
	err, authBack := service.CopyAuthority(copyInfo)
	if err != nil {
74
		response.FailWithMessage(fmt.Sprintf("拷贝失败,%v", err), c)
75 76 77 78 79
	} else {
		response.OkWithData(resp.SysAuthorityResponse{Authority: authBack}, c)
	}
}

Mr.奇淼('s avatar
Mr.奇淼( 已提交
80 81 82 83 84
// @Tags authority
// @Summary 删除角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
85
// @Param data body model.SysAuthority true "删除角色"
86
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
87 88
// @Router /authority/deleteAuthority [post]
func DeleteAuthority(c *gin.Context) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
89
	var a model.SysAuthority
90
	_ = c.ShouldBindJSON(&a)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
91 92 93
	AuthorityIdVerifyErr := utils.Verify(a, utils.CustomizeMap["AuthorityIdVerify"])
	if AuthorityIdVerifyErr != nil {
		response.FailWithMessage(AuthorityIdVerifyErr.Error(), c)
94 95
		return
	}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
96
	//删除角色之前需要判断是否有用户正在使用此角色
97
	err := service.DeleteAuthority(&a)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
98
	if err != nil {
99
		response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
100
	} else {
101
		response.OkWithMessage("删除成功", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
102 103
	}
}
104

105 106 107 108 109 110 111 112 113 114 115
// @Tags authority
// @Summary 设置角色资源权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysAuthority true "设置角色资源权限"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
// @Router /authority/updateAuthority [post]
func UpdateAuthority(c *gin.Context) {
	var auth model.SysAuthority
	_ = c.ShouldBindJSON(&auth)
116
	AuthorityVerify := utils.Rules{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
117 118
		"AuthorityId":   {utils.NotEmpty()},
		"AuthorityName": {utils.NotEmpty()},
119 120 121
		"ParentId":      {utils.NotEmpty()},
	}
	AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
122
	if AuthorityVerifyErr != nil {
123 124 125
		response.FailWithMessage(AuthorityVerifyErr.Error(), c)
		return
	}
126 127 128 129 130 131 132 133
	err, authority := service.UpdateAuthority(auth)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("更新失败,%v", err), c)
	} else {
		response.OkWithData(resp.SysAuthorityResponse{authority}, c)
	}
}

134 135 136 137 138
// @Tags authority
// @Summary 分页获取角色列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
Mr.奇淼('s avatar
Mr.奇淼( 已提交
139
// @Param data body request.PageInfo true "分页获取用户列表"
140
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
141
// @Router /authority/getAuthorityList [post]
142
func GetAuthorityList(c *gin.Context) {
143
	var pageInfo request.PageInfo
144
	_ = c.ShouldBindJSON(&pageInfo)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
145 146 147
	PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"])
	if PageVerifyErr != nil {
		response.FailWithMessage(PageVerifyErr.Error(), c)
148 149
		return
	}
150
	err, list, total := service.GetAuthorityInfoList(pageInfo)
151
	if err != nil {
152
		response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
153
	} else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
154 155 156 157 158 159
		response.OkWithData(resp.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, c)
160
	}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
161 162
}

163 164 165 166 167
// @Tags authority
// @Summary 设置角色资源权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
168
// @Param data body model.SysAuthority true "设置角色资源权限"
169 170 171
// @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
// @Router /authority/setDataAuthority [post]
func SetDataAuthority(c *gin.Context) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
172
	var auth model.SysAuthority
173
	_ = c.ShouldBindJSON(&auth)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
174 175 176
	AuthorityIdVerifyErr := utils.Verify(auth, utils.CustomizeMap["AuthorityIdVerify"])
	if AuthorityIdVerifyErr != nil {
		response.FailWithMessage(AuthorityIdVerifyErr.Error(), c)
177 178
		return
	}
179
	err := service.SetDataAuthority(auth)
180
	if err != nil {
181
		response.FailWithMessage(fmt.Sprintf("设置关联失败,%v", err), c)
182
	} else {
183
		response.Ok(c)
184
	}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
185
}