import { $fetch } from 'ofetch' import { createError, defineEventHandler, readBody, getHeader, getCookie } from 'h3' import { useRuntimeConfig } from '#imports' import { getTenantFromContext } from '../../../utils/tenant' /** * 修改密码(**需登录态**,双因子:旧密码 + 账号绑定手机号短信验证码) * PUT /api/tender/password/change { oldPassword, smsCode, newPassword, confirmPassword } * 代理到 mp-api /api/hjc/auth/password/change。 * * 原样透传 ApiResult:后端对「旧密码不正确 / 短信验证码不正确 / 密码强度不够」都有明确文案, * 由前端直接展示。 */ export default defineEventHandler(async (event) => { const config = useRuntimeConfig() const ctx = getTenantFromContext(event, config) const body = await readBody(event) const cookieToken = getCookie(event, 'hjc_token') const auth = getHeader(event, 'authorization') || (cookieToken ? `Bearer ${cookieToken}` : null) try { return await $fetch('/hjc/auth/password/change', { baseURL: config.public.modulesApiBase, method: 'PUT', headers: { TenantId: ctx.tenantId, ...(auth ? { Authorization: auth } : {}) }, query: { TenantId: ctx.tenantId }, body }) } catch (error: any) { throw createError({ statusCode: error?.statusCode || error?.response?.status || 502, statusMessage: error?.data?.message || error?.statusMessage || '修改密码失败' }) } })