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