feat(supervisor): add invite, bind, and read-only overview
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"wx_service/internal/middleware"
|
||||
"wx_service/internal/model"
|
||||
)
|
||||
|
||||
type createSupervisorInviteRequest struct {
|
||||
Days int `json:"days"`
|
||||
}
|
||||
|
||||
// CreateSupervisorInvite POST /api/v2/supervisor/invites
|
||||
func (h *Handler) CreateSupervisorInvite(c *gin.Context) {
|
||||
user := middleware.MustCurrentUser(c)
|
||||
|
||||
var req createSupervisorInviteRequest
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
|
||||
res, err := h.service.CreateSupervisorInvite(c.Request.Context(), int(user.ID), time.Now(), req.Days)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "生成邀请失败,请稍后重试"))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Success(res))
|
||||
}
|
||||
|
||||
type bindSupervisorInviteRequest struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// BindSupervisorInvite POST /api/v2/supervisor/bind
|
||||
func (h *Handler) BindSupervisorInvite(c *gin.Context) {
|
||||
user := middleware.MustCurrentUser(c)
|
||||
|
||||
var req bindSupervisorInviteRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil || strings.TrimSpace(req.Token) == "" {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.BindSupervisorInvite(c.Request.Context(), int(user.ID), req.Token, time.Now()); err != nil {
|
||||
msg := "绑定失败,请稍后重试"
|
||||
switch err {
|
||||
case nil:
|
||||
// no-op
|
||||
default:
|
||||
// 针对业务错误给出更友好的提示
|
||||
switch err.Error() {
|
||||
case "邀请不存在":
|
||||
msg = "邀请不存在"
|
||||
case "邀请已过期":
|
||||
msg = "邀请已过期"
|
||||
case "邀请已被使用":
|
||||
msg = "邀请已被使用"
|
||||
case "不能绑定自己为监督人":
|
||||
msg = "不能绑定自己"
|
||||
case "监督关系已存在":
|
||||
msg = "已绑定,无需重复操作"
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, msg))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, model.Success(gin.H{"ok": true}))
|
||||
}
|
||||
|
||||
// GetSupervisorOverview GET /api/v2/supervisor/overview
|
||||
func (h *Handler) GetSupervisorOverview(c *gin.Context) {
|
||||
user := middleware.MustCurrentUser(c)
|
||||
|
||||
res, err := h.service.GetSupervisorOverview(c.Request.Context(), int(user.ID), time.Now())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取监督概览失败,请稍后重试"))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Success(res))
|
||||
}
|
||||
|
||||
// GetSupervisorStatus GET /api/v2/supervisor/status
|
||||
func (h *Handler) GetSupervisorStatus(c *gin.Context) {
|
||||
user := middleware.MustCurrentUser(c)
|
||||
|
||||
res, err := h.service.GetSupervisorStatus(c.Request.Context(), int(user.ID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取监督信息失败,请稍后重试"))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Success(res))
|
||||
}
|
||||
Reference in New Issue
Block a user