Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
O
Open-Platform
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
杜宇森
Open-Platform
Commits
d4077bde
Commit
d4077bde
authored
Sep 02, 2021
by
UtopiaXC
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
📨
完成邮件发送,修改Redis工具,更改验证码保存方式
parent
7db4a661
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
115 additions
and
283 deletions
+115
-283
CaptchaController.php
app/Http/Controllers/CaptchaController.php
+15
-14
UserController.php
app/Http/Controllers/UserController.php
+17
-8
RedisAndCache.php
app/Http/Utils/RedisAndCache.php
+17
-1
RegisterVerifyLinkMail.php
app/Mail/RegisterVerifyLinkMail.php
+4
-4
constants.php
config/System/constants.php
+12
-0
register_verify.blade.php
resources/views/email/register_verify.blade.php
+1
-1
register_verify_link.blade.php
resources/views/email/register_verify_link.blade.php
+47
-253
web.php
routes/web.php
+2
-2
No files found.
app/Http/Controllers/CaptchaController.php
View file @
d4077bde
<?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
()
->
addMinute
s
(
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
=
RedisAnd
Cache
::
get
(
$session
);
RedisAnd
Cache
::
forget
(
$session
);
if
(
$captchaData
==
null
)
{
return
false
;
}
//判断传入的验证码与缓存是否相等
if
(
$captcha
==
$captchaData
[
'captcha'
])
{
if
(
$captcha
==
$captchaData
)
{
return
true
;
}
else
{
}
else
{
return
false
;
}
}
...
...
app/Http/Controllers/UserController.php
View file @
d4077bde
...
...
@@ -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
);
}
...
...
app/Http/Utils/RedisAndCache.php
View file @
d4077bde
...
...
@@ -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
;
}
}
app/Mail/
Verit
yLinkMail.php
→
app/Mail/
RegisterVerif
yLinkMail.php
View file @
d4077bde
...
...
@@ -7,7 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use
Illuminate\Mail\Mailable
;
use
Illuminate\Queue\SerializesModels
;
class
Verit
yLinkMail
extends
Mailable
{
class
RegisterVerif
yLinkMail
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"
)])
;
}
}
config/System/constants.php
View file @
d4077bde
...
...
@@ -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"
;
}
resources/views/email/register_verify.blade.php
View file @
d4077bde
...
...
@@ -3,5 +3,5 @@
@
section
(
'title'
)
-
注册验证
@
endsection
@
section
(
'body'
)
注册验证页
{{
$code
}}
@
endsection
resources/views/email/register_verify_link.blade.php
View file @
d4077bde
This diff is collapsed.
Click to expand it.
routes/web.php
View file @
d4077bde
...
...
@@ -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
)
;
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment