chuan 4 months ago
parent
commit
7f9375da52
  1. 2
      app/admin/service/AdminService.php
  2. 60
      app/utility/SendSms.php
  3. BIN
      vendor.zip

2
app/admin/service/AdminService.php

@ -689,7 +689,7 @@ class AdminService extends AdminBaseService
}
$to = $user->phone_number;
$content = $smsTemplate->content;
$bool = (new \app\utility\SendSms())->send($to, $content, $accessKey, $secret);
$bool = (new \app\utility\SendSms())->sendMessageWithTemplate($to, $content, $accessKey, $secret);
if (!$bool) {
return $this->toData('500', '短信发送失败');
}

60
app/utility/SendSms.php

@ -19,28 +19,11 @@ class SendSms
->regionId('ap-southeast-1')
->asDefaultClient();
// $result = AlibabaCloud::rpc()
// ->product('Dysmsapi') // 短信服务的产品名称
// ->version('2017-05-25') // API 版本
// ->action('SendSms') // 调用 SendSms 接口
// ->method('POST')
// ->host('dysmsapi.ap-southeast-1.aliyuncs.com') // 短信服务的域名
// ->options([
// 'query' => [
// 'RegionId' => 'ap-southeast-1', // 区域 ID
// 'PhoneNumbers' => $toNum, // 接收短信的手机号 多个号码用逗号分隔
// 'SignName' => $content, // 短信签名名称 短信签名名称,在阿里云控制台中创建。
// 'TemplateCode' => 'SMS_1234567', // 短信模板 ID, 短信模板 ID,在阿里云控制台中创建。
// 'TemplateParam' => json_encode(['code' => '123456']), // 模板参数(JSON 格式) 短信模板中的变量参数,以 JSON 格式传递(如验证码、动态内容等)。
// ],
// ])
// ->request();
$result = AlibabaCloud::rpcRequest()
->product('Dysmsapi')
->host('dysmsapi.ap-southeast-1.aliyuncs.com')
->version('2018-05-01')
->action('SendMessageToGlobe')
->action('SendMessageToGlobe') // 该接口不支持往中国内地发送短信, QPS 限制为 2000 次/秒。超过限制,API 调用会被限流
->method('POST')
->options([
'query' => [
@ -67,4 +50,45 @@ class SendSms
return false;
}
// 发送中国大陆短信 【目前阿里云国际站账号中提示暂不支持发送国内短信】
public function sendMessageWithTemplate($to, $content, $accessKey, $secret)
{
try {
AlibabaCloud::accessKeyClient($accessKey, $secret)
->regionId('cn-qingdao')
->asDefaultClient();
$result = AlibabaCloud::rpc()
->product('Dysmsapi') // 短信服务的产品名称
->version('2018-05-01') // API 版本
->action('SendMessageWithTemplate') // 该方法仅支持发送中国大陆地区短信
->method('POST')
->host('dysmsapi.aliyuncs.com') // 短信服务的域名
->options([
'query' => [
'RegionId' => 'cn-qingdao', // 区域 ID
'PhoneNumbers' => $to, // 接收短信的手机号 多个号码用逗号分隔
'SignName' => $content, // 短信签名名称 短信签名名称,在阿里云控制台中创建。
'TemplateCode' => 'SMS_1234567', // 短信模板 ID, 短信模板 ID,在阿里云控制台中创建。
'TemplateParam' => json_encode(['code' => '123456']), // 模板参数(JSON 格式) 短信模板中的变量参数,以 JSON 格式传递(如验证码、动态内容等)。
],
])
->request();
$res = $result->toArray();
Log::error("----国内发送短信结果-----".json_encode([$res]));
if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){
trace('国内短信发送失败:'.json_encode([$res]), 'error');
}
return true;
} catch (ClientException $clientException) {
trace('国内短信发送失败1 - '.$clientException->getErrorMessage(), 'error');
} catch (ServerException $serverException) {
trace('国内短信发送失败2 - '.$serverException->getErrorMessage(), 'error');
} catch (\Exception $exception) {
trace('国内短信发送失败3 - '.$exception->getMessage(), 'error');
}
}
}

BIN
vendor.zip

Binary file not shown.
Loading…
Cancel
Save