master
lion 4 days ago
parent 2f0481add0
commit 8849ea65e6

@ -6,6 +6,7 @@ use App\Helpers\ResponseCode;
use App\Helpers\StarterResponseCode;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
class CommonController extends Controller
@ -44,6 +45,11 @@ class CommonController extends Controller
*/
public function sendSms(){
$all = \request()->all();
Log::info('sms.endpoint.send_sms.called', [
'mobile' => $all['mobile'] ?? null,
'type' => $all['type'] ?? null,
'path' => request()->path(),
]);
$messages = [
'mobile.required' => '手机号必填',
'mobile.numeric' => '手机号格式错误',
@ -54,6 +60,7 @@ class CommonController extends Controller
'type' => 'required',
], $messages);
if ($validator->fails()) {
Log::warning('sms.endpoint.send_sms.validation_failed', ['errors' => $validator->errors()->all()]);
return $this->fail([StarterResponseCode::START_ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$key = $all['mobile'];
@ -67,6 +74,7 @@ class CommonController extends Controller
}
$check = Cache::get($key);
if(isset($check) && time() - $check['time'] <= 60){
Log::info('sms.endpoint.send_sms.rate_limited', ['key' => $key]);
return $this->fail([ResponseCode::ERROR_BUSINESS, '请勿频繁发送']);
}
$code = randStr(4,true);
@ -75,8 +83,10 @@ class CommonController extends Controller
if($result){
// 缓存
Cache::put($key,['code'=>$code,'time'=>time()],5);
Log::info('sms.endpoint.send_sms.success', ['mobile' => $all['mobile'], 'type' => $all['type']]);
return $this->success("发送成功");
}
Log::warning('sms.endpoint.send_sms.submail_failed', ['mobile' => $all['mobile'], 'type' => $all['type'], 'template_id' => $template_id]);
return $this->fail([StarterResponseCode::START_ERROR_PARAMETER,"发送失败"]);
}

@ -359,6 +359,35 @@ function curl($url, $post = array(), $arr_return = true)
}
}
/**
* Submail xsend POST返回解码结果与原始响应便于日志与排错
*
* @return array{decoded: array|null, raw: string, curl_errno: int, curl_error: string}
*/
function submail_message_xsend_post($url, $postBody)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postBody);
$raw = curl_exec($curl);
$errno = curl_errno($curl);
$error = curl_error($curl);
curl_close($curl);
$rawStr = ($raw === false) ? '' : (string) $raw;
$decoded = json_decode($rawStr, true);
return [
'decoded' => is_array($decoded) ? $decoded : null,
'raw' => $rawStr,
'curl_errno' => $errno,
'curl_error' => $error,
];
}
function is_wechat()
{
return strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger");
@ -372,23 +401,70 @@ function sms($to, $vars, $template_id, $belongs_type = null, $belongs_id = null)
is_array($vars) ? $vars = json_encode($vars) : '';
$data = "appid=" . $sms_app_id . "&signature=" . $sms_app_key . "&project=" . $template_id . "&vars=" . $vars . "&to=" . $to;
$url = "https://api.submail.cn/message/xsend.json";
$return = curl($url, $data);
\Illuminate\Support\Facades\Log::info('sms.submail.request', [
'to' => $to,
'template_id' => $template_id,
'vars' => $vars,
'appid_set' => ($sms_app_id !== null && $sms_app_id !== ''),
'appkey_set' => ($sms_app_key !== null && $sms_app_key !== ''),
'belongs_type' => $belongs_type,
'belongs_id' => $belongs_id,
]);
$http = submail_message_xsend_post($url, $data);
$return = $http['decoded'];
if ($http['curl_errno'] !== 0) {
\Illuminate\Support\Facades\Log::error('sms.submail.curl_failed', [
'to' => $to,
'template_id' => $template_id,
'curl_errno' => $http['curl_errno'],
'curl_error' => $http['curl_error'],
'body_preview' => mb_substr($http['raw'], 0, 800),
]);
$return = ['status' => 'error', 'code' => 'curl', 'msg' => $http['curl_error'] ?: 'curl_exec failed'];
} elseif (!is_array($return)) {
\Illuminate\Support\Facades\Log::error('sms.submail.invalid_json', [
'to' => $to,
'template_id' => $template_id,
'json_error' => json_last_error_msg(),
'body_preview' => mb_substr($http['raw'], 0, 800),
]);
$return = ['status' => 'error', 'code' => 'json', 'msg' => 'invalid json response'];
} else {
if (($return['status'] ?? '') === 'success') {
\Illuminate\Support\Facades\Log::info('sms.submail.response', [
'to' => $to,
'template_id' => $template_id,
'status' => $return['status'] ?? null,
]);
} else {
\Illuminate\Support\Facades\Log::warning('sms.submail.api_rejected', [
'to' => $to,
'template_id' => $template_id,
'status' => $return['status'] ?? null,
'code' => $return['code'] ?? null,
'msg' => $return['msg'] ?? null,
]);
}
}
$sms['mobile'] = $to;
$sms['vars'] = $vars;
$sms['template_id'] = $template_id;
$sms['ip'] = request()->getClientIp();
$sms['status'] = $return['status'];
$sms['status'] = $return['status'] ?? 'error';
$sms['belongs_type'] = $belongs_type;
$sms['belongs_id'] = $belongs_id;
if ($return['status'] != "success") {
$sms['result_code'] = $return['code'];
$sms['result_msg'] = $return['msg'];
if (($return['status'] ?? '') != "success") {
$sms['result_code'] = $return['code'] ?? null;
$sms['result_msg'] = $return['msg'] ?? null;
}
$sms['created_at'] = date("Y-m-d H:i:s");
(new \App\Models\Sms())->insert($sms);
return $return['status'] == "success";
return ($return['status'] ?? '') == "success";
}
function get_http_type()

Loading…
Cancel
Save