2023-09-18 09:11:13 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\api\logic;
|
2023-09-19 11:47:58 +08:00
|
|
|
use app\common\{enum\notice\NoticeEnum, logic\BaseLogic, model\auth\Admin, model\user\User, service\sms\SmsDriver};
|
2023-09-18 09:11:13 +08:00
|
|
|
use think\facade\Config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 会员逻辑层
|
|
|
|
* Class UserLogic
|
|
|
|
* @package app\shopapi\logic
|
|
|
|
*/
|
|
|
|
class UserLogic extends BaseLogic
|
|
|
|
{
|
|
|
|
//重置登录密码
|
|
|
|
public static function resetPassword(array $params): bool
|
|
|
|
{
|
|
|
|
try {
|
2023-09-19 17:39:40 +08:00
|
|
|
//验证手机号码
|
|
|
|
$user = User::field('id')->where('phone',$params['phone'])->findOrEmpty();
|
|
|
|
if($user->isEmpty()){
|
|
|
|
throw new \Exception('该手机号未注册');
|
|
|
|
}
|
2023-09-18 09:11:13 +08:00
|
|
|
// 校验验证码
|
|
|
|
$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);
|
|
|
|
// 更新
|
2023-09-19 17:39:40 +08:00
|
|
|
$user->password = $password;
|
|
|
|
$user->save();
|
2023-09-18 09:11:13 +08:00
|
|
|
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);
|
2023-09-19 11:47:58 +08:00
|
|
|
if($oldPassword != $user['password']){
|
2023-09-18 09:11:13 +08:00
|
|
|
throw new \Exception('原密码不正确');
|
|
|
|
}
|
|
|
|
// 保存密码
|
|
|
|
$password = create_password($params['password'], $passwordSalt);
|
2023-09-19 11:47:58 +08:00
|
|
|
$user->password = $password;
|
2023-09-18 09:11:13 +08:00
|
|
|
$user->save();
|
|
|
|
return true;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
self::setError($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 11:47:58 +08:00
|
|
|
//修改手机号
|
|
|
|
public static function changeMobile(array $params,int $uid): bool
|
2023-09-18 09:11:13 +08:00
|
|
|
{
|
|
|
|
try {
|
2023-09-19 11:47:58 +08:00
|
|
|
$user = User::field('admin_id')->where('id',$uid)->findOrEmpty();
|
|
|
|
if($user->isEmpty()){
|
|
|
|
throw new \Exception('数据错误');
|
2023-09-18 09:11:13 +08:00
|
|
|
}
|
|
|
|
// 校验短信
|
2023-09-19 11:47:58 +08:00
|
|
|
$checkSmsCode = (new SmsDriver())->verify($params['phone'], $params['code'], NoticeEnum::CHANGE_MOBILE_CAPTCHA);
|
2023-09-18 09:11:13 +08:00
|
|
|
if (!$checkSmsCode) {
|
|
|
|
throw new \Exception('验证码错误');
|
|
|
|
}
|
|
|
|
User::update([
|
2023-09-19 11:47:58 +08:00
|
|
|
'id' => $uid,
|
|
|
|
'phone' => $params['phone'],
|
|
|
|
]);
|
|
|
|
Admin::update([
|
|
|
|
'id' => $user['admin_id'],
|
|
|
|
'account' => $params['phone']
|
2023-09-18 09:11:13 +08:00
|
|
|
]);
|
|
|
|
return true;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
self::setError($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 11:47:58 +08:00
|
|
|
//更新用户信息
|
|
|
|
public static function updateUser(array $params, int $uid): bool
|
|
|
|
{
|
|
|
|
if(empty($params)){
|
|
|
|
self::setError('参数列表为空');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$user = User::where('id',$uid)->findOrEmpty();
|
|
|
|
if ($user->isEmpty()) {
|
|
|
|
self::setError('数据不存在');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$params['id'] = $uid;
|
|
|
|
try {
|
|
|
|
User::update($params);
|
|
|
|
return true;
|
|
|
|
}catch (\Exception $e){
|
|
|
|
self::setError($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 09:11:13 +08:00
|
|
|
}
|