// +---------------------------------------------------------------------- namespace App\Services; use AlibabaCloud\Tea\Exception\TeaUnableRetryError; use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; /** * 创信短信管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class SmsService * @package App\Services */ class SmsService extends BaseService { // 静态对象 protected static $instance = null; /** * 静态入口 * @return SmsService|static|null */ public static function make(){ if(!self::$instance){ self::$instance = new static(); } return self::$instance; } /** * 发送短信验证码 * @param $mobile * @param string $type * @return bool */ public function send($mobile, $type='login') { $cacheKey = "caches:sms:cx_{$mobile}:{$type}"; if(RedisService::get($cacheKey.'_lock')){ $this->error = '1060'; return false; } $config = ConfigService::make()->getConfigOptionByGroup(16); $apiUrl = isset($config['cx_api_url'])? trim($config['cx_api_url']) : ''; $account = isset($config['cx_sms_account'])? trim($config['cx_sms_account']) : ''; $password = isset($config['cx_sms_password'])? trim($config['cx_sms_password']) : ''; $extno = isset($config['cx_sms_extno'])? trim($config['cx_sms_extno']) : ''; $signName = isset($config['cx_sms_signname']) && $config['cx_sms_signname']? trim($config['cx_sms_signname']) : '星链社交'; if(empty($apiUrl) || empty($account) || empty($password) || empty($extno)){ $this->error = 1056; return false; } // 发送逻辑 $code = rand(1000,9999); $content="【{$signName}】您的验证码是:{$code},该验证码5分钟内有效,请在登录页面中相应位置输入完成验证。请勿向他人泄漏该验证码。";//短信内容 // $content="【{$signName}】您的验证码是:{$code},请在10分钟内使用,请不要将验证码泄漏个他人。";//短信内容 $content1 = iconv('UTF-8','GBK', $content); $url="{$apiUrl}/sms?action=send&password={$password}&extno={$extno}&mobile={$mobile}&account={$account}&content="; try { $response = httpRequest($url.$content1,'','get','',5,[],'text'); $responseObject = simplexml_load_string($response); $response = json_decode(json_encode((array)$responseObject),true); $returnStatus = isset($response['returnstatus'])? $response['returnstatus'] : ''; if($returnStatus != 'Failed'){ $this->error = 1063; RedisService::set($cacheKey,['code'=> $code,'mobile'=>$mobile,'config'=>$config,'code'=>$code,'result'=>$response,'date'=> date('Y-m-d H:i:s')], 600); return true; }else{ $this->error = 1059; RedisService::set($cacheKey.'_fail', ['url'=>$url,'mobile'=> $mobile,'config'=>$config,'response'=>$response,'error'=>$this->error], 6 * 3600); return false; } } catch (TeaUnableRetryError $e){ $date = date('Y-m-d H:i:s'); logger()->error("【{$date} 创信SMS短信验证码】发送失败:".$e->getMessage()); RedisService::set($cacheKey.'_error', ['mobile'=> $mobile,'config'=>$config,'error'=>$e->getMessage()], 6 * 3600); } $this->error = 1059; return false; } /** * 短信验证码验证 * @param string $mobile 手机号 * @param string $code 当前验证码 * @param string $type 验证码场景类型,login-登录,reg-注册 * @return bool */ public function check($mobile, $code, $type='login') { if($code == '1100'){ return true; } $cacheKey = "caches:sms:cx_{$mobile}:{$type}"; $data = RedisService::get($cacheKey); $smsCode = isset($data['code'])? $data['code'] : ''; if(empty($data) || empty($smsCode)){ $this->error = '1061'; return false; } if($smsCode != $code){ $this->error = '1062'; return false; } return true; } }