携带参数的退出功能

目录

1.常用的实现用户退出

分析

思路

步骤 

小结

2.退出再进入能回到原来的页面 - 分析

分析

1.登录成功后进入指定的页面

2.退出系统时回传当前路径


1.常用的实现用户退出

分析

弹框提示用户是否确定退出

  • 如果点击确认

          1.  首先看看接口文档有没有退出接口,有的话就调用(注意并不是所有的项目,都有退出接口)

          2. 退出接口成功调用之后清空本地用户信息(token、userInfo)

          3. 如果需要携带必要参数跳回到登录页面准备重新登录操作

  • 如果点击取消 不做任何操作

思路

步骤 

1.在 action里面封装一个 用来退出的代码

userLogout(context){
    context.commit('removeUserInfo')
    context.commit('removeToken')
}

2.调用action 当用户点击退出时,显示确认对话框


      logout() {
      // 弹层询问,是否退出
      this.$confirm('你确定要离开吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await this.$store.dispatch('user/logout')
        this.$router.push(`/login`)
      }).catch(() => {
        // 用户取消退出
      })
    }

3.补充action

store/modules/user.js

logout(context) {
      // 删除token
      context.commit('reomveToken')
      // 删除userInfo
      context.commit('reomveUserInfo')
    }

查看效果 

在vue调试工具中,检查本地用户信息,检查是否已经清空了

小结

退出之前要询问,confirm对话框

退出之前要清空数据,由于这个数据是保存在vuex中的,使用action来删除

2.退出再进入能回到原来的页面 - 分析

分析

从http://localhost:9528/#/a中退出之后,随后立即登录,还能再回到http://localhost:9528/#/a这里

  1. 登录成功之后,进入指定的目标页面(不要每次都进入主页)
  2. 用户退出,跳入登录页时,携带目标页面地址告诉登录页

1.登录成功后进入指定的页面

在访问login这个页面时,可以在地址栏中补充一个查询字符串来指定登陆成功之后要去到的页面。

例如

# 访问登录页,并且告诉它,登录成功之后要进入 /abc
http://localhost:9528/#/login?return_url=/abc

实现代码

login/index.vue中的代码

async doLogin() {
      try {
        // 在组件中调用带命名空间的action

        // dispatch是异步的,需要加async await
        await this.$store.dispatch('user/userLogin', this.loginForm)
        // 登录成功,路由跳转
+        this.$router.push(this.$route.query.return_url || '/')
      } catch (err) {
        alert('用户登录,失败')
        console.log('用户登录,失败', err)
      }
    },

2.退出系统时回传当前路径

在退出时,跳回login时,回传当前的路径

实现代码

logout() {
      // 弹层询问,是否退出
      this.$confirm('你确定要离开吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确认
        // 删除信息
        await this.$store.dispatch('user/userLogout')
        // 去到登录页
        // this.$router.push('/login?return_url=当前的路径')
        // 跳转路由-回登陆
        // 如何获取当前页面的地址 : this.$route.fullPath
        // this.$route.path只有路径的信息
        // this.$route.fullPath:路径+查询参数的信息
+       this.$router.push('/login?return_url=' + encodeURIComponent(this.$route.fullPath))
      }).catch(() => {

      })
    }
  • $route.path:只有路径的信息
  • $route.fullPath:路径+查询参数的信息
  • return_url: 这个名字是自己约定的,它要和login/index.vue中跳转代码保持一致。
  • encodeURIComponent: 是js的内置API,用来对url进行编码

 this.$router.push('/login?return_url=' + encodeURIComponent(this.$route.fullPath))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MultipartFile是Spring框架中用于处理HTTP请求中的文件上传的对象。它通常用于处理表单中包含文件的情况。 MultipartFile对象本身并不能直接携带参数,因为它只是表示一个上传的文件。如果你需要在文件上传的同时传递其他参数,可以考虑以下两种方式: 1. 将参数一起封装在一个表单中,然后一起提交。在服务器端,你可以通过@RequestParam注解或者@RequestBody注解来接收参数。 例如,前端的表单可以这样设计: ```html <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="text" name="param1" /> <input type="text" name="param2" /> <button type="submit">上传</button> </form> ``` 在后端的Controller中,可以这样接收参数: ```java @PostMapping("/upload") public String upload(@RequestParam("file") MultipartFile file, @RequestParam("param1") String param1, @RequestParam("param2") String param2) { // 处理文件上传和参数 // ... } ``` 2. 将参数封装在一个JSON对象中,然后一起传递。在服务器端,你可以通过@RequestBody注解来接收该JSON对象。 例如,前端可以使用JavaScript将文件和其他参数封装成一个JSON对象,并通过Ajax请求发送给服务器: ```javascript var formData = new FormData(); formData.append('file', file); formData.append('param1', param1); formData.append('param2', param2); $.ajax({ url: '/upload', type: 'POST', data: formData, processData: false, contentType: false, success: function(response) { // 处理上传成功后的逻辑 } }); ``` 在后端的Controller中,可以这样接收参数: ```java @PostMapping("/upload") public String upload(@RequestBody Map<String, Object> request) { MultipartFile file = (MultipartFile) request.get("file"); String param1 = (String) request.get("param1"); String param2 = (String) request.get("param2"); // 处理文件上传和参数 // ... } ``` 这样,你就可以同时上传文件和携带参数了。记得在前端和后端都进行相应的参数验证和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值