完成用户名修改

This commit is contained in:
柏码の讲师 2023-06-19 15:57:40 +08:00
parent 4a71703250
commit cb149257d0
5 changed files with 30 additions and 9 deletions

View File

@ -20,10 +20,13 @@ public class UserController {
}
@PostMapping("/save-info")
public RestBean<Void> saveInfo(@RequestBody AccountInfo info,
public RestBean<String> saveInfo(@RequestBody AccountInfo info,
@SessionAttribute("account") AccountUser user){
info.setUid(user.getId());
service.saveUserInfo(info);
return RestBean.success();
if(service.saveUserInfo(info)) {
return RestBean.success();
} else {
return RestBean.failure(400, "用户名称已被其他用户使用,无法修改");
}
}
}

View File

@ -29,4 +29,7 @@ public interface UserMapper {
qq=#{qq}, wx=#{wx}, blog=#{blog}, `desc`=#{desc}
""")
void saveInfo(AccountInfo info);
@Update("update db_account set username=#{name} where id=#{uid}")
void updateUsername(String name, int uid);
}

View File

@ -3,5 +3,5 @@ package com.example.service;
import com.example.entity.user.AccountInfo;
public interface UserService {
void saveUserInfo(AccountInfo info);
boolean saveUserInfo(AccountInfo info);
}

View File

@ -1,5 +1,6 @@
package com.example.service.impl;
import com.example.entity.auth.Account;
import com.example.entity.user.AccountInfo;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
@ -12,7 +13,14 @@ public class UserServiceImpl implements UserService {
UserMapper mapper;
@Override
public void saveUserInfo(AccountInfo info) {
public boolean saveUserInfo(AccountInfo info) {
Account account = mapper.findAccountByNameOrEmail(info.getUsername());
if(account == null) {
mapper.updateUsername(info.getUsername(), info.getUid());
} else if(account.getId() != info.getUid()){
return false;
}
mapper.saveInfo(info);
return true;
}
}

View File

@ -1,6 +1,9 @@
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";
import {useStore} from "@/stores";
const store = useStore()
const defaultError = () => ElMessage.error('发生了一些错误,请联系管理员')
const defaultFailure = (message) => ElMessage.warning(message)
@ -12,9 +15,11 @@ function post(url, data, success, type = 'x-www-form-urlencoded', failure = defa
},
withCredentials: true
}).then(({data}) => {
if(data.status === 401)
if(data.status === 401) {
localStorage.removeItem('user')
store.auth.user = undefined
router.push('/')
else if(data.success)
} else if(data.success)
success(data.message, data.status)
else
failure(data.message, data.status)
@ -25,9 +30,11 @@ function get(url, success, failure = defaultFailure, error = defaultError) {
axios.get(url, {
withCredentials: true
}).then(({data}) => {
if(data.status === 401)
if(data.status === 401) {
localStorage.removeItem('user')
store.auth.user = undefined
router.push('/')
else if(data.success)
} else if(data.success)
success(data.message, data.status)
else
failure(data.message, data.status)