浪花 - 用户信息展示+更新

news/2024/7/10 1:31:06 标签: 前端, vue, javascript, java

1. 用户登录获取登录凭证

  • 已登录的用户才能获取个人信息
  • 发送 Aixos 请求登录
const user = ref();

onMounted(async () => {
  const res = await myAxios.get('/user/current');
  if (res.code === 0) {
    console.log("获取用户信息成功");
    user.value = res.data;
  } else {
    console.log("获取用户信息失败")
  }
})

2. 获取登录用户的个人信息

  • 点击个人页时加载个人信息

踩坑记录:前端发送请求后端无法识别已登录用户

  • 原因:前端发送请求时没有携带登录时后端返回的 Cookie
  • 解决:给 Axios 加上携带 Cookie 的配置,表示请求要携带 Cookie
javascript>javascript">myAxios.defaults.withCredentials = true;
  • 携带 Cookie 成功

  •  获取当前登录用户信息:后端通过 Cookie 识别到前端的登录态,获取成功✔

 3. 展示用户信息

<template>
  <template v-if="user">
    <van-cell title="昵称" is-link :value="user.username" @click="toEdit('username', '昵称',user.username)"/>
    <van-cell title="账户" :value="user.userAccount"/>
    <van-cell title="头像" :value="user.avatarUrl" @click="toEdit('avatarUrl', '头像',user.avatarUrl)">
      <img :src="user.avatarUrl"/>
    </van-cell>
    <van-cell title="性别" is-link :value="user.gender" @click="toEdit('gender', '性别',user.gender)"/>
    <van-cell title="手机号" is-link :value="user.phone" @click="toEdit('phone', '手机号',user.phone)"/>
    <van-cell title="邮箱" is-link :value="user.email" @click="toEdit('email', '邮箱',user.email)"/>
    <van-cell title="星球编号" :value="user.planetCode" />
    <van-cell title="标签信息" is-link :value="user.tags" @click="toEdit('tags', '标签信息',user.tags)" />
    <van-cell title="注册时间" :value="user.createTime" />
  </template>
</template>

4. 更新用户信息

前端

  •  点击提交按钮,发送更新用户个人信息请求
  • 收到后端更新成功的响应,返回信息展示页面
const onSubmit = async () => {
  const currentUser = await getCurrentUser();
  if (!currentUser) {
    console.log("用户未登录")
    return;
  }
  // 发送更新请求
  const res = await myAxios.post('/user/update', {
    'id': currentUser.id,
    [editUser.value.editKey as string]: editUser.value.currentValue,
  })
  if (res.code === 0 && res.data > 0) {
    router.back();
    console.log("修改成功!");
  } else {
    console.log("修改失败!");
  }
};

后端:

  • 校验用户权限,满足权限则执行 UPDATE 语句更新个人信息

  • 完善逻辑(校验参数):前端传来的参数为空时,直接抛出异常,不执行 UPDATE 语句
java">
    /**
     * 更新用户信息
     * @param user 要更新的用户
     * @param loginUser 当前登录用户
     * @return
     */
    @Override
    public int updateUser(User user, User loginUser) {
        long userId = user.getId();
        if (userId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // TODO 如果用户没有传递任何需要更新的值,直接报错,不执行 UPDATE 语句
        if(user == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 管理员:更新任意用户的信息
        // 普通用户:只允许更新自己的信息
        if (!isAdmin(loginUser) && userId != loginUser.getId()) {
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        User oldUser = userMapper.selectById(userId);
         if (oldUser == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return userMapper.updateById(user);
    }

http://www.niftyadmin.cn/n/5336893.html

相关文章

【Spring 篇】MyBatis核心配置文件解密:数据之门的守护精灵

欢迎来到MyBatis的幕后花絮&#xff0c;今天我们将深入解析MyBatis的核心配置文件&#xff0c;这个神秘的数据之门的守护精灵。这份配置文件是连接你的应用程序和数据库之间的纽带&#xff0c;也是整个MyBatis舞台背后的幕后工作者。在这篇博客中&#xff0c;我们将揭开核心配置…

Javaweb之SpringBootWeb案例员工管理之新增员工的详细解析

SpringBootWeb案例 前面我们已经实现了员工信息的条件分页查询以及删除操作。 关于员工管理的功能&#xff0c;还有两个需要实现&#xff1a; 新增员工 修改员工 首先我们先完成"新增员工"的功能开发&#xff0c;再完成"修改员工"的功能开发。而在&q…

DP活动:以太网HMI线下培训RA6M3 HMI Board[MQTT Squareline LVGL]

以太网HMI线下培训-环境准备 这是官方社群的文档&#xff1a;【腾讯文档】以太网线下培训&#xff08;HMI-Board&#xff09;所有教程都在这~ https://docs.qq.com/doc/DY0FIWFVuTEpORlNn R A 6 M 3 H M I − B o a r d \textcolor{#4183c4}{RA6M3 HMI-Board} RA6M3HMI−Board…

领略指针之妙

&#x1d649;&#x1d65e;&#x1d658;&#x1d65a;!!&#x1f44f;&#x1f3fb;‧✧̣̥̇‧✦&#x1f44f;&#x1f3fb;‧✧̣̥̇‧✦ &#x1f44f;&#x1f3fb;‧✧̣̥̇:Solitary-walk ⸝⋆ ━━━┓ - 个性标签 - &#xff1a;来于“云”的“羽球人”。…

假设检验:以样本服从二项分布举例

目录 假设检验一、假设检验的思想二、假设检验的基本步骤1. 确定要进行检验的假设2. 选择检验统计量3. 确定用于做决策的拒绝域4. 求出检验统计量的值5. 查看样本结果是否位于拒绝域内6. 做出决策 三、举例说明例子1——某公司治疗打鼾例子2——女士品茶的故事 假设检验 一、假…

麒麟V10挂载iso,配置yum源

本文介绍yum 如何挂载本地镜像源 1) 拷贝镜像到本地 2) 执行以下命令&#xff1a; # mount -o loop 镜像路径及镜像名字 /mnt&#xff08;或 media&#xff09; 挂载前 挂载后 3) 进入/etc/yum.repos.d&#xff08;yum.repos.d 是一个目录&#xff0c;该目录是分析 RPM 软件…

03 OSPF

参考文章 1 初步认识OSPF的大致内容(第三课)-CSDN博客 2

【设计模式 创建型】单例模式

类的单例设计模式&#xff0c;就是采取一定的方法保证在整个的软件系统中&#xff0c;对某个类只能存在一个对象实例&#xff0c;并且该类只提供一个取得其对象实例的方法&#xff08;静态方法&#xff09; 指一个类只有一个实例&#xff0c;且该类能自行创建这个实例的一种模…