Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵增煜
/
tzt-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
3e8336c8
authored
Nov 18, 2024
by
lujunyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
药店后台登录
parent
793776c1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
77 additions
and
11 deletions
+77
-11
.env.example
+6
-2
app/Admin/Controllers/AuthController.php
+61
-3
app/Admin/routes.php
+1
-0
config/admin.php
+1
-0
resources/views/admin/loginstore.blade.php
+8
-6
No files found.
.env.example
View file @
3e8336c8
...
...
@@ -47,4 +47,8 @@ BAIDU_MAP_API_KEY=
# 小程序
WECHAT_MINI_PROGRAM_APPID=wx848e75ebc215b462
WECHAT_MINI_PROGRAM_SECRET=
\ No newline at end of file
WECHAT_MINI_PROGRAM_SECRET=
# 短信
SMS_USERNAME=tzthy
SMS_PASSWORD=
\ No newline at end of file
app/Admin/Controllers/AuthController.php
View file @
3e8336c8
...
...
@@ -3,6 +3,7 @@
namespace
App\Admin\Controllers
;
use
App\Models\AdminUsers
;
use
App\Services\SmsService
;
use
Dcat\Admin\Http\Controllers\AuthController
as
BaseAuthController
;
use
Dcat\Admin\Layout\Content
;
use
Illuminate\Http\Request
;
...
...
@@ -45,7 +46,8 @@ public function postLogin(Request $request)
/** @var \Illuminate\Validation\Validator $validator */
$validator
=
Validator
::
make
(
$credentials
,
[
$this
->
username
()
=>
'required'
,
'password'
=>
'required'
,
'password'
=>
'required_with:role'
,
'verification_code'
=>
'required_if:role,store'
,
]);
if
(
$validator
->
fails
())
{
...
...
@@ -60,11 +62,19 @@ public function postLogin(Request $request)
}
// 检查用户角色是否为 pharmacy
if
(
$role
===
'store'
&&
$user
&&
$user
->
role
===
'pharmacy'
)
{
// if ($role === 'store' && $user && $user->role === 'pharmacy') {
if
(
$role
===
'store'
)
{
// 验证短信验证码
$verificationCode
=
$request
->
input
(
'verification_code'
);
// 假设您有一个方法来验证短信验证码
if
(
!
$this
->
verifySmsCode
(
$user
->
username
,
$verificationCode
))
{
if
(
$this
->
verifySmsCode
(
$user
->
username
,
$verificationCode
))
{
$this
->
guard
()
->
login
(
$user
,
$remember
);
// 登录成功后,删除缓存中的短信验证码
cache
()
->
forget
(
"sms_verification_code_
{
$user
->
username
}
"
);
return
$this
->
sendLoginResponse
(
$request
);
}
else
{
return
$this
->
validationErrorsResponse
([
'verification_code'
=>
'验证码错误或已过期!'
,
]);
...
...
@@ -82,4 +92,52 @@ public function postLogin(Request $request)
$this
->
username
()
=>
$this
->
getFailedLoginMessage
(),
]);
}
// 药店管理员角色登录使用
public
function
getLoginSmsCode
(
Request
$request
)
{
// 验证手机号是否存在
$phone
=
$request
->
input
(
'username'
);
if
(
!
$phone
)
{
return
response
()
->
json
([
'error'
=>
'手机号不能为空'
]);
}
// 验证手机号格式
if
(
!
preg_match
(
'/^1[3-9]\d{9}$/'
,
$phone
))
{
return
response
()
->
json
([
'error'
=>
'手机号格式不正确'
]);
}
// 检查手机号在管理员表中是否存在
$user
=
AdminUsers
::
where
(
'username'
,
$phone
)
->
first
();
if
(
!
$user
)
{
return
response
()
->
json
([
'error'
=>
'该药店手机号未注册,请联系管理员~'
]);
}
// 生成短信验证码
$verificationCode
=
rand
(
100000
,
999999
);
// 生成6位随机验证码
// 存储验证码和有效期(10分钟)
cache
()
->
put
(
"sms_verification_code_
{
$phone
}
"
,
$verificationCode
,
600
);
// 600秒 = 10分钟
$templateName
=
'verification_code'
;
$templateData
=
[
'code'
=>
$verificationCode
];
$smsService
=
new
SmsService
();
$response
=
$smsService
->
sendSms
(
$phone
,
$templateName
,
$templateData
);
return
response
()
->
json
([
'success'
=>
true
,
'message'
=>
'验证码已发送'
]);
}
/**
* 验证短信验证码
*
* @param string $username
* @param string $code
* @return bool
*/
protected
function
verifySmsCode
(
$username
,
$code
)
{
$cachedCode
=
cache
()
->
get
(
"sms_verification_code_
{
$username
}
"
);
return
$cachedCode
&&
$cachedCode
===
$code
;
}
}
app/Admin/routes.php
View file @
3e8336c8
...
...
@@ -67,5 +67,6 @@
// 搜索打印处方单
$router
->
get
(
'prescription-search'
,
'PrescriptionPrintController@search'
);
});
$router
->
get
(
'/auth/smscode'
,
'AuthController@getLoginSmsCode'
);
/** 药店菜单-end **/
});
config/admin.php
View file @
3e8336c8
...
...
@@ -157,6 +157,7 @@
'except'
=>
[
'auth/login'
,
'auth/logout'
,
'auth/smscode'
,
],
'enable_session_middleware'
=>
false
,
...
...
resources/views/admin/loginstore.blade.php
View file @
3e8336c8
...
...
@@ -74,16 +74,19 @@
<form
id=
"login-form"
method=
"POST"
action=
"{{ admin_url('auth/login') }}"
>
<input
type=
"hidden"
name=
"_token"
value=
"{{ csrf_token() }}"
/>
<input
type=
"hidden"
name=
"role"
value=
"store"
/>
<fieldset
class=
"form-label-group form-group position-relative has-icon-left"
>
<input
type=
"te
xt
"
type=
"te
l
"
class=
"form-control {{ $errors->has('username') ? 'is-invalid' : '' }}"
name=
"username"
placeholder=
"手机号"
value=
"{{ old('username') }}"
required
autofocus
pattern=
"^[0-9]*$"
title=
"请输入有效的手机号"
>
<div
class=
"form-control-position"
>
...
...
@@ -121,7 +124,7 @@
<div
class=
"form-control-position"
style=
"display: none;"
>
<i
class=
"feather icon-lock"
></i>
</div>
<label
for=
"verification_code"
>
{{ trans('admin.verification_code') }}
</label>
<label
for=
"verification_code"
>
验证码
</label>
<div
class=
"help-block with-errors"
></div>
@if($errors->has('verification_code'))
<span
class=
"invalid-feedback text-danger"
role=
"alert"
>
...
...
@@ -184,11 +187,10 @@ function getVerificationCode() {
// 发送 AJAX 请求获取验证码
$
.
ajax
({
url
:
"{{ admin_url('auth/
get-verification-
code') }}"
,
// 替换为您的接口地址
method
:
'
POS
T'
,
url
:
"{{ admin_url('auth/
sms
code') }}"
,
// 替换为您的接口地址
method
:
'
GE
T'
,
data
:
{
username
:
username
,
role
:
store
,
_token
:
'{{ csrf_token() }}'
},
success
:
function
(
response
)
{
...
...
@@ -196,7 +198,7 @@ function getVerificationCode() {
alert
(
'验证码已发送'
);
startCountdown
();
}
else
{
alert
(
response
.
message
||
'获取验证码失败'
);
alert
(
response
.
error
||
'获取验证码失败'
);
}
},
error
:
function
()
{
...
...
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