首页 > 其他分享 >Springboot+Vue集成个人中心、修改头像、数据联动、修改密码

Springboot+Vue集成个人中心、修改头像、数据联动、修改密码

时间:2023-05-26 11:03:09浏览次数:53  
标签:username Vue Springboot form res 修改 user message password


源码:https://gitee.com/xqnode/pure-design/tree/master

学习视频:https://www.bilibili.com/video/BV1U44y1W77D

开始讲解

个人信息的下拉菜单:

<el-dropdown style="width: 150px; cursor: pointer; text-align: right">
 <div style="display: inline-block">
    <img :src="user.avatarUrl" alt=""
         style="width: 30px; border-radius: 50%; position: relative; top: 10px; right: 5px">
    <span>{{ user.nickname }}</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
  </div>
  <el-dropdown-menu slot="dropdown" style="width: 100px; text-align: center">
    <el-dropdown-item style="font-size: 14px; padding: 5px 0">
      <router-link to="/password">修改密码</router-link>
    </el-dropdown-item>
    <el-dropdown-item style="font-size: 14px; padding: 5px 0">
      <router-link to="/person">个人信息</router-link>
    </el-dropdown-item>
    <el-dropdown-item style="font-size: 14px; padding: 5px 0">
      <span style="text-decoration: none" @click="logout">退出</span>
    </el-dropdown-item>
  </el-dropdown-menu>
</el-dropdown>

路由设置(静态)

Springboot+Vue集成个人中心、修改头像、数据联动、修改密码_vue

const manageRoute = { path: '/', name: 'Manage', component: () => import('../views/Manage.vue'), redirect: "/home", children: [
        { path: 'person', name: '个人信息', component: () => import('../views/Person.vue')},
        { path: 'password', name: '修改密码', component: () => import('../views/Password.vue')},
      ] }

新建 Person.vue页面

<template>
  <div>
    <el-card style="width: 600px">
      <el-form label-width="80px" size="small">
        <div style="text-align: center; margin: 10px 0">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:9090/file/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
          >
            <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <el-form-item label="用户名">
          <el-input v-model="form.username" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div style="text-align: center">
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-card>
  </div>
</template>

然后再写请求接口的JS:

