fix(sms): handle empty Smsbao responses
This commit is contained in:
@@ -9,19 +9,6 @@ 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';
|
||||
}
|
||||
@@ -47,20 +34,54 @@ class SMSProviderSmsbao implements ProviderInterface {
|
||||
'p' => $password,
|
||||
'm' => $phone,
|
||||
'c' => $template,
|
||||
'f' => 'json',
|
||||
], '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'];
|
||||
$status = (int) wp_remote_retrieve_response_code($response);
|
||||
if ($status !== 200) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => sprintf(__('短信宝接口请求失败,HTTP 状态码:%d', 'aksrt'), $status),
|
||||
'transport' => 'smsbao_http',
|
||||
];
|
||||
}
|
||||
$result = trim(wp_remote_retrieve_body($response));
|
||||
|
||||
$body = trim((string) wp_remote_retrieve_body($response));
|
||||
if ($body === '') {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => sprintf(__('短信宝接口返回空响应(HTTP %d),请检查服务器到 api.smsbao.com 的 HTTPS 连接,并查看短信宝后台发送记录。', 'aksrt'), $status),
|
||||
'transport' => 'smsbao_http',
|
||||
];
|
||||
}
|
||||
|
||||
$json = json_decode($body, true);
|
||||
if (
|
||||
!is_array($json)
|
||||
|| !isset($json['code'])
|
||||
|| (!is_int($json['code']) && !is_string($json['code']))
|
||||
|| trim((string) $json['code']) === ''
|
||||
) {
|
||||
return ['success' => false, 'message' => __('短信宝返回了无效的 JSON 响应', 'aksrt'), 'transport' => 'smsbao_http'];
|
||||
}
|
||||
|
||||
$result = (string) $json['code'];
|
||||
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'];
|
||||
|
||||
$message = trim((string) ($json['msg'] ?? ''));
|
||||
if ($message === '') {
|
||||
$message = __('短信宝发送失败', 'aksrt');
|
||||
}
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => $message . ' | ' . __('错误码:', 'aksrt') . ' ' . $result,
|
||||
'transport' => 'smsbao_http',
|
||||
];
|
||||
}
|
||||
|
||||
public function get_status(array $config): array {
|
||||
|
||||
Reference in New Issue
Block a user