refactor(chat): 欢迎语仅保留账号群配置

This commit is contained in:
yml2213
2026-10-30 17:50:37 +08:00
parent 77b5c6123a
commit 0a85634a58
18 changed files with 29 additions and 236 deletions
@@ -78,26 +78,6 @@ func (h *Handler) AdminDeleteQuickReply(c *gin.Context) {
response.OK(c, gin.H{"deleted": true})
}
func (h *Handler) AdminGetAutoWelcome(c *gin.Context) {
message := h.service.GetAutoWelcomeMessage(c.Request.Context())
response.OK(c, gin.H{"message": message})
}
func (h *Handler) AdminUpdateAutoWelcome(c *gin.Context) {
var req struct {
Message string `json:"message" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "话术内容不能为空")
return
}
if err := h.service.UpdateAutoWelcomeMessage(c.Request.Context(), req.Message); err != nil {
writeChatError(c, err)
return
}
response.OK(c, gin.H{"updated": true})
}
func (h *Handler) AdminGetListingGroupWelcome(c *gin.Context) {
message := h.service.GetListingGroupWelcomeMessage(c.Request.Context())
response.OK(c, gin.H{"message": message})
@@ -433,12 +433,6 @@ func adminIsSupport(tx *gorm.DB, id uint64) bool {
}
return count > 0
}
func orderConversationTitle(order model.RentalOrder) string {
if order.OrderNo == "" {
return fmt.Sprintf("订单群聊 #%d", order.ID)
}
return "订单群聊 " + order.OrderNo
}
func fallbackName(participantType string, id uint64, name string) string {
if name != "" {
return name
@@ -11,8 +11,6 @@ import (
)
const (
autoWelcomeConfigKey = "chat.auto_welcome_message"
defaultAutoWelcomeMessage = "欢迎加入订单群聊!如有任何问题,请随时沟通。"
listingGroupWelcomeConfigKey = "chat.listing_group_welcome"
defaultListingGroupWelcome = "欢迎加入账号群!请号主扫描下方二维码加入企业微信群,方便客服与您及时联系。"
)
@@ -104,19 +102,6 @@ func (r *Repository) DeleteQuickReply(ctx context.Context, adminID uint64, reply
return r.db.WithContext(ctx).Where("id = ? AND admin_user_id = ?", replyID, adminID).
Delete(&model.ChatQuickReply{}).Error
}
func (r *Repository) GetAutoWelcomeMessage(ctx context.Context) string {
var cfg model.SystemConfig
if err := r.db.WithContext(ctx).Where("`key` = ?", autoWelcomeConfigKey).First(&cfg).Error; err != nil {
return defaultAutoWelcomeMessage
}
return cfg.Value
}
func (r *Repository) UpdateAutoWelcomeMessage(ctx context.Context, message string) error {
return r.db.WithContext(ctx).Model(&model.SystemConfig{}).
Where("`key` = ?", autoWelcomeConfigKey).
Update("value", message).Error
}
func (r *Repository) GetListingGroupWelcomeMessage(ctx context.Context) string {
var cfg model.SystemConfig
if err := r.db.WithContext(ctx).Where("`key` = ?", listingGroupWelcomeConfigKey).First(&cfg).Error; err != nil {
@@ -2,15 +2,12 @@ package chat
import (
"context"
"errors"
"net/http"
"time"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/modules/chathub"
)
@@ -77,88 +74,6 @@ func NewRepository(db *gorm.DB, hub *chathub.Hub, redis *redis.Client, options .
}
return repo
}
func EnsureOrderConversation(tx *gorm.DB, order model.RentalOrder) (*model.ChatConversation, error) {
var existing model.ChatConversation
err := tx.Where("order_id = ?", order.ID).First(&existing).Error
if err == nil {
return &existing, nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
var siteID *uint64
if order.DistributionSiteID != nil && *order.DistributionSiteID > 0 && order.DistributionMode != "two_party_distributor_receive" {
siteID = order.DistributionSiteID
}
now := time.Now()
conversation := model.ChatConversation{
OrderID: &order.ID,
DistributionSiteID: siteID,
Type: "order_group",
Title: orderConversationTitle(order),
Status: "active",
}
if err := tx.Create(&conversation).Error; err != nil {
return nil, err
}
participants := []model.ChatParticipant{
{
ConversationID: conversation.ID,
ParticipantType: "user",
ParticipantID: order.RenterID,
Role: "renter",
JoinedAt: now,
},
{
ConversationID: conversation.ID,
ParticipantType: "user",
ParticipantID: order.OwnerID,
Role: "owner",
JoinedAt: now,
},
}
if supportID := defaultSupportAdminID(tx); supportID > 0 {
participants = append(participants, model.ChatParticipant{
ConversationID: conversation.ID,
ParticipantType: "admin",
ParticipantID: supportID,
Role: "support",
JoinedAt: now,
})
}
for _, participant := range participants {
if err := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&participant).Error; err != nil {
return nil, err
}
}
// 获取自动话术
autoMessage := "订单已支付,群聊已创建。租客、号主和客服可在这里沟通交接与结账问题。"
var cfg model.SystemConfig
if err := tx.Where("`key` = ?", "chat.auto_welcome_message").First(&cfg).Error; err == nil && cfg.Value != "" {
autoMessage = cfg.Value
}
message := model.ChatMessage{
ConversationID: conversation.ID,
SenderType: "system",
SenderRole: "system",
ContentType: "system",
Content: autoMessage,
AttachmentURLS: emptyJSONList(),
}
if err := tx.Create(&message).Error; err != nil {
return nil, err
}
conversation.LastMessageID = &message.ID
conversation.LastMessagePreview = truncatePreview(message.Content)
conversation.LastMessageAt = &message.CreatedAt
if err := tx.Save(&conversation).Error; err != nil {
return nil, err
}
return &conversation, nil
}
func (r *Repository) NotifyNewConversation(conversationID uint64) {
if r.hub == nil {
return
-14
View File
@@ -216,20 +216,6 @@ func (s *Service) DeleteQuickReply(ctx context.Context, adminID uint64, replyID
return s.repo.DeleteQuickReply(ctx, adminID, replyID)
}
func (s *Service) GetAutoWelcomeMessage(ctx context.Context) string {
if s.repo == nil {
return ""
}
return s.repo.GetAutoWelcomeMessage(ctx)
}
func (s *Service) UpdateAutoWelcomeMessage(ctx context.Context, message string) error {
if s.repo == nil {
return ErrDependencyUnavailable
}
return s.repo.UpdateAutoWelcomeMessage(ctx, message)
}
func (s *Service) GetListingGroupWelcomeMessage(ctx context.Context) string {
if s.repo == nil {
return ""
@@ -45,7 +45,6 @@ var defaultConfigs = []defaultConfig{
{Key: rentergrowth.ConfigKey, Value: rentergrowth.DefaultConfigValue(), Description: "租客成长等级规则(积分门槛、租金折扣、等级免押额度)"},
{Key: smsnode.ConfigKey, Value: smsnode.DefaultConfigValue(), Description: "节点短信模板配置(总开关、各业务节点模板 CODE)"},
{Key: "chat.default_support_admin_id", Value: "2", Description: "默认客服 ID(必须是启用状态的客服角色)"},
{Key: "chat.auto_welcome_message", Value: "欢迎加入订单群聊!如有任何问题,请随时沟通。", Description: "建群后自动发送的欢迎话术"},
{Key: "chat.listing_group_welcome", Value: "欢迎加入账号群!请号主扫描下方二维码加入企业微信群,方便客服与您及时联系。", Description: "发布群创建后自动发送的欢迎语"},
{Key: "chat.renter_retention_days_after_order_end", Value: "5", Description: "订单结束后租客保留在发布群的天数(3-7)"},
{Key: "integration.paddle_ocr_token", Value: "", Description: "PaddleOCR API Token,用于二维码群名自动识别"},
@@ -66,7 +65,6 @@ var defaultConfigs = []defaultConfig{
}
var adminVisibleConfigKeys = []string{
"chat.auto_welcome_message",
"chat.default_support_admin_id",
"chat.listing_group_welcome",
"chat.renter_retention_days_after_order_end",
-2
View File
@@ -896,8 +896,6 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
adminRoutes.POST("/chats/quick-replies", requirePerm("chat:send"), chatHandler.AdminCreateQuickReply)
adminRoutes.PUT("/chats/quick-replies/:id", requirePerm("chat:send"), chatHandler.AdminUpdateQuickReply)
adminRoutes.DELETE("/chats/quick-replies/:id", requirePerm("chat:send"), chatHandler.AdminDeleteQuickReply)
adminRoutes.GET("/chats/auto-welcome", requirePerm("system_config:view"), chatHandler.AdminGetAutoWelcome)
adminRoutes.PUT("/chats/auto-welcome", requirePerm("system_config:update"), chatHandler.AdminUpdateAutoWelcome)
adminRoutes.GET("/chats/listing-group-welcome", requirePerm("system_config:view"), chatHandler.AdminGetListingGroupWelcome)
adminRoutes.PUT("/chats/listing-group-welcome", requirePerm("system_config:update"), chatHandler.AdminUpdateListingGroupWelcome)
+1 -2
View File
@@ -666,8 +666,7 @@ WHERE r.code = 'finance' AND p.code IN (
INSERT INTO system_configs (`key`, `value`, description) VALUES
('listing.review_required', 'true', '商品是否需要审核'),
('order.pending_payment_timeout_minutes', '15', '待支付超时时间(分钟)'),
('chat.default_support_admin_id', '2', '默认客服 ID(必须是启用状态的客服角色)'),
('chat.auto_welcome_message', '欢迎加入订单群聊!如有任何问题,请随时沟通。', '建群后自动发送的欢迎话术')
('chat.default_support_admin_id', '2', '默认客服 ID(必须是启用状态的客服角色)')
ON DUPLICATE KEY UPDATE
`value` = VALUES(`value`),
description = VALUES(description);