111 lines
2.9 KiB
Go
111 lines
2.9 KiB
Go
package message
|
||
|
||
import (
|
||
"github.com/gin-gonic/gin"
|
||
"go-admin/app/admin/models"
|
||
orm "go-admin/common/global"
|
||
"go-admin/tools"
|
||
"go-admin/tools/app"
|
||
"net/http"
|
||
"time"
|
||
)
|
||
|
||
// UserMessageList 用户消息列表
|
||
// @Summary 用户消息列表
|
||
// @Description 获取当前用户的消息记录
|
||
// @Tags 消息中心,V1.5.0
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param data body models.UserMessageListReq true "消息查询参数"
|
||
// @Success 200 {object} models.UserMessageListResp
|
||
// @Router /api/v1/user_message/list [post]
|
||
func UserMessageList(c *gin.Context) {
|
||
var req models.UserMessageListReq
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
app.Error(c, http.StatusBadRequest, nil, "参数错误: "+err.Error())
|
||
return
|
||
}
|
||
|
||
if req.Page <= 0 {
|
||
req.Page = 1
|
||
}
|
||
if req.PageSize <= 0 {
|
||
req.PageSize = 10
|
||
}
|
||
|
||
userID := tools.GetUserId(c)
|
||
query := orm.Eloquent.Where("user_id = ? AND is_visible = ?", userID, true)
|
||
|
||
if req.IsRead != 0 && req.IsRead != 1 {
|
||
app.Error(c, http.StatusBadRequest, nil, "不支持的目标状态")
|
||
return
|
||
} else {
|
||
query = query.Where("is_read = ?", req.IsRead)
|
||
}
|
||
|
||
var total int64
|
||
if err := query.Model(&models.SystemUserMessage{}).Count(&total).Error; err != nil {
|
||
app.Error(c, http.StatusInternalServerError, nil, "查询总数失败: "+err.Error())
|
||
return
|
||
}
|
||
|
||
var messages []models.SystemUserMessage
|
||
if err := query.Order("created_at desc").
|
||
Offset((req.Page - 1) * req.PageSize).
|
||
Limit(req.PageSize).
|
||
Find(&messages).Error; err != nil {
|
||
app.Error(c, http.StatusInternalServerError, nil, "查询列表失败: "+err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, models.UserMessageListResp{
|
||
List: messages,
|
||
Total: total,
|
||
Page: req.Page,
|
||
PageSize: req.PageSize,
|
||
}, "查询成功")
|
||
}
|
||
|
||
// UserMessageSetStatus 设置用户消息为已读(支持单条或全部)
|
||
// @Summary 设置用户消息为已读
|
||
// @Description 设为单条或全部已读
|
||
// @Tags 消息中心,V1.5.0
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param data body models.UserMessageSetStatusReq true "设置参数"
|
||
// @Success 200 {object} app.Response
|
||
// @Router /api/v1/user_message/set_status [post]
|
||
func UserMessageSetStatus(c *gin.Context) {
|
||
var req models.UserMessageSetStatusReq
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
app.Error(c, http.StatusBadRequest, nil, "参数错误: "+err.Error())
|
||
return
|
||
}
|
||
|
||
userID := tools.GetUserId(c)
|
||
now := time.Now()
|
||
query := orm.Eloquent.Model(&models.SystemUserMessage{}).
|
||
Where("user_id = ? AND is_read = ?", userID, false)
|
||
|
||
if req.ReadAll {
|
||
// 全部设为已读
|
||
} else {
|
||
if req.ID == 0 {
|
||
app.Error(c, http.StatusBadRequest, nil, "参数错误: 未提供消息ID")
|
||
return
|
||
}
|
||
query = query.Where("id = ?", req.ID)
|
||
}
|
||
|
||
err := query.Updates(map[string]interface{}{
|
||
"is_read": true,
|
||
"read_time": &now,
|
||
}).Error
|
||
if err != nil {
|
||
app.Error(c, http.StatusInternalServerError, nil, "设置失败: "+err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, nil, "设置成功")
|
||
}
|