88 lines
2.8 KiB
Go
88 lines
2.8 KiB
Go
|
|
package handler
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"encoding/json"
|
||
|
|
"github.com/Nanqipro/YunQue-Tech-Projects/ai_english_learning/serve/internal/service"
|
||
|
|
)
|
||
|
|
|
||
|
|
type UserHandler struct {
|
||
|
|
userService *service.UserService
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewUserHandler(userService *service.UserService) *UserHandler {
|
||
|
|
return &UserHandler{userService: userService}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 用户注册
|
||
|
|
func (h *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||
|
|
var req struct {
|
||
|
|
Username string `json:"username"`
|
||
|
|
Email string `json:"email"`
|
||
|
|
Password string `json:"password"`
|
||
|
|
}
|
||
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
|
w.WriteHeader(http.StatusBadRequest)
|
||
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "参数错误"})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
userID, err := h.userService.Register(req.Username, req.Email, req.Password)
|
||
|
|
if err != nil {
|
||
|
|
w.WriteHeader(http.StatusInternalServerError)
|
||
|
|
json.NewEncoder(w).Encode(map[string]string{"message": err.Error()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
json.NewEncoder(w).Encode(map[string]interface{}{"userID": userID, "message": "注册成功"})
|
||
|
|
}
|
||
|
|
|
||
|
|
// 用户登录
|
||
|
|
func (h *UserHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||
|
|
var req struct {
|
||
|
|
Email string `json:"email"`
|
||
|
|
Password string `json:"password"`
|
||
|
|
}
|
||
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
|
w.WriteHeader(http.StatusBadRequest)
|
||
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "参数错误"})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
token, userID, err := h.userService.Login(req.Email, req.Password)
|
||
|
|
if err != nil {
|
||
|
|
w.WriteHeader(http.StatusUnauthorized)
|
||
|
|
json.NewEncoder(w).Encode(map[string]string{"message": err.Error()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
json.NewEncoder(w).Encode(map[string]interface{}{"token": token, "userID": userID, "message": "登录成功"})
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取用户信息
|
||
|
|
func (h *UserHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
||
|
|
userID := r.Context().Value("userID").(string)
|
||
|
|
profile, err := h.userService.GetProfile(userID)
|
||
|
|
if err != nil {
|
||
|
|
w.WriteHeader(http.StatusNotFound)
|
||
|
|
json.NewEncoder(w).Encode(map[string]string{"message": err.Error()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
json.NewEncoder(w).Encode(profile)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 更新用户信息
|
||
|
|
func (h *UserHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||
|
|
userID := r.Context().Value("userID").(string)
|
||
|
|
var req struct {
|
||
|
|
Username string `json:"username"`
|
||
|
|
Avatar string `json:"avatar"`
|
||
|
|
}
|
||
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
|
w.WriteHeader(http.StatusBadRequest)
|
||
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "参数错误"})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := h.userService.UpdateProfile(userID, req.Username, req.Avatar); err != nil {
|
||
|
|
w.WriteHeader(http.StatusInternalServerError)
|
||
|
|
json.NewEncoder(w).Encode(map[string]string{"message": err.Error()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "更新成功"})
|
||
|
|
}
|