changePassword.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. pattern: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,.\/]).{8,20}$/,
  78. message: "密码长度在8-20位之间,由字母+数字+特殊字符组成",
  79. },],
  80. confirmPassword: [{ required: true, validator: validatePass2, trigger: "blur" }],
  81. },
  82. };
  83. },
  84. methods: {
  85. openDialog: function () {
  86. this.showDialog = true;
  87. },
  88. onSubmit: function () {
  89. this.$axios({
  90. method: 'post',
  91. url:"./api/user/changePassword",
  92. params: {
  93. oldPassword: crypto.createHash('md5').update(this.oldPassword, "utf8").digest('hex'),
  94. password: this.newPassword
  95. }
  96. }).then((res)=> {
  97. if (res.data.code === 0) {
  98. this.$message({
  99. showClose: true,
  100. message: '修改成功,请重新登录',
  101. type: 'success'
  102. });
  103. this.showDialog = false;
  104. setTimeout(()=>{
  105. // 删除cookie,回到登录页面
  106. this.$cookies.remove("session");
  107. this.$router.push('/login');
  108. this.sseSource.close();
  109. },800)
  110. }else {
  111. this.$message({
  112. showClose: true,
  113. message: '修改密码失败,是否已登录(接口鉴权关闭无法修改密码)',
  114. type: 'error'
  115. });
  116. }
  117. }).catch((error)=> {
  118. console.error(error)
  119. });
  120. },
  121. close: function () {
  122. this.showDialog = false;
  123. this.oldPassword = null;
  124. this.newPassword = null;
  125. this.confirmPassword = null;
  126. },
  127. },
  128. };
  129. </script>