| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace addons\DepositPresale\common\helpers\sms;
- use Yii;
- use Overtrue\EasySms\EasySms;
- use addons\DepositPresale\common\enums\DepositPresaleEnum;
- use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
- class Sms
- {
- public $config;
- public $smsConfig;
- public $easySms;
- public $minutes = 1; // 短信发送间隔(分钟)
- public static $validityMinutes = 5; // 验证码有效时间(分钟)
- public function __construct($mall_id = '')
- {
- try {
- $this->config = [
- // HTTP 请求的超时时间(秒)
- 'timeout' => 5.0,
- // 默认发送配置
- 'default' => [
- // 网关调用策略,默认:顺序调用
- 'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
- // 默认可用的发送网关
- 'gateways' => [
- 'aliyun',
- ],
- ],
- // 可用的网关配置
- 'gateways' => [
- 'errorlog' => [
- 'file' => '/runtime/easy-sms.log',
- ],
- //...
- ],
- ];
- // 阿里云短信配置
- $notice_sms = Yii::$app->services->setting->addons->get($mall_id, DepositPresaleEnum::ADDONS_NAME, 'basic.notice_sms');
- if (empty($notice_sms)) {
- $this->notice_wechat = false;
- throw new \Exception('没有配置信息');
- }
- if (is_string($notice_sms)) {
- $template_info = json_decode($notice_sms, true);
- } else {
- if (is_array($notice_sms)) {
- $template_info = $notice_sms;
- } else {
- $this->notice_wechat = false;
- return false;
- }
- }
- if (!empty($template_info) && $template_info['platform']['value'] == 'aliyun') {
- $this->config['gateways']['aliyun'] = [
- 'access_key_id' => $template_info['access_key_id']['value'],
- 'access_key_secret' => $template_info['access_key_secret']['value'],
- 'sign_name' => $template_info['template_name']['value'],
- ];
- $this->smsConfig['order'] = [
- 'template_id' => $template_info['order_template_id']['value'],
- 'template_variable' => $template_info['order_template_variable']['value'],
- ];
- $this->smsConfig['order_ship'] = [
- 'template_id' => $template_info['sale_template_id']['value'],
- 'template_variable' => $template_info['sale_template_variable']['value'],
- ];
- }
- $this->easySms = new EasySms($this->config);
- } catch (\Exception $e) {
- Yii::error('插件DepositPresale:短信配置出现问题--' . $e->getMessage());
- }
- }
- /**
- * @Author: observer3
- * @DateTime 2022-05-12 20:35:05
- * @param string $mobile
- * @param array $content
- * @return bool
- */
- public function createOrder(string $mobile, array $content)
- {
- if(!$this->easySms) return false;
-
- try {
- $message = new OrderCreateMessage($content, $this->smsConfig['order']);
- $this->easySms->send($mobile, $message);
- return true;
- } catch (NoGatewayAvailableException $e) {
- $res = $e->getExceptions();
- if (!empty($res)) {
- foreach ($res as $exception) {
- if (is_string($exception->getMessage())) {
- \Yii::error('短信发送失败:' . $exception->getMessage());
- }
- }
- }
- return false;
- }
- }
- /**
- * @Author: observer3
- * @DateTime 2022-05-12 20:35:37
- * @param string $mobile
- * @param array $content
- * @return bool
- */
- public function shipOrder(string $mobile, array $content)
- {
- if(!$this->easySms) return false;
- try {
- $message = new OrderShipMessage($content, $this->smsConfig['order_ship']);
- $this->easySms->send($mobile, $message);
- return true;
- } catch (NoGatewayAvailableException $e) {
- $res = $e->getExceptions();
- if (!empty($res)) {
- foreach ($res as $exception) {
- if (is_string($exception->getMessage())) {
- \Yii::error('短信发送失败:' . $exception->getMessage());
- }
- }
- }
- return false;
- }
- }
- }
|