@@ -11,29 +11,32 @@ package controller
11
11
import (
12
12
"fmt"
13
13
"net/http"
14
+ "strconv"
15
+ "strings"
16
+ "time"
17
+
14
18
"ohurlshortener/core"
15
19
"ohurlshortener/service"
16
20
"ohurlshortener/utils"
17
21
"ohurlshortener/utils/export"
18
- "strconv"
19
- "strings"
20
- "time"
21
22
22
23
"github.com/dchest/captcha"
23
24
"github.com/gin-gonic/gin"
24
25
)
25
26
26
27
const (
27
- DEFAULT_PAGE_NUM = 1
28
- DEFAULT_PAGE_SIZE = 20
28
+ DefaultPageNum = 1
29
+ DefaultPageSize = 20
29
30
)
30
31
32
+ // LoginPage 登录页面
31
33
func LoginPage (c * gin.Context ) {
32
34
c .HTML (http .StatusOK , "login.html" , gin.H {
33
35
"title" : "登录 - ohUrlShortener" ,
34
36
})
35
37
}
36
38
39
+ // DoLogin 登录
37
40
func DoLogin (c * gin.Context ) {
38
41
account := c .PostForm ("account" )
39
42
password := c .PostForm ("password" )
@@ -56,7 +59,7 @@ func DoLogin(c *gin.Context) {
56
59
return
57
60
}
58
61
59
- //验证码有效性验证
62
+ // 验证码有效性验证
60
63
if ! captcha .VerifyString (captchaId , captchaText ) {
61
64
c .HTML (http .StatusOK , "login.html" , gin.H {
62
65
"title" : "错误 - ohUrlShortener" ,
@@ -65,7 +68,7 @@ func DoLogin(c *gin.Context) {
65
68
return
66
69
}
67
70
68
- //用户名密码有效性验证
71
+ // 用户名密码有效性验证
69
72
loginUser , err := service .Login (account , password )
70
73
if err != nil || loginUser .IsEmpty () {
71
74
c .HTML (http .StatusOK , "login.html" , gin.H {
@@ -75,7 +78,7 @@ func DoLogin(c *gin.Context) {
75
78
return
76
79
}
77
80
78
- //Write Cookie to browser
81
+ // Write Cookie to browser
79
82
cValue , err := AdminCookieValue (loginUser )
80
83
if err != nil {
81
84
c .HTML (http .StatusOK , "login.html" , gin.H {
@@ -89,21 +92,25 @@ func DoLogin(c *gin.Context) {
89
92
c .Redirect (http .StatusFound , "/admin/dashboard" )
90
93
}
91
94
95
+ // DoLogout 登出
92
96
func DoLogout (c * gin.Context ) {
93
97
c .SetCookie ("ohUrlShortenerAdmin" , "" , - 1 , "/" , "" , false , true )
94
98
c .SetCookie ("ohUrlShortenerCookie" , "" , - 1 , "/" , "" , false , true )
95
99
c .Redirect (http .StatusFound , "/login" )
96
100
}
97
101
102
+ // ServeCaptchaImage 生成验证码
98
103
func ServeCaptchaImage (c * gin.Context ) {
99
104
captcha .Server (200 , 45 ).ServeHTTP (c .Writer , c .Request )
100
105
}
101
106
107
+ // RequestCaptchaImage 请求验证码
102
108
func RequestCaptchaImage (c * gin.Context ) {
103
109
imageId := captcha .New ()
104
110
c .JSON (http .StatusOK , core .ResultJsonSuccessWithData (imageId ))
105
111
}
106
112
113
+ // ChangeState 修改状态
107
114
func ChangeState (c * gin.Context ) {
108
115
destUrl := c .PostForm ("dest_url" )
109
116
enable := c .PostForm ("enable" )
@@ -128,6 +135,7 @@ func ChangeState(c *gin.Context) {
128
135
c .JSON (http .StatusOK , core .ResultJsonSuccessWithData (result ))
129
136
}
130
137
138
+ // DeleteShortUrl 删除短链接
131
139
func DeleteShortUrl (c * gin.Context ) {
132
140
url := c .PostForm ("short_url" )
133
141
if utils .EmptyString (strings .TrimSpace (url )) {
@@ -144,6 +152,7 @@ func DeleteShortUrl(c *gin.Context) {
144
152
c .JSON (http .StatusOK , core .ResultJsonSuccess ())
145
153
}
146
154
155
+ // GenerateShortUrl 生成短链接
147
156
func GenerateShortUrl (c * gin.Context ) {
148
157
destUrl := c .PostForm ("dest_url" )
149
158
memo := c .PostForm ("memo" )
@@ -165,17 +174,18 @@ func GenerateShortUrl(c *gin.Context) {
165
174
c .JSON (http .StatusOK , core .ResultJsonSuccessWithData (json ))
166
175
}
167
176
177
+ // StatsPage 统计页面
168
178
func StatsPage (c * gin.Context ) {
169
179
url := c .DefaultQuery ("url" , "" )
170
- strPage := c .DefaultQuery ("page" , strconv .Itoa (DEFAULT_PAGE_NUM ))
171
- strSize := c .DefaultQuery ("size" , strconv .Itoa (DEFAULT_PAGE_SIZE ))
180
+ strPage := c .DefaultQuery ("page" , strconv .Itoa (DefaultPageNum ))
181
+ strSize := c .DefaultQuery ("size" , strconv .Itoa (DefaultPageSize ))
172
182
page , err := strconv .Atoi (strPage )
173
183
if err != nil {
174
- page = DEFAULT_PAGE_NUM
184
+ page = DefaultPageNum
175
185
}
176
186
size , err := strconv .Atoi (strSize )
177
187
if err != nil {
178
- size = DEFAULT_PAGE_SIZE
188
+ size = DefaultPageSize
179
189
}
180
190
urls , err := service .GetPagedUrlIpCountStats (strings .TrimSpace (url ), page , size )
181
191
c .HTML (http .StatusOK , "stats.html" , gin.H {
@@ -192,17 +202,18 @@ func StatsPage(c *gin.Context) {
192
202
})
193
203
}
194
204
205
+ // SearchStatsPage 查询统计页面
195
206
func SearchStatsPage (c * gin.Context ) {
196
207
url := c .DefaultQuery ("url" , "" )
197
- strPage := c .DefaultQuery ("page" , strconv .Itoa (DEFAULT_PAGE_NUM ))
198
- strSize := c .DefaultQuery ("size" , strconv .Itoa (DEFAULT_PAGE_SIZE ))
208
+ strPage := c .DefaultQuery ("page" , strconv .Itoa (DefaultPageNum ))
209
+ strSize := c .DefaultQuery ("size" , strconv .Itoa (DefaultPageSize ))
199
210
page , err := strconv .Atoi (strPage )
200
211
if err != nil {
201
- page = DEFAULT_PAGE_NUM
212
+ page = DefaultPageNum
202
213
}
203
214
size , err := strconv .Atoi (strSize )
204
215
if err != nil {
205
- size = DEFAULT_PAGE_SIZE
216
+ size = DefaultPageSize
206
217
}
207
218
urls , err := service .GetPagedUrlIpCountStats (strings .TrimSpace (url ), page , size )
208
219
c .HTML (http .StatusOK , "search_stats.html" , gin.H {
@@ -219,17 +230,18 @@ func SearchStatsPage(c *gin.Context) {
219
230
})
220
231
}
221
232
233
+ // UrlsPage 短链接列表页面
222
234
func UrlsPage (c * gin.Context ) {
223
235
url := c .DefaultQuery ("url" , "" )
224
- strPage := c .DefaultQuery ("page" , strconv .Itoa (DEFAULT_PAGE_NUM ))
225
- strSize := c .DefaultQuery ("size" , strconv .Itoa (DEFAULT_PAGE_SIZE ))
236
+ strPage := c .DefaultQuery ("page" , strconv .Itoa (DefaultPageNum ))
237
+ strSize := c .DefaultQuery ("size" , strconv .Itoa (DefaultPageSize ))
226
238
page , err := strconv .Atoi (strPage )
227
239
if err != nil {
228
- page = DEFAULT_PAGE_NUM
240
+ page = DefaultPageNum
229
241
}
230
242
size , err := strconv .Atoi (strSize )
231
243
if err != nil {
232
- size = DEFAULT_PAGE_SIZE
244
+ size = DefaultPageSize
233
245
}
234
246
urls , err := service .GetPagesShortUrls (strings .TrimSpace (url ), page , size )
235
247
c .HTML (http .StatusOK , "urls.html" , gin.H {
@@ -246,19 +258,20 @@ func UrlsPage(c *gin.Context) {
246
258
})
247
259
}
248
260
261
+ // AccessLogsPage 访问日志页面
249
262
func AccessLogsPage (c * gin.Context ) {
250
263
url := c .DefaultQuery ("url" , "" )
251
- strPage := c .DefaultQuery ("page" , strconv .Itoa (DEFAULT_PAGE_NUM ))
252
- strSize := c .DefaultQuery ("size" , strconv .Itoa (DEFAULT_PAGE_SIZE ))
264
+ strPage := c .DefaultQuery ("page" , strconv .Itoa (DefaultPageNum ))
265
+ strSize := c .DefaultQuery ("size" , strconv .Itoa (DefaultPageSize ))
253
266
start := c .DefaultQuery ("start" , "" )
254
267
end := c .DefaultQuery ("end" , "" )
255
268
page , err := strconv .Atoi (strPage )
256
269
if err != nil {
257
- page = DEFAULT_PAGE_NUM
270
+ page = DefaultPageNum
258
271
}
259
272
size , err := strconv .Atoi (strSize )
260
273
if err != nil {
261
- size = DEFAULT_PAGE_SIZE
274
+ size = DefaultPageSize
262
275
}
263
276
264
277
totalCount , distinctIpCount , err := service .GetAccessLogsCount (strings .TrimSpace (url ), start , end )
@@ -281,6 +294,7 @@ func AccessLogsPage(c *gin.Context) {
281
294
})
282
295
}
283
296
297
+ // AccessLogsExport 导出访问日志
284
298
func AccessLogsExport (c * gin.Context ) {
285
299
url := c .PostForm ("url" )
286
300
logs , err := service .GetAllAccessLogs (strings .TrimSpace (url ))
@@ -314,6 +328,7 @@ func AccessLogsExport(c *gin.Context) {
314
328
c .Data (http .StatusOK , "pplication/octet-stream" , fileContent )
315
329
}
316
330
331
+ // DashboardPage 仪表盘页面
317
332
func DashboardPage (c * gin.Context ) {
318
333
count , stats , err := service .GetSumOfUrlStats ()
319
334
if err != nil {
0 commit comments