截流自动化的商城平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AliSms.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\server\sms\engine;
  20. use AlibabaCloud\Client\AlibabaCloud;
  21. use AlibabaCloud\Client\Exception\ClientException;
  22. use AlibabaCloud\Client\Exception\ServerException;
  23. /**
  24. * 阿里短信
  25. * Class AliSms
  26. */
  27. class AliSms extends Server
  28. {
  29. protected $config;
  30. protected $mobile; //下发手机号
  31. protected $template_code; //验证码
  32. protected $template_param; //模板参数
  33. public function __construct($config = [])
  34. {
  35. if (empty($config)) {
  36. $this->error = '请联系管理员配置参数';
  37. return false;
  38. }
  39. $this->config = $config;
  40. }
  41. /**
  42. * Notes: 设置手机号
  43. * @param string $mobile
  44. * @author 段誉(2021/5/18 17:39)
  45. * @return $this
  46. */
  47. public function setMobile($mobile = '')
  48. {
  49. $this->mobile = $mobile;
  50. return $this;
  51. }
  52. /**
  53. * Notes: 设置模板id
  54. * @param string $template_code
  55. * @author 段誉(2021/5/18 17:39)
  56. * @return $this
  57. */
  58. public function setTemplateId($template_code = '')
  59. {
  60. $this->template_code = $template_code;
  61. return $this;
  62. }
  63. /**
  64. * Notes: 设置模板参数
  65. * @param string $template_param
  66. * @author 段誉(2021/5/18 17:39)
  67. * @return $this
  68. */
  69. public function setTemplateParam($template_param = '')
  70. {
  71. $this->template_param = json_encode($template_param);
  72. return $this;
  73. }
  74. /**
  75. * Notes: 发送短信
  76. * @author 段誉(2021/5/18 17:40)
  77. * @return array|bool
  78. * @throws ClientException
  79. */
  80. public function send()
  81. {
  82. AlibabaCloud::accessKeyClient($this->config['app_key'], $this->config['secret_key'])
  83. ->regionId('cn-hangzhou')
  84. ->asDefaultClient();
  85. try {
  86. $result = AlibabaCloud::rpcRequest()
  87. ->product('Dysmsapi')
  88. ->host('dysmsapi.aliyuncs.com')
  89. ->version('2017-05-25')
  90. ->action('SendSms')
  91. ->method('POST')
  92. ->options([
  93. 'query' => [
  94. 'PhoneNumbers' => $this->mobile, //发送手机号
  95. 'SignName' => $this->config['sign'], //短信签名
  96. 'TemplateCode' => $this->template_code, //短信模板CODE
  97. 'TemplateParam' => $this->template_param, //自定义随机数
  98. ],
  99. ])
  100. ->request();
  101. $res = $result->toArray();
  102. if (isset($res['Code']) && $res['Code'] == 'OK') {
  103. return $res;
  104. }
  105. $message = $res['Message'] ?? $res;
  106. throw new \Exception('短信错误:' . $message);
  107. } catch (ClientException $e) {
  108. $this->error = $e->getErrorMessage();
  109. return false;
  110. } catch (ServerException $e) {
  111. $this->error = $e->getErrorMessage();
  112. return false;
  113. } catch (\Exception $e) {
  114. $this->error = $e->getMessage();
  115. return false;
  116. }
  117. }
  118. }