149 lines
4.4 KiB
Go
149 lines
4.4 KiB
Go
// profile_handler.go Day 28:上级切换 / 标星 / 服务团队 / 实名认证端点。
|
|
package v1
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go-sms/internal/pkg/errors"
|
|
"go-sms/internal/pkg/response"
|
|
"go-sms/internal/service/account"
|
|
)
|
|
|
|
// changeParent POST /accounts/:id/parent {parent_id}(account:write):
|
|
// 物化路径防环 + 子孙级联重写。
|
|
func (h *accountHandler) changeParent(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req struct {
|
|
ParentID int64 `json:"parent_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
if err := h.svc.ChangeParent(c.Request.Context(), id, req.ParentID); err != nil {
|
|
if bizErr, ok := err.(*errors.Error); ok {
|
|
response.Error(c, bizErr)
|
|
return
|
|
}
|
|
c.Set("biz_error", err.Error())
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "parent_id": req.ParentID})
|
|
}
|
|
|
|
// setStar POST /accounts/:id/star {starred}(account:write):标星置顶。
|
|
func (h *accountHandler) setStar(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req struct {
|
|
Starred bool `json:"starred"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
if err := h.svc.SetStar(c.Request.Context(), id, req.Starred); err != nil {
|
|
if bizErr, ok := err.(*errors.Error); ok {
|
|
response.Error(c, bizErr)
|
|
return
|
|
}
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "starred": req.Starred})
|
|
}
|
|
|
|
// setSvcTeam PUT /accounts/:id/svc-team {svc_team}(account:write)。
|
|
func (h *accountHandler) setSvcTeam(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req struct {
|
|
SvcTeam string `json:"svc_team"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
if err := h.svc.SetSvcTeam(c.Request.Context(), id, req.SvcTeam); err != nil {
|
|
if bizErr, ok := err.(*errors.Error); ok {
|
|
response.Error(c, bizErr)
|
|
return
|
|
}
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "svc_team": req.SvcTeam})
|
|
}
|
|
|
|
// getRealname GET /accounts/:id/realname(本人或 admin;证件号脱敏)。
|
|
func (h *accountHandler) getRealname(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if !requireSelfOrAdmin(c, id) {
|
|
return
|
|
}
|
|
info, err := h.svc.GetRealname(c.Request.Context(), id)
|
|
if err != nil {
|
|
if bizErr, ok := err.(*errors.Error); ok {
|
|
response.Error(c, bizErr)
|
|
return
|
|
}
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "realname": info})
|
|
}
|
|
|
|
// submitRealname POST /accounts/:id/realname {real_name, id_card}(本人或 admin 代提交)。
|
|
func (h *accountHandler) submitRealname(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if !requireSelfOrAdmin(c, id) {
|
|
return
|
|
}
|
|
var req struct {
|
|
RealName string `json:"real_name" binding:"required"`
|
|
IDCard string `json:"id_card" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
if err := h.svc.RealnameSubmit(c.Request.Context(), id, req.RealName, req.IDCard); err != nil {
|
|
if bizErr, ok := err.(*errors.Error); ok {
|
|
response.Error(c, bizErr)
|
|
return
|
|
}
|
|
c.Set("biz_error", err.Error())
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "status": account.RealnamePending})
|
|
}
|
|
|
|
// reviewRealname POST /accounts/:id/realname/review {approve, reason}(account:write)。
|
|
func (h *accountHandler) reviewRealname(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req struct {
|
|
Approve bool `json:"approve"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
if err := h.svc.RealnameReview(c.Request.Context(), id, req.Approve, req.Reason); err != nil {
|
|
if bizErr, ok := err.(*errors.Error); ok {
|
|
response.Error(c, bizErr)
|
|
return
|
|
}
|
|
c.Set("biz_error", err.Error())
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
status := account.RealnameApproved
|
|
if !req.Approve {
|
|
status = account.RealnameRejected
|
|
}
|
|
response.Success(c, gin.H{"id": id, "status": status})
|
|
}
|