// +---------------------------------------------------------------------- namespace App\Services; use app\Lib\smtpMail\smtp as smtpService; use Illuminate\Mail\Mailable; use Illuminate\Support\Facades\Mail; use PHPMailer\PHPMailer\PHPMailer; /** * 邮件管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class ConfigService * @package App\Services */ class EmailService extends BaseService { /** * 配置参数 * @var array */ protected $config = []; // 静态对象 protected static $instance = null; /** * 初始化配置 * @return bool */ public function __construct() { $config = ConfigService::make()->getConfigByGroup(3); $mail = config('mail'); $mailKey = md5(json_encode($mail)); $mail['default'] = isset($config['mail_mailer']['value']) && $config['mail_mailer']['value'] ? $config['mail_mailer']['value'] : $mail['default']; $mail['markdown']['paths'] = [resource_path('views/vendor/mail')]; $smtp = isset($mail['mailers']['smtp']) ? $mail['mailers']['smtp'] : []; $smtp['host'] = isset($config['mail_host']['value']) && $config['mail_host']['value'] ? $config['mail_host']['value'] : $smtp['host']; $smtp['port'] = isset($config['mail_port']['value']) && $config['mail_port']['value'] ? $config['mail_port']['value'] : $smtp['port']; $smtp['username'] = isset($config['mail_username']['value']) && $config['mail_username']['value'] ? $config['mail_username']['value'] : $smtp['host']; $smtp['password'] = isset($config['mail_password']['value']) && $config['mail_password']['value'] ? $config['mail_password']['value'] : $smtp['password']; $smtp['timeout'] = isset($config['mail_timeout']['value']) && $config['mail_timeout']['value'] ? $config['mail_timeout']['value'] : $smtp['timeout']; $smtp['encryption'] = isset($config['mail_source']['value']) && $config['mail_source']['value'] ? $config['mail_source']['value'] : $smtp['encryption']; $smtp['timeout'] = 10; if ($mail['default'] != 'smtp') { return false; } $mail['mailers']['smtp'] = $smtp; $mail['from']['address'] = $smtp['username']; $mail['from']['name'] = 'UTC交易平台'; $this->config = array_merge($config, $smtp); if ($mailKey != md5(json_encode($mail))) { file_put_contents(base_path() . '/config/mail.php', ""); } return true; } /** * 静态入口 * @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($email, $scene = 'reg') { try { $cacheKey = "stores:codes:email_{$scene}:" . md5($email); // 发送频繁 if (RedisService::get($cacheKey . '_lock')) { $this->error = '1014'; return false; } $title = isset($this->config['mail_title_' . $scene]['value']) && $this->config['mail_title_' . $scene]['value'] ? $this->config['mail_title_' . $scene]['value'] : "OTC168网站注册邮箱验证码"; $template = isset($this->config['mail_template_' . $scene]['value']) && $this->config['mail_template_' . $scene]['value'] ? $this->config['mail_template_' . $scene]['value'] : "您的验证码是:{code},5分钟有效期!!!"; // 生成验证码 $code = rand(100000, 999999); $template = str_replace('{code}', $code, $template); // 上锁 RedisService::set($cacheKey.'_lock', ['email'=> $email, 'code'=> $code, 'date'=> date('Y-m-d H:i:s')], rand(5, 10)); Mail::send('emails.index',['title'=> $title,'template'=> $template], function($mail) use($email, $title){ $mail->from($this->config['username'],'OTC168'); // 发送者 $mail->subject($title); // 标题 $mail->to($email); // 接受者 }); $error = Mail::failures(); if (!$error) { RedisService::set($cacheKey, ['email'=> $email, 'code'=> $code, 'date'=> date('Y-m-d H:i:s')], 300); return true; } return false; } catch (\Exception $exception){ $this->error = $exception->getMessage(); return false; } } /** * 验证邮件验证码 * @param $email * @param $code * @param string $scene 场景:默认reg-注册 * @return bool */ public function check($email, $code, $scene = 'reg') { $cacheKey = "stores:codes:email_{$scene}:" . md5($email); $codeData = RedisService::get($cacheKey); $checkCode = isset($codeData['code'])? $codeData['code'] : 0; $checkEmail = isset($codeData['email'])? $codeData['email'] : 0; if(empty($codeData) || empty($checkCode)){ $this->error = 2027; return false; } if($checkEmail != $email || $checkCode != $code){ $this->error = 2006; return false; } return true; } }