data_center/app/api/logic/UserLogic.php

117 lines
3.7 KiB
PHP

<?php
namespace app\api\logic;
use app\common\{enum\notice\NoticeEnum, logic\BaseLogic, model\auth\Admin, 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 {
//验证手机号码
$user = User::field('id')->where('phone',$params['phone'])->findOrEmpty();
if($user->isEmpty()){
throw new \Exception('该手机号未注册');
}
// 校验验证码
$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->password = $password;
$user->save();
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['password']){
throw new \Exception('原密码不正确');
}
// 保存密码
$password = create_password($params['password'], $passwordSalt);
$user->password = $password;
$user->save();
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
//修改手机号
public static function changeMobile(array $params,int $uid): bool
{
try {
$user = User::field('admin_id')->where('id',$uid)->findOrEmpty();
if($user->isEmpty()){
throw new \Exception('数据错误');
}
// 校验短信
$checkSmsCode = (new SmsDriver())->verify($params['phone'], $params['code'], NoticeEnum::CHANGE_MOBILE_CAPTCHA);
if (!$checkSmsCode) {
throw new \Exception('验证码错误');
}
User::update([
'id' => $uid,
'phone' => $params['phone'],
]);
Admin::update([
'id' => $user['admin_id'],
'account' => $params['phone']
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
//更新用户信息
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;
}
}
}