Commit 3e8336c8 by lujunyi

药店后台登录

parent 793776c1
...@@ -48,3 +48,7 @@ BAIDU_MAP_API_KEY= ...@@ -48,3 +48,7 @@ BAIDU_MAP_API_KEY=
# 小程序 # 小程序
WECHAT_MINI_PROGRAM_APPID=wx848e75ebc215b462 WECHAT_MINI_PROGRAM_APPID=wx848e75ebc215b462
WECHAT_MINI_PROGRAM_SECRET= WECHAT_MINI_PROGRAM_SECRET=
# 短信
SMS_USERNAME=tzthy
SMS_PASSWORD=
\ No newline at end of file
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Admin\Controllers; namespace App\Admin\Controllers;
use App\Models\AdminUsers; use App\Models\AdminUsers;
use App\Services\SmsService;
use Dcat\Admin\Http\Controllers\AuthController as BaseAuthController; use Dcat\Admin\Http\Controllers\AuthController as BaseAuthController;
use Dcat\Admin\Layout\Content; use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request; use Illuminate\Http\Request;
...@@ -45,7 +46,8 @@ public function postLogin(Request $request) ...@@ -45,7 +46,8 @@ public function postLogin(Request $request)
/** @var \Illuminate\Validation\Validator $validator */ /** @var \Illuminate\Validation\Validator $validator */
$validator = Validator::make($credentials, [ $validator = Validator::make($credentials, [
$this->username() => 'required', $this->username() => 'required',
'password' => 'required', 'password' => 'required_with:role',
'verification_code' => 'required_if:role,store',
]); ]);
if ($validator->fails()) { if ($validator->fails()) {
...@@ -60,11 +62,19 @@ public function postLogin(Request $request) ...@@ -60,11 +62,19 @@ public function postLogin(Request $request)
} }
// 检查用户角色是否为 pharmacy // 检查用户角色是否为 pharmacy
if ($role === 'store' && $user && $user->role === 'pharmacy') { // if ($role === 'store' && $user && $user->role === 'pharmacy') {
if ($role === 'store') {
// 验证短信验证码 // 验证短信验证码
$verificationCode = $request->input('verification_code'); $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([ return $this->validationErrorsResponse([
'verification_code' => '验证码错误或已过期!', 'verification_code' => '验证码错误或已过期!',
]); ]);
...@@ -82,4 +92,52 @@ public function postLogin(Request $request) ...@@ -82,4 +92,52 @@ public function postLogin(Request $request)
$this->username() => $this->getFailedLoginMessage(), $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;
}
} }
...@@ -67,5 +67,6 @@ ...@@ -67,5 +67,6 @@
// 搜索打印处方单 // 搜索打印处方单
$router->get('prescription-search', 'PrescriptionPrintController@search'); $router->get('prescription-search', 'PrescriptionPrintController@search');
}); });
$router->get('/auth/smscode', 'AuthController@getLoginSmsCode');
/** 药店菜单-end **/ /** 药店菜单-end **/
}); });
...@@ -157,6 +157,7 @@ ...@@ -157,6 +157,7 @@
'except' => [ 'except' => [
'auth/login', 'auth/login',
'auth/logout', 'auth/logout',
'auth/smscode',
], ],
'enable_session_middleware' => false, 'enable_session_middleware' => false,
......
...@@ -74,16 +74,19 @@ ...@@ -74,16 +74,19 @@
<form id="login-form" method="POST" action="{{ admin_url('auth/login') }}"> <form id="login-form" method="POST" action="{{ admin_url('auth/login') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/> <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"> <fieldset class="form-label-group form-group position-relative has-icon-left">
<input <input
type="text" type="tel"
class="form-control {{ $errors->has('username') ? 'is-invalid' : '' }}" class="form-control {{ $errors->has('username') ? 'is-invalid' : '' }}"
name="username" name="username"
placeholder="手机号" placeholder="手机号"
value="{{ old('username') }}" value="{{ old('username') }}"
required required
autofocus autofocus
pattern="^[0-9]*$"
title="请输入有效的手机号"
> >
<div class="form-control-position"> <div class="form-control-position">
...@@ -121,7 +124,7 @@ ...@@ -121,7 +124,7 @@
<div class="form-control-position" style="display: none;"> <div class="form-control-position" style="display: none;">
<i class="feather icon-lock"></i> <i class="feather icon-lock"></i>
</div> </div>
<label for="verification_code">{{ trans('admin.verification_code') }}</label> <label for="verification_code">验证码</label>
<div class="help-block with-errors"></div> <div class="help-block with-errors"></div>
@if($errors->has('verification_code')) @if($errors->has('verification_code'))
<span class="invalid-feedback text-danger" role="alert"> <span class="invalid-feedback text-danger" role="alert">
...@@ -184,11 +187,10 @@ function getVerificationCode() { ...@@ -184,11 +187,10 @@ function getVerificationCode() {
// 发送 AJAX 请求获取验证码 // 发送 AJAX 请求获取验证码
$.ajax({ $.ajax({
url: "{{ admin_url('auth/get-verification-code') }}", // 替换为您的接口地址 url: "{{ admin_url('auth/smscode') }}", // 替换为您的接口地址
method: 'POST', method: 'GET',
data: { data: {
username: username, username: username,
role: store,
_token: '{{ csrf_token() }}' _token: '{{ csrf_token() }}'
}, },
success: function(response) { success: function(response) {
...@@ -196,7 +198,7 @@ function getVerificationCode() { ...@@ -196,7 +198,7 @@ function getVerificationCode() {
alert('验证码已发送'); alert('验证码已发送');
startCountdown(); startCountdown();
} else { } else {
alert(response.message || '获取验证码失败'); alert(response.error || '获取验证码失败');
} }
}, },
error: function() { error: function() {
......
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 sign in to comment