Skip to content

Commit

Permalink
fix: use ptr struct resp (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky authored Jul 22, 2024
1 parent 35e7686 commit 1cd6bb9
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/service/user/invitation.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func InvitationGen(c *gin.Context) {
return
}

resp.OKWithData(c, InvitationGenResponse{InvitationCode: codeGen})
resp.OKWithData(c, &InvitationGenResponse{InvitationCode: codeGen})
log.Logger.Infof("User %d generated invitation code_gen %s successfully!", userID, codeGen)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/service/user/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Login(c *gin.Context) {
return
}

resp.OKWithData(c, LoginResponse{
resp.OKWithData(c, &LoginResponse{
Expiration: claims.ExpiresAt.Unix(),
Token: token,
})
Expand All @@ -68,7 +68,7 @@ func TokenRefresh(c *gin.Context) {
return
}

resp.OKWithData(c, LoginResponse{
resp.OKWithData(c, &LoginResponse{
Expiration: claims.ExpiresAt.Unix(),
Token: token,
})
Expand Down
6 changes: 3 additions & 3 deletions internal/service/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ProfileMe(c *gin.Context) {
roles = []string{}
}

resp.OKWithData(c, ProfileResponse{
resp.OKWithData(c, &ProfileResponse{
Avatar: user.Avatar,
Background: user.Background,
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
Expand Down Expand Up @@ -99,7 +99,7 @@ func ProfileOthers(c *gin.Context) {
// 判断是否为隐私账号
if user.Private {
// 只显示最基础信息
resp.OKWithData(c, ProfileResponse{
resp.OKWithData(c, &ProfileResponse{
Avatar: user.Avatar,
Background: user.Background,
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
Expand All @@ -115,7 +115,7 @@ func ProfileOthers(c *gin.Context) {
})
} else {
// 显示全部信息
resp.OKWithData(c, ProfileResponse{
resp.OKWithData(c, &ProfileResponse{
Avatar: user.Avatar,
Background: user.Background,
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
Expand Down
2 changes: 1 addition & 1 deletion internal/service/user/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func Register(c *gin.Context) {
}
}

resp.OKWithData(c, RegisterDataResponse{
resp.OKWithData(c, &RegisterDataResponse{
Email: user.Email,
UserID: user.UserID,
Username: user.Username,
Expand Down
2 changes: 1 addition & 1 deletion internal/service/user/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func RoleMe(c *gin.Context) {
return
}

resp.OKWithData(c, RoleResponse{
resp.OKWithData(c, &RoleResponse{
Roles: roles,
})

Expand Down
14 changes: 7 additions & 7 deletions module/resp/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (

// OK 返回成功信息
func OK(c *gin.Context) {
resp := map[string]interface{}{
resp := &gin.H{
"success": true,
}
c.JSON(http.StatusOK, resp)
}

// OKWithData 返回成功信息,携带自定义数据(结构体)
func OKWithData(c *gin.Context, data interface{}) {
resp := map[string]interface{}{
resp := &gin.H{
"success": true,
"data": data,
}
Expand All @@ -27,11 +27,11 @@ func OKWithData(c *gin.Context, data interface{}) {

// Abort 返回错误码
func Abort(c *gin.Context, code code.Code) {
errorResp := map[string]interface{}{
errorResp := &gin.H{
"code": code,
"message": code.String(),
}
resp := map[string]interface{}{
resp := &gin.H{
"success": false,
"error": errorResp,
}
Expand All @@ -40,13 +40,13 @@ func Abort(c *gin.Context, code code.Code) {

// AbortWithMsg 返回错误码,自定义错误信息
func AbortWithMsg(c *gin.Context, code code.Code, msg string) {
errorResp := map[string]interface{}{
errorResp := &gin.H{
"code": code,
"message": code.String() + ": " + msg,
}
resp := map[string]interface{}{
resp := &gin.H{
"success": false,
"error": errorResp,
"error": &errorResp,
}
c.AbortWithStatusJSON(http.StatusOK, resp)
}

0 comments on commit 1cd6bb9

Please sign in to comment.