Sms.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace addons\DepositPresale\common\helpers\sms;
  3. use Yii;
  4. use Overtrue\EasySms\EasySms;
  5. use addons\DepositPresale\common\enums\DepositPresaleEnum;
  6. use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
  7. class Sms
  8. {
  9. public $config;
  10. public $smsConfig;
  11. public $easySms;
  12. public $minutes = 1; // 短信发送间隔(分钟)
  13. public static $validityMinutes = 5; // 验证码有效时间(分钟)
  14. public function __construct($mall_id = '')
  15. {
  16. try {
  17. $this->config = [
  18. // HTTP 请求的超时时间(秒)
  19. 'timeout' => 5.0,
  20. // 默认发送配置
  21. 'default' => [
  22. // 网关调用策略,默认:顺序调用
  23. 'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
  24. // 默认可用的发送网关
  25. 'gateways' => [
  26. 'aliyun',
  27. ],
  28. ],
  29. // 可用的网关配置
  30. 'gateways' => [
  31. 'errorlog' => [
  32. 'file' => '/runtime/easy-sms.log',
  33. ],
  34. //...
  35. ],
  36. ];
  37. // 阿里云短信配置
  38. $notice_sms = Yii::$app->services->setting->addons->get($mall_id, DepositPresaleEnum::ADDONS_NAME, 'basic.notice_sms');
  39. if (empty($notice_sms)) {
  40. $this->notice_wechat = false;
  41. throw new \Exception('没有配置信息');
  42. }
  43. if (is_string($notice_sms)) {
  44. $template_info = json_decode($notice_sms, true);
  45. } else {
  46. if (is_array($notice_sms)) {
  47. $template_info = $notice_sms;
  48. } else {
  49. $this->notice_wechat = false;
  50. return false;
  51. }
  52. }
  53. if (!empty($template_info) && $template_info['platform']['value'] == 'aliyun') {
  54. $this->config['gateways']['aliyun'] = [
  55. 'access_key_id' => $template_info['access_key_id']['value'],
  56. 'access_key_secret' => $template_info['access_key_secret']['value'],
  57. 'sign_name' => $template_info['template_name']['value'],
  58. ];
  59. $this->smsConfig['order'] = [
  60. 'template_id' => $template_info['order_template_id']['value'],
  61. 'template_variable' => $template_info['order_template_variable']['value'],
  62. ];
  63. $this->smsConfig['order_ship'] = [
  64. 'template_id' => $template_info['sale_template_id']['value'],
  65. 'template_variable' => $template_info['sale_template_variable']['value'],
  66. ];
  67. }
  68. $this->easySms = new EasySms($this->config);
  69. } catch (\Exception $e) {
  70. Yii::error('插件DepositPresale:短信配置出现问题--' . $e->getMessage());
  71. }
  72. }
  73. /**
  74. * @Author: observer3
  75. * @DateTime 2022-05-12 20:35:05
  76. * @param string $mobile
  77. * @param array $content
  78. * @return bool
  79. */
  80. public function createOrder(string $mobile, array $content)
  81. {
  82. if(!$this->easySms) return false;
  83. try {
  84. $message = new OrderCreateMessage($content, $this->smsConfig['order']);
  85. $this->easySms->send($mobile, $message);
  86. return true;
  87. } catch (NoGatewayAvailableException $e) {
  88. $res = $e->getExceptions();
  89. if (!empty($res)) {
  90. foreach ($res as $exception) {
  91. if (is_string($exception->getMessage())) {
  92. \Yii::error('短信发送失败:' . $exception->getMessage());
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. }
  99. /**
  100. * @Author: observer3
  101. * @DateTime 2022-05-12 20:35:37
  102. * @param string $mobile
  103. * @param array $content
  104. * @return bool
  105. */
  106. public function shipOrder(string $mobile, array $content)
  107. {
  108. if(!$this->easySms) return false;
  109. try {
  110. $message = new OrderShipMessage($content, $this->smsConfig['order_ship']);
  111. $this->easySms->send($mobile, $message);
  112. return true;
  113. } catch (NoGatewayAvailableException $e) {
  114. $res = $e->getExceptions();
  115. if (!empty($res)) {
  116. foreach ($res as $exception) {
  117. if (is_string($exception->getMessage())) {
  118. \Yii::error('短信发送失败:' . $exception->getMessage());
  119. }
  120. }
  121. }
  122. return false;
  123. }
  124. }
  125. }