175 lines
4.9 KiB
Go
175 lines
4.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"photowall/internal/middleware"
|
|
"photowall/internal/model"
|
|
"photowall/internal/service"
|
|
"photowall/pkg/response"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type UserHandler struct {
|
|
svc *service.UserService
|
|
}
|
|
|
|
func NewUserHandler(svc *service.UserService) *UserHandler {
|
|
return &UserHandler{svc: svc}
|
|
}
|
|
|
|
// Profile GET /api/user/profile
|
|
func (h *UserHandler) Profile(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
profile, err := h.svc.GetProfile(userID)
|
|
if err != nil {
|
|
response.NotFound(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, profile)
|
|
}
|
|
|
|
// UpdateProfile PUT /api/user/profile
|
|
func (h *UserHandler) UpdateProfile(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
var req service.UpdateProfileReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.svc.UpdateProfile(userID, &req); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
u, err := h.svc.GetUserByID(userID)
|
|
if err != nil {
|
|
response.OK(c, gin.H{"message": "更新成功"})
|
|
return
|
|
}
|
|
response.OK(c, u)
|
|
}
|
|
|
|
// AddContact POST /api/user/contacts
|
|
func (h *UserHandler) AddContact(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
var body struct {
|
|
Type model.ContactType `json:"type" binding:"required"`
|
|
Value string `json:"value" binding:"required"`
|
|
IsPrimary bool `json:"is_primary"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
contact, err := h.svc.AddContact(userID, body.Type, body.Value, body.IsPrimary)
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.Created(c, contact)
|
|
}
|
|
|
|
// UpdateContact PUT /api/user/contacts/:id
|
|
func (h *UserHandler) UpdateContact(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var body struct {
|
|
Value string `json:"value"`
|
|
IsPrimary *bool `json:"is_primary"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.svc.UpdateContact(userID, uint(id), body.Value, body.IsPrimary); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"message": "更新成功"})
|
|
}
|
|
|
|
// DeleteContact DELETE /api/user/contacts/:id
|
|
func (h *UserHandler) DeleteContact(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.svc.DeleteContact(userID, uint(id)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"message": "删除成功"})
|
|
}
|
|
|
|
// AddEducation POST /api/user/education
|
|
func (h *UserHandler) AddEducation(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
var e model.EducationHistory
|
|
if err := c.ShouldBindJSON(&e); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
if e.SchoolName == "" {
|
|
response.BadRequest(c, "学校名称不能为空")
|
|
return
|
|
}
|
|
if err := h.svc.AddEducation(userID, &e); err != nil {
|
|
response.Internal(c, err.Error())
|
|
return
|
|
}
|
|
response.Created(c, e)
|
|
}
|
|
|
|
// UpdateEducation PUT /api/user/education/:id
|
|
func (h *UserHandler) UpdateEducation(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var e model.EducationHistory
|
|
if err := c.ShouldBindJSON(&e); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.svc.UpdateEducation(userID, uint(id), &e); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"message": "更新成功"})
|
|
}
|
|
|
|
// DeleteEducation DELETE /api/user/education/:id
|
|
func (h *UserHandler) DeleteEducation(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.svc.DeleteEducation(userID, uint(id)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"message": "删除成功"})
|
|
}
|
|
|
|
// HasEducation GET /api/user/has-education (创建班级前置检查)
|
|
func (h *UserHandler) HasEducation(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
response.OK(c, gin.H{"has_education": h.svc.HasEducation(userID)})
|
|
}
|
|
|
|
// PublicProfile GET /api/user/:id 公开信息(班级成员查看)
|
|
func (h *UserHandler) PublicProfile(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
currentUserID := middleware.CurrentUserID(c)
|
|
user, err := h.svc.GetUserByID(uint(id))
|
|
if err != nil {
|
|
response.NotFound(c, err.Error())
|
|
return
|
|
}
|
|
// 隐私脱敏保护:非本人且非管理员,对身份证号码脱敏
|
|
isAdmin := middleware.CurrentRole(c) == "admin"
|
|
if currentUserID != user.ID && !isAdmin {
|
|
if len(user.IdCard) == 18 {
|
|
user.IdCard = user.IdCard[:6] + "********" + user.IdCard[14:]
|
|
} else if user.IdCard != "" {
|
|
user.IdCard = "****************"
|
|
}
|
|
}
|
|
contacts := h.svc.GetUserContacts(uint(id))
|
|
response.OK(c, gin.H{"user": user, "contacts": contacts})
|
|
}
|