import { Controller, Get, Put, Inject, Query, Param } from "@midwayjs/core"; import { Context } from "@midwayjs/koa"; import { NoticeService } from "../service/notice.service"; @Controller("/api/notices") export class NoticeController { @Inject() ctx!: Context; @Inject() noticeService!: NoticeService; @Get("/") async list(@Query() query: any) { return await this.noticeService.listMy("user", this.currentUserId(), query); } // 注意:静态路径必须注册在动态路径 /:id/read 之前 @Get("/unread-count") async unreadCount() { return await this.noticeService.unreadCount("user", this.currentUserId()); } @Put("/read-all") async markAllRead() { return await this.noticeService.markAllRead("user", this.currentUserId()); } @Put("/:id/read") async markRead(@Param("id") id: string) { return await this.noticeService.markRead("user", this.currentUserId(), Number(id)); } private currentUserId(): number { return Number(this.ctx.state.user?.userId); } }