EmailService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use app\Lib\smtpMail\smtp as smtpService;
  13. use Illuminate\Mail\Mailable;
  14. use Illuminate\Support\Facades\Mail;
  15. use PHPMailer\PHPMailer\PHPMailer;
  16. /**
  17. * 邮件管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class ConfigService
  21. * @package App\Services
  22. */
  23. class EmailService extends BaseService
  24. {
  25. /**
  26. * 配置参数
  27. * @var array
  28. */
  29. protected $config = [];
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 初始化配置
  34. * @return bool
  35. */
  36. public function __construct()
  37. {
  38. $config = ConfigService::make()->getConfigByGroup(3);
  39. $mail = config('mail');
  40. $mailKey = md5(json_encode($mail));
  41. $mail['default'] = isset($config['mail_mailer']['value']) && $config['mail_mailer']['value'] ? $config['mail_mailer']['value'] : $mail['default'];
  42. $mail['markdown']['paths'] = [resource_path('views/vendor/mail')];
  43. $smtp = isset($mail['mailers']['smtp']) ? $mail['mailers']['smtp'] : [];
  44. $smtp['host'] = isset($config['mail_host']['value']) && $config['mail_host']['value'] ? $config['mail_host']['value'] : $smtp['host'];
  45. $smtp['port'] = isset($config['mail_port']['value']) && $config['mail_port']['value'] ? $config['mail_port']['value'] : $smtp['port'];
  46. $smtp['username'] = isset($config['mail_username']['value']) && $config['mail_username']['value'] ? $config['mail_username']['value'] : $smtp['host'];
  47. $smtp['password'] = isset($config['mail_password']['value']) && $config['mail_password']['value'] ? $config['mail_password']['value'] : $smtp['password'];
  48. $smtp['timeout'] = isset($config['mail_timeout']['value']) && $config['mail_timeout']['value'] ? $config['mail_timeout']['value'] : $smtp['timeout'];
  49. $smtp['encryption'] = isset($config['mail_source']['value']) && $config['mail_source']['value'] ? $config['mail_source']['value'] : $smtp['encryption'];
  50. $smtp['timeout'] = 10;
  51. if ($mail['default'] != 'smtp') {
  52. return false;
  53. }
  54. $mail['mailers']['smtp'] = $smtp;
  55. $mail['from']['address'] = $smtp['username'];
  56. $mail['from']['name'] = 'UTC交易平台';
  57. $this->config = array_merge($config, $smtp);
  58. if ($mailKey != md5(json_encode($mail))) {
  59. file_put_contents(base_path() . '/config/mail.php', "<?php \n /* 邮箱服务配置 */ \n return " . var_export($mail, true) . ';' . "\n ?>");
  60. }
  61. return true;
  62. }
  63. /**
  64. * 静态入口
  65. * @return static|null
  66. */
  67. public static function make()
  68. {
  69. if(!self::$instance){
  70. self::$instance = (new static());
  71. }
  72. return self::$instance;
  73. }
  74. /**
  75. * 发送邮件验证码
  76. * @param $email
  77. * @param string $scene 场景:默认reg-注册
  78. * @return bool
  79. */
  80. public function sendCode($email, $scene = 'reg')
  81. {
  82. try {
  83. $cacheKey = "stores:codes:email_{$scene}:" . md5($email);
  84. // 发送频繁
  85. if (RedisService::get($cacheKey . '_lock')) {
  86. $this->error = '1014';
  87. return false;
  88. }
  89. $title = isset($this->config['mail_title_' . $scene]['value']) && $this->config['mail_title_' . $scene]['value'] ? $this->config['mail_title_' . $scene]['value'] : "OTC168网站注册邮箱验证码";
  90. $template = isset($this->config['mail_template_' . $scene]['value']) && $this->config['mail_template_' . $scene]['value'] ? $this->config['mail_template_' . $scene]['value'] : "您的验证码是:{code},5分钟有效期!!!";
  91. // 生成验证码
  92. $code = rand(100000, 999999);
  93. $template = str_replace('{code}', $code, $template);
  94. // 上锁
  95. RedisService::set($cacheKey.'_lock', ['email'=> $email, 'code'=> $code, 'date'=> date('Y-m-d H:i:s')], rand(5, 10));
  96. Mail::send('emails.index',['title'=> $title,'template'=> $template], function($mail) use($email, $title){
  97. $mail->from($this->config['username'],'OTC168'); // 发送者
  98. $mail->subject($title); // 标题
  99. $mail->to($email); // 接受者
  100. });
  101. $error = Mail::failures();
  102. if (!$error) {
  103. RedisService::set($cacheKey, ['email'=> $email, 'code'=> $code, 'date'=> date('Y-m-d H:i:s')], 300);
  104. return true;
  105. }
  106. return false;
  107. } catch (\Exception $exception){
  108. $this->error = $exception->getMessage();
  109. return false;
  110. }
  111. }
  112. /**
  113. * 验证邮件验证码
  114. * @param $email
  115. * @param $code
  116. * @param string $scene 场景:默认reg-注册
  117. * @return bool
  118. */
  119. public function check($email, $code, $scene = 'reg')
  120. {
  121. $cacheKey = "stores:codes:email_{$scene}:" . md5($email);
  122. $codeData = RedisService::get($cacheKey);
  123. $checkCode = isset($codeData['code'])? $codeData['code'] : 0;
  124. $checkEmail = isset($codeData['email'])? $codeData['email'] : 0;
  125. if(empty($codeData) || empty($checkCode)){
  126. $this->error = 2027;
  127. return false;
  128. }
  129. if($checkEmail != $email || $checkCode != $code){
  130. $this->error = 2006;
  131. return false;
  132. }
  133. return true;
  134. }
  135. }