feat: major update - MySQL, chat, wishlist, PWA, admin overhaul
This commit is contained in:
88
mengyastore-backend/internal/handlers/admin_chat.go
Normal file
88
mengyastore-backend/internal/handlers/admin_chat.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetAllConversations returns all conversations (map of accountID -> messages).
|
||||
func (h *AdminHandler) GetAllConversations(c *gin.Context) {
|
||||
if !h.requireAdmin(c) {
|
||||
return
|
||||
}
|
||||
convs, err := h.chatStore.ListConversations()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": gin.H{"conversations": convs}})
|
||||
}
|
||||
|
||||
// GetConversation returns all messages for a specific account.
|
||||
func (h *AdminHandler) GetConversation(c *gin.Context) {
|
||||
if !h.requireAdmin(c) {
|
||||
return
|
||||
}
|
||||
accountID := c.Param("account")
|
||||
if accountID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing account"})
|
||||
return
|
||||
}
|
||||
msgs, err := h.chatStore.GetMessages(accountID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": gin.H{"messages": msgs}})
|
||||
}
|
||||
|
||||
type adminChatPayload struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// AdminReply sends a reply from admin to a specific user.
|
||||
func (h *AdminHandler) AdminReply(c *gin.Context) {
|
||||
if !h.requireAdmin(c) {
|
||||
return
|
||||
}
|
||||
accountID := c.Param("account")
|
||||
if accountID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing account"})
|
||||
return
|
||||
}
|
||||
var payload adminChatPayload
|
||||
if err := c.ShouldBindJSON(&payload); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid payload"})
|
||||
return
|
||||
}
|
||||
content := strings.TrimSpace(payload.Content)
|
||||
if content == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "消息不能为空"})
|
||||
return
|
||||
}
|
||||
msg, err := h.chatStore.SendAdminMessage(accountID, content)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": gin.H{"message": msg}})
|
||||
}
|
||||
|
||||
// ClearConversation deletes all messages with a specific user.
|
||||
func (h *AdminHandler) ClearConversation(c *gin.Context) {
|
||||
if !h.requireAdmin(c) {
|
||||
return
|
||||
}
|
||||
accountID := c.Param("account")
|
||||
if accountID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing account"})
|
||||
return
|
||||
}
|
||||
if err := h.chatStore.ClearConversation(accountID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": gin.H{"ok": true}})
|
||||
}
|
||||
Reference in New Issue
Block a user