<script>
export default {
  name: 'Person',
  data() {
    return {
      form: {},
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      const username = this.user.username
      if (!username) {
        this.$message.error("当前无法获取用户信息!")
        return
      }
      this.request.get("/user/username/" + username).then(res => {
        this.form = res.data
      })
    },
    save() {
      this.request.post("/user", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()

          this.$emit('refreshUser')
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAvatarSuccess(res) {
      // res就是文件的路径
      this.form.avatarUrl = res
    }
  }
}
</script>

我要求你去检查你的请求接口,这样可以防止404的错误!

Springboot+Vue集成个人中心、修改头像、数据联动、修改密码_spring boot_02

Manage.vue需要做的改动:

//  传入 user对象到 Header组件里面
 <Header :collapseBtnClass="collapseBtnClass" @asideCollapse="collapse" :user="user" />


data() {
	return {
		 user: {}
	}
}
created() {
    // 从后台获取最新的User数据
    this.getUser()
  },

// 在methos里面写一个函数, 获取用户的最新数据
    getUser() {
      let username = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")).username : ""
      if (username) {
        // 从后台获取User数据
        this.request.get("/user/username/" + username).then(res => {
          // 重新赋值后台的最新User数据
          this.user = res.data
        })
      }
    }

Header.vue需要做的改动

// 定义一个user属性接受从Manage.vue传进来的user对象
props: {
    user: Object
  },

// 然后在Header的这个地方使用:

 <el-dropdown style="width: 150px; cursor: pointer; text-align: right">
      <div style="display: inline-block">
        <img :src="user.avatarUrl" alt=""
             style="width: 30px; border-radius: 50%; position: relative; top: 10px; right: 5px">
        <span>{{ user.nickname }}</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
      </div>
      <el-dropdown-menu slot="dropdown" style="width: 100px; text-align: center">
        <el-dropdown-item style="font-size: 14px; padding: 5px 0">
          <router-link to="/password">修改密码</router-link>
        </el-dropdown-item>
        <el-dropdown-item style="font-size: 14px; padding: 5px 0">
          <router-link to="/person">个人信息</router-link>
        </el-dropdown-item>
        <el-dropdown-item style="font-size: 14px; padding: 5px 0">
          <span style="text-decoration: none" @click="logout">退出</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

Springboot+Vue集成个人中心、修改头像、数据联动、修改密码_vue_03

最后一步,在Person.vue里面写触发方法:

this.$emit('refreshUser')

Springboot+Vue集成个人中心、修改头像、数据联动、修改密码_修改密码_04


然后在 Manage里面做相应的修改即可:

<router-view @refreshUser="getUser" />

Springboot+Vue集成个人中心、修改头像、数据联动、修改密码_修改密码_05

修改密码:

<template>
  <el-card style="width: 500px;">
    <el-form label-width="120px" size="small" :model="form" :rules="rules" ref="pass">

      <el-form-item label="原密码" prop="password">
        <el-input v-model="form.password" autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="form.newPassword" autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmPassword">
        <el-input v-model="form.confirmPassword" autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "Password",
  data() {
    return {
      form: {},  // username, password, newPassword, confirmNewPassword 这4个属性
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      rules: {
        password: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { min: 3, message: '长度不少于3位', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 3, message: '长度不少于3位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, message: '长度不少于3位', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.form.username = this.user.username
  },
  methods: {
    save() {
      this.$refs.pass.validate((valid) => {
        if (valid) {  //合法
          if (this.form.newPassword !== this.form.confirmPassword) {
            this.$message.error("2次输入的新密码不相同")
            return false
          }
          this.request.post("/user/password", this.form).then(res => {
            if (res.code === '200') {
              this.$message.success("修改成功")
              this.$store.commit("logout")
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
  }
}
</script>

<style>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>

后台:

import lombok.Data;

@Data
public class UserPasswordDTO {
    private String username;
    private String phone;
    private String password;
    private String newPassword;
}

Controller接口

@PostMapping("/password")   //    /user/password
    public Result password(@RequestBody UserPasswordDTO userPasswordDTO) {
        userService.updatePassword(userPasswordDTO);
        return Result.success();
    }

业务类UserServiceImpl.java:

@Override
    public void updatePassword(UserPasswordDTO userPasswordDTO) {
        int update = userMapper.updatePassword(userPasswordDTO);
        if (update < 1) {
            throw new ServiceException(Constants.CODE_600, "密码错误");
        }
    }

数据Mapper层:

@Update("update sys_user set password = #{newPassword} where username = #{username} and password = #{password}")
    int updatePassword(UserPasswordDTO userPasswordDTO);

好的,至此 个人中心、修改头像、数据联动、修改密码四个内容集成完毕!撒花~


标签:username,Vue,Springboot,form,res,修改,user,message,password
From: https://blog.51cto.com/u_16130732/6354405

相关文章

  • Springboot集成百度地图实现定位打卡功能
    打卡sign表sqlCREATETABLE`sign`(`id`int(11)NOTNULLAUTO_INCREMENT,`user`varchar(255)COLLATEutf8mb4_unicode_ciDEFAULTNULLCOMMENT'用户名称',`location`varchar(255)COLLATEutf8mb4_unicode_ciDEFAULTNULLCOMMENT'打卡位置',`......
  • SpringBoot集成swagger-ui以及swagger分组显示
    文章目录1.swagger配置类2.使用swagger3.额外的学习经历大家好,这篇文章展示下如何在springboot项目中集成swagger-ui。有人说,这都是老生常谈,网上的例子数不胜数。确实swagger诞生至今已经很久了,但是在使用过程中我遇到一个问题,下面给大家分享下我的使用心得吧。1.swagger配置类第......
  • Springboot集成支付宝沙箱支付补充版本(退款功能)
    接上一次讲解:B站视频讲解:https://www.bilibili.com/video/BV1w44y1379q/这次的讲解涉及到完整的支付流程,大家请仔细查看!包括:支付宝沙箱支付+异步通知+退款功能正式版本的sdk通用版本SDK文档:https://opendocs.alipay.com/open/02np94<dependency><groupId>com.alipay.sdk......
  • 解决vue3自动引入element-plus后eslint警告
    配置vue.config.js......module.exports=defineConfig({......configureWebpack:{plugins:[AutoImport({resolvers:[ElementPlusResolver()],eslintrc:{enabled:true,filepath:"./.eslintrc-auto......
  • windows server2016 操作系统修改默认远程端口
    一、需求   远程端口,windows默认的3389.linux的22,这种都是知名端口,如果IP地址暴露,很可能会被攻击,这时候就需要更改端口号。二、操作步骤2.1打开注册表   快捷键WIN+R,命令行窗口输入regedit2.2进入以下路径  这里是默认端口,修改为自己除1024以后,以及未被......
  • Vue2路由嵌套是注意子路由path问题
    1、当子路由以/开始时,它会被视为根路由,并且会显示在URL的根路径中2、当子路由不以/开始时,它将被视为相对路径,相对于父路由的路径......
  • Vue CLI配置
    全局安装首先配置淘宝镜像cmd打开,输入npmconfigsetregistryhttps://registry.npm.taobao.org 安装命令npminstall-g@vue/cli发现报错如下,没有权限!!使用管理员模式。 重新开始,完美!! 然而输入查看vue 发现错误信息:系统禁止运行脚本接下来参考文章进行解除进......
  • Springboot+Mybatisplus+ClickHouse集成
    核心依赖引入<dependency><groupId>ru.yandex.clickhouse</groupId><artifactId>clickhouse-jdbc</artifactId><version>0.1.53</version></dependency><!--Mybati......
  • Android 修改 android/hardware/interfaces 下HIDL接口编译报异常问题解决
    最近要增加hostapd的一个HIDL接口,修改android/hardware/interfaces/wifi/hostapd/1.2/IHostapd.hal文件后编译报错如下:ERROR:[email protected]::IHostapdhashashacaed0a159a521bd4964e0fb8117320849109d3eeaff6a08b4d2506156ce6987whichdoesnotmatch......
  • vue3组件间通信
    props父子组件之间通信最好的方式//父组件<template><divclass="box"><h1>props:这里是父组件</h1><hr/><Child:money="money"></Child></div></template><scriptsetuplang="......