| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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', "<?php \n /* 邮箱服务配置 */ \n return " . var_export($mail, true) . ';' . "\n ?>");
- }
- 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;
- }
- }
|