2022-09-27 17:59:05 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/handler"
|
|
|
|
"github.com/answerdev/answer/internal/base/middleware"
|
|
|
|
"github.com/answerdev/answer/internal/base/reason"
|
|
|
|
"github.com/answerdev/answer/internal/base/translator"
|
2022-11-17 18:59:09 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/validator"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/schema"
|
|
|
|
"github.com/answerdev/answer/internal/service"
|
|
|
|
"github.com/answerdev/answer/internal/service/action"
|
|
|
|
"github.com/answerdev/answer/internal/service/auth"
|
|
|
|
"github.com/answerdev/answer/internal/service/export"
|
2022-12-07 15:21:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/service/siteinfo_common"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/service/uploader"
|
2022-09-27 17:59:05 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/segmentfault/pacman/errors"
|
2022-12-16 16:35:55 +08:00
|
|
|
"github.com/segmentfault/pacman/log"
|
2022-09-27 17:59:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// UserController user controller
|
|
|
|
type UserController struct {
|
2022-12-07 15:21:05 +08:00
|
|
|
userService *service.UserService
|
|
|
|
authService *auth.AuthService
|
|
|
|
actionService *action.CaptchaService
|
|
|
|
uploaderService *uploader.UploaderService
|
|
|
|
emailService *export.EmailService
|
|
|
|
siteInfoCommonService *siteinfo_common.SiteInfoCommonService
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUserController new controller
|
|
|
|
func NewUserController(
|
|
|
|
authService *auth.AuthService,
|
|
|
|
userService *service.UserService,
|
|
|
|
actionService *action.CaptchaService,
|
|
|
|
emailService *export.EmailService,
|
2022-11-01 15:25:44 +08:00
|
|
|
uploaderService *uploader.UploaderService,
|
2022-12-07 15:21:05 +08:00
|
|
|
siteInfoCommonService *siteinfo_common.SiteInfoCommonService,
|
2022-11-01 15:25:44 +08:00
|
|
|
) *UserController {
|
2022-09-27 17:59:05 +08:00
|
|
|
return &UserController{
|
2022-12-07 15:21:05 +08:00
|
|
|
authService: authService,
|
|
|
|
userService: userService,
|
|
|
|
actionService: actionService,
|
|
|
|
uploaderService: uploaderService,
|
|
|
|
emailService: emailService,
|
|
|
|
siteInfoCommonService: siteInfoCommonService,
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:13:49 +08:00
|
|
|
// GetUserInfoByUserID get user info, if user no login response http code is 200, but user info is null
|
2022-09-27 17:59:05 +08:00
|
|
|
// @Summary GetUserInfoByUserID
|
2022-10-28 17:13:49 +08:00
|
|
|
// @Description get user info, if user no login response http code is 200, but user info is null
|
2022-09-27 17:59:05 +08:00
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
2022-10-28 15:46:05 +08:00
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.GetUserToSetShowResp}
|
2022-09-27 17:59:05 +08:00
|
|
|
// @Router /answer/api/v1/user/info [get]
|
|
|
|
func (uc *UserController) GetUserInfoByUserID(ctx *gin.Context) {
|
|
|
|
token := middleware.ExtractToken(ctx)
|
2022-12-13 12:11:29 +08:00
|
|
|
if len(token) == 0 {
|
|
|
|
handler.HandleResponse(ctx, nil, nil)
|
|
|
|
return
|
|
|
|
}
|
2022-10-28 17:13:49 +08:00
|
|
|
|
|
|
|
// if user is no login return null in data
|
2022-12-13 12:11:29 +08:00
|
|
|
userInfo, _ := uc.authService.GetUserCacheInfo(ctx, token)
|
|
|
|
if userInfo == nil {
|
2022-10-28 17:13:49 +08:00
|
|
|
handler.HandleResponse(ctx, nil, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-13 12:11:29 +08:00
|
|
|
resp, err := uc.userService.GetUserInfoByUserID(ctx, token, userInfo.UserID)
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOtherUserInfoByUsername godoc
|
|
|
|
// @Summary GetOtherUserInfoByUsername
|
|
|
|
// @Description GetOtherUserInfoByUsername
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param username query string true "username"
|
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.GetOtherUserInfoResp}
|
|
|
|
// @Router /answer/api/v1/personal/user/info [get]
|
|
|
|
func (uc *UserController) GetOtherUserInfoByUsername(ctx *gin.Context) {
|
|
|
|
req := &schema.GetOtherUserInfoByUsernameReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := uc.userService.GetOtherUserInfoByUsername(ctx, req.Username)
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserEmailLogin godoc
|
|
|
|
// @Summary UserEmailLogin
|
|
|
|
// @Description UserEmailLogin
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param data body schema.UserEmailLogin true "UserEmailLogin"
|
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.GetUserResp}
|
|
|
|
// @Router /answer/api/v1/user/login/email [post]
|
|
|
|
func (uc *UserController) UserEmailLogin(ctx *gin.Context) {
|
|
|
|
req := &schema.UserEmailLogin{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 19:28:08 +08:00
|
|
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
2022-09-27 17:59:05 +08:00
|
|
|
if !captchaPass {
|
2022-11-17 18:59:09 +08:00
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "captcha_code",
|
2023-01-06 18:00:51 +08:00
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
2022-11-17 18:59:09 +08:00
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := uc.userService.EmailLogin(ctx, req)
|
|
|
|
if err != nil {
|
2022-10-28 19:28:08 +08:00
|
|
|
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
2022-11-17 18:59:09 +08:00
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "e_mail",
|
2023-01-06 18:00:51 +08:00
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.EmailOrPasswordWrong),
|
2022-11-17 18:59:09 +08:00
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.EmailOrPasswordWrong), errFields)
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
2022-10-28 19:28:08 +08:00
|
|
|
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, nil, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetrievePassWord godoc
|
|
|
|
// @Summary RetrievePassWord
|
|
|
|
// @Description RetrievePassWord
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param data body schema.UserRetrievePassWordRequest true "UserRetrievePassWordRequest"
|
|
|
|
// @Success 200 {string} string ""
|
|
|
|
// @Router /answer/api/v1/user/password/reset [post]
|
|
|
|
func (uc *UserController) RetrievePassWord(ctx *gin.Context) {
|
|
|
|
req := &schema.UserRetrievePassWordRequest{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
2022-10-28 19:28:08 +08:00
|
|
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
2022-09-27 17:59:05 +08:00
|
|
|
if !captchaPass {
|
2022-11-17 18:59:09 +08:00
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "captcha_code",
|
2023-01-06 18:00:51 +08:00
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
2022-11-17 18:59:09 +08:00
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
2022-10-28 19:28:08 +08:00
|
|
|
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP())
|
2023-02-23 11:55:40 +08:00
|
|
|
err := uc.userService.RetrievePassWord(ctx, req)
|
2023-01-31 10:04:52 +08:00
|
|
|
handler.HandleResponse(ctx, err, nil)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UseRePassWord godoc
|
|
|
|
// @Summary UseRePassWord
|
|
|
|
// @Description UseRePassWord
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param data body schema.UserRePassWordRequest true "UserRePassWordRequest"
|
|
|
|
// @Success 200 {string} string ""
|
|
|
|
// @Router /answer/api/v1/user/password/replacement [post]
|
|
|
|
func (uc *UserController) UseRePassWord(ctx *gin.Context) {
|
|
|
|
req := &schema.UserRePassWordRequest{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Content = uc.emailService.VerifyUrlExpired(ctx, req.Code)
|
|
|
|
if len(req.Content) == 0 {
|
2022-11-01 15:25:44 +08:00
|
|
|
handler.HandleResponse(ctx, errors.Forbidden(reason.EmailVerifyURLExpired),
|
|
|
|
&schema.ForbiddenResp{Type: schema.ForbiddenReasonTypeURLExpired})
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 19:28:08 +08:00
|
|
|
resp, err := uc.userService.UseRePassword(ctx, req)
|
|
|
|
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP())
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserLogout user logout
|
|
|
|
// @Summary user logout
|
|
|
|
// @Description user logout
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/api/v1/user/logout [get]
|
|
|
|
func (uc *UserController) UserLogout(ctx *gin.Context) {
|
|
|
|
accessToken := middleware.ExtractToken(ctx)
|
2022-12-16 17:19:04 +08:00
|
|
|
if len(accessToken) == 0 {
|
|
|
|
handler.HandleResponse(ctx, nil, nil)
|
|
|
|
return
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
_ = uc.authService.RemoveUserCacheInfo(ctx, accessToken)
|
|
|
|
handler.HandleResponse(ctx, nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserRegisterByEmail godoc
|
|
|
|
// @Summary UserRegisterByEmail
|
|
|
|
// @Description UserRegisterByEmail
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2022-10-14 17:01:06 +08:00
|
|
|
// @Param data body schema.UserRegisterReq true "UserRegisterReq"
|
2022-09-27 17:59:05 +08:00
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.GetUserResp}
|
|
|
|
// @Router /answer/api/v1/user/register/email [post]
|
|
|
|
func (uc *UserController) UserRegisterByEmail(ctx *gin.Context) {
|
2022-12-07 15:21:05 +08:00
|
|
|
// check whether site allow register or not
|
|
|
|
siteInfo, err := uc.siteInfoCommonService.GetSiteLogin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !siteInfo.AllowNewRegistrations {
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.NotAllowedRegistration), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:01:06 +08:00
|
|
|
req := &schema.UserRegisterReq{}
|
2022-09-27 17:59:05 +08:00
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.IP = ctx.ClientIP()
|
2022-12-18 00:14:50 +08:00
|
|
|
captchaPass := uc.actionService.UserRegisterVerifyCaptcha(ctx, req.CaptchaID, req.CaptchaCode)
|
|
|
|
if !captchaPass {
|
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "captcha_code",
|
2023-01-06 18:00:51 +08:00
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
2022-12-18 00:14:50 +08:00
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
|
|
|
return
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
|
2022-12-29 14:49:40 +08:00
|
|
|
resp, errFields, err := uc.userService.UserRegisterByEmail(ctx, req)
|
|
|
|
if len(errFields) > 0 {
|
|
|
|
for _, field := range errFields {
|
2023-01-06 18:00:51 +08:00
|
|
|
field.ErrorMsg = translator.
|
|
|
|
Tr(handler.GetLang(ctx), field.ErrorMsg)
|
2022-12-29 14:49:40 +08:00
|
|
|
}
|
|
|
|
handler.HandleResponse(ctx, err, errFields)
|
|
|
|
} else {
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UserVerifyEmail godoc
|
|
|
|
// @Summary UserVerifyEmail
|
|
|
|
// @Description UserVerifyEmail
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param code query string true "code" default()
|
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.GetUserResp}
|
|
|
|
// @Router /answer/api/v1/user/email/verification [post]
|
|
|
|
func (uc *UserController) UserVerifyEmail(ctx *gin.Context) {
|
|
|
|
req := &schema.UserVerifyEmailReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Content = uc.emailService.VerifyUrlExpired(ctx, req.Code)
|
|
|
|
if len(req.Content) == 0 {
|
2022-11-01 15:25:44 +08:00
|
|
|
handler.HandleResponse(ctx, errors.Forbidden(reason.EmailVerifyURLExpired),
|
|
|
|
&schema.ForbiddenResp{Type: schema.ForbiddenReasonTypeURLExpired})
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := uc.userService.UserVerifyEmail(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 19:28:08 +08:00
|
|
|
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserVerifyEmailSend godoc
|
|
|
|
// @Summary UserVerifyEmailSend
|
|
|
|
// @Description UserVerifyEmailSend
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param captcha_id query string false "captcha_id" default()
|
|
|
|
// @Param captcha_code query string false "captcha_code" default()
|
|
|
|
// @Success 200 {string} string ""
|
|
|
|
// @Router /answer/api/v1/user/email/verification/send [post]
|
|
|
|
func (uc *UserController) UserVerifyEmailSend(ctx *gin.Context) {
|
|
|
|
req := &schema.UserVerifyEmailSendReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userInfo := middleware.GetUserInfoFromContext(ctx)
|
|
|
|
if userInfo == nil {
|
|
|
|
handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 19:28:08 +08:00
|
|
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(),
|
2022-09-27 17:59:05 +08:00
|
|
|
req.CaptchaID, req.CaptchaCode)
|
|
|
|
if !captchaPass {
|
2022-11-17 18:59:09 +08:00
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "captcha_code",
|
2023-01-06 18:00:51 +08:00
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
2022-11-17 18:59:09 +08:00
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
2022-12-16 16:35:55 +08:00
|
|
|
_, err := uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
err = uc.userService.UserVerifyEmailSend(ctx, userInfo.UserID)
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserModifyPassWord godoc
|
|
|
|
// @Summary UserModifyPassWord
|
|
|
|
// @Description UserModifyPassWord
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param data body schema.UserModifyPassWordRequest true "UserModifyPassWordRequest"
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/api/v1/user/password [put]
|
|
|
|
func (uc *UserController) UserModifyPassWord(ctx *gin.Context) {
|
|
|
|
req := &schema.UserModifyPassWordRequest{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
2022-11-01 15:25:44 +08:00
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
oldPassVerification, err := uc.userService.UserModifyPassWordVerification(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !oldPassVerification {
|
2022-11-17 18:59:09 +08:00
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "old_pass",
|
2023-01-06 18:00:51 +08:00
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.OldPasswordVerificationFailed),
|
2022-11-17 18:59:09 +08:00
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.OldPasswordVerificationFailed), errFields)
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.OldPass == req.Pass {
|
2022-11-17 18:59:09 +08:00
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "pass",
|
2023-01-06 18:00:51 +08:00
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.NewPasswordSameAsPreviousSetting),
|
2022-11-17 18:59:09 +08:00
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.NewPasswordSameAsPreviousSetting), errFields)
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
2022-10-28 19:28:08 +08:00
|
|
|
err = uc.userService.UserModifyPassword(ctx, req)
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|
|
|
|
|
2022-09-29 16:39:34 +08:00
|
|
|
// UserUpdateInfo update user info
|
|
|
|
// @Summary UserUpdateInfo update user info
|
|
|
|
// @Description UserUpdateInfo update user info
|
2022-09-27 17:59:05 +08:00
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param Authorization header string true "access-token"
|
2022-09-29 16:39:34 +08:00
|
|
|
// @Param data body schema.UpdateInfoRequest true "UpdateInfoRequest"
|
2022-09-27 17:59:05 +08:00
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/api/v1/user/info [put]
|
|
|
|
func (uc *UserController) UserUpdateInfo(ctx *gin.Context) {
|
|
|
|
req := &schema.UpdateInfoRequest{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
2022-11-01 15:25:44 +08:00
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2022-12-30 10:51:54 +08:00
|
|
|
errFields, err := uc.userService.UpdateInfo(ctx, req)
|
|
|
|
for _, field := range errFields {
|
2023-01-06 18:00:51 +08:00
|
|
|
field.ErrorMsg = translator.Tr(handler.GetLang(ctx), field.ErrorMsg)
|
2022-12-30 10:51:54 +08:00
|
|
|
}
|
|
|
|
handler.HandleResponse(ctx, err, errFields)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
2022-11-02 15:25:27 +08:00
|
|
|
// UserUpdateInterface update user interface config
|
|
|
|
// @Summary UserUpdateInterface update user interface config
|
|
|
|
// @Description UserUpdateInterface update user interface config
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param Authorization header string true "access-token"
|
|
|
|
// @Param data body schema.UpdateUserInterfaceRequest true "UpdateInfoRequest"
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/api/v1/user/interface [put]
|
|
|
|
func (uc *UserController) UserUpdateInterface(ctx *gin.Context) {
|
|
|
|
req := &schema.UpdateUserInterfaceRequest{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.UserId = middleware.GetLoginUserIDFromContext(ctx)
|
|
|
|
err := uc.userService.UserUpdateInterface(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
// ActionRecord godoc
|
|
|
|
// @Summary ActionRecord
|
|
|
|
// @Description ActionRecord
|
|
|
|
// @Tags User
|
|
|
|
// @Param action query string true "action" Enums(login, e_mail, find_pass)
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.ActionRecordResp}
|
|
|
|
// @Router /answer/api/v1/user/action/record [get]
|
|
|
|
func (uc *UserController) ActionRecord(ctx *gin.Context) {
|
|
|
|
req := &schema.ActionRecordReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
2022-11-01 15:25:44 +08:00
|
|
|
req.IP = ctx.ClientIP()
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
resp, err := uc.actionService.ActionRecord(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
2022-12-18 00:14:50 +08:00
|
|
|
// UserRegisterCaptcha godoc
|
|
|
|
// @Summary UserRegisterCaptcha
|
|
|
|
// @Description UserRegisterCaptcha
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.GetUserResp}
|
|
|
|
// @Router /answer/api/v1/user/register/captcha [get]
|
|
|
|
func (uc *UserController) UserRegisterCaptcha(ctx *gin.Context) {
|
|
|
|
resp, err := uc.actionService.UserRegisterCaptcha(ctx)
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
// UserNoticeSet godoc
|
|
|
|
// @Summary UserNoticeSet
|
|
|
|
// @Description UserNoticeSet
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param data body schema.UserNoticeSetRequest true "UserNoticeSetRequest"
|
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.UserNoticeSetResp}
|
|
|
|
// @Router /answer/api/v1/user/notice/set [post]
|
|
|
|
func (uc *UserController) UserNoticeSet(ctx *gin.Context) {
|
|
|
|
req := &schema.UserNoticeSetRequest{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:25:44 +08:00
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
|
|
|
resp, err := uc.userService.UserNoticeSet(ctx, req.UserID, req.NoticeSwitch)
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserChangeEmailSendCode send email to the user email then change their email
|
|
|
|
// @Summary send email to the user email then change their email
|
|
|
|
// @Description send email to the user email then change their email
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param data body schema.UserChangeEmailSendCodeReq true "UserChangeEmailSendCodeReq"
|
|
|
|
// @Success 200 {object} handler.RespBody{}
|
|
|
|
// @Router /answer/api/v1/user/email/change/code [post]
|
|
|
|
func (uc *UserController) UserChangeEmailSendCode(ctx *gin.Context) {
|
|
|
|
req := &schema.UserChangeEmailSendCodeReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2022-10-28 14:42:27 +08:00
|
|
|
// If the user is not logged in, the api cannot be used.
|
|
|
|
// If the user email is not verified, that also can use this api to modify the email.
|
2022-11-09 14:59:07 +08:00
|
|
|
if len(req.UserID) == 0 {
|
|
|
|
handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil)
|
|
|
|
return
|
|
|
|
}
|
2022-10-28 15:07:19 +08:00
|
|
|
|
2022-11-01 15:38:06 +08:00
|
|
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
2022-10-28 15:07:19 +08:00
|
|
|
if !captchaPass {
|
2022-11-17 18:59:09 +08:00
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "captcha_code",
|
2023-01-06 18:00:51 +08:00
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
2022-11-17 18:59:09 +08:00
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
2022-10-28 15:07:19 +08:00
|
|
|
return
|
|
|
|
}
|
2022-11-09 14:59:07 +08:00
|
|
|
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
|
|
|
|
resp, err := uc.userService.UserChangeEmailSendCode(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
2022-10-28 14:42:27 +08:00
|
|
|
return
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserChangeEmailVerify user change email verification
|
|
|
|
// @Summary user change email verification
|
|
|
|
// @Description user change email verification
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param data body schema.UserChangeEmailVerifyReq true "UserChangeEmailVerifyReq"
|
|
|
|
// @Success 200 {object} handler.RespBody{}
|
|
|
|
// @Router /answer/api/v1/user/email [put]
|
|
|
|
func (uc *UserController) UserChangeEmailVerify(ctx *gin.Context) {
|
|
|
|
req := &schema.UserChangeEmailVerifyReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Content = uc.emailService.VerifyUrlExpired(ctx, req.Code)
|
|
|
|
if len(req.Content) == 0 {
|
2022-11-01 15:25:44 +08:00
|
|
|
handler.HandleResponse(ctx, errors.Forbidden(reason.EmailVerifyURLExpired),
|
|
|
|
&schema.ForbiddenResp{Type: schema.ForbiddenReasonTypeURLExpired})
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := uc.userService.UserChangeEmailVerify(ctx, req.Content)
|
2022-11-01 15:38:06 +08:00
|
|
|
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|
2022-12-10 17:12:31 +08:00
|
|
|
|
|
|
|
// UserRanking get user ranking
|
|
|
|
// @Summary get user ranking
|
|
|
|
// @Description get user ranking
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
2022-12-12 16:54:04 +08:00
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.UserRankingResp}
|
2022-12-10 17:12:31 +08:00
|
|
|
// @Router /answer/api/v1/user/ranking [get]
|
|
|
|
func (uc *UserController) UserRanking(ctx *gin.Context) {
|
|
|
|
resp, err := uc.userService.UserRanking(ctx)
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
2022-12-27 17:42:23 +08:00
|
|
|
|
|
|
|
// UserUnsubscribeEmailNotification unsubscribe email notification
|
|
|
|
// @Summary unsubscribe email notification
|
|
|
|
// @Description unsubscribe email notification
|
|
|
|
// @Tags User
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} handler.RespBody{}
|
|
|
|
// @Router /answer/api/v1/user/email/notification [put]
|
|
|
|
func (uc *UserController) UserUnsubscribeEmailNotification(ctx *gin.Context) {
|
|
|
|
req := &schema.UserUnsubscribeEmailNotificationReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Content = uc.emailService.VerifyUrlExpired(ctx, req.Code)
|
|
|
|
if len(req.Content) == 0 {
|
|
|
|
handler.HandleResponse(ctx, errors.Forbidden(reason.EmailVerifyURLExpired),
|
|
|
|
&schema.ForbiddenResp{Type: schema.ForbiddenReasonTypeURLExpired})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := uc.userService.UserUnsubscribeEmailNotification(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|