AliSms.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Aix\Sms;
  3. use Aix\Sms\Contracts\Smser;
  4. use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
  5. use Aliyun\Core\Config;
  6. use Aliyun\Core\DefaultAcsClient;
  7. use Aliyun\Core\Profile\DefaultProfile;
  8. use App\Models\SmsTemplate;
  9. use Illuminate\Support\Facades\Cache;
  10. /**
  11. * 啊里云短信
  12. * Class AliSms
  13. * @package Aix\Sms
  14. * Auth Zhong
  15. * Date 2018/11/2
  16. */
  17. class AliSms implements Smser
  18. {
  19. /**
  20. * @var DefaultAcsClient
  21. */
  22. protected static $acsClient;
  23. private $app_key;
  24. private $app_secret;
  25. private $sign_key;
  26. /**
  27. * SmsService constructor.
  28. * @param $app_key
  29. * @param $app_secret
  30. * @param $sign_key
  31. */
  32. public function __construct($app_key, $app_secret, $sign_key)
  33. {
  34. Config::load();
  35. $this->app_key = $app_key;
  36. $this->app_secret = $app_secret;
  37. $this->registerAcsClient();
  38. $this->sign_key = $sign_key;
  39. }
  40. /**
  41. * 取得AcsClient
  42. *
  43. * @return void
  44. */
  45. private function registerAcsClient()
  46. {
  47. //产品名称:云通信短信服务API产品,开发者无需替换
  48. $product = "Dysmsapi";
  49. //产品域名,开发者无需替换
  50. $domain = "dysmsapi.aliyuncs.com";
  51. // 暂时不支持多Region
  52. $region = "cn-hangzhou";
  53. // 服务结点
  54. $endPointName = "cn-hangzhou";
  55. if (static::$acsClient == null) {
  56. //初始化acsClient,暂不支持region化
  57. $profile = DefaultProfile::getProfile($region, $this->app_key, $this->app_secret);
  58. // 增加服务结点
  59. DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
  60. // 初始化AcsClient用于发起请求
  61. static::$acsClient = new DefaultAcsClient($profile);
  62. }
  63. }
  64. /**
  65. * 发送基本短信
  66. * @param string $mobile
  67. * @param SmsTemplate $template
  68. * @param array $params
  69. * @return bool
  70. * @throws \Exception
  71. */
  72. public function sendSms(string $mobile, SmsTemplate $template, array $params = []): bool
  73. {
  74. // 初始化SendSmsRequest实例用于设置发送短信的参数
  75. $request = new SendSmsRequest();
  76. //可选-启用https协议
  77. //$request->setProtocol("https");
  78. // 必填,设置短信接收号码
  79. $request->setPhoneNumbers($mobile);
  80. // 必填,设置签名名称,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  81. $request->setSignName($this->sign_key);
  82. // 必填,设置模板CODE,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  83. $request->setTemplateCode($template->outer_template_id);
  84. // 可选,设置模板参数, 假如模板中存在变量需要替换则为必填项
  85. if (!empty($params)) {
  86. $request->setTemplateParam(json_encode($params, JSON_UNESCAPED_UNICODE));
  87. }
  88. // 可选,设置流水号
  89. //$request->setOutId("yourOutId");
  90. // 选填,上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
  91. //$request->setSmsUpExtendCode("1234567");
  92. // 发起访问请求
  93. $acsResponse = static::$acsClient->getAcsResponse($request);
  94. if ($acsResponse->Message != 'OK') {
  95. throw new \Exception($acsResponse->Message);
  96. }
  97. return true;
  98. }
  99. /**
  100. * 发送确认短信,需要缓存验证码code
  101. * @param string $mobile
  102. * @param SmsTemplate $template
  103. * @return bool
  104. * @throws \Exception
  105. */
  106. public function sendAuthSms(string $mobile, SmsTemplate $template): bool
  107. {
  108. $params['code']=rand(100000, 999999);
  109. $this->sendSms($mobile, $template, $params);
  110. Cache::put($template->alias.'_'.$mobile, $params['code'], 15);
  111. return true;
  112. }
  113. /**
  114. * 批量发送短信
  115. * @param array $mobiles
  116. * @param SmsTemplate $template
  117. * @param array $params
  118. * @return bool
  119. */
  120. public function sendBatchSms(array $mobiles, SmsTemplate $template, array $params = []): bool
  121. {
  122. return true;
  123. }
  124. }