- 新增数据库迁移 000008: chat_conversations 增加 listing_id, 新建 chat_qrcode_pool 表 - 更新 GORM 模型: ChatConversation 新增 ListingID 字段, 新增 ChatQrCode 模型 - 实现二维码池管理后端 API: 创建/列表/统计/更新/删除 - 新增管理后台路由: /api/admin/chats/qrcodes - 新增系统配置: chat.listing_group_welcome, chat.qrcode_low_stock_threshold - 更新优化方案文档: 废弃双群方案, 采用单群方案 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
package chat
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// CreateQrCodeHandler 创建二维码
|
|
func (h *Handler) CreateQrCodeHandler(c *gin.Context) {
|
|
var req CreateQrCodeRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
adminID := c.GetUint64("admin_id")
|
|
qrcode, err := h.service.repo.CreateQrCode(c.Request.Context(), adminID, req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": qrcode})
|
|
}
|
|
|
|
// ListQrCodesHandler 列表查询二维码
|
|
func (h *Handler) ListQrCodesHandler(c *gin.Context) {
|
|
var req QrCodeListRequest
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
qrcodes, total, err := h.service.repo.ListQrCodes(c.Request.Context(), req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": qrcodes,
|
|
"pagination": gin.H{
|
|
"total": total,
|
|
"page": req.Page,
|
|
"limit": req.Limit,
|
|
},
|
|
})
|
|
}
|
|
|
|
// GetQrCodeStatsHandler 获取二维码统计
|
|
func (h *Handler) GetQrCodeStatsHandler(c *gin.Context) {
|
|
stats, err := h.service.repo.GetQrCodeStats(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": stats})
|
|
}
|
|
|
|
// UpdateQrCodeHandler 更新二维码
|
|
func (h *Handler) UpdateQrCodeHandler(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的ID"})
|
|
return
|
|
}
|
|
|
|
var req UpdateQrCodeRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.service.repo.UpdateQrCode(c.Request.Context(), id, req); err != nil {
|
|
if err == ErrQrCodeNotFound {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "二维码不存在"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "更新成功"})
|
|
}
|
|
|
|
// DeleteQrCodeHandler 删除二维码
|
|
func (h *Handler) DeleteQrCodeHandler(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的ID"})
|
|
return
|
|
}
|
|
|
|
if err := h.service.repo.DeleteQrCode(c.Request.Context(), id); err != nil {
|
|
if err == ErrQrCodeNotFound {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "二维码不存在"})
|
|
return
|
|
}
|
|
if err == ErrQrCodeCannotDelete {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "已使用的二维码不能删除"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "删除成功"})
|
|
}
|