From c689157ce6ba97c3ee2842284f1131bfa5175324 Mon Sep 17 00:00:00 2001 From: zzlgreat Date: Fri, 12 Dec 2025 00:42:55 +0800 Subject: [PATCH] update pay ui --- app.py | 56 +++++++++++++++++++++++++-------------- sms_sender.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 20 deletions(-) create mode 100644 sms_sender.py diff --git a/app.py b/app.py index a99ce982..e97ac0c7 100755 --- a/app.py +++ b/app.py @@ -3140,30 +3140,46 @@ def register(): def send_sms_code(phone, code, template_id): - """发送短信验证码""" + """发送短信验证码(使用 subprocess 绕过 eventlet DNS 问题)""" + import subprocess + import os + try: - cred = credential.Credential(SMS_SECRET_ID, SMS_SECRET_KEY) - httpProfile = HttpProfile() - httpProfile.endpoint = "sms.tencentcloudapi.com" + # 获取脚本路径 + script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'sms_sender.py') - clientProfile = ClientProfile() - clientProfile.httpProfile = httpProfile - client = sms_client.SmsClient(cred, "ap-beijing", clientProfile) + print(f"[短信] 准备发送验证码到 {phone},模板ID: {template_id}") - req = models.SendSmsRequest() - params = { - "PhoneNumberSet": [phone], - "SmsSdkAppId": SMS_SDK_APP_ID, - "TemplateId": template_id, - "SignName": SMS_SIGN_NAME, - "TemplateParamSet": [code, "5"] if template_id == SMS_TEMPLATE_LOGIN else [code] - } - req.from_json_string(json.dumps(params)) + # 使用 subprocess 在独立进程中发送短信(绕过 eventlet DNS) + result = subprocess.run( + [ + sys.executable, # 使用当前 Python 解释器 + script_path, + phone, + code, + template_id, + SMS_SECRET_ID, + SMS_SECRET_KEY, + SMS_SDK_APP_ID, + SMS_SIGN_NAME + ], + capture_output=True, + text=True, + timeout=30 + ) - resp = client.SendSms(req) - return True - except TencentCloudSDKException as err: - print(f"SMS Error: {err}") + if result.returncode == 0: + print(f"[短信] ✓ 发送成功: {result.stdout.strip()}") + return True + else: + print(f"[短信] ✗ 发送失败: {result.stderr.strip()}") + return False + + except subprocess.TimeoutExpired: + print(f"[短信] ✗ 发送超时") + return False + except Exception as e: + print(f"[短信] ✗ 发送异常: {type(e).__name__}: {e}") return False diff --git a/sms_sender.py b/sms_sender.py new file mode 100644 index 00000000..143af9be --- /dev/null +++ b/sms_sender.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +独立的短信发送脚本(绕过 eventlet DNS 问题) + +使用方式: + python sms_sender.py + +返回值: + 成功返回 0,失败返回 1 +""" + +import sys +import json + + +def send_sms(phone, code, template_id, secret_id, secret_key, app_id, sign_name): + """发送短信验证码""" + try: + from tencentcloud.common import credential + from tencentcloud.common.profile.client_profile import ClientProfile + from tencentcloud.common.profile.http_profile import HttpProfile + from tencentcloud.sms.v20210111 import sms_client, models + from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException + + cred = credential.Credential(secret_id, secret_key) + httpProfile = HttpProfile() + httpProfile.endpoint = "sms.tencentcloudapi.com" + + clientProfile = ClientProfile() + clientProfile.httpProfile = httpProfile + client = sms_client.SmsClient(cred, "ap-beijing", clientProfile) + + req = models.SendSmsRequest() + # 登录模板需要两个参数:验证码和有效期 + template_params = [code, "5"] if "login" in template_id.lower() or template_id == "2365498" else [code] + + params = { + "PhoneNumberSet": [phone], + "SmsSdkAppId": app_id, + "TemplateId": template_id, + "SignName": sign_name, + "TemplateParamSet": template_params + } + req.from_json_string(json.dumps(params)) + + resp = client.SendSms(req) + print(f"SMS sent successfully: {resp.to_json_string()}") + return True + + except TencentCloudSDKException as err: + print(f"SMS Error: {err}") + return False + except Exception as e: + print(f"Unexpected error: {e}") + return False + + +if __name__ == "__main__": + if len(sys.argv) != 8: + print("Usage: python sms_sender.py ") + sys.exit(1) + + phone = sys.argv[1] + code = sys.argv[2] + template_id = sys.argv[3] + secret_id = sys.argv[4] + secret_key = sys.argv[5] + app_id = sys.argv[6] + sign_name = sys.argv[7] + + success = send_sms(phone, code, template_id, secret_id, secret_key, app_id, sign_name) + sys.exit(0 if success else 1)