当前位置:   article > 正文

Spring Boot 笔记 029 用户模块

Spring Boot 笔记 029 用户模块

1.1 用户信息需要在多个链接使用,所以需要用pinia持久化

1.1.1 定义store

  1. import {defineStore} from 'pinia'
  2. import {ref} from 'vue'
  3. const useUserInfoStore = defineStore('userInfo',()=>{
  4. //定义状态相关的内容
  5. const info = ref({})
  6. const setInfo = (newInfo)=>{
  7. info.value = newInfo
  8. }
  9. const removeInfo = ()=>{
  10. info.value = {}
  11. }
  12. return {info,setInfo,removeInfo}
  13. },{persist:true})
  14. export default useUserInfoStore;

1.1.2 接口函数

  1. import {userInfoService} from '@/api/user.js'
  2. import useUserInfoStore from '@/stores/userInfo.js'
  3. import {useTokenStore} from '@/stores/token.js'
  4. const tokenStore = useTokenStore();
  5. const userInfoStore = useUserInfoStore();
  6. //调用函数,获取用户详细信息
  7. const getUserInfo = async()=>{
  8. //调用接口
  9. let result = await userInfoService();
  10. //数据存储到pinia中
  11. userInfoStore.setInfo(result.data);
  12. }
  13. getUserInfo();

1.1.3 layout.vue中调用接口显示昵称和头像

1.2 下拉菜单

1.2.1 绑定事件

1.2.2 编写函数

  1. //条目被点击后,调用的函数
  2. import {useRouter} from 'vue-router'
  3. const router = useRouter();
  4. import {ElMessage,ElMessageBox} from 'element-plus'
  5. const handleCommand = (command)=>{
  6. //判断指令
  7. if(command === 'logout'){
  8. //退出登录
  9. ElMessageBox.confirm(
  10. '您确认要退出吗?',
  11. '温馨提示',
  12. {
  13. confirmButtonText: '确认',
  14. cancelButtonText: '取消',
  15. type: 'warning',
  16. }
  17. )
  18. .then(async () => {
  19. //退出登录
  20. //1.清空pinia中存储的token以及个人信息
  21. tokenStore.removeToken()
  22. userInfoStore.removeInfo()
  23. //2.跳转到登录页面
  24. router.push('/login')
  25. ElMessage({
  26. type: 'success',
  27. message: '退出登录成功',
  28. })
  29. })
  30. .catch(() => {
  31. ElMessage({
  32. type: 'info',
  33. message: '用户取消了退出登录',
  34. })
  35. })
  36. }else{
  37. //路由
  38. router.push('/user/'+command)
  39. }
  40. }

1.3 基本资料的修改

1.3.1 定义接口

1.3.2 页面搭建

1.3.3 数据回显

