106 lines
3.0 KiB
Go
106 lines
3.0 KiB
Go
package user
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"gitea.com/bitwsd/document_ai/pkg/log"
|
|
"gitea.com/bitwsd/document_ai/config"
|
|
model "gitea.com/bitwsd/document_ai/internal/model/user"
|
|
"gitea.com/bitwsd/document_ai/internal/service"
|
|
"gitea.com/bitwsd/document_ai/pkg/common"
|
|
"gitea.com/bitwsd/document_ai/pkg/constant"
|
|
"gitea.com/bitwsd/document_ai/pkg/jwt"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type UserEndpoint struct {
|
|
userService *service.UserService
|
|
}
|
|
|
|
func NewUserEndpoint() *UserEndpoint {
|
|
return &UserEndpoint{
|
|
userService: service.NewUserService(),
|
|
}
|
|
}
|
|
|
|
func (h *UserEndpoint) SendVerificationCode(ctx *gin.Context) {
|
|
req := model.SmsSendRequest{}
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeParamError, common.CodeParamErrorMsg))
|
|
return
|
|
}
|
|
|
|
code, err := h.userService.GetSmsCode(ctx, req.Phone)
|
|
if err != nil {
|
|
log.Error(ctx, "func", "SendVerificationCode", "msg", "发送验证码失败", "error", err)
|
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeSystemError, common.CodeSystemErrorMsg))
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, common.SuccessResponse(ctx, model.SmsSendResponse{Code: code}))
|
|
}
|
|
|
|
func (h *UserEndpoint) LoginByPhoneCode(ctx *gin.Context) {
|
|
req := model.PhoneLoginRequest{}
|
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeParamError, common.CodeParamErrorMsg))
|
|
return
|
|
}
|
|
|
|
if req.Code == "" || req.Phone == "" {
|
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeParamError, common.CodeParamErrorMsg))
|
|
return
|
|
}
|
|
|
|
if config.GlobalConfig.Server.IsDebug() {
|
|
uid := 1
|
|
token, err := jwt.CreateToken(jwt.User{UserId: int64(uid)})
|
|
if err != nil {
|
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeUnauthorized, common.CodeUnauthorizedMsg))
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusOK, common.SuccessResponse(ctx, model.PhoneLoginResponse{Token: token}))
|
|
return
|
|
}
|
|
|
|
uid, err := h.userService.VerifySmsCode(ctx, req.Phone, req.Code)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeUnauthorized, common.CodeSmsCodeErrorMsg))
|
|
return
|
|
}
|
|
|
|
token, err := jwt.CreateToken(jwt.User{UserId: uid})
|
|
if err != nil {
|
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeUnauthorized, common.CodeUnauthorizedMsg))
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, common.SuccessResponse(ctx, model.PhoneLoginResponse{Token: token}))
|
|
}
|
|
|
|
func (h *UserEndpoint) GetUserInfo(ctx *gin.Context) {
|
|
uid := ctx.GetInt64(constant.ContextUserID)
|
|
if uid == 0 {
|
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeUnauthorized, common.CodeUnauthorizedMsg))
|
|
return
|
|
}
|
|
|
|
user, err := h.userService.GetUserInfo(ctx, uid)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeSystemError, common.CodeSystemErrorMsg))
|
|
return
|
|
}
|
|
|
|
status := 0
|
|
if user.ID > 0 {
|
|
status = 1
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, common.SuccessResponse(ctx, model.UserInfoResponse{
|
|
Username: user.Username,
|
|
Phone: user.Phone,
|
|
Status: status,
|
|
}))
|
|
}
|