| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- /**
- * @link:http://www.gdqijianshi.com/
- * @copyright: Copyright (c) 2020 广东七件事集团
- * 短信信息
- * Author: zal
- * Date: 2020-04-18
- * Time: 15:11
- */
- namespace common\helpers\sms;
- use common\models\common\MallSetting;
- use common\models\common\SmsLog;
- use Overtrue\EasySms\EasySms;
- use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
- use Overtrue\EasySms\PhoneNumber;
- use yii\db\Exception;
- use common\models\common\PlatformSetting;
- class Sms
- {
- const SMS_TYPE_USER_LOGIN = 'user_login'; //类型:用户登录
- const SMS_TYPE_REGISTER = 'register'; //类型:注册
- const SMS_TYPE_UPDATE_PASS = 'update_pass'; //类型:修改登录密码
- const SMS_TYPE_UPDATE_PAY_PASS = 'pay_pass'; //类型:修改支付密码
- const SMS_TYPE_MOBILE_BIND = 'mobile_bind'; //类型:绑定手机号
- const SMS_TYPE_UPDATE_MOBILE = 'update_mobile'; //类型:修改手机号
- const SMS_TYPE_BANK_CARD_DELETE = 'card_delete'; //类型:删除银行卡
- const SMS_TYPE_REGISTER_INFO = 'register_info'; //类型:注册信息插件
- const SMS_TYPE_USER_CANCELLATION = 'user_cancellation'; //类型:注册信息插件
- public $config;
- public $smsConfig;
- public $easySms;
- public $minutes = 1;// 短信发送间隔(分钟)
- public static $validityMinutes = 5;// 验证码有效时间(分钟)
- public $gateways = 'domestic';// domestic:国内,international:国际
- public $mall_id = 0;
- public $sms_status;
- public function __construct($mall_id='', $gateways = 'domestic', $type = 'mall')
- {
- $this->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;
- }
- }
- }
|