gaoshp
2024-01-05 b5db49ce1e4c678ce4a7120928811a9621128b8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<template>
  <el-dialog title="修改密码" :visible.sync="isShow" custom-class="custom-dialog" @close='cancelDialog()'>
    <el-form label-position="right"  status-icon :model="info" :rules="passRule" size="small" ref="alterPwForm">
      <el-form-item label="旧密码:" prop="oldpass">
        <el-input  type="password" auto-complete="off" v-model="info.oldpass"></el-input>
      </el-form-item>
      <el-form-item label="新密码:" prop="newpass">
        <el-input  type="password" auto-complete="off" v-model="info.newpass"></el-input>
      </el-form-item>
      <el-form-item label="再次输入新密码:" prop="newpass2">
        <el-input  type="password" auto-complete="off" v-model="info.newpass2"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelDialog()">取 消</el-button>
      <el-button type="primary" @click="confirmDialog()">确 定</el-button>
    </div>
  </el-dialog>
</template>
 
<script>
    import { isValidPassword } from '@/utils/validate'
    export default {
      name: 'alterPasswordDialog',
      props: {
        isVisible: {
          type: Boolean,
          default: false
        }
      },
      watch: {
        isVisible(val) {
          this.isShow = val
          if (!val) {
            this.$refs.alterPwForm.resetFields()
          }
        }
      },
      data() {
        // 密码自定义规则
        const rulesPass = (rule, value, callback) => {
          if (!isValidPassword(value)) {
            callback(new Error(`可用字母、数字,长度不低于6位`))
          } else if (value.length < 6) {
            callback(new Error(`密码不能低于6位`))
          } else {
            callback()
          }
        }
        const rulesPass2 = (rule, value, callback) => {
          if (value === '' && this.info.newpass || value !== this.info.newpass) {
            callback(new Error('两次输入密码不一致!'))
          } else if (value === '') {
            callback(new Error('请再次输入密码'))
          } else {
            callback()
          }
        }
        return {
          isShow: this.isVisible,
          info: {
            oldpass: '', // 旧密码
            newpass: '', // 新密码
            newpass2: '' // 再次确认新密码
          },
          passRule: {
            oldpass: [
              { required: true, message: '旧密码不能为空', trigger: ['blur', 'change'] },
              // 自定义规则
              { validator: rulesPass, trigger: ['blur', 'change'] }
            ],
            newpass: [
              { required: true, message: '新密码不能为空', trigger: ['blur', 'change'] },
              // 自定义规则
              { validator: rulesPass, trigger: ['blur', 'change'] }
            ],
            newpass2: [
              // 自定义规则
              { validator: rulesPass2, trigger: ['blur', 'change'] }
            ]
          }
        }
      },
      methods: {
        cancelDialog() {
          this.$emit('submit', { action: 'cancel' })
        },
        confirmDialog() {
          // console.log('confirm')
          this.$refs.alterPwForm.validate((valid) => {
            if (valid) {
              this.$emit('submit', { action: 'confirm', data: { oldPass: this.info.oldpass, newPass: this.info.newpass }})
            } else {
              return false
            }
          })
        }
      }
    }
</script>
 
<style scoped>
 
</style>