Move ~94 root class shells and root *-traits into domain directories (core, auth, user, credits, content, payment/gateways, forum, shop, sms, ai, messages, extensions, widgets, settings, admin). Update bootstrap require paths, co-locate traits with owners, fix forum/user relative requires, drop duplicate shop controller requires, and wire home-modules load. Path-only; class names and init order unchanged.
114 lines
4.3 KiB
PHP
114 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT SMS Provider — 阿里云短信
|
|
*
|
|
* 通过阿里云短信 API 发送验证码。
|
|
* 使用 POST 方式 + HMAC-SHA1 签名(对齐 zibll 实现)。
|
|
*
|
|
* @package AKSRT
|
|
* @link https://help.aliyun.com/document_detail/101414.html
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_SMS_Provider_Aliyun implements AKSRT_SMS_Provider {
|
|
|
|
public function get_key(): string {
|
|
return 'aliyun';
|
|
}
|
|
|
|
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_sign_name'])) $missing[] = 'sms_sign_name';
|
|
if (empty($config['sms_template_code'])) $missing[] = 'sms_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'] ?? '');
|
|
$region_id = (string) ($config['sms_region_id'] ?? 'cn-hangzhou');
|
|
|
|
$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);
|
|
|
|
if (is_wp_error($result)) {
|
|
$this->log('error', $phone, $result->get_error_message());
|
|
return ['success' => false, 'message' => $result->get_error_message(), 'transport' => 'signed_post'];
|
|
}
|
|
|
|
// 对齐 zibll 成功判断:检查 BizId 或 Code === 'OK'
|
|
if (!empty($result['BizId']) || (isset($result['Code']) && $result['Code'] === 'OK')) {
|
|
$this->log('sent', $phone, 'OK');
|
|
return ['success' => true, 'message' => __('验证码已发送', 'aksrt'), 'transport' => 'signed_post'];
|
|
}
|
|
|
|
$err_code = $result['Code'] ?? 'Unknown';
|
|
$err_msg = $result['Message'] ?? __('未知错误', 'aksrt');
|
|
$this->log('error', $phone, "{$err_code}: {$err_msg}");
|
|
return ['success' => false, 'message' => sprintf('阿里云: %s (%s)', $err_msg, $err_code), 'transport' => 'signed_post'];
|
|
}
|
|
|
|
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_aliyun_verified']),
|
|
'transport' => 'signed_post',
|
|
'missing_fields' => $missing,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 发送阿里云短信 API 请求(POST 方式)
|
|
*
|
|
* 对齐 zibll 的 SignatureHelper 实现:
|
|
* - POST 到 https://dysmsapi.aliyuncs.com/
|
|
* - body 为 Signature=xxx&Key=Value&... 格式
|
|
* - 使用 x-sdk-client header
|
|
*
|
|
* @return array|WP_Error
|
|
*/
|
|
private function request(string $access_key, string $access_secret, string $region_id, array $biz_params) {
|
|
if (!class_exists('AKSRT_SMS_Signature_Helper')) {
|
|
return new WP_Error('sms_helper_missing', '签名助手类未加载');
|
|
}
|
|
|
|
$helper = new AKSRT_SMS_Signature_Helper();
|
|
|
|
return $helper->request(
|
|
$access_key,
|
|
$access_secret,
|
|
'dysmsapi.aliyuncs.com',
|
|
array_merge($biz_params, [
|
|
'RegionId' => $region_id,
|
|
'Action' => 'SendSms',
|
|
'Version' => '2017-05-25',
|
|
]),
|
|
true // HTTPS
|
|
);
|
|
}
|
|
|
|
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] [aliyun] [%s] Phone: %s — %s', $level, $masked, $message));
|
|
}
|
|
}
|