wyyl/beike/Admin/Services/UserService.php

71 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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();
}
}