change-password-form.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div>
  3. <el-dialog v-model="state.showDialog" destroy-on-close :title="title" draggable width="475px">
  4. <el-form ref="formRef" :model="form" size="default" label-width="80px" label-position="left">
  5. <el-row :gutter="35">
  6. <el-col :span="24">
  7. <el-form-item label="旧密码" prop="oldPassword" :rules="[{ required: true, message: '请输入旧密码', trigger: ['blur', 'change'] }]">
  8. <el-input v-model="form.oldPassword" show-password autocomplete="off" clearable />
  9. </el-form-item>
  10. </el-col>
  11. <el-col :span="24">
  12. <el-form-item
  13. label="新密码"
  14. prop="newPassword"
  15. :rules="[
  16. { required: true, message: '请输入新密码', trigger: ['blur', 'change'] },
  17. { validator: testNewPassword, trigger: ['blur', 'change'] },
  18. ]"
  19. >
  20. <el-input v-model="form.newPassword" show-password autocomplete="off" clearable />
  21. </el-form-item>
  22. </el-col>
  23. <el-col :span="24">
  24. <el-form-item
  25. label="确认密码"
  26. prop="confirmPassword"
  27. :rules="[
  28. { required: true, message: '请输入确认密码', trigger: ['blur', 'change'] },
  29. { validator: testConfirmPassword, trigger: ['blur', 'change'] },
  30. ]"
  31. >
  32. <el-input v-model="form.confirmPassword" show-password autocomplete="off" clearable />
  33. </el-form-item>
  34. </el-col>
  35. </el-row>
  36. </el-form>
  37. <template #footer>
  38. <span class="dialog-footer">
  39. <el-button @click="onCancel" size="default">取 消</el-button>
  40. <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
  41. </span>
  42. </template>
  43. </el-dialog>
  44. </div>
  45. </template>
  46. <script lang="ts" setup>
  47. import { reactive, toRefs, ref } from 'vue'
  48. import { UserChangePasswordInput } from '/@/api/admin/data-contracts'
  49. import { UserApi } from '/@/api/admin/User'
  50. defineProps({
  51. title: {
  52. type: String,
  53. default: '',
  54. },
  55. })
  56. const formRef = ref()
  57. const state = reactive({
  58. showDialog: false,
  59. sureLoading: false,
  60. form: {} as UserChangePasswordInput,
  61. })
  62. const { form } = toRefs(state)
  63. // 新密码验证器
  64. const testNewPassword = (rule: any, value: any, callback: any) => {
  65. if (value) {
  66. if (state.form.confirmPassword !== '') {
  67. formRef.value.validateField('confirmPassword')
  68. }
  69. callback()
  70. }
  71. }
  72. // 确认密码验证器
  73. const testConfirmPassword = (rule: any, value: any, callback: any) => {
  74. if (value) {
  75. if (value !== state.form.newPassword) {
  76. callback(new Error('新密码和确认密码不一致!'))
  77. } else {
  78. callback()
  79. }
  80. }
  81. }
  82. // 打开对话框
  83. const open = async () => {
  84. state.showDialog = true
  85. }
  86. // 取消
  87. const onCancel = () => {
  88. state.showDialog = false
  89. }
  90. // 确定
  91. const onSure = () => {
  92. formRef.value.validate(async (valid: boolean) => {
  93. if (!valid) return
  94. state.sureLoading = true
  95. const res = await new UserApi().changePassword(state.form, { showSuccessMessage: true }).catch(() => {
  96. state.sureLoading = false
  97. })
  98. state.sureLoading = false
  99. if (res?.success) {
  100. state.showDialog = false
  101. }
  102. })
  103. }
  104. defineExpose({
  105. open,
  106. })
  107. </script>
  108. <script lang="ts">
  109. import { defineComponent } from 'vue'
  110. export default defineComponent({
  111. name: 'admin/personal/change-password-form',
  112. })
  113. </script>