main-navbar-update-password.vue 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <el-dialog
  3. :visible.sync="visible"
  4. :title="$t('updatePassword.title')"
  5. :close-on-click-modal="false"
  6. :close-on-press-escape="false"
  7. :append-to-body="true">
  8. <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
  9. <el-form-item :label="$t('updatePassword.username')">
  10. <span>{{ $store.state.user.name }}</span>
  11. </el-form-item>
  12. <el-form-item prop="password" :label="$t('updatePassword.password')">
  13. <el-input v-model="dataForm.password" type="password" :placeholder="$t('updatePassword.password')"></el-input>
  14. </el-form-item>
  15. <el-form-item prop="newPassword" :label="$t('updatePassword.newPassword')">
  16. <el-input v-model="dataForm.newPassword" type="password" :placeholder="$t('updatePassword.newPassword')"></el-input>
  17. </el-form-item>
  18. <el-form-item prop="confirmPassword" :label="$t('updatePassword.confirmPassword')">
  19. <el-input v-model="dataForm.confirmPassword" type="password" :placeholder="$t('updatePassword.confirmPassword')"></el-input>
  20. </el-form-item>
  21. </el-form>
  22. <template slot="footer">
  23. <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
  24. <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
  25. </template>
  26. </el-dialog>
  27. </template>
  28. <script>
  29. import debounce from 'lodash/debounce'
  30. import { clearLoginInfo } from '@/utils'
  31. export default {
  32. data () {
  33. return {
  34. visible: false,
  35. dataForm: {
  36. password: '',
  37. newPassword: '',
  38. confirmPassword: ''
  39. }
  40. }
  41. },
  42. computed: {
  43. dataRule () {
  44. var validateConfirmPassword = (rule, value, callback) => {
  45. if (this.dataForm.newPassword !== value) {
  46. return callback(new Error(this.$t('updatePassword.validate.confirmPassword')))
  47. }
  48. callback()
  49. }
  50. return {
  51. password: [
  52. { required: true, message: this.$t('validate.required'), trigger: 'blur' }
  53. ],
  54. newPassword: [
  55. { required: true, message: this.$t('validate.required'), trigger: 'blur' }
  56. ],
  57. confirmPassword: [
  58. { required: true, message: this.$t('validate.required'), trigger: 'blur' },
  59. { validator: validateConfirmPassword, trigger: 'blur' }
  60. ]
  61. }
  62. }
  63. },
  64. methods: {
  65. init () {
  66. this.visible = true
  67. this.$nextTick(() => {
  68. this.$refs['dataForm'].resetFields()
  69. })
  70. },
  71. // 表单提交
  72. dataFormSubmitHandle: debounce(function () {
  73. this.$refs['dataForm'].validate((valid) => {
  74. if (!valid) {
  75. return false
  76. }
  77. this.$http.put('/sys/user/password', this.dataForm).then(({ data: res }) => {
  78. if (res.code !== 0) {
  79. return this.$message.error(res.msg)
  80. }
  81. this.$message({
  82. message: this.$t('prompt.success'),
  83. type: 'success',
  84. duration: 500,
  85. onClose: () => {
  86. this.visible = false
  87. clearLoginInfo()
  88. this.$router.replace({ name: 'login' })
  89. }
  90. })
  91. }).catch(() => {})
  92. })
  93. }, 1000, { 'leading': true, 'trailing': false })
  94. }
  95. }
  96. </script>