vue-element添加修改密码弹窗

1.新建修改密码vue文件CgPwd.vue

代码如下:

复制代码
<template>
    <!-- 修改密码界面 -->
    <el-dialog :title="$t('common.changePassword')" width="40%" :visible.sync="cgpwdVisible" :close-on-click-modal="false" :modal-append-to-body='false'>
        <el-form :model="dataForm" label-width="80px" :rules="dataFormRules" ref="dataForm" :size="size"
            label-position="right">
            <el-form-item label="旧密码" prop="oldpassword">
                <el-input v-model="dataForm.oldpassword" type="password" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newpassword">
                <el-input v-model="dataForm.newpassword" type="password" auto-complete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" style="margin-top: 5px;">
            <el-button :size="size" @click.native="cgpwdVisible = false">{{$t('action.cancel')}}</el-button>
            <el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">{{$t('action.submit')}}</el-button>
        </div>
    </el-dialog>
</template>

<script>
import axios from 'axios';
export default {
  data() {
    return {
      size: 'small',
            cgpwdVisible: false, // 编辑界面是否显示
      editLoading: false,   //载入图标
      //初始化数据
      dataForm: {
                oldpassword: '',
                newpassword: ''
      },
      //设置属性
      dataFormRules: {
                oldpassword: [
                    { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
                newpassword: [
                    { required: true, message: '请输入新密码', trigger: 'blur' }
        ]
      },
      //获取全局url
      baseUrl: this.global.baseUrl
    }
  },
  methods: {
   // 设置可见性
    setCgpwdVisible: function (cgpwdVisible) {
      this.cgpwdVisible = cgpwdVisible
    },
      // 提交请求
     submitForm: function () {
      //this.$refs.XXX 获取ref绑定的节点
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗?', '提示', {}).then(() => {
            let params = Object.assign({}, this.dataForm)
            params.user = 'admin'
            this.$api.cgpwd.pwdUpd(params).then((res) => {
              this.editLoading = true
              if(res.code == 200) {
                this.$message({ message: '操作成功' + res.msg, type: 'success' })
                this.cgpwdVisible = false       //隐藏该窗口
              } else {
                this.$message({message: '操作失败, ' + res.msg, type: 'error'})
              }
              this.editLoading = false
              this.$refs['dataForm'].resetFields()    //重置表单
            })
          })
        }
      })
     }
  },
//mounted: 在这发起后端请求,拿回数据,配合路由钩子做一些事情  (dom渲染完成 组件挂载完成 )
  mounted() {
        
    }
}
</script>

<style scoped>

</style>
复制代码

2.修改原有密码修改button

        <span class="main-operation-item">
          <el-button size="small" icon="fa fa-key" @click="showCgpwdDialog"> 修改密码</el-button>
        </span>

3.增加动态引用

    <!--修改密码界面-->
    <CgPwd ref="cgpwdDialog" @afterRestore="afterCgpwd"></CgPwd>

4.在原有vue文件script中进行修改

复制代码
//引入Cgpwd.vue文件
import CgPwd from "@/views/Sys/CgPwd"

export default {
  ...
  //在components中添加CgPwd,这样<CgPwd>才不会报错
  components:{
    ...
    CgPwd
  },
  ...
  methods: {
    ...
    //显示密码修改弹窗界面
    showCgpwdDialog: function() {
      this.$refs.cgpwdDialog.setCgpwdVisible(true)
    },
    ...
  },
  mounted() {
  }
}
复制代码

5.添加路由

新建文件cgpwd.js

复制代码
import axios from '../axios'

/*
 * 用户密码修改
 */

// 保存
export const pwdUpd = (data) => {
    return axios({
        url: '/user/pwdupd',
        method: 'post',
        data
    })
}
复制代码

6.在接口统一集成模块api.js中添加

import * as cgpwd from './moudules/cgpwd'

export default {
    ...
    cgpwd
}

 

7.在后台controller中添加代码

使用@RequestBody来接收body

复制代码
/**
* 修改密码
* @return
*/
@RequestMapping(value="/pwdupd")
public String pwdupd(@RequestBody String body) {
return body;
}
复制代码

 

8.添加权限例外

复制代码

import com.vuebg.admin.security.JwtAuthenticationFilter;
import com.vuebg.admin.security.JwtAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;


/**
 * Spring Security Config
 * @author
 * @date 2018-12-12
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 使用自定义身份验证组件
        auth.authenticationProvider(new JwtAuthenticationProvider(userDetailsService));
    }

    /**
     * 添加不需要进行权限验证的url
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 禁用 csrf, 由于使用的是JWT,我们这里不需要csrf
        http.cors().and().csrf().disable()
                .authorizeRequests()
                // 跨域预检请求
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                ...//修改密码
                .antMatchers("/user/pwdupd").permitAll()
                // 其他所有请求需要身份认证
                .anyRequest().authenticated();
        // 退出登录处理器
        http.logout().logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler());
        // token验证过滤器
        http.addFilterBefore(new JwtAuthenticationFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

}
复制代码

 

 

9.结果如下

posted @   suphowe  阅读(5888)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示