完成个人信息设置

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.entity.user.AccountUser;
import com.example.service.UserService; import com.example.service.UserService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
@ -20,13 +21,19 @@ public class UserController {
} }
@PostMapping("/save-info") @PostMapping("/save-info")
public RestBean<String> saveInfo(@RequestBody AccountInfo info, public RestBean<String> saveInfo(@RequestBody @Validated AccountInfo info,
@SessionAttribute("account") AccountUser user){ @SessionAttribute("account") AccountUser user){
info.setUid(user.getId()); info.setUid(user.getId());
if(service.saveUserInfo(info)) { if(service.saveUserInfo(info)) {
user.setUsername(info.getUsername());
return RestBean.success(); return RestBean.success();
} else { } else {
return RestBean.failure(400, "用户名称已被其他用户使用,无法修改"); 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; package com.example.entity.user;
import lombok.Data; import lombok.Data;
import org.hibernate.validator.constraints.Length;
@Data @Data
public class AccountInfo { public class AccountInfo {
int uid; int uid;
@Length(min = 2, max = 8)
String username; String username;
String sex; String sex;
@Length(max = 11)
String phone; String phone;
@Length(max = 11)
String qq; String qq;
@Length(max = 30)
String wx; String wx;
@Length(max = 50)
String blog; String blog;
@Length(max = 500)
String desc; String desc;
} }

View File

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

View File

@ -32,4 +32,7 @@ public interface UserMapper {
@Update("update db_account set username=#{name} where id=#{uid}") @Update("update db_account set username=#{name} where id=#{uid}")
void updateUsername(String name, int 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 { public interface UserService {
boolean saveUserInfo(AccountInfo info); boolean saveUserInfo(AccountInfo info);
AccountInfo userInfo(int uid);
} }

View File

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

View File

@ -1,7 +1,7 @@
<script setup> <script setup>
import {reactive, ref} from "vue"; import {onMounted, reactive, ref} from "vue";
import {Select} from "@element-plus/icons-vue"; import {Select} from "@element-plus/icons-vue";
import {post} from "@/net"; import {get, post} from "@/net";
import {ElMessage} from "element-plus"; import {ElMessage} from "element-plus";
const form = ref() const form = ref()
@ -57,13 +57,13 @@ const rules = {
} }
const infoForm = reactive({ const infoForm = reactive({
username: '', username: null,
desc: '', desc: '',
phone: '', phone: '',
qq: '', qq: '',
wx: '', wx: '',
blog: '', blog: '',
sex: 'male' sex: ''
}) })
const save = () => { 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> </script>
<template> <template>