sys_authority.go 6.0 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 26 27 28 29 30 31 32 33 34
	AuthorityVerify := utils.Rules{
		"AuthorityId":      {utils.NotEmpty()},
		"AuthorityName":      {utils.NotEmpty()},
		"ParentId":      {utils.NotEmpty()},
	}
	AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
	if AuthorityVerifyErr!=nil {
		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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	OldAuthorityVerify := utils.Rules{
		"OldAuthorityId":      {utils.NotEmpty()},
	}
	OldAuthorityVerifyErr := utils.Verify(copyInfo, OldAuthorityVerify)
	if OldAuthorityVerifyErr!=nil {
		response.FailWithMessage(OldAuthorityVerifyErr.Error(), c)
		return
	}
	AuthorityVerify := utils.Rules{
		"AuthorityId":      {utils.NotEmpty()},
		"AuthorityName":      {utils.NotEmpty()},
		"ParentId":      {utils.NotEmpty()},
	}
	AuthorityVerifyErr := utils.Verify(copyInfo.Authority, AuthorityVerify)
	if AuthorityVerifyErr!=nil {
		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)
91 92 93 94 95 96 97 98
	AuthorityVerify := utils.Rules{
		"AuthorityId":      {utils.NotEmpty()},
	}
	AuthorityVerifyErr := utils.Verify(a, AuthorityVerify)
	if AuthorityVerifyErr!=nil {
		response.FailWithMessage(AuthorityVerifyErr.Error(), c)
		return
	}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
99
	//删除角色之前需要判断是否有用户正在使用此角色
100
	err := service.DeleteAuthority(&a)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
101
	if err != nil {
102
		response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
103
	} else {
104
		response.OkWithMessage("删除成功", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
105 106
	}
}
107

108 109 110 111 112 113 114 115 116 117 118
// @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)
119 120 121 122 123 124 125 126 127 128
	AuthorityVerify := utils.Rules{
		"AuthorityId":      {utils.NotEmpty()},
		"AuthorityName":      {utils.NotEmpty()},
		"ParentId":      {utils.NotEmpty()},
	}
	AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
	if AuthorityVerifyErr!=nil {
		response.FailWithMessage(AuthorityVerifyErr.Error(), c)
		return
	}
129 130 131 132 133 134 135 136
	err, authority := service.UpdateAuthority(auth)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("更新失败,%v", err), c)
	} else {
		response.OkWithData(resp.SysAuthorityResponse{authority}, c)
	}
}

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

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