update login module
This commit is contained in:
parent
17a8c02415
commit
66fd313640
|
@ -16,7 +16,7 @@ use think\response\Json;
|
||||||
class LoginController extends BaseApiController
|
class LoginController extends BaseApiController
|
||||||
{
|
{
|
||||||
// 免登录方法
|
// 免登录方法
|
||||||
public array $notNeedLogin = ['register', 'login', 'logout', 'verify'];
|
public array $notNeedLogin = ['register', 'login', 'verify'];
|
||||||
|
|
||||||
// 注册
|
// 注册
|
||||||
public function register(): Json
|
public function register(): Json
|
||||||
|
@ -43,8 +43,12 @@ class LoginController extends BaseApiController
|
||||||
// 退出
|
// 退出
|
||||||
public function logout(): Json
|
public function logout(): Json
|
||||||
{
|
{
|
||||||
LoginLogic::logout($this->userInfo);
|
$result = LoginLogic::logout($this->userInfo);
|
||||||
return $this->success();
|
if($result){
|
||||||
|
return $this->success('成功');
|
||||||
|
}else{
|
||||||
|
return $this->fail('失败');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证token
|
// 验证token
|
||||||
|
|
|
@ -26,7 +26,7 @@ class LoginLogic extends BaseLogic
|
||||||
$adminPassword = create_password($params['phone'], $passwordSalt);
|
$adminPassword = create_password($params['phone'], $passwordSalt);
|
||||||
$userPassword = create_password($params['password'], $passwordSalt);
|
$userPassword = create_password($params['password'], $passwordSalt);
|
||||||
$avatar = env('project.web_domain').'/'.ConfigService::get('default_image', 'user_avatar');
|
$avatar = env('project.web_domain').'/'.ConfigService::get('default_image', 'user_avatar');
|
||||||
(new Db)->startTrans();
|
Db::startTrans();
|
||||||
try {
|
try {
|
||||||
//添加到管理员表
|
//添加到管理员表
|
||||||
$admin = Admin::create([
|
$admin = Admin::create([
|
||||||
|
@ -42,28 +42,30 @@ class LoginLogic extends BaseLogic
|
||||||
$user = User::create([
|
$user = User::create([
|
||||||
'admin_id' => $admin['id'],
|
'admin_id' => $admin['id'],
|
||||||
'phone' => $params['phone'],
|
'phone' => $params['phone'],
|
||||||
'nickname' => '用户'.$params['phone'],
|
'nick_name' => '用户'.$params['phone'],
|
||||||
'password' => $userPassword,
|
'password' => $userPassword,
|
||||||
'avatar' => empty($params['avatar']) ? $avatar : $params['avatar'],
|
'avatar' => empty($params['avatar']) ? $avatar : $params['avatar'],
|
||||||
'age' => $params['age'],
|
'age' => empty($params['age']) ? 0 : $params['age'],
|
||||||
'gender' => $params['gender'],
|
'gender' => empty($params['gender']) ? 0 : $params['gender'],
|
||||||
'province' => $params['province'],
|
'real_name' => empty($params['real_name']) ? '' : $params['real_name'],
|
||||||
'city' => $params['city'],
|
'id_card' => empty($params['id_card']) ? '' : $params['id_card'],
|
||||||
'area' => $params['area'],
|
'province' => empty($params['province']) ? 0 : $params['province'],
|
||||||
'street' => $params['street'],
|
'city' => empty($params['city']) ? 0 : $params['city'],
|
||||||
'village' => $params['village'],
|
'area' => empty($params['area']) ? 0 : $params['area'],
|
||||||
'brigade' => $params['brigade'],
|
'street' => empty($params['street']) ? 0 : $params['street'],
|
||||||
'address' => $params['address'],
|
'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'])){
|
if(!empty($admin['id']) && !empty($user['id'])){
|
||||||
(new Db)->commit();
|
Db::commit();
|
||||||
return true;
|
return true;
|
||||||
}else{
|
}else{
|
||||||
(new Db)->rollback();
|
Db::rollback();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
(new Db)->rollback();
|
Db::rollback();
|
||||||
Log::error($e->getMessage());
|
Log::error($e->getMessage());
|
||||||
self::setError($e->getMessage());
|
self::setError($e->getMessage());
|
||||||
return false;
|
return false;
|
||||||
|
@ -83,7 +85,7 @@ class LoginLogic extends BaseLogic
|
||||||
//设置token
|
//设置token
|
||||||
$userInfo = UserTokenService::setToken($user->id,0);
|
$userInfo = UserTokenService::setToken($user->id,0);
|
||||||
return [
|
return [
|
||||||
'user_id' => $userInfo['user_id'],
|
'uid' => $userInfo['user_id'],
|
||||||
'phone' => $userInfo['phone'],
|
'phone' => $userInfo['phone'],
|
||||||
'token' => $userInfo['token'],
|
'token' => $userInfo['token'],
|
||||||
];
|
];
|
||||||
|
@ -111,12 +113,12 @@ class LoginLogic extends BaseLogic
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$userInfo = (new UserTokenCache())->getUserInfo($params['token']);
|
$userInfo = (new UserTokenCache())->getUserInfo($params['token']);
|
||||||
if(empty($userInfo)){
|
if(empty($userInfo) || $userInfo['user_id'] != $params['uid']){
|
||||||
self::setError('token无效');
|
self::setError('token无效');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return [
|
return [
|
||||||
'user_id' => $userInfo['user_id'],
|
'uid' => $userInfo['user_id'],
|
||||||
'phone' => $userInfo['phone'],
|
'phone' => $userInfo['phone'],
|
||||||
'token' => $userInfo['token'],
|
'token' => $userInfo['token'],
|
||||||
];
|
];
|
||||||
|
|
|
@ -17,16 +17,28 @@ use think\facade\Config;
|
||||||
class LoginAccountValidate extends BaseValidate
|
class LoginAccountValidate extends BaseValidate
|
||||||
{
|
{
|
||||||
protected $rule = [
|
protected $rule = [
|
||||||
|
'account' => 'require|checkAccount',
|
||||||
'scene' => 'require|in:' . LoginEnum::ACCOUNT_PASSWORD . ',' . LoginEnum::MOBILE_CAPTCHA . '|checkScene',
|
'scene' => 'require|in:' . LoginEnum::ACCOUNT_PASSWORD . ',' . LoginEnum::MOBILE_CAPTCHA . '|checkScene',
|
||||||
'account' => 'require',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $message = [
|
protected $message = [
|
||||||
|
'account.require' => '请输入账号',
|
||||||
'scene.require' => '场景不能为空',
|
'scene.require' => '场景不能为空',
|
||||||
'scene.in' => '场景值错误',
|
'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
|
public function checkScene($scene, $rule, $data): bool|string
|
||||||
{
|
{
|
||||||
// 判断scene的值
|
// 判断scene的值
|
||||||
|
@ -55,17 +67,7 @@ class LoginAccountValidate extends BaseValidate
|
||||||
if (!$userAccountSafeCache->isSafe()) {
|
if (!$userAccountSafeCache->isSafe()) {
|
||||||
return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试';
|
return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试';
|
||||||
}
|
}
|
||||||
$userInfo = User::where('phone',$data['account'])->field('password,status')->findOrEmpty();
|
$userInfo = User::field('password')->where('phone',$data['account'])->findOrEmpty();
|
||||||
if ($userInfo->isEmpty()) {
|
|
||||||
return '用户不存在';
|
|
||||||
}
|
|
||||||
if ($userInfo['status'] != 0) {
|
|
||||||
return '用户已冻结或删除';
|
|
||||||
}
|
|
||||||
if (empty($userInfo['password'])) {
|
|
||||||
$userAccountSafeCache->record();
|
|
||||||
return '密码不存在';
|
|
||||||
}
|
|
||||||
$passwordSalt = Config::get('project.unique_identification');
|
$passwordSalt = Config::get('project.unique_identification');
|
||||||
if ($userInfo['password'] !== create_password($password, $passwordSalt)) {
|
if ($userInfo['password'] !== create_password($password, $passwordSalt)) {
|
||||||
$userAccountSafeCache->record();
|
$userAccountSafeCache->record();
|
||||||
|
@ -77,13 +79,6 @@ class LoginAccountValidate extends BaseValidate
|
||||||
|
|
||||||
public function checkCode($code, $rule, $data): bool|string
|
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();
|
$smsDriver = new SmsDriver();
|
||||||
$result = $smsDriver->verify($data['account'], $code, NoticeEnum::LOGIN_CAPTCHA);
|
$result = $smsDriver->verify($data['account'], $code, NoticeEnum::LOGIN_CAPTCHA);
|
||||||
if ($result) {
|
if ($result) {
|
||||||
|
|
|
@ -17,18 +17,19 @@ class RegisterValidate extends BaseValidate
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $rule = [
|
protected $rule = [
|
||||||
'phone' => 'require|mobile|unique:user,user_phone',
|
'phone' => 'require|mobile|unique:user',
|
||||||
'password' => 'require|length:6,20',
|
'password' => 'require|length:6,20',
|
||||||
'password_confirm' => 'require|confirm',
|
'password_confirm' => 'require|confirm',
|
||||||
'age' => 'require|number|gt:0',
|
'age' => 'number|gt:0',
|
||||||
'gender' => 'require|in:0,1,2',
|
'gender' => 'in:0,1,2',
|
||||||
'province' => 'require|number',
|
'real_name' => 'chs|length:2,25',
|
||||||
'city' => 'require|number',
|
'id_card' => 'idCard',
|
||||||
'area' => 'require|number',
|
'province' => 'number',
|
||||||
'street' => 'require|number',
|
'city' => 'number',
|
||||||
'village' => 'require|number',
|
'area' => 'number',
|
||||||
'brigade' => 'require|number',
|
'street' => 'number',
|
||||||
'address' => 'require'
|
'village' => 'number',
|
||||||
|
'brigade' => 'number',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $message = [
|
protected $message = [
|
||||||
|
@ -39,24 +40,18 @@ class RegisterValidate extends BaseValidate
|
||||||
'password.length' => '密码须在6-25位之间',
|
'password.length' => '密码须在6-25位之间',
|
||||||
'password_confirm.require' => '请输入确认密码',
|
'password_confirm.require' => '请输入确认密码',
|
||||||
'password_confirm.confirm' => '两次输入的密码不一致',
|
'password_confirm.confirm' => '两次输入的密码不一致',
|
||||||
'age.require' => '请输入用户年龄',
|
|
||||||
'age.number' => '年龄必须是数字',
|
'age.number' => '年龄必须是数字',
|
||||||
'age.gt' => '年龄必须大于0',
|
'age.gt' => '年龄必须大于0',
|
||||||
'gender.require' => '请输入用户性别',
|
|
||||||
'gender.in' => '用户性别值错误',
|
'gender.in' => '用户性别值错误',
|
||||||
'province.require' => '请输入所在省份编码',
|
'real_name.chs' => '真实姓名必须是汉字',
|
||||||
|
'real_name.length' => '真实姓名长度必须大于2个汉字且不得超过25个汉字',
|
||||||
|
'id_card.idCard' => '身份证号码不正确',
|
||||||
'province.number' => '省份编码值错误',
|
'province.number' => '省份编码值错误',
|
||||||
'city.require' => '请输入所在城市编码',
|
|
||||||
'city.number' => '城市编码值错误',
|
'city.number' => '城市编码值错误',
|
||||||
'area.require' => '请输入所在区县编码',
|
|
||||||
'area.number' => '区县编码值错误',
|
'area.number' => '区县编码值错误',
|
||||||
'street.require' => '请输入所在镇街编码',
|
|
||||||
'street.number' => '镇街编码值错误',
|
'street.number' => '镇街编码值错误',
|
||||||
'village.require' => '请输入所在村社编码',
|
|
||||||
'village.number' => '村社编码值错误',
|
'village.number' => '村社编码值错误',
|
||||||
'brigade.require' => '请输入所在小组编码',
|
|
||||||
'brigade.number' => '小组编码值错误',
|
'brigade.number' => '小组编码值错误',
|
||||||
'address.require' => '请输入详细地址'
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,11 @@ class TokenValidate extends BaseValidate
|
||||||
{
|
{
|
||||||
protected $rule = [
|
protected $rule = [
|
||||||
'token' => 'require',
|
'token' => 'require',
|
||||||
|
'uid' => 'require',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $message = [
|
protected $message = [
|
||||||
'phone.require' => '缺少必要参数',
|
'token.require' => '缺少必要参数token',
|
||||||
|
'uid.require' => '缺少必要参数uid',
|
||||||
];
|
];
|
||||||
}
|
}
|
Loading…
Reference in New Issue