feat(1.0): 新增消息列表接口
This commit is contained in:
parent
a2e9d28ae7
commit
e47e7fe70b
113
docs/docs.go
113
docs/docs.go
@ -707,6 +707,67 @@ const docTemplate = `{
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/app/messages": {
|
||||
"get": {
|
||||
"description": "获取消息日志",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"消息"
|
||||
],
|
||||
"summary": "获取消息日志",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "小程序ID",
|
||||
"name": "app_id",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "用户ID",
|
||||
"name": "user_id",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"default": 1,
|
||||
"description": "当前页码",
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"default": 20,
|
||||
"description": "每页返回的数据量,最多 100 条",
|
||||
"name": "page_size",
|
||||
"in": "query",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/message.appMessagePageListResponse"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/code.Failure"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
@ -1183,6 +1244,58 @@ const docTemplate = `{
|
||||
}
|
||||
}
|
||||
},
|
||||
"message.appMessagePageListResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"list": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/message.listMessageData"
|
||||
}
|
||||
},
|
||||
"page": {
|
||||
"description": "当前页码",
|
||||
"type": "integer"
|
||||
},
|
||||
"page_size": {
|
||||
"description": "每页返回的数据量",
|
||||
"type": "integer"
|
||||
},
|
||||
"total": {
|
||||
"description": "符合查询条件的总记录数",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"message.listMessageData": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"content": {
|
||||
"description": "消息内容",
|
||||
"type": "string"
|
||||
},
|
||||
"msg_type": {
|
||||
"description": "消息类型(1:文本 2:图片)",
|
||||
"type": "integer"
|
||||
},
|
||||
"receiver_id": {
|
||||
"description": "接收人ID",
|
||||
"type": "string"
|
||||
},
|
||||
"send_time": {
|
||||
"description": "发送时间",
|
||||
"type": "string"
|
||||
},
|
||||
"sender_id": {
|
||||
"description": "发送人ID",
|
||||
"type": "string"
|
||||
},
|
||||
"sender_name": {
|
||||
"description": "发送人昵称",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"upload.uploadImageResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
@ -699,6 +699,67 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/app/messages": {
|
||||
"get": {
|
||||
"description": "获取消息日志",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"消息"
|
||||
],
|
||||
"summary": "获取消息日志",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "小程序ID",
|
||||
"name": "app_id",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "用户ID",
|
||||
"name": "user_id",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"default": 1,
|
||||
"description": "当前页码",
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"default": 20,
|
||||
"description": "每页返回的数据量,最多 100 条",
|
||||
"name": "page_size",
|
||||
"in": "query",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/message.appMessagePageListResponse"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/code.Failure"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
@ -1175,6 +1236,58 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"message.appMessagePageListResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"list": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/message.listMessageData"
|
||||
}
|
||||
},
|
||||
"page": {
|
||||
"description": "当前页码",
|
||||
"type": "integer"
|
||||
},
|
||||
"page_size": {
|
||||
"description": "每页返回的数据量",
|
||||
"type": "integer"
|
||||
},
|
||||
"total": {
|
||||
"description": "符合查询条件的总记录数",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"message.listMessageData": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"content": {
|
||||
"description": "消息内容",
|
||||
"type": "string"
|
||||
},
|
||||
"msg_type": {
|
||||
"description": "消息类型(1:文本 2:图片)",
|
||||
"type": "integer"
|
||||
},
|
||||
"receiver_id": {
|
||||
"description": "接收人ID",
|
||||
"type": "string"
|
||||
},
|
||||
"send_time": {
|
||||
"description": "发送时间",
|
||||
"type": "string"
|
||||
},
|
||||
"sender_id": {
|
||||
"description": "发送人ID",
|
||||
"type": "string"
|
||||
},
|
||||
"sender_name": {
|
||||
"description": "发送人昵称",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"upload.uploadImageResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
@ -333,6 +333,43 @@ definitions:
|
||||
description: 提示信息
|
||||
type: string
|
||||
type: object
|
||||
message.appMessagePageListResponse:
|
||||
properties:
|
||||
list:
|
||||
items:
|
||||
$ref: '#/definitions/message.listMessageData'
|
||||
type: array
|
||||
page:
|
||||
description: 当前页码
|
||||
type: integer
|
||||
page_size:
|
||||
description: 每页返回的数据量
|
||||
type: integer
|
||||
total:
|
||||
description: 符合查询条件的总记录数
|
||||
type: integer
|
||||
type: object
|
||||
message.listMessageData:
|
||||
properties:
|
||||
content:
|
||||
description: 消息内容
|
||||
type: string
|
||||
msg_type:
|
||||
description: 消息类型(1:文本 2:图片)
|
||||
type: integer
|
||||
receiver_id:
|
||||
description: 接收人ID
|
||||
type: string
|
||||
send_time:
|
||||
description: 发送时间
|
||||
type: string
|
||||
sender_id:
|
||||
description: 发送人ID
|
||||
type: string
|
||||
sender_name:
|
||||
description: 发送人昵称
|
||||
type: string
|
||||
type: object
|
||||
upload.uploadImageResponse:
|
||||
properties:
|
||||
preview_image_url:
|
||||
@ -791,6 +828,48 @@ paths:
|
||||
summary: 上传图片
|
||||
tags:
|
||||
- 通用
|
||||
/app/messages:
|
||||
get:
|
||||
consumes:
|
||||
- application/json
|
||||
description: 获取消息日志
|
||||
parameters:
|
||||
- description: 小程序ID
|
||||
in: query
|
||||
name: app_id
|
||||
required: true
|
||||
type: string
|
||||
- description: 用户ID
|
||||
in: query
|
||||
name: user_id
|
||||
required: true
|
||||
type: string
|
||||
- default: 1
|
||||
description: 当前页码
|
||||
in: query
|
||||
name: page
|
||||
required: true
|
||||
type: integer
|
||||
- default: 20
|
||||
description: 每页返回的数据量,最多 100 条
|
||||
in: query
|
||||
name: page_size
|
||||
required: true
|
||||
type: integer
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/message.appMessagePageListResponse'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/code.Failure'
|
||||
summary: 获取消息日志
|
||||
tags:
|
||||
- 消息
|
||||
securityDefinitions:
|
||||
LoginVerifyToken:
|
||||
in: header
|
||||
|
||||
130
internal/api/message/message_list.go
Normal file
130
internal/api/message/message_list.go
Normal file
@ -0,0 +1,130 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"mini-chat/internal/code"
|
||||
"mini-chat/internal/pkg/core"
|
||||
"mini-chat/internal/pkg/timeutil"
|
||||
"mini-chat/internal/pkg/validation"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type appMessagePageListRequest struct {
|
||||
AppID string `form:"app_id" binding:"required"` // 小程序ID
|
||||
UserID string `form:"user_id" binding:"required"` // 用户ID
|
||||
Page int `form:"page"` // 当前页码,默认为第一页
|
||||
PageSize int `form:"page_size"` // 每页返回的数据量
|
||||
}
|
||||
|
||||
type listMessageData struct {
|
||||
SendTime string `json:"send_time"` // 发送时间
|
||||
SenderID string `json:"sender_id"` // 发送人ID
|
||||
SenderName string `json:"sender_name"` // 发送人昵称
|
||||
ReceiverID string `json:"receiver_id"` // 接收人ID
|
||||
Content string `json:"content"` // 消息内容
|
||||
MsgType int32 `json:"msg_type"` // 消息类型(1:文本 2:图片)
|
||||
}
|
||||
|
||||
type appMessagePageListResponse struct {
|
||||
Page int `json:"page"` // 当前页码
|
||||
PageSize int `json:"page_size"` // 每页返回的数据量
|
||||
Total int64 `json:"total"` // 符合查询条件的总记录数
|
||||
List []listMessageData `json:"list"`
|
||||
}
|
||||
|
||||
// AppMessagePageList 获取消息日志
|
||||
// @Summary 获取消息日志
|
||||
// @Description 获取消息日志
|
||||
// @Tags 消息
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param app_id query string true "小程序ID"
|
||||
// @Param user_id query string true "用户ID"
|
||||
// @Param page query int true "当前页码" default(1)
|
||||
// @Param page_size query int true "每页返回的数据量,最多 100 条" default(20)
|
||||
// @Success 200 {object} appMessagePageListResponse
|
||||
// @Failure 400 {object} code.Failure
|
||||
// @Router /app/messages [get]
|
||||
func (h *handler) AppMessagePageList() core.HandlerFunc {
|
||||
return func(ctx core.Context) {
|
||||
req := new(appMessagePageListRequest)
|
||||
res := new(appMessagePageListResponse)
|
||||
if err := ctx.ShouldBindForm(req); err != nil {
|
||||
ctx.AbortWithError(core.Error(
|
||||
http.StatusBadRequest,
|
||||
code.ParamBindError,
|
||||
validation.Error(err)),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Page == 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
|
||||
if req.PageSize > 100 {
|
||||
ctx.AbortWithError(core.Error(
|
||||
http.StatusBadRequest,
|
||||
code.ListMessageError,
|
||||
fmt.Sprintf("%s: 一次最多只能查询 100 条", code.Text(code.ListMessageError)),
|
||||
))
|
||||
return
|
||||
}
|
||||
|
||||
query := h.readDB.AppMessageLog.WithContext(ctx.RequestContext()).
|
||||
Where(h.readDB.AppMessageLog.AppID.Eq(req.AppID)).
|
||||
Where(h.readDB.AppMessageLog.SenderID.Eq(req.UserID))
|
||||
|
||||
listQueryDB := query.Session(&gorm.Session{})
|
||||
countQueryDB := query.Session(&gorm.Session{})
|
||||
|
||||
resultData, err := listQueryDB.
|
||||
Order(h.readDB.AppMessageLog.SendTime.Desc()).
|
||||
Limit(req.PageSize).
|
||||
Offset((req.Page - 1) * req.PageSize).
|
||||
Find()
|
||||
if err != nil {
|
||||
ctx.AbortWithError(core.Error(
|
||||
http.StatusBadRequest,
|
||||
code.ListMessageError,
|
||||
fmt.Sprintf("%s:%s", code.Text(code.ListMessageError), err.Error())),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
count, err := countQueryDB.Count()
|
||||
if err != nil {
|
||||
ctx.AbortWithError(core.Error(
|
||||
http.StatusBadRequest,
|
||||
code.ListMessageError,
|
||||
fmt.Sprintf("%s:%s", code.Text(code.ListMessageError), err.Error())),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
res.Page = req.Page
|
||||
res.PageSize = req.PageSize
|
||||
res.Total = count
|
||||
res.List = make([]listMessageData, len(resultData))
|
||||
|
||||
for k, v := range resultData {
|
||||
res.List[k] = listMessageData{
|
||||
SendTime: timeutil.FriendlyTime(v.SendTime),
|
||||
SenderID: v.SenderID,
|
||||
SenderName: v.SenderName,
|
||||
ReceiverID: v.ReceiverID,
|
||||
Content: v.Content,
|
||||
MsgType: v.MsgType,
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Payload(res)
|
||||
}
|
||||
}
|
||||
@ -37,6 +37,7 @@ const (
|
||||
DeleteKeywordMaterialError = 20308
|
||||
|
||||
SendMessageError = 20401
|
||||
ListMessageError = 20402
|
||||
)
|
||||
|
||||
func Text(code int) string {
|
||||
|
||||
@ -23,4 +23,5 @@ var zhCNText = map[int]string{
|
||||
ModifyKeywordMaterialError: "修改关键字素材失败",
|
||||
|
||||
SendMessageError: "发送消息失败",
|
||||
ListMessageError: "获取消息列表失败",
|
||||
}
|
||||
|
||||
@ -59,6 +59,11 @@ func NewHTTPMux(logger logger.CustomLogger, db mysql.Repo, cron cron.Server) (co
|
||||
adminNonAuthApiRouter.POST("/upload/image", uploadHandler.UploadImage()) // 上传图片
|
||||
}
|
||||
|
||||
appNonAuthApiRouter := mux.Group("/app")
|
||||
{
|
||||
appNonAuthApiRouter.GET("/messages", messageHandler.AppMessagePageList()) // 消息列表
|
||||
}
|
||||
|
||||
// 管理端认证接口路由组
|
||||
adminAuthApiRouter := mux.Group("/admin", core.WrapAuthHandler(interceptorHandler.AdminTokenAuthVerify))
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user