1.3.4 调用接口

  1. <script setup>
  2. import { ref } from 'vue'
  3. import useUserInfoStore from '@/stores/userInfo.js'
  4. const userInfoStore = useUserInfoStore();
  5. const userInfo = ref({...userInfoStore.info})
  6. const rules = {
  7. nickname: [
  8. { required: true, message: '请输入用户昵称', trigger: 'blur' },
  9. {
  10. pattern: /^\S{2,10}$/,
  11. message: '昵称必须是2-10位的非空字符串',
  12. trigger: 'blur'
  13. }
  14. ],
  15. email: [
  16. { required: true, message: '请输入用户邮箱', trigger: 'blur' },
  17. { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
  18. ]
  19. }
  20. //修改个人信息
  21. import {userInfoUpdateService} from '@/api/user.js'
  22. import {ElMessage} from 'element-plus'
  23. const updateUserInfo = async ()=>{
  24. //调用接口
  25. let result = await userInfoUpdateService(userInfo.value);
  26. ElMessage.success(result.msg? result.msg : '修改成功');
  27. //修改pinia中的个人信息
  28. userInfoStore.setInfo(userInfo.value)
  29. }
  30. </script>
  31. <template>
  32. <el-card class="page-container">
  33. <template #header>
  34. <div class="header">
  35. <span>基本资料</span>
  36. </div>
  37. </template>
  38. <el-row>
  39. <el-col :span="12">
  40. <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
  41. <el-form-item label="登录名称">
  42. <el-input v-model="userInfo.username" disabled></el-input>
  43. </el-form-item>
  44. <el-form-item label="用户昵称" prop="nickname">
  45. <el-input v-model="userInfo.nickname"></el-input>
  46. </el-form-item>
  47. <el-form-item label="用户邮箱" prop="email">
  48. <el-input v-model="userInfo.email"></el-input>
  49. </el-form-item>
  50. <el-form-item>
  51. <el-button type="primary" @click="updateUserInfo">提交修改</el-button>
  52. </el-form-item>
  53. </el-form>
  54. </el-col>
  55. </el-row>
  56. </el-card>
  57. </template>

1.4修改头像

1.4.1 编辑页面

1.4.2 回显头像

1.4.3 图片上传

  1. <script setup>
  2. import { Plus, Upload } from '@element-plus/icons-vue'
  3. import {ref} from 'vue'
  4. // import avatar from '@/assets/default.png'
  5. const uploadRef = ref()
  6. import {useTokenStore} from '@/stores/token.js'
  7. const tokenStore = useTokenStore();
  8. import useUserInfoStore from '@/stores/userInfo.js'
  9. const userInfoStore = useUserInfoStore();
  10. //用户头像地址
  11. const imgUrl= ref(userInfoStore.info.userPic)
  12. //图片上传成功的回调函数
  13. const uploadSuccess = (result)=>{
  14. imgUrl.value = result.data;
  15. }
  16. import {userAvatarUpdateService} from '@/api/user.js'
  17. import {ElMessage} from 'element-plus'
  18. //头像修改
  19. const updateAvatar = async ()=>{
  20. //调用接口
  21. let result = await userAvatarUpdateService(imgUrl.value);
  22. ElMessage.success(result.msg? result.msg:'修改成功')
  23. //修改pinia中的数据
  24. userInfoStore.info.userPic = imgUrl.value
  25. }
  26. </script>
  27. <template>
  28. <el-card class="page-container">
  29. <template #header>
  30. <div class="header">
  31. <span>更换头像</span>
  32. </div>
  33. </template>
  34. <el-row>
  35. <el-col :span="12">
  36. <el-upload
  37. ref="uploadRef"
  38. class="avatar-uploader"
  39. :show-file-list="false"
  40. :auto-upload="true"
  41. action="/api/upload"
  42. name="file"
  43. :headers="{'Authorization':tokenStore.token}"
  44. :on-success="uploadSuccess"
  45. >
  46. <img v-if="imgUrl" :src="imgUrl" class="avatar" />
  47. <img v-else width="278" />
  48. </el-upload>
  49. <br />
  50. <el-button type="primary" :icon="Plus" size="large" @click="uploadRef.$el.querySelector('input').click()">
  51. 选择图片
  52. </el-button>
  53. <el-button type="success" :icon="Upload" size="large" @click="updateAvatar">
  54. 上传头像
  55. </el-button>
  56. </el-col>
  57. </el-row>
  58. </el-card>
  59. </template>
  60. <style lang="scss" scoped>
  61. .avatar-uploader {
  62. :deep() {
  63. .avatar {
  64. width: 278px;
  65. height: 278px;
  66. display: block;
  67. }
  68. .el-upload {
  69. border: 1px dashed var(--el-border-color);
  70. border-radius: 6px;
  71. cursor: pointer;
  72. position: relative;
  73. overflow: hidden;
  74. transition: var(--el-transition-duration-fast);
  75. }
  76. .el-upload:hover {
  77. border-color: var(--el-color-primary);
  78. }
  79. .el-icon.avatar-uploader-icon {
  80. font-size: 28px;
  81. color: #8c939d;
  82. width: 278px;
  83. height: 278px;
  84. text-align: center;
  85. }
  86. }
  87. }
  88. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/125545
推荐阅读
相关标签
  

闽ICP备14008679号