修复了一些问题
This commit is contained in:
126
internal/api/settings_handler.go
Normal file
126
internal/api/settings_handler.go
Normal file
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* package api
|
||||
* 作用:用户设置与消息已读回执 HTTP 接口
|
||||
*/
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"xk-websocket-v2/internal/model"
|
||||
"xk-websocket-v2/internal/service"
|
||||
"xk-websocket-v2/internal/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
/**
|
||||
* GetUserSettingsHandler
|
||||
* 功能:获取当前用户的全部设置
|
||||
*/
|
||||
func GetUserSettingsHandler(c *gin.Context) {
|
||||
userID, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
utils.Unauthorized(c, "未认证")
|
||||
return
|
||||
}
|
||||
|
||||
var rows []model.UserSetting
|
||||
if err := service.ChatSvc.DB.Where("user_id = ?", userID.(string)).Find(&rows).Error; err != nil {
|
||||
utils.InternalError(c, "查询失败")
|
||||
return
|
||||
}
|
||||
|
||||
result := make(map[string]string, len(rows))
|
||||
for _, row := range rows {
|
||||
result[row.SettingKey] = row.SettingValue
|
||||
}
|
||||
utils.SuccessWithData(c, result, "获取成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* UpdateUserSettingsHandler
|
||||
* 功能:批量更新用户设置
|
||||
*/
|
||||
func UpdateUserSettingsHandler(c *gin.Context) {
|
||||
userID, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
utils.Unauthorized(c, "未认证")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Settings map[string]string `json:"settings" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
uid := userID.(string)
|
||||
now := time.Now()
|
||||
for key, val := range req.Settings {
|
||||
setting := model.UserSetting{
|
||||
UserID: uid,
|
||||
SettingKey: key,
|
||||
SettingValue: val,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
service.ChatSvc.DB.Save(&setting)
|
||||
}
|
||||
|
||||
GetUserSettingsHandler(c)
|
||||
}
|
||||
|
||||
/**
|
||||
* MarkMessagesReadHandler
|
||||
* 功能:批量标记消息已读
|
||||
*/
|
||||
func MarkMessagesReadHandler(c *gin.Context) {
|
||||
userID, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
utils.Unauthorized(c, "未认证")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
MessageIDs []uint `json:"message_ids" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
uid := userID.(string)
|
||||
now := time.Now()
|
||||
readIDs := make([]uint, 0, len(req.MessageIDs))
|
||||
for _, mid := range req.MessageIDs {
|
||||
receipt := model.MessageReadReceipt{
|
||||
MessageID: mid,
|
||||
UserID: uid,
|
||||
ReadAt: now,
|
||||
}
|
||||
service.ChatSvc.DB.Where("message_id = ? AND user_id = ?", mid, uid).
|
||||
Assign(receipt).FirstOrCreate(&receipt)
|
||||
readIDs = append(readIDs, mid)
|
||||
|
||||
// 通知消息发送者(已读回执)
|
||||
var msg model.ChatMessage
|
||||
if err := service.ChatSvc.DB.First(&msg, mid).Error; err == nil && msg.SenderUserID != "" && msg.SenderUserID != uid {
|
||||
payload := model.WsPayload{
|
||||
RequestType: "messages_read",
|
||||
Data: map[string]interface{}{
|
||||
"message_ids": []uint{mid},
|
||||
"reader_id": uid,
|
||||
"room_id": msg.RoomID,
|
||||
},
|
||||
}
|
||||
if bytes, err := json.Marshal(payload); err == nil {
|
||||
service.ChatSvc.DispatchMessage(msg.SenderUserID, bytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, gin.H{"message_ids": readIDs}, "已标记已读")
|
||||
}
|
||||
Reference in New Issue
Block a user