From 6e6723a664c6dede134594a65de95723269a26a0 Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 24 Feb 2026 01:00:30 -0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=20=E5=8F=82=E7=85=A7rwdurian?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=87=8D=E5=86=99AliyunSmsProvider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用 sendSmsWithOptions + RuntimeOptions 替代 sendSms, 与 rwdurian identity-service 已验证的实现保持一致。 Co-Authored-By: Claude Opus 4.6 --- .../infrastructure/sms/aliyun-sms.provider.ts | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/backend/services/auth-service/src/infrastructure/sms/aliyun-sms.provider.ts b/backend/services/auth-service/src/infrastructure/sms/aliyun-sms.provider.ts index fc6aedd..8669a73 100644 --- a/backend/services/auth-service/src/infrastructure/sms/aliyun-sms.provider.ts +++ b/backend/services/auth-service/src/infrastructure/sms/aliyun-sms.provider.ts @@ -5,8 +5,8 @@ import { ISmsProvider, SmsDeliveryResult } from './sms-provider.interface'; /** * 阿里云 SMS Provider * - * 使用阿里云短信服务 (dysmsapi) 发送验证码。 - * 需要安装: npm install @alicloud/dysmsapi20170525 @alicloud/openapi-client + * 参考 rwdurian identity-service 已验证的实现, + * 使用 sendSmsWithOptions + RuntimeOptions 调用阿里云短信 API。 * * 环境变量: * - ALIYUN_ACCESS_KEY_ID @@ -36,28 +36,40 @@ export class AliyunSmsProvider implements ISmsProvider { const templateCode = this.getTemplateCode(type); // eslint-disable-next-line @typescript-eslint/no-var-requires - const { SendSmsRequest } = require('@alicloud/dysmsapi20170525'); - const request = new SendSmsRequest({ + const Dysmsapi = require('@alicloud/dysmsapi20170525'); + // eslint-disable-next-line @typescript-eslint/no-var-requires + const $Util = require('@alicloud/tea-util'); + + const sendSmsRequest = new Dysmsapi.SendSmsRequest({ phoneNumbers: phoneNumber, signName, templateCode, templateParam, }); - const result = await client.sendSms(request); - if (result.body?.code === 'OK') { + const runtime = new $Util.RuntimeOptions({ + connectTimeout: 15000, + readTimeout: 15000, + }); + + const response = await client.sendSmsWithOptions(sendSmsRequest, runtime); + + if (response.body?.code === 'OK') { + this.logger.log( + `SMS sent: phone=${phone.slice(0, 3)}****${phone.slice(-4)} bizId=${response.body.bizId}`, + ); return { success: true, - providerId: result.body.bizId, + providerId: response.body.bizId, }; } this.logger.error( - `Aliyun SMS failed: ${result.body?.code} - ${result.body?.message}`, + `Aliyun SMS failed: ${response.body?.code} - ${response.body?.message}`, ); return { success: false, - errorMsg: `${result.body?.code}: ${result.body?.message}`, + errorMsg: `${response.body?.code}: ${response.body?.message}`, }; } catch (error: any) { this.logger.error(`Aliyun SMS error: ${error.message}`); @@ -66,14 +78,12 @@ export class AliyunSmsProvider implements ISmsProvider { } private getTemplateCode(type: SmsVerificationType): string { - // 可为不同类型配置不同模板,默认使用通用模板 return process.env.ALIYUN_SMS_TEMPLATE_CODE || ''; } private async getClient() { if (this.client) return this.client; - // 动态 require (仅 SMS_ENABLED=true 时调用, 开发环境不安装这些包) // eslint-disable-next-line @typescript-eslint/no-var-requires const Dysmsapi20170525 = require('@alicloud/dysmsapi20170525').default; // eslint-disable-next-line @typescript-eslint/no-var-requires @@ -86,6 +96,7 @@ export class AliyunSmsProvider implements ISmsProvider { }); this.client = new Dysmsapi20170525(config); + this.logger.log('Aliyun SMS client initialized'); return this.client; } }