Skip to content

Commit

Permalink
perf: 优化用户邮箱验证
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaifan committed Apr 7, 2022
1 parent 601d037 commit 01313b1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 59 deletions.
3 changes: 1 addition & 2 deletions app/Http/Controllers/Api/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Captcha;
use Carbon\Carbon;
use Request;
use Validator;

/**
* @apiDefine users
Expand Down Expand Up @@ -92,7 +91,7 @@ public function login()
Cache::forget("code::" . $email);
if ($isRegVerify && $user->email_verity === 0) {
UserEmailVerification::userEmailSend($user);
return $retError('您还没有验证邮箱,请先登录邮箱通过验证邮件验证邮箱');
return Base::retError('您还没有验证邮箱,请先登录邮箱通过验证邮件验证邮箱', ['code' => 'email']);
}
}
//
Expand Down
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static function reg($email, $password, $other = [])
$user = self::whereUserid(User::email2userid($email))->first();
if ($isRegVerify && $user->email_verity === 0) {
UserEmailVerification::userEmailSend($user);
throw new ApiException('您的账号已注册过,请验证邮箱');
throw new ApiException('您的账号已注册过,请验证邮箱', ['code' => 'email']);
}
throw new ApiException('邮箱地址已存在');
}
Expand Down
68 changes: 30 additions & 38 deletions app/Models/UserEmailVerification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
use App\Exceptions\ApiException;
use App\Module\Base;
use Carbon\Carbon;
use Config;
use Exception;
use Mail;
use Guanguans\Notify\Factory;
use Guanguans\Notify\Messages\EmailMessage;

/**
* App\Models\UserEmailVerification
Expand Down Expand Up @@ -40,49 +39,42 @@ class UserEmailVerification extends AbstractModel
*/
public static function userEmailSend(User $user)
{
$res = self::where('userid', $user->userid)->where('created_at', '>', Carbon::now()->subMinutes(1440))->first();
$res = self::whereUserid($user->userid)->where('created_at', '>', Carbon::now()->subMinutes(1440))->first();
if ($res) return;
//删除
self::where('userid', $user->userid)->delete();
$info['created_at'] = date("Y-m-d H:i:s");
$info['userid'] = $user->userid;
$info['email'] = $user->email;
$info['code'] = md5(uniqid(md5(microtime(true)), true)) . md5($user->userid . md5('lddsgagsgkdiid' . microtime(true)));
$info['status'] = 0;
$userEmailVerification = self::createInstance($info);
self::whereUserid($user->userid)->delete();
$userEmailVerification = self::createInstance([
'userid' => $user->userid,
'email' => $user->email,
'code' => Base::generatePassword(64),
'status' => 0,
]);
$userEmailVerification->save();
$url = Base::fillUrl('single/valid/email') . '?code=' . $info['code'];

$setting = Base::setting('emailSetting');
$url = Base::fillUrl('single/valid/email') . '?code=' . $userEmailVerification->code;
$subject = "绑定邮箱验证";
$content = "您好,您正在绑定 " . env('APP_NAME') . " 的邮箱,请于24小时之内点击该链接完成验证 :<div style='display: flex; justify-content: center;'><a href='{$url}' target='_blank'>{$url}</a></div>";
try {
// 15秒后超时
self::initMailConfig();
Mail::send('email', ['url' => $url], function ($m) use ($user) {
$m->from(Config::get("mail.mailers.smtp.username"), env('APP_NAME'));
$m->to($user->email);
$m->subject("绑定邮箱验证");
});
} catch (Exception $exception) {
// 一般是请求超时
if (str_contains($exception->getMessage(), "Timed Out")) {
if (!Base::isEmail($user->email)) {
throw new \Exception("User email '{$user->email}' address error");
}
Factory::mailer()
->setDsn("smtp://{$setting['account']}:{$setting['password']}@{$setting['smtp_server']}:{$setting['port']}?verify_peer=0")
->setMessage(EmailMessage::create()
->from(env('APP_NAME', 'Task') . " <{$setting['account']}>")
->to($user->email)
->subject($subject)
->html($content))
->send();
} catch (\Exception $e) {
if (str_contains($e->getMessage(), "Timed Out")) {
throw new ApiException("language.TimedOut");
} elseif ($exception->getCode() == 550) {
} elseif ($e->getCode() === 550) {
throw new ApiException('邮件内容被拒绝,请检查邮箱是否开启接收功能');
} else {
throw new ApiException($exception->getMessage());
throw new ApiException($e->getMessage());
}
}
}

/**
* 初始化邮箱配置
* @return void
*/
public static function initMailConfig()
{
$config = Base::setting('emailSetting');
Config::set("mail.mailers.smtp.host", $config['smtp_server'] ?: Config::get("mail.mailers.smtp.host"));
Config::set("mail.mailers.smtp.port", $config['port'] ?: Config::get("mail.mailers.smtp.port"));
Config::set("mail.mailers.smtp.username", $config['account'] ?: Config::get("mail.mailers.smtp.username"));
Config::set("mail.mailers.smtp.password", $config['password'] ?: Config::get("mail.mailers.smtp.password"));
Config::set("mail.mailers.smtp.encryption", 'ssl');
}
}
4 changes: 2 additions & 2 deletions app/Tasks/OverdueRemindEmailTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ private function overdueBeforeAfterEmail(ProjectTask $task, $isBefore)
$hours2 = floatval($setting['task_remind_hours2']);
if ($isBefore) {
$subject = "任务提醒";
$content = "<p>用户您好, " . env('APP_NAME') . " 任务到期提醒。</p><p>您有一个任务【{{$task->name}}】还有{{$hours}}小时即将超时,请及时处理</p>";
$content = "<p>用户您好, " . env('APP_NAME') . " 任务到期提醒。</p><p>您有一个任务【{$task->name}】还有{$hours}小时即将超时,请及时处理</p>";
} else {
$subject = "任务过期提醒";
$content = "<p>用户您好, " . env('APP_NAME') . " 任务到期提醒。</p><p>您的任务【{{$task->name}}】已经超时{{$hours2}}小时,请及时处理</p>";
$content = "<p>用户您好, " . env('APP_NAME') . " 任务到期提醒。</p><p>您的任务【{$task->name}】已经超时{$hours2}小时,请及时处理</p>";
}

/** @var User $user */
Expand Down
2 changes: 1 addition & 1 deletion resources/assets/js/pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default {
loadIng: 0,
codeNeed: false,
codeUrl: $A.apiUrl('users/login/codeimg'),
codeUrl: $A.apiUrl('users/login/codeimg?_=' + Math.random()),
loginType: 'login',
loginJump: false,
Expand Down
15 changes: 0 additions & 15 deletions resources/views/email.blade.php

This file was deleted.

0 comments on commit 01313b1

Please sign in to comment.