| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\ConfigModel;
- use Illuminate\Support\Facades\Mail;
- /**
- * 邮件管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class ConfigService
- * @package App\Services
- */
- class EmailService extends BaseService
- {
- /**
- * 配置参数
- * @var array
- */
- protected $config = [];
- /**
- * 初始化配置
- * @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'];
- 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;
- }
- /**
- * 发送邮件验证码
- * @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'] : "UTC网站注册邮箱验证码";
- $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));
- if (Mail::send('emails.index', ['title' => $title,'template'=> $template], function ($message) use ($email, $template) {
- $to = $email;
- $message->to($to)->subject($template);
- })) {
- RedisService::set($cacheKey, ['email'=> $email, 'code'=> $code, 'date'=> date('Y-m-d H:i:s')], 300);
- return true;
- }
- } catch (\Exception $exception){
- $this->error = $exception->getMessage();
- return false;
- }
- $this->error = '1005';
- return false;
- }
- /**
- * 验证邮件验证码
- * @param $email
- * @param string $scene 场景:默认reg-注册
- * @return bool
- */
- public function check($email, $scene = 'reg')
- {
- return true;
- }
- }
|