feat: 新增微信小程序客户端鉴权与登录功能

This commit is contained in:
andyesfly committed 2026-10-24 21:15:14 +08:00
1 parent 9d3bb13ecf
commit 4c48b04d5e
13 files changed
+685 -5

No files matched your search

@@ -0,0 +1,91 @@
import { Body, Controller, Get, Inject, Param, Post, Put, Query, Files } from "@midwayjs/core";
import { Context } from "@midwayjs/koa";
import { UploadFileInfo } from "@midwayjs/upload";
import { CustomerAppService } from "../service/customer_app.service";
import { UploadService } from "../service/upload.service";
import {
CustomerAcceptRepairDTO,
CustomerCreateRepairDTO,
CustomerLoginDTO,
CustomerWxBindDTO,
CustomerWxLoginDTO
} from "../dto/customer_app.dto";
@Controller("/api/customer-app")
export class CustomerAppController {
@Inject()
ctx!: Context;
@Inject()
customerAppService!: CustomerAppService;
@Inject()
uploadService!: UploadService;
// 账号密码登录
@Post("/auth/login")
async login(@Body() dto: CustomerLoginDTO) {
return this.customerAppService.login(dto.phone, dto.password);
}
// 微信静默登录(未绑定返回 bindToken)
@Post("/auth/wx-login")
async wxLogin(@Body() dto: CustomerWxLoginDTO) {
return this.customerAppService.wxLogin(dto.code);
}
// 微信首次绑定:手机号 + 密码 + bindToken
@Post("/auth/wx-bind")
async wxBind(@Body() dto: CustomerWxBindDTO) {
return this.customerAppService.wxBind(dto.bindToken, dto.phone, dto.password);
}
// 我的:账号、客户、统计、客户经理
@Get("/profile")
async profile() {
return this.customerAppService.profile(this.currentAccountId());
}
// 报修工单列表(按状态页签过滤)
@Get("/repairs")
async listRepairs(
@Query("tab") tab: string,
@Query("page") page: string,
@Query("pageSize") pageSize: string
) {
return this.customerAppService.listRepairs(this.currentAccountId(), tab, Number(page) || 1, Number(pageSize) || 20);
}
// 客户提交报修,落库为待处理工单
@Post("/repairs")
async createRepair(@Body() dto: CustomerCreateRepairDTO) {
return this.customerAppService.createRepair(this.currentAccountId(), dto);
}
// 客户验收并评价
@Put("/repairs/:id/accept")
async acceptRepair(@Param("id") id: string, @Body() dto: CustomerAcceptRepairDTO) {
return this.customerAppService.acceptRepair(this.currentAccountId(), Number(id), dto.rating);
}
// 报修照片上传
@Post("/upload/image")
async uploadImage(@Files() files: UploadFileInfo<string>[]) {
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 };
}
}
private currentAccountId(): number {
const customer = this.ctx.state.customer;
if (!customer?.accountId) {
throw new Error("未登录");
}
return Number(customer.accountId);
}
}