Commit d4077bde authored by UtopiaXC's avatar UtopiaXC

📨 完成邮件发送,修改Redis工具,更改验证码保存方式

parent 7db4a661
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Utils\CustomCaptcha;
use App\Http\Utils\R;
use App\Http\Utils\RedisAndCache;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Mews\Captcha\Captcha;
class CaptchaController extends Controller{
function getCaptcha(Request $request, Captcha $captchaBuilder)
{
class CaptchaController extends Controller {
function getCaptcha(Request $request, Captcha $captchaBuilder) {
//获取laravel的session token,这里的思想是通过缓存token与验证码值来验证以避免重复提交同一hash问题
$key = $request->cookie(app()->getNamespace() . "session");
$key = $request->cookie(env("APP_NAME", "utopia_open_platform") . "_session");
//创建自定义验证码对象,需要将构建器传入
$captcha = new CustomCaptcha($captchaBuilder);
//设置过期时间。我设置了两分钟
$expiredAt = now()->addMinute(2);
$expiredAt = now()->addMinutes(2);
//将验证码值,session token放入缓存并设置过期时间
Cache::put($key, ['captcha' => $captcha->getCode()], $expiredAt);
RedisAndCache::setWithExpire(\RedisCacheKey::CAPTCHA . $key, $captcha->getCode(), 2);
//构建返回数组,包括有效期截止时间和BASE64格式图片
$result = [
'expired_at' => $expiredAt->toDateTimeString(),
......@@ -28,18 +27,20 @@ class CaptchaController extends Controller{
return R::ok($result);
}
//验证用户提交的验证码,返回值bool(私有方法)
static function check_captcha($captcha,$session):bool{
//验证用户提交的验证码,返回值bool
static function check_captcha($request) {
$captcha = $request->get(\FormKey::CAPTCHA);
$session = \RedisCacheKey::CAPTCHA . $request->cookie(env("APP_NAME", "utopia_open_platform") . "_session");
//通过传入的session获取缓存中的验证码对象,不存在则返回验证失败
$captchaData = Cache::get($session);
Cache::forget($session);
$captchaData = RedisAndCache::get($session);
RedisAndCache::forget($session);
if ($captchaData == null) {
return false;
}
//判断传入的验证码与缓存是否相等
if ($captcha == $captchaData['captcha']){
if ($captcha == $captchaData) {
return true;
}else{
} else {
return false;
}
}
......
......@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Http\Utils\R;
use App\Http\Utils\RedisAndCache;
use App\Mail\RegisterVerifyLinkMail;
use App\Models\Users\User;
use App\Models\Users\UserProfile;
use Exception;
......@@ -11,21 +12,25 @@ use HTTP_CODE;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Mail;
use RedisCacheKey;
use Webpatser\Uuid\Uuid;
class UserController extends Controller {
/**
* @throws \Throwable
*/
function register(Request $request) {
if (!CaptchaController::check_captcha($request->get("captcha"), $request->cookie(app()->getNamespace() . "session"))) {
if (!CaptchaController::check_captcha($request)) {
return R::error(HTTP_CODE::UNAUTHORIZED_CAPTCHA);
}
try {
if (!$request->get("email") || !$request->get("user_name") || !$request->get("password")) {
if (!$request->get(\FormKey::EMAIL) || !$request->get(\FormKey::EMAIL) || !$request->get(\FormKey::PASSWORD)) {
return R::error(HTTP_CODE::NOT_ACCEPT_PARAMS_CONTENT_WRONG);
}
$email = $request->get("email");
$user_name = $request->get("user_name");
$password = password_hash($request->get("password"), PASSWORD_DEFAULT);
$email = $request->get(\FormKey::EMAIL);
$user_name = $request->get(\FormKey::USER_NAME);
$password = password_hash($request->get(\FormKey::PASSWORD), PASSWORD_DEFAULT);
$user = User::query()
->where("user_name", $user_name)
->orWhere("user_name", $email)
......@@ -47,6 +52,10 @@ class UserController extends Controller {
$user->save();
$user_profile->save();
DB::commit();
$code = md5(Uuid::generate());
$link = env("APP_URL") . \WebUrl::REGISTER_VERIFY . "/" . $code;
R::ok(RedisAndCache::setWithExpire(RedisCacheKey::REGISTER_VERIFY . $code, $user->id,15));
Mail::to($email)->send(new RegisterVerifyLinkMail($link, $user_name));
} catch (Exception $e) {
DB::rollBack();
return R::error(HTTP_CODE::INTERNAL_SERVER_ERROR);
......@@ -55,11 +64,11 @@ class UserController extends Controller {
}
function login(Request $request) {
if (!CaptchaController::check_captcha($request->get("captcha"), $request->cookie(app()->getNamespace() . "session"))) {
if (!CaptchaController::check_captcha($request)) {
return R::error(HTTP_CODE::UNAUTHORIZED_CAPTCHA);
}
$username = $request->get("user");
$password = $request->get("password");
$username = $request->get(\FormKey::USER);
$password = $request->get(\FormKey::PASSWORD);
if (!$username || !$password) {
return R::error(HTTP_CODE::NOT_ACCEPT_PARAMS_CONTENT_WRONG);
}
......
......@@ -61,7 +61,6 @@ class RedisAndCache {
return $value;
}
public static function getWithJson($key) {
try {
if (env(EnvKey::REDIS_USE, false) == true) {
......@@ -78,4 +77,21 @@ class RedisAndCache {
}
return $value;
}
public static function forget($key) {
try {
if (env(EnvKey::REDIS_USE, false) == true) {
try {
Redis::del($key);
} catch (ConnectionException $e) {
Cache::forget($key);
}
} else {
Cache::forget($key);
}
} catch (Exception $e) {
return false;
}
return true;
}
}
......@@ -7,7 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class VerityLinkMail extends Mailable {
class RegisterVerifyLinkMail extends Mailable {
use Queueable, SerializesModels;
protected $link;
......@@ -20,8 +20,8 @@ class VerityLinkMail extends Mailable {
*/
public function __construct($link, $user) {
//
$this->$user = $user;
$this->$link = $link;
$this->user = $user;
$this->link = $link;
}
/**
......@@ -33,6 +33,6 @@ class VerityLinkMail extends Mailable {
return $this->view('email.register_verify_link')->with([
"link" => $this->link,
"user" => $this->user,
]);
])->from(['address' => env("MAIL_FROM_ADDRESS"), 'name' => env("APP_NAME")]);
}
}
......@@ -8,6 +8,7 @@ class WebUrl
const FIND_PASSWORD = '/find_password';
const PRIVACY_POLICY = "/privacy_policy";
const REGISTER_VERIFY="/register_verify";
}
class ApiUrl
......@@ -44,6 +45,8 @@ class RedisCacheKey
{
const SITE_PROFILE = "site_profile";
const USER_TOKEN = "user_token:";
const REGISTER_VERIFY="register_verify:";
const CAPTCHA="captcha:";
}
class CookieKey
......@@ -58,6 +61,14 @@ class HeaderKey
const SITE_PROFILE="site_profile";
}
class FormKey{
const CAPTCHA="captcha";
const USER_NAME="user_name";
const EMAIL="email";
const PASSWORD="password";
const USER="user";
}
class SiteProfileTypeEnum
{
const WEB_TITLE = "01";
......@@ -75,3 +86,4 @@ class DefaultSiteProfile
class EnvKey{
const REDIS_USE="REDIS_USE";
}
......@@ -3,5 +3,5 @@
@section('title') - 注册验证 @endsection
@section('body')
注册验证页
{{$code}}
@endsection
......@@ -23,7 +23,7 @@ Route::group(['prefix' => '/', 'middleware' => [Middleware::SITE_PROFILE_MIDDLEW
Route::get("/register", function () {
return view('register');
});
Route::get("/register_verify", function () {
return view('email.register_verify');
Route::get("/register_verify/{code}", function ($code) {
return view('email.register_verify')->with("code",$code);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment