From 41f163edf1ce83350dbd1ba1e3047a9614555147 Mon Sep 17 00:00:00 2001 From: TL Date: Thu, 14 Jul 2022 15:05:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=89=BE=E5=9B=9E=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Http/Controllers/ForgottenController.php | 51 +++++++++++ .../Admin/Http/Requests/ForgottenRequest.php | 37 ++++++++ beike/Admin/Routes/admin.php | 3 + beike/Admin/Services/UserService.php | 70 +++++++++++++++ beike/Repositories/UserRepo.php | 90 +++++++++++++++++++ 5 files changed, 251 insertions(+) create mode 100644 beike/Admin/Http/Controllers/ForgottenController.php create mode 100644 beike/Admin/Http/Requests/ForgottenRequest.php create mode 100644 beike/Admin/Services/UserService.php create mode 100644 beike/Repositories/UserRepo.php diff --git a/beike/Admin/Http/Controllers/ForgottenController.php b/beike/Admin/Http/Controllers/ForgottenController.php new file mode 100644 index 00000000..a6ed15eb --- /dev/null +++ b/beike/Admin/Http/Controllers/ForgottenController.php @@ -0,0 +1,51 @@ + + * @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('密码已修改'); + } +} diff --git a/beike/Admin/Http/Requests/ForgottenRequest.php b/beike/Admin/Http/Requests/ForgottenRequest.php new file mode 100644 index 00000000..d9d87449 --- /dev/null +++ b/beike/Admin/Http/Requests/ForgottenRequest.php @@ -0,0 +1,37 @@ + 'required|confirmed', + ]; + } + + public function attributes() + { + return [ + 'password' => '密码' + ]; + } +} diff --git a/beike/Admin/Routes/admin.php b/beike/Admin/Routes/admin.php index 531e8d3f..8282bb4d 100644 --- a/beike/Admin/Routes/admin.php +++ b/beike/Admin/Routes/admin.php @@ -51,6 +51,9 @@ Route::prefix($adminName) 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'); }); }); diff --git a/beike/Admin/Services/UserService.php b/beike/Admin/Services/UserService.php new file mode 100644 index 00000000..598c0a6c --- /dev/null +++ b/beike/Admin/Services/UserService.php @@ -0,0 +1,70 @@ + + * @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(); + } +} diff --git a/beike/Repositories/UserRepo.php b/beike/Repositories/UserRepo.php new file mode 100644 index 00000000..8d1a8ed1 --- /dev/null +++ b/beike/Repositories/UserRepo.php @@ -0,0 +1,90 @@ + + * @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(); + } +} +