修复了聊天数据异常丢失的bug

This commit is contained in:
2024-10-18 18:15:44 +08:00
parent b27d2b80f7
commit 8a8a6e0bfa
6 changed files with 40 additions and 24 deletions
+13 -11
View File
@@ -5,11 +5,12 @@ import (
)
type ChatMessage struct {
UserID string
UserName string
CreateTx time.Time
Type string
Text string
UserID string // 发言对象
UserName string // 发言对象名字
TarUserID string // 接收对象
CreateTx time.Time
Type string
Text string
}
const (
@@ -17,13 +18,14 @@ const (
ChatTypeCard = "CARD" // 工具卡片分享
)
func NewTextChatMsg(user *Users, text string) *ChatMessage {
func NewTextChatMsg(user *Users, tarUid string, text string) *ChatMessage {
return &ChatMessage{
UserID: user.ID,
UserName: user.Name,
CreateTx: time.Now(),
Type: ChatTypeText,
Text: text,
UserID: user.ID,
UserName: user.Name,
TarUserID: tarUid,
CreateTx: time.Now(),
Type: ChatTypeText,
Text: text,
}
}
+3 -1
View File
@@ -12,7 +12,9 @@ var ChatMsg = &chatMessageRepo{}
func (*chatMessageRepo) GetUserMsgs(db *gorm.DB, uuid string, limit int) []models.ChatMessage {
var msgs []models.ChatMessage
// ASC:升序(默认),DESC:降序。
db.Order("create_tx ASC").Limit(limit).Where("user_id = ?", uuid).Find(&msgs)
db.Order("create_tx ASC").Limit(limit).
Where("user_id = ? OR tar_user_id = ?", uuid, uuid).
Find(&msgs)
return msgs
}
+6 -3
View File
@@ -87,9 +87,12 @@ func (s *serverService) StartListenServer() error {
return
}
message := &models.Message{
Cmd: "chat",
User: *user,
ChatMessage: *models.NewTextChatMsg(&user.Users, msg["text"].(string)),
Cmd: "chat",
User: *user,
ChatMessage: *models.NewTextChatMsg(
&user.Users,
repo.User.GetUserInfo(global.DB).ID,
msg["text"].(string)),
}
global.Send.SendChan <- message
c.JSON(200, gin.H{"message": "ok"})
+1 -1
View File
@@ -87,7 +87,7 @@ func OpenChat(user models.Users) {
return
}
my := repo.User.GetUserInfo(global.DB)
chatItem := models.NewTextChatMsg(my, en.Text)
chatItem := models.NewTextChatMsg(my, user.ID, en.Text)
userInfo.messages = append(userInfo.messages, *chatItem)
_ = repo.ChatMsg.Create(global.DB, chatItem)
list.Add(itemMessage(*chatItem))
+5 -2
View File
@@ -53,7 +53,10 @@ func StartGameListen() {
case "/close_game":
g := global.GetGameInfo(msg.GameUuid)
if g != nil {
dialog.ShowInformation("提示", "对方已退出", g.Obj.(*TenGame).w)
dialog.NewConfirm("提示", "对方已退出", func(b bool) {
g.Obj.(*TenGame).w.Close()
}, g.Obj.(*TenGame).w).Show()
//dialog.ShowInformation("提示", "对方已退出", g.Obj.(*TenGame).w)
}
case "/start_game":
g := global.GetGameInfo(msg.GameUuid)
@@ -244,7 +247,7 @@ func (t *TenGame) advance() {
}
func (t *TenGame) setTitle() {
title := fmt.Sprintf("Game %0.f*%0.f win:%d", t.itemX, t.itemY, t.winSum)
title := fmt.Sprintf("让我们来下棋 %0.f*%0.f win:%d", t.itemX, t.itemY, t.winSum)
if t.isNet {
title += fmt.Sprintf(" | 我 vs %s", t.user.Name)
if t.currentRoundPlayer == t.myIndex {
+12 -6
View File
@@ -1,6 +1,7 @@
package views
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
@@ -44,12 +45,17 @@ func baseCardView(
baseCardV = widget.NewCard(baseCardCopy.Title, baseCardCopy.Text, container.NewBorder(nil, widget.NewToolbar(
widget.NewToolbarAction(theme.ContentAddIcon(), func() {}),
widget.NewToolbarAction(theme.DeleteIcon(), func() {
err := service.BaseCard.Delete(global.DB, baseCardCopy)
if err != nil {
dialog.ShowInformation("删除失败", err.Error(), w)
return
}
gridWrap.Remove(baseCardV)
dialog.NewConfirm("提示", fmt.Sprintf("确定删除:%s?", baseCardCopy.Title), func(b bool) {
if b {
err := service.BaseCard.Delete(global.DB, baseCardCopy)
if err != nil {
dialog.ShowInformation("删除失败", err.Error(), w)
return
}
gridWrap.Remove(baseCardV)
}
}, w).Show()
}),
widget.NewToolbarAction(theme.SettingsIcon(), func() {
info := cardTypeMap[baseCardCopy.ToolType]