feat: major update - MySQL, chat, wishlist, PWA, admin overhaul
This commit is contained in:
82
mengyastore-backend/internal/handlers/chat.go
Normal file
82
mengyastore-backend/internal/handlers/chat.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"mengyastore-backend/internal/auth"
|
||||
"mengyastore-backend/internal/storage"
|
||||
)
|
||||
|
||||
type ChatHandler struct {
|
||||
chatStore *storage.ChatStore
|
||||
authClient *auth.SproutGateClient
|
||||
}
|
||||
|
||||
func NewChatHandler(chatStore *storage.ChatStore, authClient *auth.SproutGateClient) *ChatHandler {
|
||||
return &ChatHandler{chatStore: chatStore, authClient: authClient}
|
||||
}
|
||||
|
||||
func (h *ChatHandler) requireChatUser(c *gin.Context) (account, name string, ok bool) {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" || !strings.HasPrefix(authHeader, "Bearer ") {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "请先登录"})
|
||||
return "", "", false
|
||||
}
|
||||
token := strings.TrimPrefix(authHeader, "Bearer ")
|
||||
result, err := h.authClient.VerifyToken(token)
|
||||
if err != nil || !result.Valid || result.User == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "登录已过期,请重新登录"})
|
||||
return "", "", false
|
||||
}
|
||||
return result.User.Account, result.User.Username, true
|
||||
}
|
||||
|
||||
// GetMyMessages returns all chat messages for the currently logged-in user.
|
||||
func (h *ChatHandler) GetMyMessages(c *gin.Context) {
|
||||
account, _, ok := h.requireChatUser(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
msgs, err := h.chatStore.GetMessages(account)
|
||||
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 chatMessagePayload struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// SendMyMessage sends a message from the current user to admin.
|
||||
func (h *ChatHandler) SendMyMessage(c *gin.Context) {
|
||||
account, name, ok := h.requireChatUser(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var payload chatMessagePayload
|
||||
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, rateLimited, err := h.chatStore.SendUserMessage(account, name, content)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if rateLimited {
|
||||
c.JSON(http.StatusTooManyRequests, gin.H{"error": "发送太频繁,请稍候"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": gin.H{"message": msg}})
|
||||
}
|
||||
Reference in New Issue
Block a user