后台找回密码功能
This commit is contained in:
parent
3500482767
commit
41f163edf1
|
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ForgottenController.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author TL <mengwb@opencart.cn>
|
||||||
|
* @created 2022-07-14 11:39:08
|
||||||
|
* @modified 2022-07-14 11:39:08
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Beike\Admin\Http\Controllers;
|
||||||
|
|
||||||
|
use Beike\Admin\Http\Requests\ForgottenRequest;
|
||||||
|
use Beike\Admin\Services\UserService;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class ForgottenController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 找回密码页面
|
||||||
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
return view('account/forgotten');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接收email地址,生成验证码发送到邮件地址
|
||||||
|
* @param Request $request
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function sendVerifyCode(Request $request)
|
||||||
|
{
|
||||||
|
UserService::sendVerifyCodeForForgotten($request->get('email'));
|
||||||
|
return json_success('验证码已发送,请查看并输入验证码');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接收验证码和新密码、确认密码,验证验证码是否正确、密码和确认密码是否相等,然后修改密码
|
||||||
|
* @param Request $request
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function changePassword(ForgottenRequest $request)
|
||||||
|
{
|
||||||
|
UserService::verifyAndChangePassword($request->get('code'), $request->get('email'), $request->get('password'));
|
||||||
|
|
||||||
|
return json_success('密码已修改');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Beike\Admin\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class ForgottenRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function authorize()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'password' => 'required|confirmed',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function attributes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'password' => '密码'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -51,6 +51,9 @@ Route::prefix($adminName)
|
||||||
|
|
||||||
Route::get('logout', [Controllers\LogoutController::class, 'index'])->name('logout.index');
|
Route::get('logout', [Controllers\LogoutController::class, 'index'])->name('logout.index');
|
||||||
|
|
||||||
|
Route::get('forgotten', [\Beike\Admin\Http\Controllers\ForgottenController::class, 'index'])->name('forgotten.index');
|
||||||
|
Route::post('forgotten/send_code', [\Beike\Admin\Http\Controllers\ForgottenController::class, 'sendVerifyCode'])->name('forgotten.send_code');
|
||||||
|
Route::post('forgotten/password', [\Beike\Admin\Http\Controllers\ForgottenController::class, 'changePassword'])->name('forgotten.password');
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* UserService.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author TL <mengwb@opencart.cn>
|
||||||
|
* @created 2022-07-14 12:12:57
|
||||||
|
* @modified 2022-07-14 12:12:57
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Beike\Admin\Services;
|
||||||
|
|
||||||
|
|
||||||
|
use Beike\Libraries\Notification;
|
||||||
|
use Beike\Repositories\UserRepo;
|
||||||
|
use Beike\Repositories\VerifyCodeRepo;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
class UserService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 发送验证码通过$type方式,type为email或telephone
|
||||||
|
* @param $email
|
||||||
|
* @param $type
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function sendVerifyCodeForForgotten($email, $type) {
|
||||||
|
$code = str_pad(mt_rand(10, 999999), 6, '0', STR_PAD_LEFT);
|
||||||
|
|
||||||
|
VerifyCodeRepo::deleteByAccount($email);
|
||||||
|
VerifyCodeRepo::create([
|
||||||
|
'account' => $email,
|
||||||
|
'code' => $code,
|
||||||
|
]);
|
||||||
|
|
||||||
|
Log::info("找回密码验证码:{$code}");
|
||||||
|
|
||||||
|
Notification::verifyCode($code, "您的验证码是%s,该验证码仅用于找回密码。", $type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证验证码是否正确,并修改密码为新密码
|
||||||
|
* @param $code
|
||||||
|
* @param $account
|
||||||
|
* @param $password
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function verifyAndChangePassword($code, $account, $password)
|
||||||
|
{
|
||||||
|
$verifyCode = VerifyCodeRepo::findByAccount($account);
|
||||||
|
if ($verifyCode->created_at->addMinutes(10) < Carbon::now()) {
|
||||||
|
$verifyCode->delete();
|
||||||
|
throw new \Exception("您的验证码已过期(10分钟),请重新获取");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($verifyCode->code != $code) {
|
||||||
|
throw new \Exception("您的验证码错误");
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = UserRepo::findByEmail($account);
|
||||||
|
if (!$user) {
|
||||||
|
throw new \Exception("账号不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
UserRepo::update($user, ['password' => $password]);
|
||||||
|
$verifyCode->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* UserRepo.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author TL <mengwb@opencart.cn>
|
||||||
|
* @created 2022-07-14 11:45:41
|
||||||
|
* @modified 2022-07-14 11:45:41
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Beike\Repositories;
|
||||||
|
|
||||||
|
use Beike\Models\AdminUser;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
|
||||||
|
class UserRepo
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 创建一个记录
|
||||||
|
* @param $data
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public static function create($data)
|
||||||
|
{
|
||||||
|
$data['password'] = Hash::make($data['password']);
|
||||||
|
return AdminUser::query()->create($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $user
|
||||||
|
* @param $data
|
||||||
|
* @return bool|int
|
||||||
|
*/
|
||||||
|
public static function update($user, $data)
|
||||||
|
{
|
||||||
|
if (!$user instanceof AdminUser) {
|
||||||
|
$user = AdminUser::query()->findOrFail($user);
|
||||||
|
}
|
||||||
|
if (isset($data['password'])) {
|
||||||
|
$data['password'] = Hash::make($data['password']);
|
||||||
|
}
|
||||||
|
return $user->update($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function findByEmail($email)
|
||||||
|
{
|
||||||
|
return AdminUser::query()->where('email', $email)->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
|
||||||
|
*/
|
||||||
|
public static function find($id)
|
||||||
|
{
|
||||||
|
return AdminUser::query()->find($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function delete($id)
|
||||||
|
{
|
||||||
|
AdminUser::query()->find($id)->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||||
|
*/
|
||||||
|
public static function list($data)
|
||||||
|
{
|
||||||
|
$builder = AdminUser::query();
|
||||||
|
|
||||||
|
if (isset($data['name'])) {
|
||||||
|
$builder->where('admin_users.name', 'like', "%{$data['name']}%");
|
||||||
|
}
|
||||||
|
if (isset($data['email'])) {
|
||||||
|
$builder->where('admin_users.email', 'like', "%{$data['email']}%");
|
||||||
|
}
|
||||||
|
if (isset($data['active'])) {
|
||||||
|
$builder->where('admin_users.active', $data['active']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $builder->paginate(20)->withQueryString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue