101 lines
3.2 KiB
PHP
101 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace app\api\logic;
|
|
use app\common\{enum\notice\NoticeEnum,
|
|
logic\BaseLogic,
|
|
model\user\User,
|
|
service\sms\SmsDriver,};
|
|
use think\facade\Config;
|
|
|
|
/**
|
|
* 会员逻辑层
|
|
* Class UserLogic
|
|
* @package app\shopapi\logic
|
|
*/
|
|
class UserLogic extends BaseLogic
|
|
{
|
|
//重置登录密码
|
|
public static function resetPassword(array $params): bool
|
|
{
|
|
try {
|
|
// 校验验证码
|
|
$smsDriver = new SmsDriver();
|
|
if (!$smsDriver->verify($params['phone'], $params['code'], NoticeEnum::FIND_LOGIN_PASSWORD_CAPTCHA)) {
|
|
throw new \Exception('验证码错误');
|
|
}
|
|
// 重置密码
|
|
$passwordSalt = Config::get('project.unique_identification');
|
|
$password = create_password($params['password'], $passwordSalt);
|
|
// 更新
|
|
User::where('user_phone', $params['phone'])->update(['user_password' => $password]);
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//修该密码
|
|
public static function changePassword(array $params, int $userId): bool
|
|
{
|
|
try {
|
|
$user = User::findOrEmpty($userId);
|
|
if ($user->isEmpty()) {
|
|
throw new \Exception('用户不存在');
|
|
}
|
|
// 密码盐
|
|
$passwordSalt = Config::get('project.unique_identification');
|
|
// 加密原密码
|
|
$oldPassword = create_password($params['old_password'], $passwordSalt);
|
|
if($oldPassword != $user['user_password']){
|
|
throw new \Exception('原密码不正确');
|
|
}
|
|
// 保存密码
|
|
$password = create_password($params['password'], $passwordSalt);
|
|
$user->user_password = $password;
|
|
$user->save();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//绑定手机号
|
|
public static function bindMobile(array $params): bool
|
|
{
|
|
try {
|
|
// 变更手机号场景
|
|
$sceneId = NoticeEnum::CHANGE_MOBILE_CAPTCHA;
|
|
$where = [
|
|
['id', '=', $params['user_id']],
|
|
['user_phone', '=', $params['phone']]
|
|
];
|
|
// 绑定手机号场景
|
|
if ($params['type'] == 'bind') {
|
|
$sceneId = NoticeEnum::BIND_MOBILE_CAPTCHA;
|
|
$where = [
|
|
['user_phone', '=', $params['phone']]
|
|
];
|
|
}
|
|
// 校验短信
|
|
$checkSmsCode = (new SmsDriver())->verify($params['phone'], $params['code'], $sceneId);
|
|
if (!$checkSmsCode) {
|
|
throw new \Exception('验证码错误');
|
|
}
|
|
$user = User::where($where)->findOrEmpty();
|
|
if (!$user->isEmpty()) {
|
|
throw new \Exception('该手机号已被使用');
|
|
}
|
|
User::update([
|
|
'id' => $params['user_id'],
|
|
'user_phone' => $params['phone'],
|
|
]);
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
} |