Commit 3e8336c8 by lujunyi

药店后台登录

parent 793776c1
......@@ -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
......@@ -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;
}
}
......@@ -67,5 +67,6 @@
// 搜索打印处方单
$router->get('prescription-search', 'PrescriptionPrintController@search');
});
$router->get('/auth/smscode', 'AuthController@getLoginSmsCode');
/** 药店菜单-end **/
});
......@@ -157,6 +157,7 @@
'except' => [
'auth/login',
'auth/logout',
'auth/smscode',
],
'enable_session_middleware' => false,
......
......@@ -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="text"
type="tel"
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: 'POST',
url: "{{ admin_url('auth/smscode') }}", // 替换为您的接口地址
method: 'GET',
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() {
......
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