-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.go
188 lines (174 loc) · 4.08 KB
/
admin.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package controller
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/gin-gonic/gin"
newJwt "github.com/lflxp/gin-template/middlewares/jwt"
"github.com/lflxp/gin-template/model"
)
func RegisterAdmin(router *gin.Engine) {
var authMiddleware = newJwt.NewGinJwtMiddlewares(newJwt.AllUserAuthorizator)
// auth CRUD
apiGroup := router.Group("/api/v1/admin")
apiGroup.Use(authMiddleware.MiddlewareFunc())
{
apiGroup.GET("/auth/all", GetAllAuth)
apiGroup.POST("/auth/add", AddAuth)
apiGroup.PUT("/auth/update/:id", PutAuth)
apiGroup.DELETE("/auth/del/:id", DelAuth)
apiGroup.GET("/auth/verify/:user/:pwd", GetAuthVerify)
}
}
// @Summary 获取所有auth
// @Description 获取所有auth
// @Tags Admin
// @Param token query string false "token"
// @Param user path string true "用户名"
// @Param pwd path string true "密码"
// @Success 200 {string} string "success"
// @Security ApiKeyAuth
// @Router /api/v1/admin/auth/verify/{user}/{pwd} [get]
func GetAuthVerify(c *gin.Context) {
user := c.Params.ByName("user")
if user == "" {
c.String(200, "user is none")
return
}
pwd := c.Params.ByName("pwd")
if pwd == "" {
c.String(200, "pwd is none")
return
}
isok, err := model.VerifyAuth(user, pwd)
if err != nil {
c.JSONP(200, gin.H{
"status": false,
"msg": err.Error(),
})
return
}
if isok {
c.JSONP(200, gin.H{
"status": true,
"data": "success",
})
} else {
c.JSONP(200, gin.H{
"status": false,
"data": "failed",
})
}
}
// @Summary 获取所有auth
// @Description 获取所有auth
// @Tags Admin
// @Param token query string false "token"
// @Success 200 {string} string "success"
// @Security ApiKeyAuth
// @Router /api/v1/admin/auth/all [get]
func GetAllAuth(c *gin.Context) {
data, err := model.GetAuth()
if err != nil {
c.JSONP(200, gin.H{
"status": false,
"msg": err.Error(),
})
return
}
c.JSONP(200, gin.H{
"status": true,
"data": data,
})
}
// @Summary 新增用户
// @Description content 新增用户,不包括全权限
// @Tags Admin
// @Param token query string false "token"
// @Param data body model.Auth true "data"
// @Success 200 {object} model.Auth model.Auth{}
// @Security ApiKeyAuth
// @Router /api/v1/admin/auth/add [post]
func AddAuth(c *gin.Context) {
var data model.Auth
info, _ := ioutil.ReadAll(c.Request.Body)
err := json.Unmarshal(info, &data)
if err != nil {
c.String(500, err.Error())
return
}
num, err := model.AddAuth(&data)
if err != nil {
c.JSONP(200, gin.H{
"status": false,
"msg": err.Error(),
})
return
}
c.JSONP(200, gin.H{
"status": true,
"message": fmt.Sprintf("success add %d", num),
})
}
// @Summary 更新Auth
// @Description content 修改用户名或密码
// @Tags Admin
// @Param token query string false "token"
// @Param id path string true "更新的目标auth id"
// @Param data body model.Auth true "data"
// @Success 200 {object} model.Auth model.Auth{}
// @Security ApiKeyAuth
// @Router /api/v1/admin/auth/update/{id} [put]
func PutAuth(c *gin.Context) {
id := c.Params.ByName("id")
if id == "" {
c.String(200, "id is none")
return
}
var data model.Auth
info, _ := ioutil.ReadAll(c.Request.Body)
err := json.Unmarshal(info, &data)
if err != nil {
c.String(500, err.Error())
return
}
num, err := model.UpdateAuth(id, &data)
if err != nil {
c.JSONP(200, gin.H{
"status": false,
"msg": err.Error(),
})
return
}
c.JSONP(200, gin.H{
"status": true,
"msg": fmt.Sprintf("success update %d", num),
})
}
// @Summary 删除Auth
// @Description 删除Auth
// @Tags Admin
// @Param token query string false "token"
// @Param id path string true "更新的目标auth id"
// @Success 200 {string} string success!
// @Security ApiKeyAuth
// @Router /api/v1/admin/auth/del/{id} [delete]
func DelAuth(c *gin.Context) {
id := c.Params.ByName("id")
if id == "" {
c.String(200, "id is none")
return
}
num, err := model.DeleteAuth(id)
if err != nil {
c.JSONP(200, gin.H{
"status": false,
"msg": err.Error(),
})
return
}
c.JSONP(200, gin.H{
"status": true,
"data": fmt.Sprintf("success delete %d", num),
})
}