Skip to content

Commit 2399794

Browse files
committed
新增zap日志、统一配置文件、封装响应
1 parent f21cad7 commit 2399794

30 files changed

+675
-700
lines changed

api/front/article.go

+51-42
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package front
33
import (
44
"gin-blog/dao"
55
"gin-blog/models"
6-
"net/http"
6+
"gin-blog/models/response"
77
"strconv"
88

9+
"go.uber.org/zap"
10+
911
"github.com/gin-gonic/gin"
1012
)
1113

@@ -19,28 +21,29 @@ func (*Article) ArticlePageNum(c *gin.Context) {
1921
categoryIds := c.Query("categoryIds")
2022
p, err := strconv.Atoi(pageNum)
2123
if err != nil {
22-
return
24+
zap.L().Error("请求参数转换出错", zap.Error(err))
25+
}
26+
list, total, err := dao.GetFrontArticleListByPage(p, tagIds, categoryIds)
27+
if err != nil {
28+
zap.L().Error("查询文章出错", zap.Error(err))
29+
response.FailWithMessage("查询出错", c)
2330
}
24-
list, total := dao.GetFrontArticleListByPage(p, tagIds, categoryIds)
2531
pageinfo.Total = total
2632
pageinfo.PageNum = p
2733
pageinfo.PageSize = 10
2834
pageinfo.Data = list
29-
c.JSON(http.StatusOK, gin.H{
30-
"msg": "success",
31-
"data": pageinfo,
32-
})
33-
35+
response.OkWithDetailed(pageinfo, "查询成功", c)
3436
}
3537

3638
// 归档请求
3739
func (*Article) GetArticlesAll(c *gin.Context) {
38-
list := dao.GetArticlesAll()
39-
c.JSON(http.StatusOK, gin.H{
40-
"code": 200,
41-
"msg": "success",
42-
"data": list,
43-
})
40+
all, err := dao.GetArticlesAll()
41+
if err != nil {
42+
zap.L().Error("查询出错", zap.Error(err))
43+
response.FailWithMessage("查询出错", c)
44+
}
45+
response.OkWithDetailed(all, "查询成功", c)
46+
4447
}
4548

4649
// 根据id查文章细节请求
@@ -49,71 +52,77 @@ func (*Article) GetArticleDetailsById(c *gin.Context) {
4952

5053
i, err := strconv.Atoi(c.Param("id"))
5154
if err != nil {
52-
return
55+
zap.L().Error("请求参数转换出错", zap.Error(err))
56+
}
57+
articledetails, err := dao.GetArticleDetails(i)
58+
if err != nil {
59+
zap.L().Error("查询出错", zap.Error(err))
60+
response.FailWithMessage("查询出错", c)
5361
}
54-
articledetails := dao.GetArticleDetails(i)
55-
articledetails.Previous = dao.GetArticlePrevious(i)
56-
articledetails.Next = dao.GetArticleNext(i)
57-
c.JSON(http.StatusOK, gin.H{
58-
"code": 200,
59-
"msg": "success",
60-
"data": articledetails,
61-
})
62+
previous, err := dao.GetArticlePrevious(i)
63+
if err != nil {
64+
zap.L().Error("查询出错", zap.Error(err))
65+
}
66+
next, err := dao.GetArticleNext(i)
67+
if err != nil {
68+
zap.L().Error("查询出错", zap.Error(err))
69+
}
70+
articledetails.Previous = previous
71+
articledetails.Next = next
72+
response.OkWithDetailed(articledetails, "查询成功", c)
73+
6274
}
6375

