@@ -11,29 +11,32 @@ package controller
1111import (
1212 "fmt"
1313 "net/http"
14+ "strconv"
15+ "strings"
16+ "time"
17+
1418 "ohurlshortener/core"
1519 "ohurlshortener/service"
1620 "ohurlshortener/utils"
1721 "ohurlshortener/utils/export"
18- "strconv"
19- "strings"
20- "time"
2122
2223 "github.com/dchest/captcha"
2324 "github.com/gin-gonic/gin"
2425)
2526
2627const (
27- DEFAULT_PAGE_NUM = 1
28- DEFAULT_PAGE_SIZE = 20
28+ DefaultPageNum = 1
29+ DefaultPageSize = 20
2930)
3031
32+ // LoginPage 登录页面
3133func LoginPage (c * gin.Context ) {
3234 c .HTML (http .StatusOK , "login.html" , gin.H {
3335 "title" : "登录 - ohUrlShortener" ,
3436 })
3537}
3638
39+ // DoLogin 登录
3740func DoLogin (c * gin.Context ) {
3841 account := c .PostForm ("account" )
3942 password := c .PostForm ("password" )
@@ -56,7 +59,7 @@ func DoLogin(c *gin.Context) {
5659 return
5760 }
5861
59- //验证码有效性验证
62+ // 验证码有效性验证
6063 if ! captcha .VerifyString (captchaId , captchaText ) {
6164 c .HTML (http .StatusOK , "login.html" , gin.H {
6265 "title" : "错误 - ohUrlShortener" ,
@@ -65,7 +68,7 @@ func DoLogin(c *gin.Context) {
6568 return
6669 }
6770
68- //用户名密码有效性验证
71+ // 用户名密码有效性验证
6972 loginUser , err := service .Login (account , password )
7073 if err != nil || loginUser .IsEmpty () {
7174 c .HTML (http .StatusOK , "login.html" , gin.H {
@@ -75,7 +78,7 @@ func DoLogin(c *gin.Context) {
7578 return
7679 }
7780
78- //Write Cookie to browser
81+ // Write Cookie to browser
7982 cValue , err := AdminCookieValue (loginUser )
8083 if err != nil {
8184 c .HTML (http .StatusOK , "login.html" , gin.H {
@@ -89,21 +92,25 @@ func DoLogin(c *gin.Context) {
8992 c .Redirect (http .StatusFound , "/admin/dashboard" )
9093}
9194
95+ // DoLogout 登出
9296func DoLogout (c * gin.Context ) {
9397 c .SetCookie ("ohUrlShortenerAdmin" , "" , - 1 , "/" , "" , false , true )
9498 c .SetCookie ("ohUrlShortenerCookie" , "" , - 1 , "/" , "" , false , true )
9599 c .Redirect (http .StatusFound , "/login" )
96100}
97101
102+ // ServeCaptchaImage 生成验证码
98103func ServeCaptchaImage (c * gin.Context ) {
99104 captcha .Server (200 , 45 ).ServeHTTP (c .Writer , c .Request )
100105}
101106
107+ // RequestCaptchaImage 请求验证码
102108func RequestCaptchaImage (c * gin.Context ) {
103109 imageId := captcha .New ()
104110 c .JSON (http .StatusOK , core .ResultJsonSuccessWithData (imageId ))
105111}
106112
113+ // ChangeState 修改状态
107114func ChangeState (c * gin.Context ) {
108115 destUrl := c .PostForm ("dest_url" )
109116 enable := c .PostForm ("enable" )
@@ -128,6 +135,7 @@ func ChangeState(c *gin.Context) {
128135 c .JSON (http .StatusOK , core .ResultJsonSuccessWithData (result ))
129136}
130137
138+ // DeleteShortUrl 删除短链接
131139func DeleteShortUrl (c * gin.Context ) {
132140 url := c .PostForm ("short_url" )
133141 if utils .EmptyString (strings .TrimSpace (url )) {
@@ -144,6 +152,7 @@ func DeleteShortUrl(c *gin.Context) {
144152 c .JSON (http .StatusOK , core .ResultJsonSuccess ())
145153}
146154
155+ // GenerateShortUrl 生成短链接
147156func GenerateShortUrl (c * gin.Context ) {
148157 destUrl := c .PostForm ("dest_url" )
149158 memo := c .PostForm ("memo" )
@@ -165,17 +174,18 @@ func GenerateShortUrl(c *gin.Context) {
165174 c .JSON (http .StatusOK , core .ResultJsonSuccessWithData (json ))
166175}
167176
177+ // StatsPage 统计页面
168178func StatsPage (c * gin.Context ) {
169179 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 ))
172182 page , err := strconv .Atoi (strPage )
173183 if err != nil {
174- page = DEFAULT_PAGE_NUM
184+ page = DefaultPageNum
175185 }
176186 size , err := strconv .Atoi (strSize )
177187 if err != nil {
178- size = DEFAULT_PAGE_SIZE
188+ size = DefaultPageSize
179189 }
180190 urls , err := service .GetPagedUrlIpCountStats (strings .TrimSpace (url ), page , size )
181191 c .HTML (http .StatusOK , "stats.html" , gin.H {
@@ -192,17 +202,18 @@ func StatsPage(c *gin.Context) {
192202 })
193203}
194204
205+ // SearchStatsPage 查询统计页面
195206func SearchStatsPage (c * gin.Context ) {
196207 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 ))
199210 page , err := strconv .Atoi (strPage )
200211 if err != nil {
201- page = DEFAULT_PAGE_NUM
212+ page = DefaultPageNum
202213 }
203214 size , err := strconv .Atoi (strSize )
204215 if err != nil {
205- size = DEFAULT_PAGE_SIZE
216+ size = DefaultPageSize
206217 }
207218 urls , err := service .GetPagedUrlIpCountStats (strings .TrimSpace (url ), page , size )
208219 c .HTML (http .StatusOK , "search_stats.html" , gin.H {
@@ -219,17 +230,18 @@ func SearchStatsPage(c *gin.Context) {
219230 })
220231}
221232
233+ // UrlsPage 短链接列表页面
222234func UrlsPage (c * gin.Context ) {
223235 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 ))
226238 page , err := strconv .Atoi (strPage )
227239 if err != nil {
228- page = DEFAULT_PAGE_NUM
240+ page = DefaultPageNum
229241 }
230242 size , err := strconv .Atoi (strSize )
231243 if err != nil {
232- size = DEFAULT_PAGE_SIZE
244+ size = DefaultPageSize
233245 }
234246 urls , err := service .GetPagesShortUrls (strings .TrimSpace (url ), page , size )
235247 c .HTML (http .StatusOK , "urls.html" , gin.H {
@@ -246,19 +258,20 @@ func UrlsPage(c *gin.Context) {
246258 })
247259}
248260
261+ // AccessLogsPage 访问日志页面
249262func AccessLogsPage (c * gin.Context ) {
250263 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 ))
253266 start := c .DefaultQuery ("start" , "" )
254267 end := c .DefaultQuery ("end" , "" )
255268 page , err := strconv .Atoi (strPage )
256269 if err != nil {
257- page = DEFAULT_PAGE_NUM
270+ page = DefaultPageNum
258271 }
259272 size , err := strconv .Atoi (strSize )
260273 if err != nil {
261- size = DEFAULT_PAGE_SIZE
274+ size = DefaultPageSize
262275 }
263276
264277 totalCount , distinctIpCount , err := service .GetAccessLogsCount (strings .TrimSpace (url ), start , end )
@@ -281,6 +294,7 @@ func AccessLogsPage(c *gin.Context) {
281294 })
282295}
283296
297+ // AccessLogsExport 导出访问日志
284298func AccessLogsExport (c * gin.Context ) {
285299 url := c .PostForm ("url" )
286300 logs , err := service .GetAllAccessLogs (strings .TrimSpace (url ))
@@ -314,6 +328,7 @@ func AccessLogsExport(c *gin.Context) {
314328 c .Data (http .StatusOK , "pplication/octet-stream" , fileContent )
315329}
316330
331+ // DashboardPage 仪表盘页面
317332func DashboardPage (c * gin.Context ) {
318333 count , stats , err := service .GetSumOfUrlStats ()
319334 if err != nil {
0 commit comments