bbs-backend/api/controller/user_controller.go

276 lines
7.2 KiB
Go
Raw Normal View History

2024-11-19 21:08:28 +08:00
package controller
import (
"bbs-backend/api/reply"
"bbs-backend/api/request"
"bbs-backend/common/errcode"
"bbs-backend/common/response"
"bbs-backend/logic/appservice"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"io"
"net/http"
"strings"
)
func Register(c *gin.Context) {
var req request.RegisterRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, http.StatusBadRequest, errcode.ErrBadRequest)
return
}
user, err := appservice.RegisterUser(req)
if err != nil {
switch err {
case errcode.ErrInvalidUsername:
response.Error(c, http.StatusBadRequest, errcode.ErrInvalidUsername)
case errcode.ErrInvalidPassword:
response.Error(c, http.StatusBadRequest, errcode.ErrInvalidPassword)
case errcode.ErrUserAlreadyExists:
response.Error(c, http.StatusBadRequest, errcode.ErrUserAlreadyExists)
case errcode.ErrEmailExists:
response.Error(c, http.StatusBadRequest, errcode.ErrEmailExists)
default:
response.Error(c, http.StatusInternalServerError, errcode.ErrInternalServerError)
}
return
}
reply := &reply.RegisterReply{}
reply.FromModel(user)
response.Success(c, reply)
}
func Login(c *gin.Context) {
var req request.LoginRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, http.StatusBadRequest, errcode.ErrBadRequest)
return
}
token, err := appservice.LoginUser(req)
if err != nil {
switch err {
case errcode.ErrInvalidUsername:
response.Error(c, http.StatusBadRequest, errcode.ErrInvalidUsername)
case errcode.ErrInvalidPassword:
response.Error(c, http.StatusBadRequest, errcode.ErrInvalidPassword)
case errcode.ErrUserNotFound:
response.Error(c, http.StatusUnauthorized, errcode.ErrUserNotFound)
default:
response.Error(c, http.StatusUnauthorized, errcode.ErrUnauthorized)
}
return
}
response.Success(c, reply.LoginReply{Token: token})
}
func GetUserInfo(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
response.Error(c, http.StatusUnauthorized, errcode.ErrUnauthorized)
return
}
tokenString := strings.Replace(authHeader, "Bearer ", "", 1)
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte("your_secret_key"), nil
})
if err != nil {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok || !token.Valid {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
userID, ok := claims["user_id"].(float64)
if !ok {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
user, err := appservice.GetUserInfo(uint(userID))
if err != nil {
switch err {
case errcode.ErrUserNotFound:
response.Error(c, http.StatusNotFound, errcode.ErrUserNotFound)
default:
response.Error(c, http.StatusInternalServerError, errcode.ErrInternalServerError)
}
return
}
reply := &reply.UserInfoReply{}
reply.FromModel(user)
response.Success(c, reply)
}
func UpdateUserInfo(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
response.Error(c, http.StatusUnauthorized, errcode.ErrUnauthorized)
return
}
tokenString := strings.Replace(authHeader, "Bearer ", "", 1)
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte("your_secret_key"), nil
})
if err != nil {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok || !token.Valid {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
userID, ok := claims["user_id"].(float64)
if !ok {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
var req request.UpdateUserInfoRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, http.StatusBadRequest, errcode.ErrBadRequest)
return
}
err = appservice.UpdateUserInfo(uint(userID), req)
if err != nil {
response.Error(c, http.StatusInternalServerError, errcode.ErrInternalServerError)
return
}
response.Success(c, gin.H{"message": "User info updated successfully"})
}
func UploadAvatar(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
response.Error(c, http.StatusUnauthorized, errcode.ErrUnauthorized)
return
}
tokenString := strings.Replace(authHeader, "Bearer ", "", 1)
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte("your_secret_key"), nil
})
if err != nil {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok || !token.Valid {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
userID, ok := claims["user_id"].(float64)
if !ok {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
// 处理图片上传
file, err := c.FormFile("avatar")
if err != nil {
response.Error(c, http.StatusBadRequest, errcode.ErrBadRequest)
return
}
avatarFile, err := file.Open()
if err != nil {
response.Error(c, http.StatusInternalServerError, errcode.ErrInternalServerError)
return
}
defer avatarFile.Close()
avatar, err := io.ReadAll(avatarFile)
if err != nil {
response.Error(c, http.StatusInternalServerError, errcode.ErrInternalServerError)
return
}
err = appservice.UploadAvatar(uint(userID), avatar)
if err != nil {
response.Error(c, http.StatusInternalServerError, errcode.ErrInternalServerError)
return
}
response.Success(c, gin.H{"message": "Avatar uploaded successfully"})
}
func SendVerificationCode(c *gin.Context) {
var req request.SendVerificationCodeRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, http.StatusBadRequest, errcode.ErrBadRequest)
return
}
err := appservice.SendVerificationCode(req.Email)
if err != nil {
response.Error(c, http.StatusInternalServerError, errcode.ErrInternalServerError)
return
}
response.Success(c, gin.H{"message": "Verification code sent successfully"})
}
func VerifyEmail(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
response.Error(c, http.StatusUnauthorized, errcode.ErrUnauthorized)
return
}
tokenString := strings.Replace(authHeader, "Bearer ", "", 1)
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte("your_secret_key"), nil
})
if err != nil {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok || !token.Valid {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
userID, ok := claims["user_id"].(float64)
if !ok {
response.Error(c, http.StatusUnauthorized, errcode.ErrInvalidToken)
return
}
var req request.VerifyEmailRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, http.StatusBadRequest, errcode.ErrBadRequest)
return
}
err = appservice.VerifyEmail(uint(userID), req.Email, req.Code)
if err != nil {
response.Error(c, http.StatusInternalServerError, errcode.ErrInternalServerError)
return
}
response.Success(c, gin.H{"message": "Email verified successfully"})
}