392 lines
8.8 KiB
Go
392 lines
8.8 KiB
Go
package listing
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"hfb_sys/backend/internal/middleware"
|
|
filemodule "hfb_sys/backend/internal/modules/file"
|
|
"hfb_sys/backend/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Handler struct {
|
|
service *Service
|
|
storage *filemodule.Storage
|
|
}
|
|
|
|
func NewHandler(service *Service, storage *filemodule.Storage) *Handler {
|
|
return &Handler{service: service, storage: storage}
|
|
}
|
|
|
|
func (h *Handler) Create(c *gin.Context) {
|
|
ownerID, ok := currentUserID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少用户上下文")
|
|
return
|
|
}
|
|
var req CreateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "发布信息不完整")
|
|
return
|
|
}
|
|
item, err := h.service.Create(ownerID, req)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.Created(c, item)
|
|
}
|
|
|
|
func (h *Handler) Update(c *gin.Context) {
|
|
ownerID, ok := currentUserID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少用户上下文")
|
|
return
|
|
}
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req UpdateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "发布信息不完整")
|
|
return
|
|
}
|
|
item, err := h.service.Update(ownerID, id, req)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func (h *Handler) SubmitReview(c *gin.Context) {
|
|
ownerID, ok := currentUserID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少用户上下文")
|
|
return
|
|
}
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
item, err := h.service.SubmitReview(ownerID, id)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func (h *Handler) ListPendingReview(c *gin.Context) {
|
|
items, err := h.service.ListPendingReview()
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *Handler) ListAdmin(c *gin.Context) {
|
|
query, ok := parseAdminListQuery(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
items, err := h.service.ListAdmin(query)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *Handler) FindAdmin(c *gin.Context) {
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
item, err := h.service.FindAdmin(id)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func (h *Handler) AdminOffline(c *gin.Context) {
|
|
h.adminAction(c, h.service.AdminOffline)
|
|
}
|
|
|
|
func (h *Handler) AdminMarkAbnormal(c *gin.Context) {
|
|
h.adminAction(c, h.service.AdminMarkAbnormal)
|
|
}
|
|
|
|
func (h *Handler) adminAction(c *gin.Context, fn func(uint64, uint64, AdminActionRequest, AuditMeta) (*ListingDTO, error)) {
|
|
adminID, ok := currentAdminID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少管理员上下文")
|
|
return
|
|
}
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req AdminActionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "操作原因不能为空")
|
|
return
|
|
}
|
|
item, err := fn(adminID, id, req, AuditMeta{IP: c.ClientIP(), UserAgent: c.GetHeader("User-Agent")})
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func (h *Handler) Approve(c *gin.Context) {
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
item, err := h.service.Approve(id)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func (h *Handler) Reject(c *gin.Context) {
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req ReviewRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "审核拒绝原因不能为空")
|
|
return
|
|
}
|
|
item, err := h.service.Reject(id, req)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func (h *Handler) Offline(c *gin.Context) {
|
|
ownerID, ok := currentUserID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少用户上下文")
|
|
return
|
|
}
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := h.service.Offline(ownerID, id); err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"offline": true})
|
|
}
|
|
|
|
func (h *Handler) ListPublic(c *gin.Context) {
|
|
items, err := h.service.ListPublic()
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *Handler) FindPublic(c *gin.Context) {
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
item, err := h.service.FindPublic(id)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func (h *Handler) Cover(c *gin.Context) {
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
key, err := h.service.FindPublicCoverKey(id)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
h.writePublicObject(c, key)
|
|
}
|
|
|
|
func (h *Handler) Screenshot(c *gin.Context) {
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
index, err := strconv.Atoi(c.Param("index"))
|
|
if err != nil || index < 0 {
|
|
response.BadRequest(c, "截图序号不正确")
|
|
return
|
|
}
|
|
key, err := h.service.FindPublicScreenshotKey(id, index)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
h.writePublicObject(c, key)
|
|
}
|
|
|
|
func (h *Handler) writePublicObject(c *gin.Context, key string) {
|
|
if h.storage == nil {
|
|
response.ServiceUnavailable(c, "文件存储未连接")
|
|
return
|
|
}
|
|
object, err := h.storage.Get(c.Request.Context(), key)
|
|
if err != nil {
|
|
response.Error(c, http.StatusNotFound, "not_found", "图片不存在或暂不可访问")
|
|
return
|
|
}
|
|
defer func() {
|
|
_ = object.Reader.Close()
|
|
}()
|
|
contentType := object.ContentType
|
|
if contentType == "" {
|
|
contentType = "application/octet-stream"
|
|
}
|
|
c.Header("Content-Type", contentType)
|
|
c.Header("Cache-Control", "public, max-age=300")
|
|
c.DataFromReader(http.StatusOK, object.Size, contentType, object.Reader, nil)
|
|
}
|
|
|
|
func (h *Handler) ListMine(c *gin.Context) {
|
|
ownerID, ok := currentUserID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少用户上下文")
|
|
return
|
|
}
|
|
items, err := h.service.ListMine(ownerID)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *Handler) FindMine(c *gin.Context) {
|
|
ownerID, ok := currentUserID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少用户上下文")
|
|
return
|
|
}
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
item, err := h.service.FindMine(ownerID, id)
|
|
if err != nil {
|
|
writeListingError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func currentUserID(c *gin.Context) (uint64, bool) {
|
|
value, ok := c.Get(middleware.ContextUserID)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
userID, ok := value.(uint64)
|
|
return userID, ok
|
|
}
|
|
|
|
func currentAdminID(c *gin.Context) (uint64, bool) {
|
|
value, ok := c.Get(middleware.ContextAdminID)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
adminID, ok := value.(uint64)
|
|
return adminID, ok
|
|
}
|
|
|
|
func parseID(c *gin.Context) (uint64, bool) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
response.BadRequest(c, "ID 不正确")
|
|
return 0, false
|
|
}
|
|
return id, true
|
|
}
|
|
|
|
func parseAdminListQuery(c *gin.Context) (AdminListQuery, bool) {
|
|
var query AdminListQuery
|
|
if raw := c.Query("owner_id"); raw != "" {
|
|
value, err := strconv.ParseUint(raw, 10, 64)
|
|
if err != nil || value == 0 {
|
|
response.BadRequest(c, "号主 ID 不正确")
|
|
return query, false
|
|
}
|
|
query.OwnerID = value
|
|
}
|
|
if raw := c.Query("limit"); raw != "" {
|
|
value, err := strconv.Atoi(raw)
|
|
if err != nil || value <= 0 {
|
|
response.BadRequest(c, "查询条数不正确")
|
|
return query, false
|
|
}
|
|
query.Limit = value
|
|
}
|
|
query.Status = c.Query("status")
|
|
query.ReviewStatus = c.Query("review_status")
|
|
return query, true
|
|
}
|
|
|
|
func writeListingError(c *gin.Context, err error) {
|
|
switch {
|
|
case errors.Is(err, ErrDependencyUnavailable):
|
|
response.ServiceUnavailable(c, "数据库未连接")
|
|
case errors.Is(err, ErrMissingTitle):
|
|
response.BadRequest(c, "发布标题不能为空")
|
|
case errors.Is(err, ErrMissingServerRegion):
|
|
response.BadRequest(c, "请选择区服")
|
|
case errors.Is(err, ErrInvalidPrice):
|
|
response.BadRequest(c, "发布价格不正确")
|
|
case errors.Is(err, ErrInvalidDeposit):
|
|
response.BadRequest(c, "押金不能小于 0")
|
|
case errors.Is(err, ErrDepositTooLow):
|
|
response.BadRequest(c, "押金必须大于额外消耗品总价值")
|
|
case errors.Is(err, ErrInvalidHafCoin):
|
|
response.BadRequest(c, "哈夫币数量不正确")
|
|
case errors.Is(err, ErrMissingScreenshot):
|
|
response.BadRequest(c, "请至少上传一张账号截图")
|
|
case isFireLevelTooLow(err):
|
|
var levelErr FireLevelTooLowError
|
|
errors.As(err, &levelErr)
|
|
response.BadRequest(c, "烽火等级低于"+strconv.Itoa(levelErr.Min)+"级的号无法发布")
|
|
case errors.Is(err, ErrInvalidInput):
|
|
response.BadRequest(c, "发布信息不符合规则")
|
|
case errors.Is(err, ErrListingLocked):
|
|
response.Error(c, http.StatusConflict, "listing_locked", "当前发布不可修改")
|
|
case IsNotFound(err):
|
|
response.Error(c, http.StatusNotFound, "not_found", "发布不存在")
|
|
default:
|
|
response.Error(c, http.StatusInternalServerError, "internal_error", "发布服务暂时不可用")
|
|
}
|
|
}
|
|
|
|
func isFireLevelTooLow(err error) bool {
|
|
var levelErr FireLevelTooLowError
|
|
return errors.As(err, &levelErr)
|
|
}
|