forked from bullteam/zeus-admin
/
data_perm.go
157 lines (149 loc) · 4.42 KB
/
data_perm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package controllers
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/CMLiang/zeus-admin/pkg/api/dto"
"github.com/CMLiang/zeus-admin/pkg/api/service"
)
var dataPermService = service.DataPermService{}
type DatePermController struct {
BaseController
}
// Show data permission
// @Tags DataPerm
// @Summary 数据权限详情
// @Security ApiKeyAuth
// @Produce json
// @Param id path string true "数据权限id"
// @Success 200 {string} json "{"code":200,"data":{"id":1,"name":"test"}}"
// @Router /v1/datas/{id} [get]
func (d *DatePermController) Get(c *gin.Context) {
var gDto dto.GeneralGetDto
if d.BindAndValidate(c, &gDto) {
data := dataPermService.InfoOfId(gDto)
if data.Id < 1 {
fail(c, ErrNoRecord)
return
}
resp(c, map[string]interface{}{
"result": data,
})
}
}
// List data permission
// @Tags DataPerm
// @Summary 数据权限列表[分页+搜索]
// @Security ApiKeyAuth
// @Produce json
// @Param limit query int false "条数"
// @Param skip query int false "偏移量"
// @Success 200 {string} json "{"code":200,"data":{"result":[...],"total":1}}"
// @Router /v1/datas [get]
func (d *DatePermController) List(c *gin.Context) {
var listDto dto.GeneralListDto
if d.BindAndValidate(c, &listDto) {
data, total := dataPermService.List(listDto)
resp(c, map[string]interface{}{
"result": data,
"total": total,
})
}
}
// Create data permission
// @Tags DataPerm
// @Summary 新增数据权限
// @Security ApiKeyAuth
// @Produce json
// @Success 200 {string} json "{"code":200,"data":{"id":1}}"
// @Router /v1/datas [post]
func (d *DatePermController) Create(c *gin.Context) {
var dataPermAddDto dto.DataPermAddDto
if d.BindAndValidate(c, &dataPermAddDto) {
created := dataPermService.Create(dataPermAddDto)
if created.Id <= 0 {
fail(c, ErrAddFail)
return
}
// insert operation log
b, _ := json.Marshal(dataPermAddDto)
orLogDto := dto.OperationLogDto{
UserId: int(c.Value("userId").(float64)),
RequestUrl: c.Request.RequestURI,
OperationMethod: c.Request.Method,
Params: string(b),
Ip: c.ClientIP(),
IpLocation: "", //TODO...待接入获取ip位置服务
OperationResult: "success",
OperationSuccess: 1,
OperationContent: "Create Data Permission",
}
_ = logService.InsertOperationLog(&orLogDto)
resp(c, map[string]interface{}{
"id": created.Id,
})
}
}
// Delete data permission
// @Tags DataPerm
// @Summary 删除项目
// @Security ApiKeyAuth
// @Param id path string true "要删除的id"
// @Produce json
// @Success 200 {string} json "{"code":200,"data":{"id":1}}"
// @Router /v1/datas/{id} [delete]
func (d *DatePermController) Delete(c *gin.Context) {
var dataPermDelDto dto.GeneralDelDto
if d.BindAndValidate(c, &dataPermDelDto) {
affected := dataPermService.Delete(dataPermDelDto)
if affected <= 0 {
fail(c, ErrDelFail)
return
}
// insert operation log
b, _ := json.Marshal(dataPermDelDto)
orLogDto := dto.OperationLogDto{
UserId: int(c.Value("userId").(float64)),
RequestUrl: c.Request.RequestURI,
OperationMethod: c.Request.Method,
Params: string(b),
Ip: c.ClientIP(),
IpLocation: "", //TODO...待接入获取ip位置服务
OperationResult: "success",
OperationSuccess: 1,
OperationContent: "Delete Data Permission",
}
_ = logService.InsertOperationLog(&orLogDto)
ok(c, "ok.DeletedDone")
}
}
// Edit data permission
// @Tags DataPerm
// @Summary 编辑数据权限
// @Security ApiKeyAuth
// @Produce json
// @Param id path int true "需要编辑的id"
// @Success 200 {string} json "{"code":200,"data":{"id":1}}"
// @Router /v1/datas/{id} [put]
func (d *DatePermController) Edit(c *gin.Context) {
var dataPermEditDto dto.DataPermEditDto
if d.BindAndValidate(c, &dataPermEditDto) {
affected := dataPermService.Update(dataPermEditDto)
if affected > 0 {
// insert operation log
b, _ := json.Marshal(dataPermEditDto)
orLogDto := dto.OperationLogDto{
UserId: int(c.Value("userId").(float64)),
RequestUrl: c.Request.RequestURI,
OperationMethod: c.Request.Method,
Params: string(b),
Ip: c.ClientIP(),
IpLocation: "", //TODO...待接入获取ip位置服务
OperationResult: "success",
OperationSuccess: 1,
OperationContent: "Edit Data Permission",
}
_ = logService.InsertOperationLog(&orLogDto)
}
ok(c, "ok.UpdateDone")
}
}