* @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 * @return void */ public static function sendVerifyCodeForForgotten($email) { $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,该验证码仅用于找回密码。", 'email'); } /** * 验证验证码是否正确,并修改密码为新密码 * @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(); } }