changePwd.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <el-dialog :close-on-click-modal="false" :title="$t('agentUser.changePwdTitle')" draggable v-model="visible" width="500px">
  3. <el-form :model="form" :rules="dataRules" label-width="100px" ref="dataFormRef" v-loading="loading">
  4. <el-form-item :label="$t('agentUser.newPassword')" prop="newPwd">
  5. <el-input :placeholder="$t('agentUser.inputNewPasswordTip')" type="password" v-model="form.newPwd" show-password />
  6. </el-form-item>
  7. <el-form-item :label="$t('agentUser.confirmPassword')" prop="confirmPwd">
  8. <el-input :placeholder="$t('agentUser.inputConfirmPasswordTip')" type="password" v-model="form.confirmPwd" show-password />
  9. </el-form-item>
  10. </el-form>
  11. <template #footer>
  12. <span class="dialog-footer">
  13. <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
  14. <el-button @click="onSubmit" type="primary" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
  15. </span>
  16. </template>
  17. </el-dialog>
  18. </template>
  19. <script lang="ts" name="ChangePwdDialog" setup>
  20. import { useMessage } from '/@/hooks/message';
  21. import { changePwd } from '/@/api/agent';
  22. import { useI18n } from 'vue-i18n';
  23. const emit = defineEmits(['refresh']);
  24. const { t } = useI18n();
  25. // 定义变量内容
  26. const dataFormRef = ref();
  27. const visible = ref(false);
  28. const loading = ref(false);
  29. const userId = ref('');
  30. // 提交表单数据
  31. const form = reactive({
  32. newPwd: '',
  33. confirmPwd: '',
  34. });
  35. // 自定义校验规则
  36. const validateConfirmPwd = (rule: any, value: any, callback: any) => {
  37. if (value === '') {
  38. callback(new Error(t('agentUser.confirmPasswordRequired')));
  39. } else if (value !== form.newPwd) {
  40. callback(new Error(t('agentUser.passwordNotMatch')));
  41. } else {
  42. callback();
  43. }
  44. };
  45. // 定义校验规则
  46. const dataRules = ref({
  47. newPwd: [
  48. { required: true, message: t('agentUser.newPasswordRequired'), trigger: 'blur' },
  49. { min: 6, max: 20, message: t('agentUser.passwordLengthTip'), trigger: 'blur' },
  50. ],
  51. confirmPwd: [{ required: true, validator: validateConfirmPwd, trigger: 'blur' }],
  52. });
  53. // 打开弹窗
  54. const openDialog = (id: string) => {
  55. visible.value = true;
  56. userId.value = id;
  57. // 重置表单数据
  58. nextTick(() => {
  59. dataFormRef.value?.resetFields();
  60. });
  61. };
  62. // 提交
  63. const onSubmit = async () => {
  64. const valid = await dataFormRef.value.validate().catch(() => {});
  65. if (!valid) return false;
  66. try {
  67. loading.value = true;
  68. await changePwd(userId.value, {
  69. newPwd: form.newPwd,
  70. });
  71. useMessage().success(t('agentUser.changePwdSuccess'));
  72. visible.value = false;
  73. emit('refresh');
  74. } catch (err: any) {
  75. useMessage().error(err.msg);
  76. } finally {
  77. loading.value = false;
  78. }
  79. };
  80. // 暴露变量
  81. defineExpose({
  82. openDialog,
  83. });
  84. </script>