changePwdDialog.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <el-dialog v-model="visible" width="50%" :show-close="false">
  3. <el-form ref="cpwd" :model="changePwdForm" :rules="chagePwdRules" label-width="150px" label-position="left" style="margin-top: 20px">
  4. <el-form-item :label="T('OldPassword')" prop="old_password">
  5. <el-input v-model="changePwdForm.old_password" :placeholder="T('For OIDC login without a password, enter any 4-20 letters')" show-password></el-input>
  6. </el-form-item>
  7. <el-form-item :label="T('NewPassword')" prop="new_password">
  8. <el-input v-model="changePwdForm.new_password" show-password></el-input>
  9. </el-form-item>
  10. <el-form-item :label="T('ConfirmPassword')" prop="confirmPwd">
  11. <el-input v-model="changePwdForm.confirmPwd" show-password></el-input>
  12. </el-form-item>
  13. <el-form-item>
  14. <el-button @click="cancelChangePwd">{{ T('Cancel') }}</el-button>
  15. <el-button type="primary" @click="changePassword">{{ T('Confirm') }}</el-button>
  16. </el-form-item>
  17. </el-form>
  18. </el-dialog>
  19. </template>
  20. <script setup>
  21. import { reactive, ref } from 'vue'
  22. import { ElMessageBox } from 'element-plus'
  23. import { changeCurPwd } from '@/api/user'
  24. import { useUserStore } from '@/store/user'
  25. import { T } from '@/utils/i18n'
  26. const props = defineProps({
  27. visible: Boolean,
  28. })
  29. const emit = defineEmits(['update:visible'])
  30. // Watch for changes to the prop and emit an event if necessary
  31. // watch(() => props.visible, (newVal) => {
  32. // emit('update:visible', newVal);
  33. // });
  34. const showChangePwd = () => {
  35. emit('update:visible', true)
  36. changePwdForm.old_password = ''
  37. changePwdForm.new_password = ''
  38. changePwdForm.confirmPwd = ''
  39. }
  40. const changePwdForm = reactive({
  41. old_password: '',
  42. new_password: '',
  43. confirmPwd: '',
  44. })
  45. const chagePwdRules = reactive({
  46. old_password: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
  47. new_password: [
  48. { required: true, message: '请输入新密码', trigger: 'blur' },
  49. {
  50. validator: (rule, value, callback) => {
  51. if (value === changePwdForm.old_password) {
  52. callback(new Error('新密码不能与旧密码相同'))
  53. } else {
  54. callback()
  55. }
  56. },
  57. trigger: 'blur',
  58. }],
  59. confirmPwd: [
  60. { required: true, message: '请再次输入新密码', trigger: 'blur' },
  61. {
  62. validator: (rule, value, callback) => {
  63. if (value !== changePwdForm.new_password) {
  64. callback(new Error('两次输入密码不一致'))
  65. } else {
  66. callback()
  67. }
  68. },
  69. trigger: 'blur',
  70. },
  71. ],
  72. })
  73. const cpwd = ref(null)
  74. const cancelChangePwd = () => {
  75. emit('update:visible', false)
  76. }
  77. const userStore = useUserStore()
  78. const changePassword = async () => {
  79. //验证
  80. const valid = await cpwd.value.validate().catch(_ => false)
  81. if (!valid) {
  82. return
  83. }
  84. console.log('changePassword')
  85. const confirm = await ElMessageBox.confirm('确定修改密码么?', {
  86. confirmButtonText: '确定',
  87. cancelButtonText: '取消',
  88. }).catch(_ => false)
  89. if (!confirm) {
  90. return
  91. }
  92. const res = await changeCurPwd(changePwdForm).catch(_ => false)
  93. if (!res) {
  94. return
  95. }
  96. ElMessageBox.alert('修改成功', '修改密码', {
  97. autofocus: true,
  98. confirmButtonText: 'OK',
  99. callback: (action) => {
  100. userStore.logout()
  101. window.location.reload()
  102. },
  103. })
  104. }
  105. </script>
  106. <style scoped lang="scss">
  107. </style>