data_center/app/api/logic/UserLogic.php

143 lines
4.7 KiB
PHP
Raw Normal View History

2023-09-18 09:11:13 +08:00
<?php
namespace app\api\logic;
2023-11-13 16:10:19 +08:00
use app\common\{enum\notice\NoticeEnum, logic\BaseLogic, model\auth\Admin, model\user\User, model\systems\SystemUser, service\sms\SmsDriver};
use app\api\service\JwtTokenService;
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;
2023-11-20 14:46:36 +08:00
$user->need_reset = 0;
2023-09-19 17:39:40 +08:00
$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
$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'],
]);
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
{
2023-11-13 16:19:48 +08:00
if (empty($params)) {
2023-09-19 11:47:58 +08:00
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;
2023-11-13 16:19:48 +08:00
} catch (\Exception $e) {
2023-09-19 11:47:58 +08:00
self::setError($e->getMessage());
return false;
}
}
2023-11-13 16:10:19 +08:00
//bind用户信息
public static function bindSystemUser(array $params, array $userinfo): bool
{
try {
$systemUser = SystemUser::where([
'app_id' => $params['app_id'],
'user_id' => $userinfo['user_id'],
'system_user_id' => $params['system_user_id']
])->findOrEmpty();
if ($systemUser->isEmpty()) {
SystemUser::create([
'app_id' => $params['app_id'],
'user_id' => $userinfo['user_id'],
'system_user_id' => $params['system_user_id'],
2023-11-13 16:15:43 +08:00
'phone' => $userinfo['phone'],
2023-11-13 16:10:19 +08:00
'create_time' => time(),
'update_time' => time()
]);
} else {
SystemUser::where('id', $systemUser['id'])->update([
2023-11-13 16:15:43 +08:00
'phone' => $userinfo['phone'],
2023-11-13 16:10:19 +08:00
'create_time' => time(),
'update_time' => time()
]);
}
return true;
2023-11-13 16:19:48 +08:00
} catch (\Exception $e) {
2023-11-13 16:10:19 +08:00
self::setError($e->getMessage());
return false;
}
}
2023-09-18 09:11:13 +08:00
}