- 新增 000070 数据库迁移:支持 chat_conversations、wallet_accounts、wallet_ledger、withdrawal_requests 按 distribution_site_id 隔离 - 消息中心:会话列表、未读数统计、站点专属客服及订单群聊与主站实现双向完全隔离 - 资金账户与提现:独立按 (user_id, distribution_site_id) 建账,分销订单自动结算至对应分站钱包,提现与流水相互独立 - 前台界面:Axios 自动透传 X-Distribution-Site-Code,钱包、提现与消息页面呈现分站专属标牌与独立说明
92 lines
2.0 KiB
Go
92 lines
2.0 KiB
Go
package chat
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"hfb_sys/backend/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func parseSiteCode(c *gin.Context) string {
|
|
for _, val := range []string{
|
|
c.Query("distribution_site_code"),
|
|
c.Query("site_code"),
|
|
c.GetHeader("X-Distribution-Site-Code"),
|
|
c.GetHeader("X-Site-Code"),
|
|
} {
|
|
if trimmed := strings.TrimSpace(val); trimmed != "" {
|
|
return trimmed
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (h *Handler) list(c *gin.Context, principal Principal) {
|
|
page, pageSize := parsePagination(c)
|
|
siteCode := parseSiteCode(c)
|
|
result, err := h.service.ListConversations(c.Request.Context(), principal, page, pageSize, siteCode)
|
|
if err != nil {
|
|
writeChatError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, result)
|
|
}
|
|
|
|
func (h *Handler) detail(c *gin.Context, principal Principal) {
|
|
id, ok := parseID(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
item, err := h.service.FindConversation(c.Request.Context(), principal, id)
|
|
if err != nil {
|
|
writeChatError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func (h *Handler) messages(c *gin.Context, principal Principal) {
|
|
id, ok := parseID(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
page, pageSize := parsePagination(c)
|
|
result, err := h.service.Messages(c.Request.Context(), principal, id, page, pageSize)
|
|
if err != nil {
|
|
writeChatError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, result)
|
|
}
|
|
|
|
func (h *Handler) send(c *gin.Context, principal Principal) {
|
|
id, ok := parseID(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
var req SendMessageRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "消息格式不正确")
|
|
return
|
|
}
|
|
message, err := h.service.SendMessage(c.Request.Context(), principal, id, req)
|
|
if err != nil {
|
|
writeChatError(c, err)
|
|
return
|
|
}
|
|
response.Created(c, message)
|
|
}
|
|
|
|
func (h *Handler) markRead(c *gin.Context, principal Principal) {
|
|
id, ok := parseID(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := h.service.MarkRead(c.Request.Context(), principal, id); err != nil {
|
|
writeChatError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"read": true})
|
|
}
|