feat: 添加通知微服务,支持站内通知已读状态
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
NotificationRpcConf zrpc.RpcClientConf
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package notification
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"juwan-backend/app/notification/api/internal/logic/notification"
|
||||
"juwan-backend/app/notification/api/internal/svc"
|
||||
"juwan-backend/app/notification/api/internal/types"
|
||||
)
|
||||
|
||||
// 获取通知列表
|
||||
func ListNotificationsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.PageReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := notification.NewListNotificationsLogic(r.Context(), svcCtx)
|
||||
resp, err := l.ListNotifications(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package notification
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"juwan-backend/app/notification/api/internal/logic/notification"
|
||||
"juwan-backend/app/notification/api/internal/svc"
|
||||
"juwan-backend/app/notification/api/internal/types"
|
||||
)
|
||||
|
||||
// 全部已读
|
||||
func ReadAllNotificationsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.EmptyResp
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := notification.NewReadAllNotificationsLogic(r.Context(), svcCtx)
|
||||
resp, err := l.ReadAllNotifications(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package notification
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"juwan-backend/app/notification/api/internal/logic/notification"
|
||||
"juwan-backend/app/notification/api/internal/svc"
|
||||
"juwan-backend/app/notification/api/internal/types"
|
||||
)
|
||||
|
||||
// 标记已读
|
||||
func ReadNotificationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.PathId
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := notification.NewReadNotificationLogic(r.Context(), svcCtx)
|
||||
resp, err := l.ReadNotification(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
notification "juwan-backend/app/notification/api/internal/handler/notification"
|
||||
"juwan-backend/app/notification/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
// 获取通知列表
|
||||
Method: http.MethodGet,
|
||||
Path: "/notifications",
|
||||
Handler: notification.ListNotificationsHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 标记已读
|
||||
Method: http.MethodPut,
|
||||
Path: "/notifications/:id/read",
|
||||
Handler: notification.ReadNotificationHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 全部已读
|
||||
Method: http.MethodPut,
|
||||
Path: "/notifications/read-all",
|
||||
Handler: notification.ReadAllNotificationsHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithPrefix("/api/v1"),
|
||||
)
|
||||
}
|
||||
@@ -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, ¬ificationservice.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, ¬ificationservice.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, ¬ificationservice.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, ¬ificationservice.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, ¬ificationservice.UpdateNotificationsReq{
|
||||
Id: req.Id,
|
||||
Read: &read,
|
||||
UpdatedAt: &now,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.EmptyResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"juwan-backend/app/notification/api/internal/config"
|
||||
"juwan-backend/app/notification/rpc/notificationservice"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
NotificationRpc notificationservice.NotificationService
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
NotificationRpc: notificationservice.NewNotificationService(zrpc.MustNewClient(c.NotificationRpcConf)),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
|
||||
package types
|
||||
|
||||
type EmptyResp struct {
|
||||
}
|
||||
|
||||
type Notification struct {
|
||||
Id int64 `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Read bool `json:"read"`
|
||||
Link string `json:"link,optional"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
|
||||
type NotificationListResp struct {
|
||||
Items []Notification `json:"items"`
|
||||
Meta PageMeta `json:"meta"`
|
||||
}
|
||||
|
||||
type PageMeta struct {
|
||||
Total int64 `json:"total"`
|
||||
Offset int64 `json:"offset"`
|
||||
Limit int64 `json:"limit"`
|
||||
}
|
||||
|
||||
type PageReq struct {
|
||||
Offset int64 `form:"offset,default=0"`
|
||||
Limit int64 `form:"limit,default=20"`
|
||||
}
|
||||
|
||||
type PathId struct {
|
||||
Id int64 `path:"id"`
|
||||
}
|
||||
|
||||
type SimpleUser struct {
|
||||
Id string `json:"id"`
|
||||
Nickname string `json:"nickname"`
|
||||
Avatar string `json:"avatar"`
|
||||
}
|
||||
|
||||
type UserProfile struct {
|
||||
Id string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Nickname string `json:"nickname"`
|
||||
Avatar string `json:"avatar"`
|
||||
Role string `json:"role"` // consumer, player, owner, admin
|
||||
VerifiedRoles []string `json:"verifiedRoles"`
|
||||
VerificationStatus map[string]string `json:"verificationStatus"`
|
||||
Phone string `json:"phone,optional"`
|
||||
Bio string `json:"bio,optional"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
Reference in New Issue
Block a user