实现用户信息修改同步到数据库功能
后端实现: - UserInfoService新增updateUsername和updateAvatarUrl方法 - UserController新增/updateUsername和/updateAvatar接口 - 支持修改用户名和头像URL - 数据同步保存到user_info表 前端实现: - api.js新增updateUsername和updateAvatar方法 - “我的”页面editUsername方法改为调用后端API - 修改成功后同步更新本地缓存 - 错误处理更友好 功能特点: - 修改用户名实时同步到数据库 - 更新本地缓存确保数据一致性 - “我的账号”页面自动加载最新用户信息 - 所有页面数据来源统一
This commit is contained in:
1 parent
aa0e7403d6
commit
5c8fcce387
4 files changed
+105
-8
No files matched your search
@@ -156,6 +156,54 @@ public class UserController {
|
||||
return result;
|
||||
}
|
||||
|
||||
@PostMapping("/updateUsername")
|
||||
public Map<String, Object> updateUsername(@RequestBody Map<String, Object> params) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
String userId = params.get("userId").toString();
|
||||
String username = params.get("username").toString();
|
||||
|
||||
UserInfo user = userInfoService.updateUsername(userId, username);
|
||||
if (user != null) {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("username", user.getUsername());
|
||||
result.put("success", true);
|
||||
result.put("data", data);
|
||||
} else {
|
||||
result.put("success", false);
|
||||
result.put("error", "用户不存在");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("error", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@PostMapping("/updateAvatar")
|
||||
public Map<String, Object> updateAvatar(@RequestBody Map<String, Object> params) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
String userId = params.get("userId").toString();
|
||||
String avatarUrl = params.get("avatarUrl").toString();
|
||||
|
||||
UserInfo user = userInfoService.updateAvatarUrl(userId, avatarUrl);
|
||||
if (user != null) {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("avatarUrl", user.getAvatarUrl());
|
||||
result.put("success", true);
|
||||
result.put("data", data);
|
||||
} else {
|
||||
result.put("success", false);
|
||||
result.put("error", "用户不存在");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("error", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@PostMapping("/permissionLog")
|
||||
public Map<String, Object> addPermissionLog(@RequestBody Map<String, Object> params) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
@@ -63,6 +63,24 @@ public class UserInfoService {
|
||||
return null;
|
||||
}
|
||||
|
||||
public UserInfo updateUsername(String userId, String username) {
|
||||
UserInfo user = userInfoRepository.findByUserId(userId);
|
||||
if (user != null) {
|
||||
user.setUsername(username);
|
||||
return userInfoRepository.save(user);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public UserInfo updateAvatarUrl(String userId, String avatarUrl) {
|
||||
UserInfo user = userInfoRepository.findByUserId(userId);
|
||||
if (user != null) {
|
||||
user.setAvatarUrl(avatarUrl);
|
||||
return userInfoRepository.save(user);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String generateUsername() {
|
||||
String prefix = "玄机用户";
|
||||
String suffix = generateRandomString(6);
|
||||
|
||||
@@ -173,14 +173,31 @@ export default {
|
||||
editable: true,
|
||||
placeholderText: '请输入用户名',
|
||||
content: this.username,
|
||||
success: (res) => {
|
||||
success: async (res) => {
|
||||
if (res.confirm && res.content) {
|
||||
this.username = res.content
|
||||
uni.setStorageSync('username', res.content)
|
||||
uni.showToast({
|
||||
title: '修改成功',
|
||||
icon: 'success'
|
||||
})
|
||||
try {
|
||||
await api.userApi.updateUsername(this.userId, res.content)
|
||||
this.username = res.content
|
||||
uni.setStorageSync('username', res.content)
|
||||
|
||||
const userInfoStr = uni.getStorageSync('userInfo')
|
||||
if (userInfoStr) {
|
||||
const userInfo = JSON.parse(userInfoStr)
|
||||
userInfo.username = res.content
|
||||
uni.setStorageSync('userInfo', JSON.stringify(userInfo))
|
||||
}
|
||||
|
||||
uni.showToast({
|
||||
title: '修改成功',
|
||||
icon: 'success'
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('修改用户名失败:', e)
|
||||
uni.showToast({
|
||||
title: '修改失败:' + (e.message || '未知错误'),
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -369,10 +369,24 @@ const userApi = {
|
||||
},
|
||||
updatePhone: (userId, phone) => {
|
||||
return request({
|
||||
url: '/user/phone',
|
||||
url: '/user/updatePhone',
|
||||
method: 'POST',
|
||||
data: { userId: userId, phone: phone }
|
||||
})
|
||||
},
|
||||
updateUsername: (userId, username) => {
|
||||
return request({
|
||||
url: '/user/updateUsername',
|
||||
method: 'POST',
|
||||
data: { userId: userId, username: username }
|
||||
})
|
||||
},
|
||||
updateAvatar: (userId, avatarUrl) => {
|
||||
return request({
|
||||
url: '/user/updateAvatar',
|
||||
method: 'POST',
|
||||
data: { userId: userId, avatarUrl: avatarUrl }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user