AliSms.php 4.0 KB

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