登录注册
This commit is contained in:
@@ -21,48 +21,43 @@ class UserController
|
||||
#[GetRoute]
|
||||
public function getUserInfo(): JsonResponse
|
||||
{
|
||||
$info = auth()->user();
|
||||
return $this->success(compact('info'));
|
||||
}
|
||||
$user = UserModel::query()->find(auth()->id());
|
||||
|
||||
#[PostRoute('/logout')]
|
||||
public function logout(): JsonResponse
|
||||
{
|
||||
$user_id = auth('users')->id();
|
||||
$model = new UserModel;
|
||||
if ($model->logout($user_id)) {
|
||||
return $this->success('退出登录成功');
|
||||
} else {
|
||||
return $this->error($model->getErrorMsg());
|
||||
if (! $user) {
|
||||
return $this->error('用户不存在');
|
||||
}
|
||||
|
||||
$data = $user->toArray();
|
||||
|
||||
// 手机号脱敏:13812345678 → 138****5678
|
||||
if (! empty($data['mobile'])) {
|
||||
$data['mobile'] = substr_replace($data['mobile'], '****', 3, 4);
|
||||
}
|
||||
|
||||
// 邮箱脱敏:test@example.com → t***@example.com
|
||||
if (! empty($data['email'])) {
|
||||
$parts = explode('@', $data['email']);
|
||||
$parts[0] = substr($parts[0], 0, 1).'***';
|
||||
$data['email'] = implode('@', $parts);
|
||||
}
|
||||
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
#[PutRoute]
|
||||
public function setUserInfo(UserUpdateInfoRequest $request): JsonResponse
|
||||
{
|
||||
UserModel::where('user_id', auth('user')->id())->update($request->validated());
|
||||
|
||||
return $this->error('更新成功');
|
||||
}
|
||||
|
||||
#[PostRoute('/setPwd')]
|
||||
public function setPassword(Request $request): JsonResponse
|
||||
/** 编辑用户信息(头像、昵称、性别) */
|
||||
#[PutRoute('/profile')]
|
||||
public function updateProfile(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'oldPassword' => 'required|string|max:20',
|
||||
'newPassword' => 'required|string|min:6|max:20',
|
||||
'rePassword' => 'required|same:newPassword',
|
||||
'avatar' => 'required|string|max:500',
|
||||
'nickname' => 'required|string|max:32',
|
||||
'gender' => 'required|integer|in:0,1,2',
|
||||
]);
|
||||
$user_id = auth('user')->id();
|
||||
$user = UserModel::query()->find($user_id);
|
||||
if (! password_verify($data['oldPassword'], $user['password'])) {
|
||||
return $this->error('旧密码不正确!');
|
||||
}
|
||||
$user->password = password_hash($data['newPassword'], PASSWORD_DEFAULT);
|
||||
if ($user->save()) {
|
||||
return $this->success('更新成功');
|
||||
}
|
||||
|
||||
return $this->error('更新失败');
|
||||
$user = UserModel::query()->find(auth()->id());
|
||||
|
||||
$user->update($data);
|
||||
|
||||
return $this->success($user->toArray(), '更新成功');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user