changePassword.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <div id="changePassword" v-loading="isLoging">
  3. <el-dialog
  4. title="修改密码"
  5. width="40%"
  6. top="2rem"
  7. :close-on-click-modal="false"
  8. :visible.sync="showDialog"
  9. :destroy-on-close="true"
  10. @close="close()"
  11. >
  12. <div id="shared" style="margin-right: 20px;">
  13. <el-form ref="passwordForm" :rules="rules" status-icon label-width="80px">
  14. <el-form-item label="旧密码" prop="oldPassword" >
  15. <el-input v-model="oldPassword" autocomplete="off"></el-input>
  16. </el-form-item>
  17. <el-form-item label="新密码" prop="newPassword" >
  18. <el-input v-model="newPassword" autocomplete="off"></el-input>
  19. </el-form-item>
  20. <el-form-item label="确认密码" prop="confirmPassword">
  21. <el-input v-model="confirmPassword" autocomplete="off"></el-input>
  22. </el-form-item>
  23. <el-form-item>
  24. <div style="float: right;">
  25. <el-button type="primary" @click="onSubmit">保存</el-button>
  26. <el-button @click="close">取消</el-button>
  27. </div>
  28. </el-form-item>
  29. </el-form>
  30. </div>
  31. </el-dialog>
  32. </div>
  33. </template>
  34. <script>
  35. import crypto from 'crypto'
  36. export default {
  37. name: "changePassword",
  38. props: {},
  39. computed: {},
  40. created() {},
  41. data() {
  42. let validatePass0 = (rule, value, callback) => {
  43. if (value === '') {
  44. callback(new Error('请输入旧密码'));
  45. } else {
  46. callback();
  47. }
  48. };
  49. let validatePass1 = (rule, value, callback) => {
  50. if (value === '') {
  51. callback(new Error('请输入新密码'));
  52. } else {
  53. if (this.confirmPassword !== '') {
  54. this.$refs.passwordForm.validateField('confirmPassword');
  55. }
  56. callback();
  57. }
  58. };
  59. let validatePass2 = (rule, value, callback) => {
  60. if (this.confirmPassword === '') {
  61. callback(new Error('请再次输入密码'));
  62. } else if (this.confirmPassword !== this.newPassword) {
  63. callback(new Error('两次输入密码不一致!'));
  64. } else {
  65. callback();
  66. }
  67. };
  68. return {
  69. oldPassword: null,
  70. newPassword: null,
  71. confirmPassword: null,
  72. showDialog: false,
  73. isLoging: false,
  74. rules: {
  75. oldPassword: [{ required: true, validator: validatePass0, trigger: "blur" }],
  76. newPassword: [{ required: true, validator: validatePass1, trigger: "blur" }],
  77. confirmPassword: [{ required: true, validator: validatePass2, trigger: "blur" }],
  78. },
  79. };
  80. },
  81. methods: {
  82. openDialog: function () {
  83. this.showDialog = true;
  84. },
  85. onSubmit: function () {
  86. this.$axios({
  87. method: 'post',
  88. url:"/api/user/changePassword",
  89. params: {
  90. oldpassword: crypto.createHash('md5').update(this.oldPassword, "utf8").digest('hex'),
  91. password: this.newPassword
  92. }
  93. }).then((res)=> {
  94. if (res.data === "success"){
  95. this.$message({
  96. showClose: true,
  97. message: '修改成功,请重新登录',
  98. type: 'success'
  99. });
  100. this.showDialog = false;
  101. setTimeout(()=>{
  102. // 删除cookie,回到登录页面
  103. this.$cookies.remove("session");
  104. this.$router.push('/login');
  105. this.sseSource.close();
  106. },800)
  107. }
  108. }).catch((error)=> {
  109. console.error(error)
  110. });
  111. },
  112. close: function () {
  113. this.showDialog = false;
  114. this.oldPassword = null;
  115. this.newPassword = null;
  116. this.confirmPassword = null;
  117. },
  118. },
  119. };
  120. </script>