Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: user info api allow no login user use and return empty data

LinkinStar9c1df37e82c7
diff --git a/docs/docs.go b/docs/docs.go
index af029e38..81ccb1a7 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -3303,7 +3303,7 @@ const docTemplate = `{
"ApiKeyAuth": []
}
],
- "description": "GetUserInfoByUserID",
+ "description": "get user info, if user no login response http code is 200, but user info is null",
"consumes": [
"application/json"
],
@@ -5628,6 +5628,14 @@ const docTemplate = `{
"e_mail"
],
"properties": {
+ "captcha_code": {
+ "type": "string",
+ "maxLength": 500
+ },
+ "captcha_id": {
+ "type": "string",
+ "maxLength": 500
+ },
"e_mail": {
"type": "string",
"maxLength": 500
diff --git a/docs/swagger.json b/docs/swagger.json
index d2273f9e..c92a6b1e 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -3291,7 +3291,7 @@
"ApiKeyAuth": []
}
],
- "description": "GetUserInfoByUserID",
+ "description": "get user info, if user no login response http code is 200, but user info is null",
"consumes": [
"application/json"
],
@@ -5616,6 +5616,14 @@
"e_mail"
],
"properties": {
+ "captcha_code": {
+ "type": "string",
+ "maxLength": 500
+ },
+ "captcha_id": {
+ "type": "string",
+ "maxLength": 500
+ },
"e_mail": {
"type": "string",
"maxLength": 500
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 66991ad2..5a261821 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -1241,6 +1241,12 @@ definitions:
type: object
schema.UserChangeEmailSendCodeReq:
properties:
+ captcha_code:
+ maxLength: 500
+ type: string
+ captcha_id:
+ maxLength: 500
+ type: string
e_mail:
maxLength: 500
type: string
@@ -3365,7 +3371,8 @@ paths:
get:
consumes:
- application/json
- description: GetUserInfoByUserID
+ description: get user info, if user no login response http code is 200, but
+ user info is null
produces:
- application/json
responses:
diff --git a/internal/controller/user_controller.go b/internal/controller/user_controller.go
index 80d040f2..6fd2abf0 100644
--- a/internal/controller/user_controller.go
+++ b/internal/controller/user_controller.go
@@ -45,9 +45,9 @@ func NewUserController(
}
}
-// GetUserInfoByUserID godoc
+// GetUserInfoByUserID get user info, if user no login response http code is 200, but user info is null
// @Summary GetUserInfoByUserID
-// @Description GetUserInfoByUserID
+// @Description get user info, if user no login response http code is 200, but user info is null
// @Tags User
// @Accept json
// @Produce json
@@ -57,6 +57,13 @@ func NewUserController(
func (uc *UserController) GetUserInfoByUserID(ctx *gin.Context) {
userID := middleware.GetLoginUserIDFromContext(ctx)
token := middleware.ExtractToken(ctx)
+
+ // if user is no login return null in data
+ if len(token) == 0 || len(userID) == 0 {
+ handler.HandleResponse(ctx, nil, nil)
+ return
+ }
+
resp, err := uc.userService.GetUserInfoByUserID(ctx, token, userID)
handler.HandleResponse(ctx, err, resp)
}
diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go
index 781f92d2..56a972a8 100644
--- a/internal/router/answer_api_router.go
+++ b/internal/router/answer_api_router.go
@@ -87,6 +87,7 @@ func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
r.GET("/comment", a.commentController.GetComment)
// user
+ r.GET("/user/info", a.userController.GetUserInfoByUserID)
r.GET("/user/status", a.userController.GetUserStatus)
r.GET("/user/action/record", a.userController.ActionRecord)
r.POST("/user/login/email", a.userController.UserEmailLogin)
@@ -174,7 +175,6 @@ func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {
r.DELETE("/answer", a.answerController.RemoveAnswer)
// user
- r.GET("/user/info", a.userController.GetUserInfoByUserID)
r.PUT("/user/password", a.userController.UserModifyPassWord)
r.PUT("/user/info", a.userController.UserUpdateInfo)
r.POST("/user/avatar/upload", a.userController.UploadUserAvatar)
diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go
index 56702ba0..ea37582f 100644
--- a/internal/schema/user_schema.go
+++ b/internal/schema/user_schema.go
@@ -95,12 +95,12 @@ func (r *GetUserToSetShowResp) GetFromUserEntity(userInfo *entity.User) {
if ok {
r.Status = statusShow
}
- AvatarInfo := &AvatarInfo{}
- err := json.Unmarshal([]byte(userInfo.Avatar), AvatarInfo)
+ avatarInfo := &AvatarInfo{}
+ err := json.Unmarshal([]byte(userInfo.Avatar), avatarInfo)
if err != nil {
log.Error("AvatarInfo json.Unmarshal Error", err)
}
- r.Avatar = AvatarInfo
+ r.Avatar = avatarInfo
}
func (us *GetUserResp) AvatarInfo(avatarJson string) string {