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)); } }