Move all domain folders from src/AKSRT/{Domain} to src/{Domain}; remap
Composer PSR-4 "AKSRT\\": "src/"; update audit script + README + ADR
paths; remove dead tools/build-psr4-map.php (scanned deleted inc/classes).
106 lines
4.1 KiB
PHP
106 lines
4.1 KiB
PHP
<?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));
|
|
}
|
|
}
|