Driver.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\sms;
  13. use app\common\library\helper;
  14. use cores\traits\ErrorTrait;
  15. use cores\exception\BaseException;
  16. use Overtrue\EasySms\EasySms;
  17. use Overtrue\EasySms\Exceptions\InvalidArgumentException;
  18. use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
  19. /**
  20. * 短信通知模块驱动
  21. * Class Driver
  22. * @package app\common\library\sms
  23. */
  24. class Driver
  25. {
  26. use ErrorTrait;
  27. // 后台短信配置信息
  28. private $smsConfig;
  29. // 当前短信平台 (后台设置)
  30. private $gateway;
  31. /**
  32. * 构造方法
  33. * Driver constructor.
  34. * @param array $smsConfig
  35. */
  36. public function __construct(array $smsConfig)
  37. {
  38. // 配置信息
  39. $this->smsConfig = $smsConfig;
  40. // 当前短信平台
  41. $this->gateway = $this->smsConfig['default'];
  42. }
  43. /**
  44. * 发送短信通知
  45. * @param string $acceptPhone 接收的手机号
  46. * @param string $templateCode 短信模板ID
  47. * @param array $templateParams 短信模板参数
  48. * @return bool
  49. * @throws BaseException
  50. * @throws InvalidArgumentException
  51. */
  52. public function sendSms(string $acceptPhone, string $templateCode, array $templateParams): bool
  53. {
  54. // 实例化EasySms
  55. $easySmsConfig = Config::getEasySmsConfig($this->smsConfig);
  56. $easySms = new EasySms($easySmsConfig);
  57. try {
  58. // 执行发送短信
  59. $result = $easySms->send($acceptPhone, [
  60. 'template' => $templateCode,
  61. 'data' => $this->getSmsTemplateData($templateParams),
  62. ]);
  63. // 短信发送成功的错误处理
  64. $sendStatus = $this->resultHandle($result);
  65. } catch (NoGatewayAvailableException $e) {
  66. // 短信发送异常的错误处理
  67. $sendStatus = false;
  68. $this->exceptionHandle($e);
  69. }
  70. // 记录日志
  71. helper::logInfo('发送短信', [
  72. 'gateway' => $this->smsConfig['default'],
  73. 'acceptPhone' => $acceptPhone,
  74. 'templateCode' => $templateCode,
  75. 'templateParams' => $templateParams,
  76. 'sendStatus' => $sendStatus ? 'true' : 'false',
  77. 'sendErrMsg' => $this->getError(),
  78. ]);
  79. // 存在异常时抛错
  80. $sendStatus === false && throwError($this->getError());
  81. return $sendStatus;
  82. }
  83. /**
  84. * 短信发送成功的错误处理
  85. * @param array $result
  86. * @return bool
  87. */
  88. private function resultHandle(array $result): bool
  89. {
  90. // 腾讯云短信错误: 模板ID不正确
  91. if ($this->gateway === 'qcloud') {
  92. $response = $result[$this->gateway]['result']['Response']['SendStatusSet'];
  93. if (isset($response[0]) && isset($response[0]['Code']) && $response[0]['Code'] !== 'Ok') {
  94. $this->error = '请检查后台短信平台参数和模板ID是否正确';
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. /**
  101. * 短信发送异常的错误处理
  102. * @param NoGatewayAvailableException $e
  103. */
  104. private function exceptionHandle(NoGatewayAvailableException $e)
  105. {
  106. // 短信发送失败
  107. $err = $e->getLastException();
  108. if ($err instanceof \GuzzleHttp\Exception\ClientException) {
  109. $body = (string)$err->getResponse()->getBody();
  110. $result = helper::jsonDecode($body);
  111. if (isset($result['Message'])) {
  112. // $errMsg = $result['Message'];
  113. $this->error = '请检查后台短信平台的参数设置';
  114. return;
  115. }
  116. if (isset($result['message'])) {
  117. $this->error = $result['message'];
  118. return;
  119. }
  120. }
  121. // if ($err instanceof \Overtrue\EasySms\Exceptions\GatewayErrorException) {
  122. // $this->error = $err->getMessage();
  123. // return;
  124. // }
  125. $this->error = $err->getMessage();
  126. }
  127. /**
  128. * 生成短信模板数据
  129. * @param array $templateParams
  130. * @return array
  131. */
  132. private function getSmsTemplateData(array $templateParams): array
  133. {
  134. if ($this->gateway === 'qcloud') {
  135. $templateParams = array_values($templateParams);
  136. }
  137. return $templateParams;
  138. }
  139. }