Skip to content

Commit 59381be

Browse files
author
yqz
committed
更新登录注册代码
1 parent 8654772 commit 59381be

26 files changed

Lines changed: 527 additions & 262 deletions

.cursor/rules/rule.mdc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ alwaysApply: true
44

55
前端规则:
66

7-
任何前端代码的编写都要求和之前项目整体的前端的,字体大小风格,颜色搭配保持一致
7+
任何前端代码的编写都要求和之前项目整体的前端的,字体大小风格,默认为不添加任何颜色
88

99

1010
后端规则:

src/main/java/com/yqz/openblog/article/service/ArticleService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public ArticleListItemResponse mapListItem(Article a) {
4949
resp.setCoverMediaKey(a.getCoverMediaKey());
5050
resp.setAuthorId(a.getAuthorId());
5151
User author = userMapper.selectById(a.getAuthorId());
52-
resp.setAuthorNickname(author == null ? null : author.getNickname());
52+
resp.setAuthorNickname(author == null ? null : author.getUsername());
5353
resp.setPublishedAt(a.getPublishedAt());
5454
resp.setStatus(a.getStatus());
5555
resp.setLikeCount(a.getLikeCount());
@@ -68,7 +68,7 @@ public ArticleDetailResponse mapDetail(Article a) {
6868
resp.setCoverMediaKey(a.getCoverMediaKey());
6969
resp.setAuthorId(a.getAuthorId());
7070
User author = userMapper.selectById(a.getAuthorId());
71-
resp.setAuthorNickname(author == null ? null : author.getNickname());
71+
resp.setAuthorNickname(author == null ? null : author.getUsername());
7272
resp.setPublishedAt(a.getPublishedAt());
7373
resp.setStatus(a.getStatus());
7474
resp.setLikeCount(a.getLikeCount());

src/main/java/com/yqz/openblog/comment/service/CommentService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ private CommentUserResponse toUser(User u, Long uid) {
160160
cu.setAvatarUrl(null);
161161
return cu;
162162
}
163-
String nn = u.getNickname();
164-
if (nn == null || nn.isBlank()) nn = u.getUsername();
165-
cu.setNickname(nn);
163+
cu.setNickname(u.getUsername());
166164
cu.setAvatarUrl(u.getAvatarUrl());
167165
return cu;
168166
}

src/main/java/com/yqz/openblog/user/controller/PublicProfileController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ public PublicProfileController(UserMapper userMapper, CurrentUser currentUser, M
3131
@GetMapping("/profile")
3232
public ApiResponse<PublicProfileResponse> profile() {
3333
// 如果前端携带了 JWT,则优先返回“当前登录用户”的公开信息
34-
// 这样在管理员/作者编辑后,前台页面也能立刻反映你改的昵称/签名/头像。
34+
// 这样在管理员/作者编辑后,前台页面也能立刻反映你改的用户名/签名/头像。
3535
Long uid = currentUser.userId();
3636
if (uid != null) {
3737
User u = userMapper.selectById(uid);
3838
if (u != null) {
3939
PublicProfileResponse resp = new PublicProfileResponse();
4040
resp.setUserId(u.getId());
41-
resp.setNickname(u.getNickname());
41+
resp.setUsername(u.getUsername());
4242
resp.setAvatarUrl(mediaService.normalizePublicMediaUrl(u.getAvatarUrl()));
4343
resp.setBio(u.getBio());
4444
return ApiResponse.ok(resp);
@@ -69,7 +69,7 @@ public ApiResponse<PublicProfileResponse> profile() {
6969

7070
PublicProfileResponse resp = new PublicProfileResponse();
7171
resp.setUserId(u.getId());
72-
resp.setNickname(u.getNickname());
72+
resp.setUsername(u.getUsername());
7373
resp.setAvatarUrl(mediaService.normalizePublicMediaUrl(u.getAvatarUrl()));
7474
resp.setBio(u.getBio());
7575
return ApiResponse.ok(resp);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.yqz.openblog.user.controller;
2+
3+
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
4+
import com.yqz.openblog.common.ApiResponse;
5+
import com.yqz.openblog.common.BizException;
6+
import com.yqz.openblog.user.dto.PendingUserResponse;
7+
import com.yqz.openblog.user.entity.User;
8+
import com.yqz.openblog.user.entity.UserRole;
9+
import com.yqz.openblog.user.repo.UserMapper;
10+
import org.springframework.security.access.prepost.PreAuthorize;
11+
import org.springframework.web.bind.annotation.CrossOrigin;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RestController;
17+
18+
import java.util.List;
19+
20+
@RestController
21+
@RequestMapping("/api/v1")
22+
@CrossOrigin(origins = "*")
23+
public class UserAdminController {
24+
25+
private final UserMapper userMapper;
26+
27+
public UserAdminController(UserMapper userMapper) {
28+
this.userMapper = userMapper;
29+
}
30+
31+
@GetMapping("/admin/users/pending")
32+
@PreAuthorize("hasRole('ADMIN')")
33+
public ApiResponse<List<PendingUserResponse>> listPendingReaders() {
34+
List<User> users = userMapper.selectList(
35+
Wrappers.lambdaQuery(User.class)
36+
.eq(User::getStatus, "PENDING")
37+
.eq(User::getRole, UserRole.READER)
38+
.orderByDesc(User::getCreatedAt));
39+
List<PendingUserResponse> list = users.stream().map(this::toPending).toList();
40+
return ApiResponse.ok(list);
41+
}
42+
43+
@PostMapping("/admin/users/{userId}/approve")
44+
@PreAuthorize("hasRole('ADMIN')")
45+
public ApiResponse<Void> approveReader(@PathVariable("userId") Long userId) {
46+
User u = userMapper.selectById(userId);
47+
if (u == null) {
48+
throw new BizException(4041, "用户不存在");
49+
}
50+
if (u.getRole() != UserRole.READER) {
51+
throw new BizException(4000, "仅支持审核读者账号");
52+
}
53+
if (!"PENDING".equals(u.getStatus())) {
54+
throw new BizException(4000, "该账号不在待审核状态");
55+
}
56+
u.setStatus("ACTIVE");
57+
userMapper.updateById(u);
58+
return ApiResponse.ok();
59+
}
60+
61+
private PendingUserResponse toPending(User u) {
62+
PendingUserResponse r = new PendingUserResponse();
63+
r.setUserId(u.getId());
64+
r.setUsername(u.getUsername());
65+
r.setEmail(u.getEmail());
66+
r.setCreatedAt(u.getCreatedAt());
67+
return r;
68+
}
69+
}

src/main/java/com/yqz/openblog/user/dto/MeResponse.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
public class MeResponse {
66
private Long userId;
77
private String username;
8-
private String nickname;
98
private String avatarUrl;
109
private String bio;
1110
private UserRole role;
@@ -26,14 +25,6 @@ public void setUsername(String username) {
2625
this.username = username;
2726
}
2827

29-
public String getNickname() {
30-
return nickname;
31-
}
32-
33-
public void setNickname(String nickname) {
34-
this.nickname = nickname;
35-
}
36-
3728
public String getAvatarUrl() {
3829
return avatarUrl;
3930
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.yqz.openblog.user.dto;
2+
3+
import java.time.Instant;
4+
5+
public class PendingUserResponse {
6+
7+
private Long userId;
8+
private String username;
9+
private String email;
10+
private Instant createdAt;
11+
12+
public Long getUserId() {
13+
return userId;
14+
}
15+
16+
public void setUserId(Long userId) {
17+
this.userId = userId;
18+
}
19+
20+
public String getUsername() {
21+
return username;
22+
}
23+
24+
public void setUsername(String username) {
25+
this.username = username;
26+
}
27+
28+
public String getEmail() {
29+
return email;
30+
}
31+
32+
public void setEmail(String email) {
33+
this.email = email;
34+
}
35+
36+
public Instant getCreatedAt() {
37+
return createdAt;
38+
}
39+
40+
public void setCreatedAt(Instant createdAt) {
41+
this.createdAt = createdAt;
42+
}
43+
}

src/main/java/com/yqz/openblog/user/dto/PublicProfileResponse.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class PublicProfileResponse {
44
private Long userId;
5-
private String nickname;
5+
private String username;
66
private String avatarUrl;
77
private String bio;
88

@@ -14,12 +14,12 @@ public void setUserId(Long userId) {
1414
this.userId = userId;
1515
}
1616

17-
public String getNickname() {
18-
return nickname;
17+
public String getUsername() {
18+
return username;
1919
}
2020

21-
public void setNickname(String nickname) {
22-
this.nickname = nickname;
21+
public void setUsername(String username) {
22+
this.username = username;
2323
}
2424

2525
public String getAvatarUrl() {

src/main/java/com/yqz/openblog/user/dto/RegisterRequest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ public class RegisterRequest {
1919
@Size(min = 6, max = 72)
2020
private String password;
2121

22-
@Size(max = 32)
23-
private String nickname;
24-
2522
private String sliderChallengeId;
2623

2724
public String getUsername() {
@@ -48,14 +45,6 @@ public void setPassword(String password) {
4845
this.password = password;
4946
}
5047

51-
public String getNickname() {
52-
return nickname;
53-
}
54-
55-
public void setNickname(String nickname) {
56-
this.nickname = nickname;
57-
}
58-
5948
public String getSliderChallengeId() {
6049
return sliderChallengeId;
6150
}

src/main/java/com/yqz/openblog/user/dto/UserUpdateRequest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ public class UserUpdateRequest {
99
@Size(min = 3, max = 32)
1010
private String username;
1111

12-
@Size(max = 32)
13-
private String nickname;
14-
1512
@Size(max = 512)
1613
private String bio;
1714

@@ -30,14 +27,6 @@ public void setUsername(String username) {
3027
this.username = username;
3128
}
3229

33-
public String getNickname() {
34-
return nickname;
35-
}
36-
37-
public void setNickname(String nickname) {
38-
this.nickname = nickname;
39-
}
40-
4130
public String getBio() {
4231
return bio;
4332
}

0 commit comments

Comments
 (0)