mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-14 12:42:51 +00:00
perf: 优化用户邮箱验证
This commit is contained in:
parent
601d037201
commit
01313b16e1
@ -10,7 +10,6 @@ use Cache;
|
|||||||
use Captcha;
|
use Captcha;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Request;
|
use Request;
|
||||||
use Validator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @apiDefine users
|
* @apiDefine users
|
||||||
@ -92,7 +91,7 @@ class UsersController extends AbstractController
|
|||||||
Cache::forget("code::" . $email);
|
Cache::forget("code::" . $email);
|
||||||
if ($isRegVerify && $user->email_verity === 0) {
|
if ($isRegVerify && $user->email_verity === 0) {
|
||||||
UserEmailVerification::userEmailSend($user);
|
UserEmailVerification::userEmailSend($user);
|
||||||
return $retError('您还没有验证邮箱,请先登录邮箱通过验证邮件验证邮箱');
|
return Base::retError('您还没有验证邮箱,请先登录邮箱通过验证邮件验证邮箱', ['code' => 'email']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
|
|||||||
@ -191,7 +191,7 @@ class User extends AbstractModel
|
|||||||
$user = self::whereUserid(User::email2userid($email))->first();
|
$user = self::whereUserid(User::email2userid($email))->first();
|
||||||
if ($isRegVerify && $user->email_verity === 0) {
|
if ($isRegVerify && $user->email_verity === 0) {
|
||||||
UserEmailVerification::userEmailSend($user);
|
UserEmailVerification::userEmailSend($user);
|
||||||
throw new ApiException('您的账号已注册过,请验证邮箱');
|
throw new ApiException('您的账号已注册过,请验证邮箱', ['code' => 'email']);
|
||||||
}
|
}
|
||||||
throw new ApiException('邮箱地址已存在');
|
throw new ApiException('邮箱地址已存在');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,9 +5,8 @@ namespace App\Models;
|
|||||||
use App\Exceptions\ApiException;
|
use App\Exceptions\ApiException;
|
||||||
use App\Module\Base;
|
use App\Module\Base;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Config;
|
use Guanguans\Notify\Factory;
|
||||||
use Exception;
|
use Guanguans\Notify\Messages\EmailMessage;
|
||||||
use Mail;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* App\Models\UserEmailVerification
|
* App\Models\UserEmailVerification
|
||||||
@ -40,49 +39,42 @@ class UserEmailVerification extends AbstractModel
|
|||||||
*/
|
*/
|
||||||
public static function userEmailSend(User $user)
|
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;
|
if ($res) return;
|
||||||
//删除
|
//删除
|
||||||
self::where('userid', $user->userid)->delete();
|
self::whereUserid($user->userid)->delete();
|
||||||
$info['created_at'] = date("Y-m-d H:i:s");
|
$userEmailVerification = self::createInstance([
|
||||||
$info['userid'] = $user->userid;
|
'userid' => $user->userid,
|
||||||
$info['email'] = $user->email;
|
'email' => $user->email,
|
||||||
$info['code'] = md5(uniqid(md5(microtime(true)), true)) . md5($user->userid . md5('lddsgagsgkdiid' . microtime(true)));
|
'code' => Base::generatePassword(64),
|
||||||
$info['status'] = 0;
|
'status' => 0,
|
||||||
$userEmailVerification = self::createInstance($info);
|
]);
|
||||||
$userEmailVerification->save();
|
$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 {
|
try {
|
||||||
// 15秒后超时
|
if (!Base::isEmail($user->email)) {
|
||||||
self::initMailConfig();
|
throw new \Exception("User email '{$user->email}' address error");
|
||||||
Mail::send('email', ['url' => $url], function ($m) use ($user) {
|
}
|
||||||
$m->from(Config::get("mail.mailers.smtp.username"), env('APP_NAME'));
|
Factory::mailer()
|
||||||
$m->to($user->email);
|
->setDsn("smtp://{$setting['account']}:{$setting['password']}@{$setting['smtp_server']}:{$setting['port']}?verify_peer=0")
|
||||||
$m->subject("绑定邮箱验证");
|
->setMessage(EmailMessage::create()
|
||||||
});
|
->from(env('APP_NAME', 'Task') . " <{$setting['account']}>")
|
||||||
} catch (Exception $exception) {
|
->to($user->email)
|
||||||
// 一般是请求超时
|
->subject($subject)
|
||||||
if (str_contains($exception->getMessage(), "Timed Out")) {
|
->html($content))
|
||||||
|
->send();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
if (str_contains($e->getMessage(), "Timed Out")) {
|
||||||
throw new ApiException("language.TimedOut");
|
throw new ApiException("language.TimedOut");
|
||||||
} elseif ($exception->getCode() == 550) {
|
} elseif ($e->getCode() === 550) {
|
||||||
throw new ApiException('邮件内容被拒绝,请检查邮箱是否开启接收功能');
|
throw new ApiException('邮件内容被拒绝,请检查邮箱是否开启接收功能');
|
||||||
} else {
|
} 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');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -76,10 +76,10 @@ class OverdueRemindEmailTask extends AbstractTask
|
|||||||
$hours2 = floatval($setting['task_remind_hours2']);
|
$hours2 = floatval($setting['task_remind_hours2']);
|
||||||
if ($isBefore) {
|
if ($isBefore) {
|
||||||
$subject = "任务提醒";
|
$subject = "任务提醒";
|
||||||
$content = "<p>用户您好, " . env('APP_NAME') . " 任务到期提醒。</p><p>您有一个任务【{{$task->name}}】还有{{$hours}}小时即将超时,请及时处理</p>";
|
$content = "<p>用户您好, " . env('APP_NAME') . " 任务到期提醒。</p><p>您有一个任务【{$task->name}】还有{$hours}小时即将超时,请及时处理</p>";
|
||||||
} else {
|
} else {
|
||||||
$subject = "任务过期提醒";
|
$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 */
|
/** @var User $user */
|
||||||
|
|||||||
@ -85,7 +85,7 @@ export default {
|
|||||||
loadIng: 0,
|
loadIng: 0,
|
||||||
|
|
||||||
codeNeed: false,
|
codeNeed: false,
|
||||||
codeUrl: $A.apiUrl('users/login/codeimg'),
|
codeUrl: $A.apiUrl('users/login/codeimg?_=' + Math.random()),
|
||||||
|
|
||||||
loginType: 'login',
|
loginType: 'login',
|
||||||
loginJump: false,
|
loginJump: false,
|
||||||
|
|||||||
@ -1,15 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="{{ app()->getLocale() }}">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
您好,您正在绑定 {{env('APP_NAME') }} 的邮箱,请于24小时之内点击该链接完成验证 :
|
|
||||||
<div style="display: flex; justify-content: center;">
|
|
||||||
<a href="{{$url}}">{{$url}}</a>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
<body>
|
|
||||||
Loading…
x
Reference in New Issue
Block a user