password-modal.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <!-- 修改密码弹窗 -->
  2. <template>
  3. <ele-modal
  4. width="420px"
  5. title="修改密码"
  6. :visible="visible"
  7. :append-to-body="true"
  8. :close-on-click-modal="true"
  9. @update:visible="updateVisible"
  10. @closed="onClose"
  11. >
  12. <el-form
  13. ref="form"
  14. :model="form"
  15. :rules="rules"
  16. label-width="82px"
  17. @keyup.enter.native="save"
  18. >
  19. <el-form-item label="旧密码:" prop="oldPassword">
  20. <el-input
  21. show-password
  22. v-model="form.oldPassword"
  23. placeholder="请输入旧密码"
  24. />
  25. </el-form-item>
  26. <el-form-item label="新密码:" prop="newPassword">
  27. <el-input
  28. show-password
  29. v-model="form.newPassword"
  30. placeholder="请输入新密码"
  31. />
  32. </el-form-item>
  33. <el-form-item label="确认密码:" prop="newPassword1">
  34. <el-input
  35. show-password
  36. v-model="form.newPassword1"
  37. placeholder="请再次输入新密码"
  38. />
  39. </el-form-item>
  40. </el-form>
  41. <template v-slot:footer>
  42. <el-button @click="updateVisible(false)">取消</el-button>
  43. <el-button type="primary" @click="save">确定</el-button>
  44. </template>
  45. </ele-modal>
  46. </template>
  47. <script>
  48. import { updatePassword } from '@/api/system/user';
  49. export default {
  50. props: {
  51. visible: Boolean
  52. },
  53. data() {
  54. return {
  55. // 按钮 loading
  56. loading: false,
  57. // 表单数据
  58. form: {
  59. oldPassword: '',
  60. newPassword: '',
  61. newPassword1: ''
  62. },
  63. // 表单验证
  64. rules: {
  65. oldPassword: [
  66. {
  67. required: true,
  68. message: '请输入旧密码',
  69. trigger: 'blur'
  70. }
  71. ],
  72. newPassword: [
  73. {
  74. required: true,
  75. message: '请输入新密码',
  76. trigger: 'blur'
  77. }
  78. ],
  79. newPassword1: [
  80. {
  81. required: true,
  82. trigger: 'blur',
  83. validator: (_rule, value, callback) => {
  84. if (!value) {
  85. return callback(new Error('请再次输入新密码'));
  86. }
  87. if (value !== this.form.newPassword) {
  88. return callback(new Error('两次输入密码不一致'));
  89. }
  90. callback();
  91. }
  92. }
  93. ]
  94. }
  95. };
  96. },
  97. methods: {
  98. /* 修改 visible */
  99. updateVisible(value) {
  100. this.$emit('update:visible', value);
  101. },
  102. /* 保存修改 */
  103. save() {
  104. this.$refs.form.validate((valid) => {
  105. if (valid) {
  106. this.loading = true;
  107. this.form.id = localStorage.getItem('userId')
  108. updatePassword(this.form)
  109. .then((msg) => {
  110. this.loading = false;
  111. this.$message.success(msg);
  112. this.updateVisible(false);
  113. })
  114. .catch((e) => {
  115. this.loading = false;
  116. this.$message.error(e.message);
  117. });
  118. } else {
  119. return false;
  120. }
  121. });
  122. },
  123. /* 关闭回调 */
  124. onClose() {
  125. this.form = {
  126. oldPassword: '',
  127. newPassword: '',
  128. newPassword1: ''
  129. };
  130. this.$refs.form.resetFields();
  131. this.loading = false;
  132. }
  133. }
  134. };
  135. </script>