AliSms.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\service\sms\engine;
  15. use AlibabaCloud\Client\AlibabaCloud;
  16. /**
  17. * 阿里云短信
  18. * Class AliSms
  19. * @package app\common\service\sms\engine
  20. */
  21. class AliSms
  22. {
  23. protected $error = null;
  24. protected $config;
  25. protected $mobile;
  26. protected $templateId;
  27. protected $templateParams;
  28. public function __construct($config)
  29. {
  30. if(empty($config)) {
  31. $this->error = '请联系管理员配置参数';
  32. return false;
  33. }
  34. $this->config = $config;
  35. }
  36. /**
  37. * @notes 设置手机号
  38. * @param $mobile
  39. * @return $this
  40. * @author 段誉
  41. * @date 2022/9/15 16:28
  42. */
  43. public function setMobile($mobile)
  44. {
  45. $this->mobile = $mobile;
  46. return $this;
  47. }
  48. /**
  49. * @notes 设置模板id
  50. * @param $templateId
  51. * @return $this
  52. * @author 段誉
  53. * @date 2022/9/15 16:28
  54. */
  55. public function setTemplateId($templateId)
  56. {
  57. $this->templateId = $templateId;
  58. return $this;
  59. }
  60. /**
  61. * @notes 设置模板参数
  62. * @param $templateParams
  63. * @return $this
  64. * @author 段誉
  65. * @date 2022/9/15 16:28
  66. */
  67. public function setTemplateParams($templateParams)
  68. {
  69. $this->templateParams = json_encode($templateParams, JSON_UNESCAPED_UNICODE);
  70. return $this;
  71. }
  72. /**
  73. * @notes 错误信息
  74. * @return string|null
  75. * @author 段誉
  76. * @date 2022/9/15 16:27
  77. */
  78. public function getError()
  79. {
  80. return $this->error;
  81. }
  82. /**
  83. * @notes 发送短信
  84. * @return array|false
  85. * @author 段誉
  86. * @date 2022/9/15 16:27
  87. */
  88. public function send()
  89. {
  90. try {
  91. AlibabaCloud::accessKeyClient($this->config['app_key'], $this->config['secret_key'])
  92. ->regionId('cn-hangzhou')
  93. ->asDefaultClient();
  94. $result = AlibabaCloud::rpcRequest()
  95. ->product('Dysmsapi')
  96. ->host('dysmsapi.aliyuncs.com')
  97. ->version('2017-05-25')
  98. ->action('SendSms')
  99. ->method('POST')
  100. ->options([
  101. 'query' => [
  102. 'PhoneNumbers' => $this->mobile, //发送手机号
  103. 'SignName' => $this->config['sign'], //短信签名
  104. 'TemplateCode' => $this->templateId, //短信模板CODE
  105. 'TemplateParam' => $this->templateParams, //自定义随机数
  106. ],
  107. ])
  108. ->request();
  109. $res = $result->toArray();
  110. if (isset($res['Code']) && $res['Code'] == 'OK') {
  111. return $res;
  112. }
  113. $message = $res['Message'] ?? $res;
  114. throw new \Exception('阿里云短信错误:' . $message);
  115. } catch(\Exception $e) {
  116. $this->error = $e->getMessage();
  117. return false;
  118. }
  119. }
  120. }