feat: 添加通知微服务,支持站内通知已读状态

This commit is contained in:
zetaloop
2026-04-24 12:44:59 +08:00
parent 95f2f10f9f
commit b557bfcc2e
52 changed files with 7035 additions and 30 deletions
@@ -0,0 +1,27 @@
package notification
import (
"time"
"juwan-backend/app/notification/api/internal/types"
"juwan-backend/app/notification/rpc/notificationservice"
)
func formatUnix(ts int64) string {
if ts <= 0 {
return ""
}
return time.Unix(ts, 0).UTC().Format(time.RFC3339)
}
func toAPINotification(n *notificationservice.Notifications) types.Notification {
return types.Notification{
Id: n.GetId(),
Type: n.GetType(),
Title: n.GetTitle(),
Content: n.GetContent(),
Read: n.GetRead(),
Link: n.GetLink(),
CreatedAt: formatUnix(n.GetCreatedAt()),
}
}
@@ -0,0 +1,63 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package notification
import (
"context"
"juwan-backend/app/notification/api/internal/svc"
"juwan-backend/app/notification/api/internal/types"
"juwan-backend/app/notification/rpc/notificationservice"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type ListNotificationsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取通知列表
func NewListNotificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListNotificationsLogic {
return &ListNotificationsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ListNotificationsLogic) ListNotifications(req *types.PageReq) (resp *types.NotificationListResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
limit := req.Limit
if limit <= 0 {
limit = 20
}
out, err := l.svcCtx.NotificationRpc.SearchNotifications(l.ctx, &notificationservice.SearchNotificationsReq{
Offset: req.Offset,
Limit: limit,
UserId: &uid,
})
if err != nil {
return nil, err
}
items := make([]types.Notification, 0, len(out.GetNotifications()))
for _, item := range out.GetNotifications() {
items = append(items, toAPINotification(item))
}
return &types.NotificationListResp{
Items: items,
Meta: types.PageMeta{
Total: int64(len(items)),
Offset: req.Offset,
Limit: limit,
},
}, nil
}
@@ -0,0 +1,62 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package notification
import (
"context"
"time"
"juwan-backend/app/notification/api/internal/svc"
"juwan-backend/app/notification/api/internal/types"
"juwan-backend/app/notification/rpc/notificationservice"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type ReadAllNotificationsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 全部已读
func NewReadAllNotificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReadAllNotificationsLogic {
return &ReadAllNotificationsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ReadAllNotificationsLogic) ReadAllNotifications(req *types.EmptyResp) (resp *types.EmptyResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
unread := false
out, err := l.svcCtx.NotificationRpc.SearchNotifications(l.ctx, &notificationservice.SearchNotificationsReq{
Offset: 0,
Limit: 100,
UserId: &uid,
Read: &unread,
})
if err != nil {
return nil, err
}
read := true
now := time.Now().Unix()
for _, item := range out.GetNotifications() {
_, err = l.svcCtx.NotificationRpc.UpdateNotifications(l.ctx, &notificationservice.UpdateNotificationsReq{
Id: item.GetId(),
Read: &read,
UpdatedAt: &now,
})
if err != nil {
return nil, err
}
}
return &types.EmptyResp{}, nil
}
@@ -0,0 +1,63 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package notification
import (
"context"
"errors"
"time"
"juwan-backend/app/notification/api/internal/svc"
"juwan-backend/app/notification/api/internal/types"
"juwan-backend/app/notification/rpc/notificationservice"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type ReadNotificationLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 标记已读
func NewReadNotificationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReadNotificationLogic {
return &ReadNotificationLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ReadNotificationLogic) ReadNotification(req *types.PathId) (resp *types.EmptyResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
current, err := l.svcCtx.NotificationRpc.GetNotificationsById(l.ctx, &notificationservice.GetNotificationsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if current.GetNotifications() == nil {
return nil, errors.New("notification not found")
}
if current.GetNotifications().GetUserId() != uid {
return nil, errors.New("notification not found")
}
read := true
now := time.Now().Unix()
_, err = l.svcCtx.NotificationRpc.UpdateNotifications(l.ctx, &notificationservice.UpdateNotificationsReq{
Id: req.Id,
Read: &read,
UpdatedAt: &now,
})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}