password-modal.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. import { SYSTEM_NAME } from '@/config/setting';
  50. export default {
  51. props: {
  52. visible: Boolean
  53. },
  54. data() {
  55. return {
  56. // 按钮 loading
  57. loading: false,
  58. // 表单数据
  59. form: {
  60. oldPassword: '',
  61. newPassword: '',
  62. newPassword1: ''
  63. },
  64. // 表单验证
  65. rules: {
  66. oldPassword: [
  67. {
  68. required: true,
  69. message: '请输入旧密码',
  70. trigger: 'blur'
  71. }
  72. ],
  73. newPassword: [
  74. {
  75. required: true,
  76. message: '请输入新密码',
  77. trigger: 'blur'
  78. }
  79. ],
  80. newPassword1: [
  81. {
  82. required: true,
  83. trigger: 'blur',
  84. validator: (_rule, value, callback) => {
  85. if (!value) {
  86. return callback(new Error('请再次输入新密码'));
  87. }
  88. if (value !== this.form.newPassword) {
  89. return callback(new Error('两次输入密码不一致'));
  90. }
  91. callback();
  92. }
  93. }
  94. ]
  95. }
  96. };
  97. },
  98. methods: {
  99. /* 修改 visible */
  100. updateVisible(value) {
  101. this.$emit('update:visible', value);
  102. },
  103. /* 保存修改 */
  104. save() {
  105. this.$refs.form.validate((valid) => {
  106. if (valid) {
  107. this.loading = true;
  108. this.form.id = localStorage.getItem(`userId-${SYSTEM_NAME}`);
  109. updatePassword(this.form)
  110. .then((msg) => {
  111. this.loading = false;
  112. this.$message.success(msg);
  113. this.updateVisible(false);
  114. })
  115. .catch((e) => {
  116. console.log(e);
  117. this.loading = false;
  118. // this.$message.error(e.message);
  119. });
  120. } else {
  121. return false;
  122. }
  123. });
  124. },
  125. /* 关闭回调 */
  126. onClose() {
  127. this.form = {
  128. oldPassword: '',
  129. newPassword: '',
  130. newPassword1: ''
  131. };
  132. this.$refs.form.resetFields();
  133. this.loading = false;
  134. }
  135. }
  136. };
  137. </script>