import { Controller, Get, Post, Put, Del, Inject, Body, Param, Query, Files } from "@midwayjs/core"; import { Context } from "@midwayjs/koa"; import { UploadFileInfo } from "@midwayjs/upload"; import { WorkerAppService } from "../service/worker_app.service"; import { NoticeService } from "../service/notice.service"; import { UploadService } from "../service/upload.service"; import { WorkerRegisterDTO, WorkerLoginDTO, WorkerProfileUpdateDTO, WorkerAppCertDTO } from "../dto/worker_app.dto"; @Controller("/api/worker-app") export class WorkerAppController { @Inject() ctx!: Context; @Inject() workerAppService!: WorkerAppService; @Inject() noticeService!: NoticeService; @Inject() uploadService!: UploadService; // 勘察现场照片上传:电工 token 走 /api/worker-app/* 通道,复用对象存储上传能力 @Post("/upload/image") async uploadImage(@Files() files: UploadFileInfo[]) { const file = files && files[0]; if (!file) { return { code: 400, message: "请选择要上传的图片", data: null }; } try { const data = await this.uploadService.uploadFile(file); return { code: 200, message: "上传成功", data }; } catch (err: any) { return { code: 500, message: `图片上传失败:${err?.message || "对象存储不可用"}`, data: null }; } } // 注册(匿名) @Post("/auth/register") async register(@Body() body: WorkerRegisterDTO) { return this.workerAppService.register(body); } // 登录(匿名) @Post("/auth/login") async login(@Body() body: WorkerLoginDTO) { return this.workerAppService.login(body); } // 个人资料 @Get("/profile") async profile() { return this.workerAppService.profile(this.currentWorkerId()); } @Put("/profile") async updateProfile(@Body() body: WorkerProfileUpdateDTO) { return this.workerAppService.updateProfile(this.currentWorkerId(), body); } // 资质证照:静态路径必须声明在动态路径之前 @Get("/certs") async listCerts() { return this.workerAppService.listCerts(this.currentWorkerId()); } @Post("/certs") async createCert(@Body() body: WorkerAppCertDTO) { return this.workerAppService.createCert(this.currentWorkerId(), body); } @Put("/certs/:id") async updateCert(@Param("id") id: string, @Body() body: WorkerAppCertDTO) { return this.workerAppService.updateCert(this.currentWorkerId(), Number(id), body); } @Del("/certs/:id") async removeCert(@Param("id") id: string) { return this.workerAppService.removeCert(this.currentWorkerId(), Number(id)); } // 重新提交审核 @Post("/audit/resubmit") async resubmit() { return this.workerAppService.resubmit(this.currentWorkerId()); } // 站内消息中心:静态路径必须声明在动态路径 /notices/:id/read 之前 @Get("/notices") async listNotices(@Query() query: any) { return this.noticeService.listMy("worker", this.currentWorkerId(), query); } @Get("/notices/unread-count") async noticeUnreadCount() { return this.noticeService.unreadCount("worker", this.currentWorkerId()); } @Put("/notices/read-all") async markAllNoticesRead() { return this.noticeService.markAllRead("worker", this.currentWorkerId()); } @Put("/notices/:id/read") async markNoticeRead(@Param("id") id: string) { return this.noticeService.markRead("worker", this.currentWorkerId(), Number(id)); } // 订单池:静态路径必须声明在动态路径 /orders/:source/:id 之前 @Get("/orders/pool") async orderPool() { return this.workerAppService.orderPool(); } // 我的订单(可按 status=pending|completed 过滤) @Get("/orders") async myOrders(@Query("status") status?: string) { return this.workerAppService.myOrders(this.currentWorkerId(), status); } // 订单详情 @Get("/orders/:source/:id") async orderDetail(@Param("source") source: string, @Param("id") id: string) { if (!this.workerAppService.isValidSource(source)) { return { code: 400, message: "不支持的订单来源" }; } return this.workerAppService.orderDetail(this.currentWorkerId(), source, Number(id)); } // 接单 @Post("/orders/:source/:id/grab") async grabOrder(@Param("source") source: string, @Param("id") id: string) { if (!this.workerAppService.isValidSource(source)) { return { code: 400, message: "不支持的订单来源" }; } return this.workerAppService.grabOrder(this.currentWorkerId(), source, Number(id)); } // 完成订单 @Put("/orders/:source/:id/complete") async completeOrder(@Param("source") source: string, @Param("id") id: string) { if (!this.workerAppService.isValidSource(source)) { return { code: 400, message: "不支持的订单来源" }; } return this.workerAppService.completeOrder(this.currentWorkerId(), source, Number(id)); } // 勘察明细维护(仅勘察单):全量替换,含建筑/楼层/房间与现场照片 @Put("/orders/:source/:id/details") async saveDetails(@Param("source") source: string, @Param("id") id: string, @Body() body: any) { if (source !== "survey") { return { code: 400, message: "仅勘察订单支持维护明细" }; } return this.workerAppService.saveSurveyDetails(this.currentWorkerId(), Number(id), body); } // 收益汇总 @Get("/earnings/summary") async earningsSummary() { return this.workerAppService.earningsSummary(this.currentWorkerId()); } // 当前登录电工 ID(由 WorkerAuthMiddleware 写入) private currentWorkerId(): number { return this.ctx.state.worker?.workerId; } }