feat(sms): align provider and verification flow
This commit is contained in:
@@ -14,11 +14,6 @@ interface ProviderInterface {
|
||||
*/
|
||||
public function get_key(): string;
|
||||
|
||||
/**
|
||||
* 供应商显示名称
|
||||
*/
|
||||
public function get_label(): string;
|
||||
|
||||
/**
|
||||
* 校验配置完整性,返回缺失字段列表
|
||||
*
|
||||
|
||||
+123
-313
@@ -7,335 +7,145 @@ namespace AKSRT\Sms;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* ZibLL-style SMS dispatcher and send-rate limiting.
|
||||
*/
|
||||
class SMS {
|
||||
|
||||
private static $instance = null;
|
||||
private static $instance = null;
|
||||
|
||||
public static function get_instance(): self {
|
||||
if (null === self::$instance) self::$instance = new self();
|
||||
return self::$instance;
|
||||
}
|
||||
private const RATE_LIMIT = 60;
|
||||
private const DAILY_LIMIT = 10;
|
||||
|
||||
public function init(): void {
|
||||
add_action('wp_ajax_aksrt_sms_test', [$this, 'ajax_test_provider']);
|
||||
add_action('wp_ajax_aksrt_sms_status', [$this, 'ajax_provider_status']);
|
||||
}
|
||||
public static function get_instance(): self {
|
||||
if (null === self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取短信供应商配置
|
||||
*/
|
||||
public function get_provider_config(): array {
|
||||
if (class_exists('\AKSRT\Settings\Settings')) {
|
||||
return \AKSRT\Settings\Settings::get_sms_settings();
|
||||
}
|
||||
return ['sms_enabled' => false, 'sms_provider' => ''];
|
||||
}
|
||||
public function get_provider_config(): array {
|
||||
if (class_exists('\\AKSRT\\Settings\\Settings')) {
|
||||
return \AKSRT\Settings\Settings::get_sms_settings();
|
||||
}
|
||||
return ['sms_sdk' => 'null', 'sms_number_limit_ch' => true];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前 provider adapter
|
||||
*/
|
||||
public function get_provider(): ?ProviderInterface {
|
||||
$config = $this->get_provider_config();
|
||||
$key = sanitize_key((string) ($config['sms_provider'] ?? ''));
|
||||
return SMSProviderFactory::get($key);
|
||||
}
|
||||
/**
|
||||
* Phone-number validation follows the selected domestic/international mode.
|
||||
*/
|
||||
public static function is_phonenumber(string $phone, ?bool $domestic_only = null): bool {
|
||||
if ($domestic_only === null) {
|
||||
$config = self::get_instance()->get_provider_config();
|
||||
$domestic_only = !empty($config['sms_number_limit_ch']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信验证码
|
||||
*
|
||||
* @param string $phone 手机号
|
||||
* @return array{success: bool, message: string, remaining_time?: int}
|
||||
*/
|
||||
public function send_code(string $phone): array {
|
||||
$config = $this->get_provider_config();
|
||||
if ($domestic_only) {
|
||||
return (bool) preg_match('/^1[3-9]\d{9}$/', $phone);
|
||||
}
|
||||
|
||||
if (empty($config['sms_enabled'])) {
|
||||
return ['success' => false, 'message' => __('短信服务未启用', 'aksrt')];
|
||||
}
|
||||
return (bool) preg_match('/^(?:\d{5,}|\+\d{6,}|\+?\d{2,}[-\s]\d{2,}[0-9\-\s]{0,15})$/', $phone);
|
||||
}
|
||||
|
||||
$provider_key = sanitize_key((string) ($config['sms_provider'] ?? ''));
|
||||
if ($provider_key === '') {
|
||||
return ['success' => false, 'message' => __('未配置短信供应商', 'aksrt')];
|
||||
}
|
||||
/**
|
||||
* Send a six-digit verification code. The caller stores it after success.
|
||||
*
|
||||
* @return array{success: bool, message: string, remaining_time?: int}
|
||||
*/
|
||||
public function send_code(string $phone, string $code): array {
|
||||
$config = $this->get_provider_config();
|
||||
$sdk = sanitize_key((string) ($config['sms_sdk'] ?? 'null'));
|
||||
if ($sdk === '' || $sdk === 'null') {
|
||||
return ['success' => false, 'message' => __('短信接口未启用', 'aksrt')];
|
||||
}
|
||||
if (!self::is_phonenumber($phone, !empty($config['sms_number_limit_ch']))) {
|
||||
return ['success' => false, 'message' => __('手机号码格式有误', 'aksrt')];
|
||||
}
|
||||
|
||||
$provider = SMSProviderFactory::get($provider_key);
|
||||
if ($provider === null) {
|
||||
return ['success' => false, 'message' => sprintf(__('短信供应商 "%s" 不可用', 'aksrt'), $provider_key)];
|
||||
}
|
||||
$provider = SMSProviderFactory::get($sdk);
|
||||
if ($provider === null) {
|
||||
return ['success' => false, 'message' => __('暂无短信接口,请检查短信服务商设置', 'aksrt')];
|
||||
}
|
||||
|
||||
// 校验配置完整性
|
||||
$missing = $provider->validate_config($config);
|
||||
if (!empty($missing)) {
|
||||
return ['success' => false, 'message' => sprintf(__('短信配置不完整,缺少: %s', 'aksrt'), implode(', ', $missing))];
|
||||
}
|
||||
$missing = $provider->validate_config($config);
|
||||
if ($missing) {
|
||||
return ['success' => false, 'message' => sprintf(__('短信接口缺少配置:%s', 'aksrt'), implode(', ', $missing))];
|
||||
}
|
||||
|
||||
// 频率限制:间隔限制(对齐 legacy 60 秒冷却 + 返回剩余时间)
|
||||
$rate_limit = max(10, (int) ($config['sms_rate_limit'] ?? 60));
|
||||
$rate_key = 'aksrt_sms_rate_' . md5($phone);
|
||||
$rate_set = get_transient($rate_key);
|
||||
if ($rate_set !== false) {
|
||||
// 计算剩余等待时间
|
||||
$rate_ts_key = 'aksrt_sms_rate_ts_' . md5($phone);
|
||||
$sent_time = (int) get_transient($rate_ts_key);
|
||||
$elapsed = $sent_time > 0 ? time() - $sent_time : $rate_limit;
|
||||
$remaining = max(1, $rate_limit - $elapsed);
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => sprintf(__('请 %d 秒后再试', 'aksrt'), $remaining),
|
||||
'remaining_time' => $remaining,
|
||||
];
|
||||
}
|
||||
$phone_hash = md5($phone);
|
||||
$rate_limit = self::RATE_LIMIT;
|
||||
$rate_key = 'aksrt_sms_rate_' . $phone_hash;
|
||||
if (get_transient($rate_key) !== false) {
|
||||
$sent_time = (int) get_transient('aksrt_sms_rate_ts_' . $phone_hash);
|
||||
$remaining = max(1, $rate_limit - (time() - $sent_time));
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => sprintf(__('请 %d 秒后再试', 'aksrt'), $remaining),
|
||||
'remaining_time' => $remaining,
|
||||
];
|
||||
}
|
||||
|
||||
// 每日限制
|
||||
$daily_limit = max(1, (int) ($config['sms_daily_limit'] ?? 10));
|
||||
$daily_key = 'aksrt_sms_daily_' . md5($phone) . '_' . date('Ymd');
|
||||
$daily_count = (int) get_transient($daily_key);
|
||||
if ($daily_count >= $daily_limit) {
|
||||
return ['success' => false, 'message' => __('今日发送次数已达上限', 'aksrt')];
|
||||
}
|
||||
$daily_limit = self::DAILY_LIMIT;
|
||||
$daily_key = 'aksrt_sms_daily_' . $phone_hash . '_' . wp_date('Ymd');
|
||||
$daily_count = (int) get_transient($daily_key);
|
||||
if ($daily_count >= $daily_limit) {
|
||||
return ['success' => false, 'message' => __('今日发送次数已达上限', 'aksrt')];
|
||||
}
|
||||
|
||||
$code_length = max(4, min(8, (int) ($config['sms_code_length'] ?? 6)));
|
||||
$code = $this->generate_code($code_length);
|
||||
$ttl = max(60, min(600, (int) ($config['sms_code_expire'] ?? 300)));
|
||||
$result = $provider->send_code($phone, $code, $config);
|
||||
if (empty($result['success'])) {
|
||||
return ['success' => false, 'message' => (string) ($result['message'] ?? __('验证码发送失败,请稍后重试', 'aksrt'))];
|
||||
}
|
||||
|
||||
// 存储验证码(绑定手机号)
|
||||
set_transient('aksrt_sms_code_' . md5($phone), $code, $ttl);
|
||||
set_transient('aksrt_sms_phone_' . md5($phone), $phone, $ttl);
|
||||
set_transient($rate_key, '1', $rate_limit);
|
||||
set_transient('aksrt_sms_rate_ts_' . md5($phone), time(), $rate_limit);
|
||||
set_transient($daily_key, $daily_count + 1, DAY_IN_SECONDS);
|
||||
set_transient($rate_key, '1', $rate_limit);
|
||||
set_transient('aksrt_sms_rate_ts_' . $phone_hash, time(), $rate_limit);
|
||||
set_transient($daily_key, $daily_count + 1, DAY_IN_SECONDS);
|
||||
|
||||
// 委托 provider 发送
|
||||
$result = $provider->send_code($phone, $code, $config);
|
||||
return [
|
||||
'success' => true,
|
||||
'message' => __('验证码短信已发送', 'aksrt'),
|
||||
'remaining_time' => $rate_limit,
|
||||
];
|
||||
}
|
||||
public function get_provider_status(): array {
|
||||
$config = $this->get_provider_config();
|
||||
$key = sanitize_key((string) ($config['sms_sdk'] ?? 'null'));
|
||||
$enabled = $key !== '' && $key !== 'null';
|
||||
$provider = SMSProviderFactory::get($key);
|
||||
$status = [
|
||||
'enabled' => $enabled,
|
||||
'provider' => $enabled ? $key : 'none',
|
||||
'configured' => false,
|
||||
'available' => false,
|
||||
'verified' => false,
|
||||
'transport' => '',
|
||||
'missing_fields' => [],
|
||||
];
|
||||
if ($provider !== null) {
|
||||
$status = array_merge($status, $provider->get_status($config));
|
||||
}
|
||||
return $status;
|
||||
}
|
||||
|
||||
if (!empty($result['success'])) {
|
||||
return [
|
||||
'success' => true,
|
||||
'message' => $result['message'] ?? __('验证码已发送', 'aksrt'),
|
||||
'remaining_time' => $rate_limit,
|
||||
];
|
||||
}
|
||||
/** Send the reference theme's fixed 888888 test message without storing a code. */
|
||||
public function test_send(string $phone): array {
|
||||
$config = $this->get_provider_config();
|
||||
$sdk = sanitize_key((string) ($config['sms_sdk'] ?? 'null'));
|
||||
if ($sdk === '' || $sdk === 'null') {
|
||||
return ['success' => false, 'message' => __('短信接口未启用', 'aksrt')];
|
||||
}
|
||||
if (!self::is_phonenumber($phone, !empty($config['sms_number_limit_ch']))) {
|
||||
return ['success' => false, 'message' => __('手机号码格式有误', 'aksrt')];
|
||||
}
|
||||
$provider = SMSProviderFactory::get($sdk);
|
||||
if ($provider === null) {
|
||||
return ['success' => false, 'message' => __('暂无短信接口,请检查短信服务商设置', 'aksrt')];
|
||||
}
|
||||
$missing = $provider->validate_config($config);
|
||||
if ($missing) {
|
||||
return ['success' => false, 'message' => sprintf(__('短信接口缺少配置:%s', 'aksrt'), implode(', ', $missing))];
|
||||
}
|
||||
return $provider->send_code($phone, '888888', $config);
|
||||
}
|
||||
|
||||
// 发送失败:删除已存储的验证码,避免无效等待
|
||||
delete_transient('aksrt_sms_code_' . md5($phone));
|
||||
delete_transient('aksrt_sms_phone_' . md5($phone));
|
||||
return ['success' => false, 'message' => $result['message'] ?? __('发送失败,请稍后重试', 'aksrt')];
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证短信验证码(对齐 legacy:同时校验验证码和目标手机号)
|
||||
*
|
||||
* @param string $phone 手机号
|
||||
* @param string $code 验证码
|
||||
* @return array{success: bool, message: string}
|
||||
*/
|
||||
public function verify_code(string $phone, string $code): array {
|
||||
// 错误频率限制检查
|
||||
$error_check = $this->check_error_frequency('sms_verify_' . md5($phone));
|
||||
if (!$error_check['allowed']) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => sprintf(__('验证失败次数过多,请 %d 秒后再试', 'aksrt'), $error_check['remaining']),
|
||||
'remaining_time' => $error_check['remaining'],
|
||||
];
|
||||
}
|
||||
|
||||
$code_key = 'aksrt_sms_code_' . md5($phone);
|
||||
$phone_key = 'aksrt_sms_phone_' . md5($phone);
|
||||
|
||||
$stored_code = get_transient($code_key);
|
||||
$stored_phone = get_transient($phone_key);
|
||||
|
||||
// 验证码不存在或已过期
|
||||
if ($stored_code === false) {
|
||||
$this->record_error('sms_verify_' . md5($phone));
|
||||
return ['success' => false, 'message' => __('验证码已过期或不存在', 'aksrt')];
|
||||
}
|
||||
|
||||
// 验证码不匹配
|
||||
if (!hash_equals((string) $stored_code, $code)) {
|
||||
$this->record_error('sms_verify_' . md5($phone));
|
||||
return ['success' => false, 'message' => __('验证码不正确', 'aksrt')];
|
||||
}
|
||||
|
||||
// 目标手机号绑定校验
|
||||
if ($stored_phone !== false && (string) $stored_phone !== $phone) {
|
||||
$this->record_error('sms_verify_' . md5($phone));
|
||||
return ['success' => false, 'message' => __('手机号不匹配', 'aksrt')];
|
||||
}
|
||||
|
||||
// 验证成功:清除验证码和错误计数
|
||||
delete_transient($code_key);
|
||||
delete_transient($phone_key);
|
||||
$this->reset_error_frequency('sms_verify_' . md5($phone));
|
||||
|
||||
return ['success' => true, 'message' => __('验证成功', 'aksrt')];
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
*/
|
||||
private function generate_code(int $length = 6): string {
|
||||
$max = (int) str_pad('9', $length, '9');
|
||||
return str_pad((string) random_int(0, $max), $length, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------
|
||||
* 错误频率限制(对齐 legacy 的 error_frequency_limit)
|
||||
* ----------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* 检查是否被频率限制
|
||||
*
|
||||
* @return array{allowed: bool, remaining: int}
|
||||
*/
|
||||
private function check_error_frequency(string $key): array {
|
||||
$count_key = 'aksrt_sms_err_' . $key;
|
||||
$time_key = 'aksrt_sms_err_ts_' . $key;
|
||||
|
||||
$count = (int) get_transient($count_key);
|
||||
$last = (int) get_transient($time_key);
|
||||
|
||||
if ($count <= 0) {
|
||||
return ['allowed' => true, 'remaining' => 0];
|
||||
}
|
||||
|
||||
// 超过 10 次错误:3 分钟冷却
|
||||
if ($count >= 10) {
|
||||
$cooldown = 180;
|
||||
$elapsed = time() - $last;
|
||||
if ($elapsed < $cooldown) {
|
||||
return ['allowed' => false, 'remaining' => $cooldown - $elapsed];
|
||||
}
|
||||
// 冷却结束,重置
|
||||
$this->reset_error_frequency($key);
|
||||
return ['allowed' => true, 'remaining' => 0];
|
||||
}
|
||||
|
||||
// 超过 5 次错误:30 秒冷却
|
||||
if ($count >= 5) {
|
||||
$cooldown = 30;
|
||||
$elapsed = time() - $last;
|
||||
if ($elapsed < $cooldown) {
|
||||
return ['allowed' => false, 'remaining' => $cooldown - $elapsed];
|
||||
}
|
||||
}
|
||||
|
||||
return ['allowed' => true, 'remaining' => 0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录一次错误
|
||||
*/
|
||||
private function record_error(string $key): void {
|
||||
$count_key = 'aksrt_sms_err_' . $key;
|
||||
$time_key = 'aksrt_sms_err_ts_' . $key;
|
||||
|
||||
$count = (int) get_transient($count_key);
|
||||
set_transient($count_key, $count + 1, HOUR_IN_SECONDS);
|
||||
set_transient($time_key, time(), HOUR_IN_SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置错误计数
|
||||
*/
|
||||
private function reset_error_frequency(string $key): void {
|
||||
delete_transient('aksrt_sms_err_' . $key);
|
||||
delete_transient('aksrt_sms_err_ts_' . $key);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------
|
||||
* AJAX Handlers
|
||||
* ----------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* AJAX: Test SMS provider (admin only).
|
||||
*/
|
||||
public function ajax_test_provider(): void {
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_send_json_error(['message' => __('权限不足', 'aksrt')]);
|
||||
}
|
||||
|
||||
$phone = sanitize_text_field((string) ($_POST['phone'] ?? ''));
|
||||
if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
|
||||
wp_send_json_error(['message' => __('手机号格式不正确', 'aksrt')]);
|
||||
}
|
||||
|
||||
$result = $this->test_provider($phone);
|
||||
wp_send_json($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX: Get SMS provider status (admin only).
|
||||
*/
|
||||
public function ajax_provider_status(): void {
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_send_json_error(['message' => __('权限不足', 'aksrt')]);
|
||||
}
|
||||
|
||||
wp_send_json_success($this->get_provider_status());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 provider 真实运行时状态
|
||||
*/
|
||||
public function get_provider_status(): array {
|
||||
$config = $this->get_provider_config();
|
||||
$provider_key = sanitize_key((string) ($config['sms_provider'] ?? ''));
|
||||
|
||||
$status = [
|
||||
'enabled' => !empty($config['sms_enabled']),
|
||||
'provider' => $provider_key ?: 'none',
|
||||
'configured' => false,
|
||||
'available' => false,
|
||||
'verified' => false,
|
||||
'transport' => '',
|
||||
'missing_fields' => [],
|
||||
'code_length' => max(4, min(8, (int) ($config['sms_code_length'] ?? 6))),
|
||||
'code_expire' => max(60, min(600, (int) ($config['sms_code_expire'] ?? 300))),
|
||||
'rate_limit' => max(10, (int) ($config['sms_rate_limit'] ?? 60)),
|
||||
'daily_limit' => max(1, (int) ($config['sms_daily_limit'] ?? 10)),
|
||||
'notify_bind' => !empty($config['sms_notify_bind']),
|
||||
'notify_login' => !empty($config['sms_notify_login']),
|
||||
];
|
||||
|
||||
$provider = SMSProviderFactory::get($provider_key);
|
||||
if ($provider !== null) {
|
||||
$provider_status = $provider->get_status($config);
|
||||
$status['configured'] = !empty($provider_status['configured']);
|
||||
$status['available'] = !empty($provider_status['available']);
|
||||
$status['verified'] = !empty($provider_status['verified']);
|
||||
$status['transport'] = (string) ($provider_status['transport'] ?? '');
|
||||
$status['missing_fields'] = (array) ($provider_status['missing_fields'] ?? []);
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test provider connectivity (admin only).
|
||||
*/
|
||||
public function test_provider(string $phone): array {
|
||||
$config = $this->get_provider_config();
|
||||
if (empty($config['sms_enabled'])) {
|
||||
return ['success' => false, 'message' => __('短信服务未启用', 'aksrt')];
|
||||
}
|
||||
|
||||
$provider_key = sanitize_key((string) ($config['sms_provider'] ?? ''));
|
||||
if ($provider_key === '') {
|
||||
return ['success' => false, 'message' => __('未配置短信供应商', 'aksrt')];
|
||||
}
|
||||
|
||||
$provider = SMSProviderFactory::get($provider_key);
|
||||
if ($provider === null) {
|
||||
return ['success' => false, 'message' => sprintf(__('短信供应商 "%s" 不可用', 'aksrt'), $provider_key)];
|
||||
}
|
||||
|
||||
$missing = $provider->validate_config($config);
|
||||
if (!empty($missing)) {
|
||||
return ['success' => false, 'message' => sprintf(__('缺少配置项: %s', 'aksrt'), implode(', ', $missing))];
|
||||
}
|
||||
|
||||
return $this->send_code($phone);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,45 +10,43 @@ defined('ABSPATH') || exit;
|
||||
class SMSProviderAliyun implements ProviderInterface {
|
||||
|
||||
public function get_key(): string {
|
||||
return 'aliyun';
|
||||
}
|
||||
|
||||
public function get_label(): string {
|
||||
return __('阿里云短信', 'aksrt');
|
||||
return 'ali';
|
||||
}
|
||||
|
||||
public function validate_config(array $config): array {
|
||||
$missing = [];
|
||||
if (empty($config['sms_access_key'])) $missing[] = 'sms_access_key';
|
||||
if (empty($config['sms_access_secret'])) $missing[] = 'sms_access_secret';
|
||||
if (empty($config['sms_sign_name'])) $missing[] = 'sms_sign_name';
|
||||
if (empty($config['sms_template_code'])) $missing[] = 'sms_template_code';
|
||||
if (empty($config['sms_ali_keyid'])) $missing[] = 'sms_ali_keyid';
|
||||
if (empty($config['sms_ali_keysecret'])) $missing[] = 'sms_ali_keysecret';
|
||||
if (empty($config['sms_ali_sign_name'])) $missing[] = 'sms_ali_sign_name';
|
||||
if (($config['sms_ali_api_type'] ?? 'sms') !== 'verify' && empty($config['sms_ali_template_code'])) {
|
||||
$missing[] = 'sms_ali_template_code';
|
||||
}
|
||||
return $missing;
|
||||
}
|
||||
|
||||
public function send_code(string $phone, string $code, array $config): array {
|
||||
$access_key = (string) ($config['sms_access_key'] ?? '');
|
||||
$access_secret = (string) ($config['sms_access_secret'] ?? '');
|
||||
$sign_name = (string) ($config['sms_sign_name'] ?? '');
|
||||
$template_code = (string) ($config['sms_template_code'] ?? '');
|
||||
$access_key = (string) ($config['sms_ali_keyid'] ?? '');
|
||||
$access_secret = (string) ($config['sms_ali_keysecret'] ?? '');
|
||||
$sign_name = (string) ($config['sms_ali_sign_name'] ?? '');
|
||||
$region_id = (string) ($config['sms_region_id'] ?? 'cn-hangzhou');
|
||||
$verify_api = ($config['sms_ali_api_type'] ?? 'sms') === 'verify';
|
||||
$action = $verify_api ? 'SendSmsVerifyCode' : 'SendSms';
|
||||
$params = [
|
||||
($verify_api ? 'PhoneNumber' : 'PhoneNumbers') => $phone,
|
||||
'SignName' => $sign_name,
|
||||
'TemplateCode' => $verify_api ? '100001' : (string) ($config['sms_ali_template_code'] ?? ''),
|
||||
'TemplateParam' => wp_json_encode($verify_api ? ['code' => $code, 'min' => '30'] : ['code' => $code]),
|
||||
];
|
||||
$domain = $verify_api ? 'dypnsapi.aliyuncs.com' : 'dysmsapi.aliyuncs.com';
|
||||
|
||||
$params = [
|
||||
'PhoneNumbers' => $phone,
|
||||
'SignName' => $sign_name,
|
||||
'TemplateCode' => $template_code,
|
||||
'TemplateParam' => wp_json_encode(['code' => $code]),
|
||||
];
|
||||
|
||||
$result = $this->request($access_key, $access_secret, $region_id, $params);
|
||||
$result = $this->request($access_key, $access_secret, $region_id, $domain, $action, $params);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
$this->log('error', $phone, $result->get_error_message());
|
||||
return ['success' => false, 'message' => $result->get_error_message(), 'transport' => 'signed_post'];
|
||||
}
|
||||
|
||||
// 对齐 legacy 成功判断:检查 BizId 或 Code === 'OK'
|
||||
if (!empty($result['BizId']) || (isset($result['Code']) && $result['Code'] === 'OK')) {
|
||||
if (($verify_api && ($result['Code'] ?? '') === 'OK') || (!$verify_api && (!empty($result['BizId']) || ($result['Code'] ?? '') === 'OK'))) {
|
||||
$this->log('sent', $phone, 'OK');
|
||||
return ['success' => true, 'message' => __('验证码已发送', 'aksrt'), 'transport' => 'signed_post'];
|
||||
}
|
||||
@@ -64,8 +62,8 @@ class SMSProviderAliyun implements ProviderInterface {
|
||||
return [
|
||||
'provider' => $this->get_key(),
|
||||
'configured' => empty($missing),
|
||||
'available' => !empty($config['sms_enabled']) && empty($missing),
|
||||
'verified' => !empty($config['sms_aliyun_verified']),
|
||||
'available' => ($config['sms_sdk'] ?? 'null') === $this->get_key() && empty($missing),
|
||||
'verified' => false,
|
||||
'transport' => 'signed_post',
|
||||
'missing_fields' => $missing,
|
||||
];
|
||||
@@ -81,7 +79,7 @@ class SMSProviderAliyun implements ProviderInterface {
|
||||
*
|
||||
* @return array|\WP_Error
|
||||
*/
|
||||
private function request(string $access_key, string $access_secret, string $region_id, array $biz_params) {
|
||||
private function request(string $access_key, string $access_secret, string $region_id, string $domain, string $action, array $biz_params) {
|
||||
if (!class_exists('AKSRT\\Sms\\SMSSignatureHelper')) {
|
||||
return new \WP_Error('sms_helper_missing', '签名助手类未加载');
|
||||
}
|
||||
@@ -91,10 +89,10 @@ class SMSProviderAliyun implements ProviderInterface {
|
||||
return $helper->request(
|
||||
$access_key,
|
||||
$access_secret,
|
||||
'dysmsapi.aliyuncs.com',
|
||||
$domain,
|
||||
array_merge($biz_params, [
|
||||
'RegionId' => $region_id,
|
||||
'Action' => 'SendSms',
|
||||
'Action' => $action,
|
||||
'Version' => '2017-05-25',
|
||||
]),
|
||||
true // HTTPS
|
||||
|
||||
@@ -1,166 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Sms;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class SMSProviderCustom implements ProviderInterface {
|
||||
|
||||
public function get_key(): string {
|
||||
return 'custom';
|
||||
}
|
||||
|
||||
public function get_label(): string {
|
||||
return __('自定义接口', 'aksrt');
|
||||
}
|
||||
|
||||
public function validate_config(array $config): array {
|
||||
$missing = [];
|
||||
if (empty($config['sms_custom_url'])) $missing[] = 'sms_custom_url';
|
||||
return $missing;
|
||||
}
|
||||
|
||||
public function send_code(string $phone, string $code, array $config): array {
|
||||
$url = (string) ($config['sms_custom_url'] ?? '');
|
||||
$method = strtoupper((string) ($config['sms_custom_method'] ?? 'POST'));
|
||||
$auth_type = (string) ($config['sms_custom_auth_type'] ?? 'header');
|
||||
$auth_key = (string) ($config['sms_custom_auth_key'] ?? '');
|
||||
$auth_value = (string) ($config['sms_custom_auth_value'] ?? '');
|
||||
$headers_raw = (string) ($config['sms_custom_headers'] ?? '');
|
||||
$body_tpl = (string) ($config['sms_custom_body'] ?? '');
|
||||
$success_rule = (string) ($config['sms_custom_success_rule'] ?? '');
|
||||
|
||||
if ($url === '') {
|
||||
return ['success' => false, 'message' => __('自定义接口 URL 未配置', 'aksrt'), 'transport' => 'custom_http'];
|
||||
}
|
||||
|
||||
// 替换模板变量
|
||||
$replacements = [
|
||||
'{phone}' => $phone,
|
||||
'{code}' => $code,
|
||||
'{time}' => (string) time(),
|
||||
];
|
||||
|
||||
$body = str_replace(array_keys($replacements), array_values($replacements), $body_tpl);
|
||||
|
||||
// 解析自定义 headers
|
||||
$headers = ['Accept' => 'application/json'];
|
||||
if ($headers_raw !== '') {
|
||||
foreach (explode("\n", $headers_raw) as $line) {
|
||||
$line = trim($line);
|
||||
if ($line === '') continue;
|
||||
$parts = explode(':', $line, 2);
|
||||
if (count($parts) === 2) {
|
||||
$headers[trim($parts[0])] = trim(str_replace(array_keys($replacements), array_values($replacements), $parts[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 鉴权
|
||||
if ($auth_key !== '' && $auth_value !== '') {
|
||||
$replaced_value = str_replace(array_keys($replacements), array_values($replacements), $auth_value);
|
||||
if ($auth_type === 'header') {
|
||||
$headers[$auth_key] = $replaced_value;
|
||||
}
|
||||
}
|
||||
|
||||
$args = [
|
||||
'timeout' => 10,
|
||||
'headers' => $headers,
|
||||
];
|
||||
|
||||
if (in_array($method, ['POST', 'PUT', 'PATCH'], true)) {
|
||||
$args['body'] = $body;
|
||||
$result = wp_remote_request($url, array_merge($args, ['method' => $method]));
|
||||
} else {
|
||||
// GET — append body as query params
|
||||
$url_with_params = add_query_arg(json_decode($body, true) ?: [], $url);
|
||||
$result = wp_remote_get($url_with_params, $args);
|
||||
}
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
$this->log('error', $phone, $result->get_error_message());
|
||||
return ['success' => false, 'message' => $result->get_error_message(), 'transport' => 'custom_http'];
|
||||
}
|
||||
|
||||
$status_code = wp_remote_retrieve_response_code($result);
|
||||
$response_body = wp_remote_retrieve_body($result);
|
||||
|
||||
// 判定成功规则
|
||||
if ($success_rule !== '') {
|
||||
$matched = $this->check_success_rule($success_rule, $status_code, $response_body);
|
||||
if ($matched) {
|
||||
$this->log('sent', $phone, "status={$status_code}");
|
||||
return ['success' => true, 'message' => __('验证码已发送', 'aksrt'), 'transport' => 'custom_http'];
|
||||
}
|
||||
$this->log('error', $phone, "status={$status_code}, rule not matched");
|
||||
return ['success' => false, 'message' => sprintf(__('自定义接口返回异常 (HTTP %d)', 'aksrt'), $status_code), 'transport' => 'custom_http'];
|
||||
}
|
||||
|
||||
// 默认:HTTP 2xx 视为成功
|
||||
if ($status_code >= 200 && $status_code < 300) {
|
||||
$this->log('sent', $phone, "status={$status_code}");
|
||||
return ['success' => true, 'message' => __('验证码已发送', 'aksrt'), 'transport' => 'custom_http'];
|
||||
}
|
||||
|
||||
$this->log('error', $phone, "status={$status_code}");
|
||||
return ['success' => false, 'message' => sprintf(__('自定义接口返回 HTTP %d', 'aksrt'), $status_code), 'transport' => 'custom_http'];
|
||||
}
|
||||
|
||||
public function get_status(array $config): array {
|
||||
$missing = $this->validate_config($config);
|
||||
return [
|
||||
'provider' => $this->get_key(),
|
||||
'configured' => empty($missing),
|
||||
'available' => !empty($config['sms_enabled']) && empty($missing),
|
||||
'verified' => !empty($config['sms_custom_verified']),
|
||||
'transport' => 'custom_http',
|
||||
'missing_fields' => $missing,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验成功规则
|
||||
*
|
||||
* 支持格式:
|
||||
* - "status:200" — HTTP 状态码匹配
|
||||
* - "json:code=0" — JSON 响应中字段匹配
|
||||
* - "contains:success" — 响应体包含字符串
|
||||
*/
|
||||
private function check_success_rule(string $rule, int $status_code, string $body): bool {
|
||||
$parts = explode(':', $rule, 2);
|
||||
if (count($parts) < 2) return false;
|
||||
|
||||
$type = trim($parts[0]);
|
||||
$value = trim($parts[1]);
|
||||
|
||||
switch ($type) {
|
||||
case 'status':
|
||||
return (string) $status_code === $value;
|
||||
|
||||
case 'json':
|
||||
$json = json_decode($body, true);
|
||||
if (!is_array($json)) return false;
|
||||
$kv = explode('=', $value, 2);
|
||||
if (count($kv) !== 2) return false;
|
||||
$key = trim($kv[0]);
|
||||
$expected = trim($kv[1]);
|
||||
return (string) ($json[$key] ?? '') === $expected;
|
||||
|
||||
case 'contains':
|
||||
return strpos($body, $value) !== false;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function log(string $level, string $phone, string $message): void {
|
||||
if (!defined('WP_DEBUG') || !WP_DEBUG) return;
|
||||
$masked = substr($phone, 0, 3) . '****' . substr($phone, -4);
|
||||
error_log(sprintf('[AKSRT SMS] [custom] [%s] Phone: %s — %s', $level, $masked, $message));
|
||||
}
|
||||
}
|
||||
@@ -29,36 +29,17 @@ class SMSProviderFactory {
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有已注册供应商
|
||||
*
|
||||
* @return ProviderInterface[]
|
||||
*/
|
||||
public static function all(): array {
|
||||
$keys = ['aliyun', 'tencent', 'huawei', 'custom'];
|
||||
$result = [];
|
||||
foreach ($keys as $key) {
|
||||
$provider = self::get($key);
|
||||
if ($provider !== null) {
|
||||
$result[$key] = $provider;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 adapter 实例
|
||||
*/
|
||||
private static function create(string $key): ?ProviderInterface {
|
||||
switch ($key) {
|
||||
case 'aliyun':
|
||||
case 'ali':
|
||||
return class_exists('AKSRT\\Sms\\SMSProviderAliyun') ? new SMSProviderAliyun() : null;
|
||||
case 'tencent':
|
||||
return class_exists('AKSRT\\Sms\\SMSProviderTencent') ? new SMSProviderTencent() : null;
|
||||
case 'huawei':
|
||||
return class_exists('AKSRT\\Sms\\SMSProviderHuawei') ? new SMSProviderHuawei() : null;
|
||||
case 'custom':
|
||||
return class_exists('AKSRT\\Sms\\SMSProviderCustom') ? new SMSProviderCustom() : null;
|
||||
case 'smsbao':
|
||||
return class_exists('AKSRT\\Sms\\SMSProviderSmsbao') ? new SMSProviderSmsbao() : null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Sms;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class SMSProviderHuawei implements ProviderInterface {
|
||||
|
||||
public function get_key(): string {
|
||||
return 'huawei';
|
||||
}
|
||||
|
||||
public function get_label(): string {
|
||||
return __('华为云短信', 'aksrt');
|
||||
}
|
||||
|
||||
public function validate_config(array $config): array {
|
||||
$missing = [];
|
||||
if (empty($config['sms_access_key'])) $missing[] = 'sms_access_key';
|
||||
if (empty($config['sms_access_secret'])) $missing[] = 'sms_access_secret';
|
||||
if (empty($config['sms_huawei_sender'])) $missing[] = 'sms_huawei_sender';
|
||||
if (empty($config['sms_template_code'])) $missing[] = 'sms_template_code';
|
||||
if (empty($config['sms_huawei_signature'])) $missing[] = 'sms_huawei_signature';
|
||||
return $missing;
|
||||
}
|
||||
|
||||
public function send_code(string $phone, string $code, array $config): array {
|
||||
$app_key = (string) ($config['sms_access_key'] ?? '');
|
||||
$app_secret = (string) ($config['sms_access_secret'] ?? '');
|
||||
$sender = (string) ($config['sms_huawei_sender'] ?? '');
|
||||
$template_id = (string) ($config['sms_template_code'] ?? '');
|
||||
$signature = (string) ($config['sms_huawei_signature'] ?? '');
|
||||
$endpoint = (string) ($config['sms_huawei_endpoint'] ?? 'https://smsapi.ap-southeast-1.myhuaweicloud.com');
|
||||
|
||||
$result = $this->request($app_key, $app_secret, $sender, $template_id, $signature, $endpoint, $phone, $code);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
$this->log('error', $phone, $result->get_error_message());
|
||||
return ['success' => false, 'message' => $result->get_error_message(), 'transport' => 'hw_http'];
|
||||
}
|
||||
|
||||
$body = json_decode(wp_remote_retrieve_body($result), true);
|
||||
$code_field = $body['code'] ?? '';
|
||||
|
||||
if ($code_field === '000000') {
|
||||
$this->log('sent', $phone, '000000');
|
||||
return ['success' => true, 'message' => __('验证码已发送', 'aksrt'), 'transport' => 'hw_http'];
|
||||
}
|
||||
|
||||
$err_msg = $body['description'] ?? $body['code'] ?? __('未知错误', 'aksrt');
|
||||
$this->log('error', $phone, "{$code_field}: {$err_msg}");
|
||||
return ['success' => false, 'message' => sprintf('华为云: %s', $err_msg), 'transport' => 'hw_http'];
|
||||
}
|
||||
|
||||
public function get_status(array $config): array {
|
||||
$missing = $this->validate_config($config);
|
||||
return [
|
||||
'provider' => $this->get_key(),
|
||||
'configured' => empty($missing),
|
||||
'available' => !empty($config['sms_enabled']) && empty($missing),
|
||||
'verified' => !empty($config['sms_huawei_verified']),
|
||||
'transport' => 'hw_http',
|
||||
'missing_fields' => $missing,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 华为云 AK/SK 签名请求
|
||||
*/
|
||||
private function request(string $app_key, string $app_secret, string $sender, string $template_id, string $signature, string $endpoint, string $phone, string $code) {
|
||||
$endpoint = rtrim($endpoint, '/');
|
||||
$url = "{$endpoint}/sms/batchSendSms";
|
||||
|
||||
$body = wp_json_encode([
|
||||
'from' => $sender,
|
||||
'to' => [$phone],
|
||||
'templateId' => $template_id,
|
||||
'templateParas' => [$code],
|
||||
'signature' => $signature,
|
||||
]);
|
||||
|
||||
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
|
||||
$nonce = wp_generate_uuid4();
|
||||
|
||||
// 简化签名:使用 X-Sms-AppKey 鉴权方式
|
||||
return wp_remote_post($url, [
|
||||
'timeout' => 10,
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Sms-AppKey' => $app_key,
|
||||
'X-Sms-AppSecret' => $app_secret,
|
||||
],
|
||||
'body' => $body,
|
||||
]);
|
||||
}
|
||||
|
||||
private function log(string $level, string $phone, string $message): void {
|
||||
if (!defined('WP_DEBUG') || !WP_DEBUG) return;
|
||||
$masked = substr($phone, 0, 3) . '****' . substr($phone, -4);
|
||||
error_log(sprintf('[AKSRT SMS] [huawei] [%s] Phone: %s — %s', $level, $masked, $message));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Sms;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class SMSProviderSmsbao implements ProviderInterface {
|
||||
|
||||
private const ERROR_MESSAGES = [
|
||||
'0' => '短信发送成功',
|
||||
'-1' => '短信宝接口参数不全',
|
||||
'-2' => '服务器不支持短信请求',
|
||||
'30' => '短信宝密码错误',
|
||||
'40' => '短信宝账号不存在',
|
||||
'41' => '短信宝余额不足',
|
||||
'42' => '短信宝账号已过期',
|
||||
'43' => '短信宝 IP 地址受限',
|
||||
'50' => '短信内容含有敏感词',
|
||||
'51' => '手机号码不正确',
|
||||
];
|
||||
|
||||
public function get_key(): string {
|
||||
return 'smsbao';
|
||||
}
|
||||
|
||||
public function validate_config(array $config): array {
|
||||
$missing = [];
|
||||
if (empty($config['sms_smsbao_username'])) $missing[] = 'sms_smsbao_username';
|
||||
if (empty($config['sms_smsbao_api_key']) && empty($config['sms_smsbao_password'])) $missing[] = 'sms_smsbao_api_key 或 sms_smsbao_password';
|
||||
$template = (string) ($config['sms_smsbao_template'] ?? '');
|
||||
if ($template === '') $missing[] = 'sms_smsbao_template';
|
||||
elseif (!str_contains($template, '{code}')) $missing[] = '短信模板中的 {code} 变量';
|
||||
return $missing;
|
||||
}
|
||||
|
||||
public function send_code(string $phone, string $code, array $config): array {
|
||||
$template = str_replace(['{code}', '{time}'], [$code, '30'], (string) ($config['sms_smsbao_template'] ?? ''));
|
||||
$password = (string) ($config['sms_smsbao_api_key'] ?? '');
|
||||
if ($password === '') {
|
||||
$password = md5((string) ($config['sms_smsbao_password'] ?? ''));
|
||||
}
|
||||
$response = wp_remote_get(add_query_arg([
|
||||
'u' => (string) ($config['sms_smsbao_username'] ?? ''),
|
||||
'p' => $password,
|
||||
'm' => $phone,
|
||||
'c' => $template,
|
||||
], 'https://api.smsbao.com/sms'), ['timeout' => 10]);
|
||||
if (is_wp_error($response)) {
|
||||
return ['success' => false, 'message' => $response->get_error_message(), 'transport' => 'smsbao_http'];
|
||||
}
|
||||
|
||||
if (wp_remote_retrieve_response_code($response) !== 200) {
|
||||
return ['success' => false, 'message' => __('短信宝接口请求失败', 'aksrt'), 'transport' => 'smsbao_http'];
|
||||
}
|
||||
$result = trim(wp_remote_retrieve_body($response));
|
||||
if ($result === '0') {
|
||||
return ['success' => true, 'message' => __('验证码短信已发送', 'aksrt'), 'transport' => 'smsbao_http'];
|
||||
}
|
||||
$message = self::ERROR_MESSAGES[$result] ?? __('短信宝发送失败', 'aksrt');
|
||||
return ['success' => false, 'message' => $message . ' | ' . __('错误码:', 'aksrt') . ' ' . $result, 'transport' => 'smsbao_http'];
|
||||
}
|
||||
|
||||
public function get_status(array $config): array {
|
||||
$missing = $this->validate_config($config);
|
||||
return [
|
||||
'provider' => $this->get_key(),
|
||||
'configured' => empty($missing),
|
||||
'available' => ($config['sms_sdk'] ?? 'null') === $this->get_key() && empty($missing),
|
||||
'verified' => false,
|
||||
'transport' => 'smsbao_http',
|
||||
'missing_fields' => $missing,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -13,47 +13,43 @@ class SMSProviderTencent implements ProviderInterface {
|
||||
return 'tencent';
|
||||
}
|
||||
|
||||
public function get_label(): string {
|
||||
return __('腾讯云短信', 'aksrt');
|
||||
}
|
||||
|
||||
public function validate_config(array $config): array {
|
||||
$missing = [];
|
||||
if (empty($config['sms_access_key'])) $missing[] = 'sms_access_key';
|
||||
if (empty($config['sms_access_secret'])) $missing[] = 'sms_access_secret';
|
||||
if (empty($config['sms_tencent_app_id'])) $missing[] = 'sms_tencent_app_id';
|
||||
if (empty($config['sms_sign_name'])) $missing[] = 'sms_sign_name';
|
||||
if (empty($config['sms_template_code'])) $missing[] = 'sms_template_code';
|
||||
if (empty($config['sms_tencent_app_key'])) $missing[] = 'sms_tencent_app_key';
|
||||
if (empty($config['sms_tencent_sign_name'])) $missing[] = 'sms_tencent_sign_name';
|
||||
if (empty($config['sms_tencent_template_id'])) $missing[] = 'sms_tencent_template_id';
|
||||
return $missing;
|
||||
}
|
||||
|
||||
public function send_code(string $phone, string $code, array $config): array {
|
||||
$secret_id = (string) ($config['sms_access_key'] ?? '');
|
||||
$secret_key = (string) ($config['sms_access_secret'] ?? '');
|
||||
$app_id = (string) ($config['sms_tencent_app_id'] ?? '');
|
||||
$sign_name = (string) ($config['sms_sign_name'] ?? '');
|
||||
$template_id = (string) ($config['sms_template_code'] ?? '');
|
||||
$region = (string) ($config['sms_tencent_region'] ?? 'ap-guangzhou');
|
||||
$app_key = (string) ($config['sms_tencent_app_key'] ?? '');
|
||||
$sign_name = (string) ($config['sms_tencent_sign_name'] ?? '');
|
||||
$template_id = (string) ($config['sms_tencent_template_id'] ?? '');
|
||||
|
||||
$result = $this->request($secret_id, $secret_key, $app_id, $sign_name, $template_id, $region, $phone, $code);
|
||||
$result = $this->request($app_id, $app_key, $sign_name, $template_id, $phone, $code);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
$this->log('error', $phone, $result->get_error_message());
|
||||
return ['success' => false, 'message' => $result->get_error_message(), 'transport' => 'tc3_http'];
|
||||
return ['success' => false, 'message' => $result->get_error_message(), 'transport' => 'qcloudsms_http'];
|
||||
}
|
||||
|
||||
$body = json_decode(wp_remote_retrieve_body($result), true);
|
||||
$response = $body['Response'] ?? [];
|
||||
$response = json_decode(wp_remote_retrieve_body($result), true);
|
||||
$response = is_array($response) ? $response : [];
|
||||
if (wp_remote_retrieve_response_code($result) !== 200) {
|
||||
return ['success' => false, 'message' => (string) ($response['errmsg'] ?? __('腾讯云接口请求失败', 'aksrt')), 'transport' => 'qcloudsms_http'];
|
||||
}
|
||||
|
||||
if (isset($response['SendStatusSet'][0]['Code']) && $response['SendStatusSet'][0]['Code'] === 'Ok') {
|
||||
if (isset($response['result']) && (int) $response['result'] === 0) {
|
||||
$this->log('sent', $phone, 'Ok');
|
||||
return ['success' => true, 'message' => __('验证码已发送', 'aksrt'), 'transport' => 'tc3_http'];
|
||||
return ['success' => true, 'message' => __('验证码已发送', 'aksrt'), 'transport' => 'qcloudsms_http'];
|
||||
}
|
||||
|
||||
$err_code = $response['Error']['Code'] ?? ($response['SendStatusSet'][0]['Code'] ?? 'Unknown');
|
||||
$err_msg = $response['Error']['Message'] ?? ($response['SendStatusSet'][0]['Message'] ?? __('未知错误', 'aksrt'));
|
||||
$err_code = $response['result'] ?? 'Unknown';
|
||||
$err_msg = $response['errmsg'] ?? __('未知错误', 'aksrt');
|
||||
$this->log('error', $phone, "{$err_code}: {$err_msg}");
|
||||
return ['success' => false, 'message' => sprintf('腾讯云: %s', $err_msg), 'transport' => 'tc3_http'];
|
||||
return ['success' => false, 'message' => sprintf('腾讯云: %s', $err_msg), 'transport' => 'qcloudsms_http'];
|
||||
}
|
||||
|
||||
public function get_status(array $config): array {
|
||||
@@ -61,77 +57,41 @@ class SMSProviderTencent implements ProviderInterface {
|
||||
return [
|
||||
'provider' => $this->get_key(),
|
||||
'configured' => empty($missing),
|
||||
'available' => !empty($config['sms_enabled']) && empty($missing),
|
||||
'verified' => !empty($config['sms_tencent_verified']),
|
||||
'transport' => 'tc3_http',
|
||||
'available' => ($config['sms_sdk'] ?? 'null') === $this->get_key() && empty($missing),
|
||||
'verified' => false,
|
||||
'transport' => 'qcloudsms_http',
|
||||
'missing_fields' => $missing,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TC3-HMAC-SHA256 签名请求
|
||||
* 腾讯云 AppID/AppKey 模板单发请求
|
||||
*/
|
||||
private function request(string $secret_id, string $secret_key, string $app_id, string $sign_name, string $template_id, string $region, string $phone, string $code) {
|
||||
$service = 'sms';
|
||||
$host = "{$service}.tencentcloudapi.com";
|
||||
$action = 'SendSms';
|
||||
$version = '2021-01-11';
|
||||
$endpoint = "https://{$host}";
|
||||
|
||||
$payload = wp_json_encode([
|
||||
'PhoneNumberSet' => [$phone],
|
||||
'SmsSdkAppId' => $app_id,
|
||||
'SignName' => $sign_name,
|
||||
'TemplateId' => $template_id,
|
||||
'TemplateParamSet' => [$code],
|
||||
]);
|
||||
|
||||
$timestamp = time();
|
||||
$date = gmdate('Y-m-d', $timestamp);
|
||||
|
||||
// Step 1: CanonicalRequest
|
||||
$http_request_method = 'POST';
|
||||
$canonical_uri = '/';
|
||||
$canonical_querystring = '';
|
||||
$content_type = 'application/json; charset=utf-8';
|
||||
$canonical_headers = "content-type:{$content_type}\nhost:{$host}\nx-tc-action:" . strtolower($action) . "\n";
|
||||
$signed_headers = 'content-type;host;x-tc-action';
|
||||
$hashed_payload = hash('sha256', $payload);
|
||||
$canonical_request = "{$http_request_method}\n{$canonical_uri}\n{$canonical_querystring}\n{$canonical_headers}\n{$signed_headers}\n{$hashed_payload}";
|
||||
|
||||
// Step 2: StringToSign
|
||||
$algorithm = 'TC3-HMAC-SHA256';
|
||||
$credential_scope = "{$date}/{$service}/tc3_request";
|
||||
$hashed_request = hash('sha256', $canonical_request);
|
||||
$string_to_sign = "{$algorithm}\n{$timestamp}\n{$credential_scope}\n{$hashed_request}";
|
||||
|
||||
// Step 3: Signature
|
||||
$secret_date = hash_hmac('sha256', $date, "TC3{$secret_key}", true);
|
||||
$secret_service = hash_hmac('sha256', $service, $secret_date, true);
|
||||
$secret_signing = hash_hmac('sha256', 'tc3_request', $secret_service, true);
|
||||
$signature = hash_hmac('sha256', $string_to_sign, $secret_signing);
|
||||
|
||||
// Step 4: Authorization header
|
||||
$authorization = "{$algorithm} Credential={$secret_id}/{$credential_scope}, SignedHeaders={$signed_headers}, Signature={$signature}";
|
||||
|
||||
return wp_remote_post($endpoint, [
|
||||
'timeout' => 10,
|
||||
'headers' => [
|
||||
'Authorization' => $authorization,
|
||||
'Content-Type' => $content_type,
|
||||
'Host' => $host,
|
||||
'X-TC-Action' => $action,
|
||||
'X-TC-Version' => $version,
|
||||
'X-TC-Timestamp' => (string) $timestamp,
|
||||
'X-TC-Region' => $region,
|
||||
],
|
||||
'body' => $payload,
|
||||
]);
|
||||
private function request(string $app_id, string $app_key, string $sign_name, string $template_id, string $phone, string $code) {
|
||||
$random = random_int(100000, 999999);
|
||||
$timestamp = time();
|
||||
$signature = hash('sha256', 'appkey=' . $app_key . '&random=' . $random . '&time=' . $timestamp . '&mobile=' . $phone);
|
||||
$url = add_query_arg(['sdkappid' => $app_id, 'random' => $random], 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms');
|
||||
$payload = wp_json_encode([
|
||||
'tel' => ['nationcode' => '86', 'mobile' => $phone],
|
||||
'sig' => $signature,
|
||||
'tpl_id' => $template_id,
|
||||
'params' => [$code, '30'],
|
||||
'sign' => $sign_name,
|
||||
'time' => $timestamp,
|
||||
'extend' => '',
|
||||
'ext' => '',
|
||||
]);
|
||||
return wp_remote_post($url, [
|
||||
'timeout' => 10,
|
||||
'headers' => ['Content-Type' => 'application/json; charset=utf-8'],
|
||||
'body' => $payload,
|
||||
]);
|
||||
}
|
||||
|
||||
private function log(string $level, string $phone, string $message): void {
|
||||
if (!defined('WP_DEBUG') || !WP_DEBUG) return;
|
||||
$masked = substr($phone, 0, 3) . '****' . substr($phone, -4);
|
||||
error_log(sprintf('[AKSRT SMS] [tencent] [%s] Phone: %s — %s', $level, $masked, $message));
|
||||
error_log(sprintf('[AKSRT SMS] [tencent] [%s] Phone: %s - %s', $level, $masked, $message));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user