6476
func (*Article) GetArticleComments(c *gin.Context) {
6577
var pageinfo models.PageInfo
6678
i, err := strconv.Atoi(c.Param("id"))
6779
if err != nil {
68-
return
80+
zap.L().Error("请求参数转换出错", zap.Error(err))
6981
}
7082
pageNum := c.Query("pageNum")
7183
p, err := strconv.Atoi(pageNum)
7284
if err != nil {
73-
return
85+
zap.L().Error("请求参数转换出错", zap.Error(err))
86+
}
87+
totle, commentslist, err := dao.GetArticleCommentsById(i, p)
88+
if err != nil {
89+
zap.L().Error("查询出错", zap.Error(err))
90+
response.FailWithMessage("查询出错", c)
7491
}
75-
totle, commentslist := dao.GetArticleCommentsById(i, p)
7692
pageinfo.Total = totle
7793
pageinfo.PageNum = p
7894
pageinfo.PageSize = 10
7995
pageinfo.Data = commentslist
80-
c.JSON(http.StatusOK, gin.H{
81-
"code": 200,
82-
"msg": "success",
83-
"data": pageinfo,
84-
})
96+
response.OkWithDetailed(pageinfo, "查询成功", c)
97+
8598
}
8699

87100
func (*Article) SearchArticle(c *gin.Context) {
88101
var pageNum, pageSize int
89102
var pageinfo models.PageInfo
90103
p := c.Query("pageNum")
91-
92104
if p == "" {
93105
pageNum = 1
94106
pageSize = 5
95107
} else {
96108
pageSize = 10
97109
pn, err := strconv.Atoi(p)
98110
if err != nil {
99-
return
111+
zap.L().Error("请求参数转换出错", zap.Error(err))
100112
}
101113
pageNum = pn
102-
103114
}
104-
105115
queryString := c.Query("queryString")
106116

107-
list, i := dao.Search(pageNum, pageSize, queryString)
108-
117+
list, i, err := dao.Search(pageNum, pageSize, queryString)
118+
if err != nil {
119+
zap.L().Error("查询出错", zap.Error(err))
120+
response.FailWithMessage("查询出错", c)
121+
}
109122
pageinfo.Total = i
110123
pageinfo.PageNum = pageNum
111124
pageinfo.PageSize = pageSize
112125
pageinfo.Data = list
113-
c.JSON(http.StatusOK, gin.H{
114-
"code": 200,
115-
"msg": "success",
116-
"data": pageinfo,
117-
})
126+
response.OkWithDetailed(pageinfo, "查询成功", c)
118127

119128
}

api/front/comment.go

+15-28
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,41 @@ package front
33
import (
44
"gin-blog/dao"
55
"gin-blog/models"
6-
"net/http"
6+
"gin-blog/models/response"
77
"strconv"
88

9+
"go.uber.org/zap"
10+
911
"github.com/gin-gonic/gin"
1012
)
1113

1214
type Comment struct{}
1315

