You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.3 KiB
82 lines
2.3 KiB
|
1 month ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Services;
|
||
|
|
|
||
|
|
use Illuminate\Support\Facades\Http;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 与 wx.sstbc.com app/Http/functions.php 中 ymSms() 相同通道与签名规则。
|
||
|
|
*/
|
||
|
|
class BtomSmsService
|
||
|
|
{
|
||
|
|
public function sendContent(string $mobile, string $content): bool
|
||
|
|
{
|
||
|
|
$appId = (string) config('sms.app_id');
|
||
|
|
$secret = (string) config('sms.secret');
|
||
|
|
$url = (string) config('sms.gateway');
|
||
|
|
|
||
|
|
if ($appId === '' || $secret === '') {
|
||
|
|
Log::warning('SMS skipped: SMS_APPID or SMS_SECRETKEY empty');
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$timestamp = date('YmdHis');
|
||
|
|
$params = [
|
||
|
|
'appId' => $appId,
|
||
|
|
'timestamp' => $timestamp,
|
||
|
|
'mobiles' => $mobile,
|
||
|
|
'content' => $content,
|
||
|
|
];
|
||
|
|
$params['sign'] = md5($appId.$secret.$timestamp);
|
||
|
|
|
||
|
|
Log::info('sms.gateway request', [
|
||
|
|
'url' => $url,
|
||
|
|
'mobile_mask' => strlen($mobile) >= 11
|
||
|
|
? substr($mobile, 0, 3).'****'.substr($mobile, -4)
|
||
|
|
: '***',
|
||
|
|
'app_id_len' => strlen($appId),
|
||
|
|
'timestamp' => $timestamp,
|
||
|
|
]);
|
||
|
|
|
||
|
|
try {
|
||
|
|
$response = Http::timeout((int) config('sms.timeout', 5))
|
||
|
|
->withHeaders(['Content-Type' => 'application/json'])
|
||
|
|
->get($url, $params);
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
Log::error('SMS request failed', ['exception' => $e->getMessage()]);
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$status = $response->status();
|
||
|
|
$bodySnippet = substr($response->body(), 0, 500);
|
||
|
|
Log::info('sms.gateway response', [
|
||
|
|
'http_status' => $status,
|
||
|
|
'body_preview' => $bodySnippet,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$return = $response->json();
|
||
|
|
if (! is_array($return)) {
|
||
|
|
Log::error('SMS invalid JSON', ['body' => $response->body()]);
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (($return['code'] ?? '') !== 'SUCCESS') {
|
||
|
|
Log::warning('SMS gateway error', array_merge($return, ['http_status' => $status]));
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
Log::info('sms.gateway success', [
|
||
|
|
'mobile_mask' => strlen($mobile) >= 11
|
||
|
|
? substr($mobile, 0, 3).'****'.substr($mobile, -4)
|
||
|
|
: '***',
|
||
|
|
]);
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|