Skip to content

Commit

Permalink
feat(api): 编辑当前登录用户信息 (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaiyuxin103 committed Feb 11, 2025
1 parent 253799f commit 3861010
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,15 @@ public function edit(User $user)
/**
* Update the specified resource in storage.
*/
public function update(UserRequest $request, User $user)
public function update(UserRequest $request): JsonResponse
{
//
$user = $request->user();

$user->update($request->validated());

return Response::success((new UserResource($user->setAppends([
'format_gender',
])))->showSensitiveFields());
}

/**
Expand Down
12 changes: 12 additions & 0 deletions app/Http/Requests/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ function (string $attribute, mixed $value, Closure $fail) {
'birthday' => ['sometimes', 'date_format:Y-m-d'],
'introduction' => ['sometimes', 'string', 'max:255'],
],
'PATCH', 'PUT' => [
'first_name' => ['sometimes', 'string', 'max:255'],
'last_name' => ['sometimes', 'string', 'max:255'],
'first_alias' => ['nullable', 'string', 'max:255'],
'last_alias' => ['nullable', 'string', 'max:255'],
'phone' => ['sometimes', 'string', 'phone:US,CN,JP'],
'zip' => ['sometimes', 'string', 'max:255'],
'address' => ['sometimes', 'string', 'max:255'],
'gender' => ['sometimes', Rule::enum(GenderEnum::class)],
'birthday' => ['sometimes', 'date_format:Y-m-d'],
'introduction' => ['sometimes', 'string', 'max:255'],
],
default => [],
};
}
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
Route::get('user', [UserController::class, 'me'])->name('user.show');
// 注销用户
Route::delete('users/{user}', [UserController::class, 'destroy']);
// 编辑当前用户信息
Route::match(['patch', 'put'], 'user', [UserController::class, 'update']);
});
});

Expand Down

0 comments on commit 3861010

Please sign in to comment.