完成设置界面全部内容

This commit is contained in:
柏码の讲师 2023-06-19 17:39:33 +08:00
parent 600efd92c3
commit daeb8701f4
10 changed files with 98 additions and 15 deletions

View File

@ -2,11 +2,13 @@ package com.example.controller;
import com.example.entity.RestBean; import com.example.entity.RestBean;
import com.example.entity.user.AccountInfo; import com.example.entity.user.AccountInfo;
import com.example.entity.user.AccountPrivacy;
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 jakarta.validation.constraints.Pattern; import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -68,4 +70,17 @@ public class UserController {
return RestBean.failure(400, "原密码错误"); return RestBean.failure(400, "原密码错误");
} }
} }
@PostMapping("/save-privacy")
public RestBean<Void> savePrivacy(@RequestBody AccountPrivacy privacy,
@SessionAttribute("account") AccountUser user){
privacy.setUid(user.getId());
service.saveUserPrivacy(privacy);
return RestBean.success();
}
@GetMapping("/privacy")
public RestBean<AccountPrivacy> privacy(@SessionAttribute("account") AccountUser user){
return RestBean.success(service.userPrivacy(user.getId()));
}
} }

View File

@ -0,0 +1,14 @@
package com.example.entity.user;
import lombok.Data;
@Data
public class AccountPrivacy {
int uid;
boolean email;
boolean sex;
boolean phone;
boolean qq;
boolean wx;
boolean blog;
}

View File

@ -2,6 +2,7 @@ package com.example.mapper;
import com.example.entity.auth.Account; import com.example.entity.auth.Account;
import com.example.entity.user.AccountInfo; import com.example.entity.user.AccountInfo;
import com.example.entity.user.AccountPrivacy;
import com.example.entity.user.AccountUser; import com.example.entity.user.AccountUser;
import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
@ -44,4 +45,15 @@ public interface UserMapper {
@Update("update db_account set password=#{password} where id=#{uid}") @Update("update db_account set password=#{password} where id=#{uid}")
void updatePassword(String password, int uid); void updatePassword(String password, int uid);
@Insert("""
insert into db_account_privacy (uid, email, sex, phone, qq, wx, blog)
values (#{uid}, #{email}, #{sex}, #{phone}, #{qq}, #{wx}, #{blog})
on duplicate key update uid=#{uid}, email=#{email}, sex=#{sex},
phone=#{phone}, wx=#{wx}, qq=#{qq}, blog=#{blog}
""")
void savePrivacy(AccountPrivacy privacy);
@Select("select * from db_account_privacy where uid = #{uid}")
AccountPrivacy findPrivacyById(int uid);
} }

View File

@ -1,10 +1,13 @@
package com.example.service; package com.example.service;
import com.example.entity.user.AccountInfo; import com.example.entity.user.AccountInfo;
import com.example.entity.user.AccountPrivacy;
public interface UserService { public interface UserService {
boolean saveUserInfo(AccountInfo info); boolean saveUserInfo(AccountInfo info);
AccountInfo userInfo(int uid); AccountInfo userInfo(int uid);
boolean saveEmail(String email, int uid); boolean saveEmail(String email, int uid);
boolean changePassword(String old, String _new, int uid); boolean changePassword(String old, String _new, int uid);
void saveUserPrivacy(AccountPrivacy privacy);
AccountPrivacy userPrivacy(int uid);
} }

View File

@ -2,6 +2,7 @@ package com.example.service.impl;
import com.example.entity.auth.Account; import com.example.entity.auth.Account;
import com.example.entity.user.AccountInfo; import com.example.entity.user.AccountInfo;
import com.example.entity.user.AccountPrivacy;
import com.example.mapper.UserMapper; import com.example.mapper.UserMapper;
import com.example.service.UserService; import com.example.service.UserService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -52,4 +53,14 @@ public class UserServiceImpl implements UserService {
return false; return false;
} }
} }
@Override
public void saveUserPrivacy(AccountPrivacy privacy) {
mapper.savePrivacy(privacy);
}
@Override
public AccountPrivacy userPrivacy(int uid) {
return mapper.findPrivacyById(uid);
}
} }

View File

