update login module

This commit is contained in:
unknown 2023-09-19 10:39:28 +08:00
parent 17a8c02415
commit 66fd313640
5 changed files with 58 additions and 60 deletions

View File

@ -16,7 +16,7 @@ use think\response\Json;
class LoginController extends BaseApiController
{
// 免登录方法
public array $notNeedLogin = ['register', 'login', 'logout', 'verify'];
public array $notNeedLogin = ['register', 'login', 'verify'];
// 注册
public function register(): Json
@ -43,8 +43,12 @@ class LoginController extends BaseApiController
// 退出
public function logout(): Json
{
LoginLogic::logout($this->userInfo);
return $this->success();
$result = LoginLogic::logout($this->userInfo);
if($result){
return $this->success('成功');
}else{
return $this->fail('失败');
}
}
// 验证token

View File

@ -26,7 +26,7 @@ class LoginLogic extends BaseLogic
$adminPassword = create_password($params['phone'], $passwordSalt);
$userPassword = create_password($params['password'], $passwordSalt);
$avatar = env('project.web_domain').'/'.ConfigService::get('default_image', 'user_avatar');
(new Db)->startTrans();
Db::startTrans();
try {
//添加到管理员表
$admin = Admin::create([
@ -42,28 +42,30 @@ class LoginLogic extends BaseLogic
$user = User::create([
'admin_id' => $admin['id'],
'phone' => $params['phone'],
'nickname' => '用户'.$params['phone'],
'nick_name' => '用户'.$params['phone'],
'password' => $userPassword,
'avatar' => empty($params['avatar']) ? $avatar : $params['avatar'],
'age' => $params['age'],
'gender' => $params['gender'],
'province' => $params['province'],
'city' => $params['city'],
'area' => $params['area'],
'street' => $params['street'],
'village' => $params['village'],
'brigade' => $params['brigade'],
'address' => $params['address'],
'age' => empty($params['age']) ? 0 : $params['age'],
'gender' => empty($params['gender']) ? 0 : $params['gender'],
'real_name' => empty($params['real_name']) ? '' : $params['real_name'],
'id_card' => empty($params['id_card']) ? '' : $params['id_card'],
'province' => empty($params['province']) ? 0 : $params['province'],
'city' => empty($params['city']) ? 0 : $params['city'],
'area' => empty($params['area']) ? 0 : $params['area'],
'street' => empty($params['street']) ? 0 : $params['street'],
'village' => empty($params['village']) ? 0 : $params['village'],
'brigade' => empty($params['brigade']) ? 0 : $params['brigade'],
'address' => empty($params['address']) ? '' : $params['address'],
]);
if(!empty($admin['id']) && !empty($user['id'])){
(new Db)->commit();
Db::commit();
return true;
}else{
(new Db)->rollback();
Db::rollback();
return false;
}
} catch (\Exception $e) {
(new Db)->rollback();
Db::rollback();
Log::error($e->getMessage());
self::setError($e->getMessage());
return false;
@ -83,7 +85,7 @@ class LoginLogic extends BaseLogic
//设置token
$userInfo = UserTokenService::setToken($user->id,0);
return [
'user_id' => $userInfo['user_id'],
'uid' => $userInfo['user_id'],
'phone' => $userInfo['phone'],
'token' => $userInfo['token'],
];
@ -111,12 +113,12 @@ class LoginLogic extends BaseLogic
{
try {
$userInfo = (new UserTokenCache())->getUserInfo($params['token']);
if(empty($userInfo)){
if(empty($userInfo) || $userInfo['user_id'] != $params['uid']){
self::setError('token无效');
return false;
}
return [
'user_id' => $userInfo['user_id'],
'uid' => $userInfo['user_id'],
'phone' => $userInfo['phone'],
'token' => $userInfo['token'],
];

View File

@ -17,16 +17,28 @@ use think\facade\Config;
class LoginAccountValidate extends BaseValidate
{
protected $rule = [
'account' => 'require|checkAccount',
'scene' => 'require|in:' . LoginEnum::ACCOUNT_PASSWORD . ',' . LoginEnum::MOBILE_CAPTCHA . '|checkScene',
'account' => 'require',
];
protected $message = [
'account.require' => '请输入账号',
'scene.require' => '场景不能为空',
'scene.in' => '场景值错误',
'account.require' => '请输入账号',
];
public function checkAccount($account): bool|string
{
$user = User::field('id,status')->where('phone',$account)->findOrEmpty();
if($user->isEmpty()){
return '账号错误';
}
if ($user['status'] != 0) {
return '用户已冻结或删除';
}
return true;
}
public function checkScene($scene, $rule, $data): bool|string
{
// 判断scene的值
@ -55,17 +67,7 @@ class LoginAccountValidate extends BaseValidate
if (!$userAccountSafeCache->isSafe()) {
return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试';
}
$userInfo = User::where('phone',$data['account'])->field('password,status')->findOrEmpty();
if ($userInfo->isEmpty()) {
return '用户不存在';
}
if ($userInfo['status'] != 0) {
return '用户已冻结或删除';
}
if (empty($userInfo['password'])) {
$userAccountSafeCache->record();
return '密码不存在';
}
$userInfo = User::field('password')->where('phone',$data['account'])->findOrEmpty();
$passwordSalt = Config::get('project.unique_identification');
if ($userInfo['password'] !== create_password($password, $passwordSalt)) {
$userAccountSafeCache->record();
@ -77,13 +79,6 @@ class LoginAccountValidate extends BaseValidate
public function checkCode($code, $rule, $data): bool|string
{
$userInfo = User::where('phone',$data['account'])->field('id,status')->findOrEmpty();
if ($userInfo->isEmpty()) {
return '用户不存在';
}
if ($userInfo['status'] != 0) {
return '用户已冻结或删除';
}
$smsDriver = new SmsDriver();
$result = $smsDriver->verify($data['account'], $code, NoticeEnum::LOGIN_CAPTCHA);
if ($result) {

View File

@ -17,18 +17,19 @@ class RegisterValidate extends BaseValidate
];
protected $rule = [
'phone' => 'require|mobile|unique:user,user_phone',
'phone' => 'require|mobile|unique:user',
'password' => 'require|length:6,20',
'password_confirm' => 'require|confirm',
'age' => 'require|number|gt:0',
'gender' => 'require|in:0,1,2',
'province' => 'require|number',
'city' => 'require|number',
'area' => 'require|number',
'street' => 'require|number',
'village' => 'require|number',
'brigade' => 'require|number',
'address' => 'require'
'age' => 'number|gt:0',
'gender' => 'in:0,1,2',
'real_name' => 'chs|length:2,25',
'id_card' => 'idCard',
'province' => 'number',
'city' => 'number',
'area' => 'number',
'street' => 'number',
'village' => 'number',
'brigade' => 'number',
];
protected $message = [
@ -39,24 +40,18 @@ class RegisterValidate extends BaseValidate
'password.length' => '密码须在6-25位之间',
'password_confirm.require' => '请输入确认密码',
'password_confirm.confirm' => '两次输入的密码不一致',
'age.require' => '请输入用户年龄',
'age.number' => '年龄必须是数字',
'age.gt' => '年龄必须大于0',
'gender.require' => '请输入用户性别',
'gender.in' => '用户性别值错误',
'province.require' => '请输入所在省份编码',
'real_name.chs' => '真实姓名必须是汉字',
'real_name.length' => '真实姓名长度必须大于2个汉字且不得超过25个汉字',
'id_card.idCard' => '身份证号码不正确',
'province.number' => '省份编码值错误',
'city.require' => '请输入所在城市编码',
'city.number' => '城市编码值错误',
'area.require' => '请输入所在区县编码',
'area.number' => '区县编码值错误',
'street.require' => '请输入所在镇街编码',
'street.number' => '镇街编码值错误',
'village.require' => '请输入所在村社编码',
'village.number' => '村社编码值错误',
'brigade.require' => '请输入所在小组编码',
'brigade.number' => '小组编码值错误',
'address.require' => '请输入详细地址'
];

View File

@ -8,9 +8,11 @@ class TokenValidate extends BaseValidate
{
protected $rule = [
'token' => 'require',
'uid' => 'require',
];
protected $message = [
'phone.require' => '缺少必要参数',
'token.require' => '缺少必要参数token',
'uid.require' => '缺少必要参数uid',
];
}