1416
func (*Comment) Save(c *gin.Context) {
15-
1617
var commentreq models.CommentReq
1718
err := c.ShouldBindJSON(&commentreq)
1819
if err != nil {
19-
c.JSON(http.StatusBadRequest, gin.H{
20-
"error": err.Error(),
21-
})
20+
zap.L().Error("请求参数绑定出错", zap.Error(err))
2221
}
23-
i := dao.SaveComment(commentreq)
24-
if i > 0 {
25-
c.JSON(http.StatusOK, gin.H{
26-
"code": 200,
27-
"message": "success",
28-
})
29-
} else {
30-
c.JSON(http.StatusInternalServerError, gin.H{
31-
"code": 500,
32-
"message": "fail",
33-
})
22+
err = dao.SaveComment(commentreq)
23+
if err != nil {
24+
zap.L().Error("保存评论出错了", zap.Error(err))
25+
response.FailWithMessage("保存评论出错了", c)
3426
}
27+
response.OkWithMessage("保存评论成功", c)
3528

3629
}
3730

3831
func (*Comment) DelectComment(c *gin.Context) {
3932
id, err := strconv.Atoi(c.Param("id"))
4033
if err != nil {
41-
return
34+
zap.L().Error("请求参数绑定出错", zap.Error(err))
4235
}
43-
i := dao.DelectCommentById(id)
44-
if i > 0 {
45-
c.JSON(http.StatusOK, gin.H{
46-
"code": 200,
47-
"message": "success",
48-
})
49-
} else {
50-
c.JSON(http.StatusInternalServerError, gin.H{
51-
"code": 500,
52-
"message": "fail",
53-
})
36+
err = dao.DelectCommentById(id)
37+
if err != nil {
38+
zap.L().Error("删除评论出错", zap.Error(err))
39+
response.FailWithMessage("删除评论出错", c)
5440
}
41+
response.OkWithMessage("删除成功", c)
5542

5643
}

api/front/oauth.go

+13-31
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import (
44
"gin-blog/config"
55
"gin-blog/dao"
66
"gin-blog/models"
7+
"gin-blog/models/response"
78
"gin-blog/utils"
9+
"gin-blog/utils/mytime"
810
"net/http"
911
"strconv"
1012
"time"
1113

14+
"go.uber.org/zap"
15+
1216
"github.com/gin-gonic/gin"
1317
)
1418

@@ -26,30 +30,19 @@ func (*Oauth) Login(c *gin.Context) {
2630
"message": "success",
2731
"authorizeUrl": url,
2832
})
29-
// c.Redirect(302, url)
33+
3034
}
3135
func (*Oauth) GithubLogincallback(c *gin.Context) {
32-
33-
// code := c.Query("code")
34-
// token, err := config.Exchange(oauth2.NoContext, code)
35-
// if err != nil {
36-
// c.AbortWithError(http.StatusBadRequest, err)
37-
// return
38-
// }
39-
40-
// c.JSON(http.StatusOK, gin.H{
41-
// "access_token": token.AccessToken,
42-
// })
4336
code := c.Query("code")
4437
authinfo := config.Setup()
4538
tok, err := config.GetToken(c, authinfo, code)
4639
if err != nil {
47-
panic(err)
40+
zap.L().Error("获取token出错", zap.Error(err))
4841
}
4942
client := authinfo.Client(c, tok)
5043
u, err := config.GetUsers(client)
5144
if err != nil {
52-
panic(err)
45+
zap.L().Error("获取第三方信息出错", zap.Error(err))
5346
}
5447

5548
var userinfo models.User
@@ -59,31 +52,20 @@ func (*Oauth) GithubLogincallback(c *gin.Context) {
5952
userinfo.AvatarUrl = u.AvatarURL
6053
userinfo.Email = u.Email
6154
userinfo.Role = "1"
62-
userinfo.LastLogin = models.MyTime(time.Now())
55+
userinfo.LastLogin = mytime.MyTime(time.Now())
6356

6457
err = dao.CreatedOrUpdate(&userinfo)
6558
if err != nil {
66-
c.JSON(http.StatusInternalServerError, gin.H{
67-
"code": 500000,
68-
"message": "fail",
69-
})
59+
zap.L().Error("创建或更新用户出错", zap.Error(err))
60+
response.FailWithMessage("fail", c)
7061
}
71-
// database.DB.Where("social_source=? and social_user_id=?", userinfo.SocialSource, userinfo.SocialUserId).First(&userinfo)
72-
// if userinfo == {
7362
token, err := utils.GetToken(userinfo)
74-
// }
7563
if err != nil {
76-
c.JSON(http.StatusInternalServerError, gin.H{
77-
"code": 500000,
78-
"message": "fail",
79-
})
64+
zap.L().Error("创建token出错", zap.Error(err))
65+
response.FailWithMessage("fail", c)
8066
}
8167
c.Header("AUTHORIZATION", "Bearer "+token)
8268

83-
// fmt.Println(token)
84-
c.JSON(http.StatusOK, gin.H{
85-
"code": 200000,
86-
"message": "success",
87-
})
69+
response.OkWithMessage("success", c)
8870

8971
}

0 commit comments

Comments
 (0)