@ -1,8 +1,9 @@
<script setup> <script setup>
import {onMounted, 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 {get, post} from "@/net"; import {get, me, post} from "@/net";
import {ElMessage} from "element-plus"; import {ElMessage} from "element-plus";
import {useStore} from "@/stores";
const form = ref() const form = ref()
@ -70,6 +71,7 @@ const save = () => {
form.value.validate((isValid) => { form.value.validate((isValid) => {
if(isValid) { if(isValid) {
post('/api/user/save-info', infoForm, () => { post('/api/user/save-info', infoForm, () => {
me()
ElMessage.success("保存成功!") ElMessage.success("保存成功!")
}, 'json') }, 'json')
} else { } else {

View File

@ -1,6 +1,8 @@
<script setup> <script setup>
import {Message, Select} from "@element-plus/icons-vue"; import {Message, Select} from "@element-plus/icons-vue";
import {reactive} from "vue"; import {onMounted, reactive} from "vue";
import {get, post} from "@/net";
import {ElMessage} from "element-plus";
const privacyForm = reactive({ const privacyForm = reactive({
email: false, email: false,
@ -10,6 +12,22 @@ const privacyForm = reactive({
blog: false, blog: false,
sex: false sex: false
}) })
const save = () => {
post('/api/user/save-privacy', privacyForm,
() => ElMessage.success('保存成功'), 'json')
}
onMounted(() => {
get('/api/user/privacy', message => {
privacyForm.email = message.email
privacyForm.phone = message.phone
privacyForm.qq = message.qq
privacyForm.wx = message.wx
privacyForm.blog = message.blog
privacyForm.sex = message.sex
})
})
</script> </script>
<template> <template>
@ -38,7 +56,7 @@ const privacyForm = reactive({
<el-checkbox v-model="privacyForm.blog" label="是否公开展示展示我的博客" size="large" /> <el-checkbox v-model="privacyForm.blog" label="是否公开展示展示我的博客" size="large" />
</el-form-item> </el-form-item>
</el-form> </el-form>
<el-button type="success" :icon="Select">保存隐私设置</el-button> <el-button type="success" :icon="Select" @click="save">保存隐私设置</el-button>
</div> </div>
</template> </template>

View File

@ -1,8 +1,9 @@
<script setup> <script setup>
import {onMounted, reactive, ref} from "vue"; import {onMounted, reactive, ref} from "vue";
import {Lock, Message, Select} from "@element-plus/icons-vue"; import {Lock, Message, Select} from "@element-plus/icons-vue";
import {get, logout, post} from "@/net"; import {get, logout, me, post} from "@/net";
import {ElMessage} from "element-plus"; import {ElMessage} from "element-plus";
import {useStore} from "@/stores";
const securityForm = reactive({ const securityForm = reactive({
email: null, email: null,
@ -15,8 +16,10 @@ const emailForm = ref()
const saveEmail = () => { const saveEmail = () => {
emailForm.value.validate((isValid) => { emailForm.value.validate((isValid) => {
if(isValid) { if(isValid) {
post('/api/user/save-email', {email: securityForm.email}, post('/api/user/save-email', {email: securityForm.email}, () => {
() => ElMessage.success("保存成功!")) me()
ElMessage.success("保存成功!")
})
} else { } else {
ElMessage.warning('邮件格式有误,请正确填写') ElMessage.warning('邮件格式有误,请正确填写')
} }

View File

@ -50,4 +50,15 @@ const logout = () => {
}) })
} }
export { get, post, logout } const me = (index = false) => {
get('/api/user/me', (message) => {
store.auth.user = message
localStorage.setItem("user", JSON.stringify(message))
if(index)
router.push('/index')
}, () => {
store.auth.user = null
})
}
export { get, post, logout, me }

View File

@ -40,7 +40,7 @@
import {User, Lock} from '@element-plus/icons-vue' import {User, Lock} from '@element-plus/icons-vue'
import {reactive} from "vue"; import {reactive} from "vue";
import {ElMessage} from "element-plus"; import {ElMessage} from "element-plus";
import {get, post} from "@/net"; import {get, me, post} from "@/net";
import router from "@/router"; import router from "@/router";
import {useStore} from "@/stores"; import {useStore} from "@/stores";
@ -62,13 +62,7 @@ const login = () => {
remember: form.remember remember: form.remember
}, (message) => { }, (message) => {
ElMessage.success(message) ElMessage.success(message)
get('/api/user/me', (message) => { me(true)
store.auth.user = message
localStorage.setItem("user", JSON.stringify(message))
router.push('/index')
}, () => {
store.auth.user = null
})
}) })
} }
} }