password-modal.vue 3.5 KB

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