config = [ // HTTP 请求的超时时间(秒) 'timeout' => 5.0, // 默认发送配置 'default' => [ // 网关调用策略,默认:顺序调用 'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class, // 默认可用的发送网关 'gateways' => [ 'aliyun', 'qcloud', 'submail', ], ], // 可用的网关配置 'gateways' => [ 'errorlog' => [ 'file' => '/runtime/easy-sms.log', ], //... ], ]; $this->mall_id = $mall_id; // 阿里云短信配置 if($type == 'mall'){ $data = MallSetting::getConfByGroup($mall_id, ['notice_sms'], true); $notice_sms = $data['notice_sms']; }else{ $notice_sms = PlatformSetting::getConfByGroup('sms', true); } $this->sms_status = $notice_sms['status']['value'] ?? 0; $this->gateways = $gateways; if (!empty($notice_sms)) { switch ($notice_sms['platform']['value']) { case 'aliyun'://阿里云配置 (new SmsConfig())->aliyunConfig($notice_sms, $this->config, $this->smsConfig, $this->gateways, $type); break; case 'qcloud'://腾讯云配置 (new SmsConfig())->qcloudConfig($notice_sms, $this->config, $this->smsConfig, $this->gateways, $type); break; case 'submail'://赛邮云配置 (new SmsConfig())->submailConfig($notice_sms, $this->config, $this->smsConfig, $this->gateways, $type); break; case 'yunpian'://云片配置 // (new SmsConfig())->yunpianConfig($notice_sms, $this->config, $this->smsConfig, $this->gateways, $type); break; } } else { //默认的短信验证码配置 $this->config['gateways']['aliyun'] = [ 'access_key_id' => \Yii::$app->params['aliyun']['access_key_id']??'', 'access_key_secret' =>\Yii::$app->params['aliyun']['access_key_secret']??'', 'sign_name' => \Yii::$app->params['aliyun']['sign_name']??'', ]; $this->smsConfig['captcha'] = [ 'template_id'=>\Yii::$app->params['aliyun']['captcha']['template_id']??'', 'template_variable'=>\Yii::$app->params['aliyun']['captcha']['template_variable']??'', ]; } $this->easySms = new EasySms($this->config); } /** * 发送短信验证码 * @param string $mobile * @return bool * @throws NoGatewayAvailableException * @throws \Exception */ public function sendCaptcha(string $mobile = '', $area_code = '',$type='') { try { if (empty($this->sms_status)) throw new \Exception('短信功能尚未开通'); $captcha = (string)mt_rand(100000, 999999); $message = new CaptchaMessage($captcha, $this->smsConfig['captcha']); $send_mobile = $mobile; if (!empty($area_code) && $mobile != 86) { $send_mobile = new PhoneNumber($mobile, $area_code); } if(SmsLog::setAliSms($mobile, $this->mall_id) === false) throw new \Exception(SmsLog::getStaticError());// 短信记录 $results = $this->easySms->send($send_mobile, $message); \Yii::$app->cache->set($mobile.'_'.$type, $captcha, self::$validityMinutes * 60); return true; } catch (NoGatewayAvailableException $e) { $res = $e->getExceptions(); if(!empty($res)){ foreach ($res as $exception){ if(is_string($exception->getMessage())){ throw new Exception($exception->getMessage()); \Yii::error('短信发送失败:' .$exception->getMessage()); } } } return false; } } public function sendNewUser(string $mobile = '', $nickname='', $area_code = '') { if(empty($this->smsConfig['new_user']['template_id']) && empty($this->smsConfig['new_user']['project'])) throw new Exception('模板ID不能为空'); if(empty($this->smsConfig['new_user']['template_variable'])) throw new Exception('模板签名不能为空'); if (empty($this->sms_status)) throw new \Exception('短信功能尚未开通'); try { $message = new CaptchaMessage($nickname, $this->smsConfig['new_user']); $send_mobile = $mobile; if (!empty($area_code) && $mobile != 86) { $send_mobile = new PhoneNumber($mobile, $area_code); } if(SmsLog::setAliSms($mobile, $this->mall_id, 'newuser') === false) throw new \Exception(SmsLog::getStaticError()); $this->easySms->send($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; } } public function sendOrderOn(string $mobile = '', $order_no='', $area_code = '') { if(empty($this->smsConfig['order']['template_id']) && empty($this->smsConfig['order']['project'])) throw new Exception('模板ID不能为空'); if(empty($this->smsConfig['order']['template_variable'])) throw new Exception('模板签名不能为空'); if (empty($this->sms_status)) throw new \Exception('短信功能尚未开通'); try { $message = new CaptchaMessage($order_no, $this->smsConfig['order']); $send_mobile = $mobile; if (!empty($area_code) && $mobile != 86) { $send_mobile = new PhoneNumber($mobile, $area_code); } if(SmsLog::setAliSms($mobile, $this->mall_id, 'order') === false) throw new \Exception(SmsLog::getStaticError()); $this->easySms->send($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; } } public function sendRefundOrderOn(string $mobile = '', $order_no='', $area_code = '') { if(empty($this->smsConfig['order_refund']['template_id']) && empty($this->smsConfig['order_refund']['project'])) throw new Exception('模板ID不能为空'); if(empty($this->smsConfig['order_refund']['template_variable'])) throw new Exception('模板签名不能为空'); if (empty($this->sms_status)) throw new \Exception('短信功能尚未开通'); try { $message = new CaptchaMessage($order_no, $this->smsConfig['order_refund']); $send_mobile = $mobile; if (!empty($area_code) && $mobile != 86) { $send_mobile = new PhoneNumber($mobile, $area_code); } if(SmsLog::setAliSms($mobile, $this->mall_id, 'refund_order') === false) throw new \Exception(SmsLog::getStaticError()); $this->easySms->send($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; } } /** * 会员到期短信提醒 * * @param string $mobile * @param string $nickname * @param string $area_code * * @return bool * @throws Exception * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException */ public function sendUserLevelExpired(string $mobile = '', string $nickname = '', string $area_code = '') { if(empty($this->smsConfig['user_level_expired']['template_id']) && empty($this->smsConfig['user_level_expired']['project'])) throw new Exception('模板ID不能为空'); if(empty($this->smsConfig['user_level_expired']['template_variable'])) throw new Exception('模板签名不能为空'); if (empty($this->sms_status)) throw new \Exception('短信功能尚未开通'); try { $message = new CaptchaMessage($nickname, $this->smsConfig['user_level_expired']); $send_mobile = $mobile; if (!empty($area_code) && $area_code != 86) { $send_mobile = new PhoneNumber($mobile, $area_code); } if(SmsLog::setAliSms($mobile, $this->mall_id, 'user_level_expired') === false) throw new \Exception(SmsLog::getStaticError()); $this->easySms->send($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; } } /** * 检测短信验证码是否有效 * @param $mobile * @param $code * @return bool * @throws */ public static function checkValidateCode($mobile, $code,$type='') { //限制验证码错误次数 $redis = \Yii::$app->redis; $error_key = 'validate_code_error_num_'.$mobile.'_'.$type; $error_num = $redis->get($error_key) ?? 0; if($error_num > 2) throw new \Exception('验证码不正确,请5分钟后重试'); $mobile = (string)$mobile; $no_type = ''; //|| \Yii::$app->cache->get($mobile.'_'.$no_type) == $code if ( !empty(\Yii::$app->cache->get($mobile.'_'.$type)) && \Yii::$app->cache->get($mobile.'_'.$type) == $code) { \Yii::$app->cache->delete($mobile.'_'.$type); return true; }else{ $redis->incr($error_key); $redis->expire($error_key,300); } return false; } /** * 支付成功发送短信 * @Author: lun * @DateTime: 2022/6/22 0022 15:06 * @Copyright: copyright (c) 2021 广东七件事集团 * @param string $order_no * @param string $mobile * @param string $area_code * @return bool * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException */ public function sendOrder($order_no, $mobile = '', $area_code = '') { try { if (empty($this->sms_status)) throw new \Exception('短信功能尚未开通'); if (empty($mobile)) return true; if (empty($this->smsConfig['order']['template_id']) && empty($this->smsConfig['order']['project'])) return true; $message = new CaptchaMessage($order_no, $this->smsConfig['order']); if (!empty($area_code) && $area_code != 86) { $mobile = new PhoneNumber($mobile, $area_code); } if(SmsLog::setAliSms($mobile, $this->mall_id, 'order') === false) throw new \Exception(SmsLog::getStaticError()); $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; } } }