// +---------------------------------------------------------------------- namespace App\Services; use App\Models\ConfigModel; /** * 手机短信管理-服务类 * @author * @since 2020/11/11 * Class SmsService * @package App\Services */ class SmsService extends BaseService { // 静态对象 protected static $instance = null; /** * 初始化配置 * @return bool */ public function __construct() { $this->config = ConfigService::make()->getConfigByGroup(2); if (empty($this->config)) { return false; } } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 发送手机短信 * @param $email * @param string $scene 场景:默认reg-注册 * @return bool */ public function sendCode($mobile, $scene = 'reg') { try { $cacheKey = "stores:codes:sms_{$scene}:" . substr($mobile, -3, 3) . '_' . md5($mobile); $apiUrl = isset($this->config['sms_api_url']['value']) ? $this->config['sms_api_url']['value'] : ''; if (empty($mobile) || empty($apiUrl)) { $this->error = '1013'; return false; } if (RedisService::get($cacheKey . '_lock')) { $this->error = '1014'; return false; } // 生成验证码 $code = rand(100000, 999999); $template = isset($this->config['sms_template_' . $scene]['value']) && $this->config['sms_template_' . $scene]['value'] ? $this->config['sms_template_' . $scene]['value'] : '您的验证码是:{code}'; $template = str_replace('{code}', $code, $template); $data = [ 'userid' => isset($this->config['sms_id']['value']) ? $this->config['sms_id']['value'] : '', 'account' => isset($this->config['sms_account']['value']) ? $this->config['sms_account']['value'] : '', 'password' => isset($this->config['sms_password']['value']) ? $this->config['sms_password']['value'] : '', 'mobile' => $mobile, 'content' => $template, 'action' => 'send', 'sendTime' => '', // 发送时间:空立即发送 ]; RedisService::set($cacheKey . '_temp', ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], 600); RedisService::set($cacheKey . '_lock', ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], rand(10, 20)); $result = curl_post($apiUrl, $data); $result = $result ? xmlToArray($result) : []; $status = isset($result['returnstatus']) ? $result['returnstatus'] : ''; if ($status === 'Success') { RedisService::set($cacheKey, ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], 300); return true; } $this->error = '1011'; return false; } catch (\Exception $exception) { $this->error = $exception->getMessage(); return false; } } /** * 验证手机短信 * @param $mobile * @param $code * @param string $scene 场景:默认reg-注册 * @return bool */ public function check($mobile, $code, $scene = 'reg') { $cacheKey = "stores:codes:sms_{$scene}:". substr($mobile, -3, 3) . '_' . md5($mobile); $codeData = RedisService::get($cacheKey); $checkCode = isset($codeData['code']) ? $codeData['code'] : 0; $checkMobile = isset($codeData['mobile']) ? $codeData['mobile'] : ''; if (empty($codeData) || empty($checkCode)) { $this->error = 2026; return false; } if ($checkMobile != $mobile || $checkCode != $code) { $this->error = 2006; return false; } return true; } }