完成个人信息设置

This commit is contained in:
柏码の讲师 2023-06-19 16:27:16 +08:00
parent cb149257d0
commit fdbd412881
7 changed files with 51 additions and 12 deletions

View File

@ -5,6 +5,7 @@ import com.example.entity.user.AccountInfo;
import com.example.entity.user.AccountUser;
import com.example.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@RestController
@ -20,13 +21,19 @@ public class UserController {
}
@PostMapping("/save-info")
public RestBean<String> saveInfo(@RequestBody AccountInfo info,
@SessionAttribute("account") AccountUser user){
public RestBean<String> saveInfo(@RequestBody @Validated AccountInfo info,
@SessionAttribute("account") AccountUser user){
info.setUid(user.getId());
if(service.saveUserInfo(info)) {
user.setUsername(info.getUsername());
return RestBean.success();
} else {
return RestBean.failure(400, "用户名称已被其他用户使用,无法修改");
}
}
@GetMapping("/info")
public RestBean<AccountInfo> info(@SessionAttribute("account") AccountUser user){
return RestBean.success(service.userInfo(user.getId()));
}
}

View File

@ -1,15 +1,22 @@
package com.example.entity.user;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
@Data
public class AccountInfo {
int uid;
@Length(min = 2, max = 8)
String username;
String sex;
@Length(max = 11)
String phone;
@Length(max = 11)
String qq;
@Length(max = 30)
String wx;
@Length(max = 50)
String blog;
@Length(max = 500)
String desc;
}

View File

@ -19,12 +19,14 @@ public class AuthorizeInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
User user = (User)authentication.getPrincipal();
String username = user.getUsername();
AccountUser account = mapper.findAccountUserByNameOrEmail(username);
request.getSession().setAttribute("account", account);
if(request.getSession().getAttribute("account") == null){
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
User user = (User)authentication.getPrincipal();
String username = user.getUsername();
AccountUser account = mapper.findAccountUserByNameOrEmail(username);
request.getSession().setAttribute("account", account);
}
return true;
}
}

View File

@ -32,4 +32,7 @@ public interface UserMapper {
@Update("update db_account set username=#{name} where id=#{uid}")
void updateUsername(String name, int uid);
@Select("select * from db_account_info left join db_account on id = uid where id = #{uid}")
AccountInfo findInfoById(int uid);
}

View File

@ -4,4 +4,5 @@ import com.example.entity.user.AccountInfo;
public interface UserService {
boolean saveUserInfo(AccountInfo info);
AccountInfo userInfo(int uid);
}

View File

@ -23,4 +23,9 @@ public class UserServiceImpl implements UserService {
mapper.saveInfo(info);
return true;
}
@Override
public AccountInfo userInfo(int uid) {
return mapper.findInfoById(uid);
}
}

View File

@ -1,7 +1,7 @@
<script setup>
import {reactive, ref} from "vue";
import {onMounted, reactive, ref} from "vue";
import {Select} from "@element-plus/icons-vue";
import {post} from "@/net";
import {get, post} from "@/net";
import {ElMessage} from "element-plus";
const form = ref()
@ -57,13 +57,13 @@ const rules = {
}
const infoForm = reactive({
username: '',
username: null,
desc: '',
phone: '',
qq: '',
wx: '',
blog: '',
sex: 'male'
sex: ''
})
const save = () => {
@ -77,6 +77,20 @@ const save = () => {
}
})
}
onMounted(() => {
if(infoForm.username == null) {
get('/api/user/info', (message) => {
infoForm.username = message.username
infoForm.desc = message.desc
infoForm.phone = message.phone
infoForm.qq = message.qq
infoForm.wx = message.wx
infoForm.blog = message.blog
infoForm.sex = message.sex ? message.sex : 'male'
})
}
})
</script>
<template>