feat: 设置页面修改用户名,需要重新发放token

This commit is contained in:
Tim
2025-07-29 19:12:39 +08:00
parent 9aec1afcf5
commit 3dbeb25a09
3 changed files with 24 additions and 8 deletions

View File

@@ -55,7 +55,7 @@
<script> <script>
import { API_BASE_URL, toast } from '../main' import { API_BASE_URL, toast } from '../main'
import { getToken, fetchCurrentUser } from '../utils/auth' import { getToken, fetchCurrentUser, setToken } from '../utils/auth'
import BaseInput from '../components/BaseInput.vue' import BaseInput from '../components/BaseInput.vue'
import Dropdown from '../components/Dropdown.vue' import Dropdown from '../components/Dropdown.vue'
import { hatch } from 'ldrs' import { hatch } from 'ldrs'
@@ -91,6 +91,9 @@ export default {
if (this.role === 'ADMIN') { if (this.role === 'ADMIN') {
this.loadAdminConfig() this.loadAdminConfig()
} }
} else {
toast.error('请先登录')
this.$router.push('/login')
} }
this.isLoadingPage = false this.isLoadingPage = false
}, },
@@ -154,7 +157,7 @@ export default {
this.isSaving = true this.isSaving = true
do { do {
const token = getToken() let token = getToken()
this.usernameError = '' this.usernameError = ''
if (!this.username) { if (!this.username) {
this.usernameError = '用户名不能为空' this.usernameError = '用户名不能为空'
@@ -184,11 +187,16 @@ export default {
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
body: JSON.stringify({ username: this.username, introduction: this.introduction }) body: JSON.stringify({ username: this.username, introduction: this.introduction })
}) })
const data = await res.json()
if (!res.ok) { if (!res.ok) {
const data = await res.json()
toast.error(data.error || '保存失败') toast.error(data.error || '保存失败')
break break
} }
if (data.token) {
setToken(data.token)
token = data.token
}
if (this.role === 'ADMIN') { if (this.role === 'ADMIN') {
await fetch(`${API_BASE_URL}/api/admin/config`, { await fetch(`${API_BASE_URL}/api/admin/config`, {
method: 'POST', method: 'POST',

View File

@@ -28,6 +28,7 @@ public class UserController {
private final PostReadService postReadService; private final PostReadService postReadService;
private final UserVisitService userVisitService; private final UserVisitService userVisitService;
private final LevelService levelService; private final LevelService levelService;
private final JwtService jwtService;
@Value("${app.upload.check-type:true}") @Value("${app.upload.check-type:true}")
private boolean checkImageType; private boolean checkImageType;
@@ -73,10 +74,13 @@ public class UserController {
} }
@PutMapping("/me") @PutMapping("/me")
public ResponseEntity<UserDto> updateProfile(@RequestBody UpdateProfileDto dto, public ResponseEntity<?> updateProfile(@RequestBody UpdateProfileDto dto,
Authentication auth) { Authentication auth) {
User user = userService.updateProfile(auth.getName(), dto.getUsername(), dto.getIntroduction()); User user = userService.updateProfile(auth.getName(), dto.getUsername(), dto.getIntroduction());
return ResponseEntity.ok(toDto(user, auth)); return ResponseEntity.ok(Map.of(
"token", jwtService.generateToken(user.getUsername()),
"user", toDto(user, auth)
));
} }
@PostMapping("/me/signin") @PostMapping("/me/signin")

View File

@@ -122,9 +122,13 @@ public class SubscriptionService {
if (subscriberName == null || targetName == null || subscriberName.equals(targetName)) { if (subscriberName == null || targetName == null || subscriberName.equals(targetName)) {
return false; return false;
} }
User subscriber = userRepo.findByUsername(subscriberName).orElseThrow(); Optional<User> subscriber = userRepo.findByUsername(subscriberName);
User target = findUser(targetName).orElseThrow(); Optional<User> target = findUser(targetName);
return userSubRepo.findBySubscriberAndTarget(subscriber, target).isPresent(); if (subscriber.isEmpty() || target.isEmpty()) {
// 修改个人信息会出现,先不抛出错误
return false;
}
return userSubRepo.findBySubscriberAndTarget(subscriber.get(), target.get()).isPresent();
} }
public boolean isPostSubscribed(String username, Long postId) { public boolean isPostSubscribed(String username, Long postId) {