cshenfu
2023-06-12 94135fd604db283f890e9f649882add4c76164f7
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
// 导入配置
import config from '@/uni_modules/uni-id-pages/config.js'
 
const {passwordStrength} = config
 
// 密码强度表达式
const passwordRules = {
    // 密码必须包含大小写字母、数字和特殊符号
    super: /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/,
    // 密码必须包含字母、数字和特殊符号
    strong: /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/,
    // 密码必须为字母、数字和特殊符号任意两种的组合
    medium: /^(?![0-9]+$)(?![a-zA-Z]+$)(?![~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]+$)[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/,
    // 密码必须包含字母和数字
    weak: /^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{6,16}$/
}
 
const ERROR = {
    normal: {
        noPwd: '请输入密码',
        noRePwd: '再次输入密码',
        rePwdErr: '两次输入密码不一致'
    },
    passwordStrengthError: {
        super: '密码必须包含大小写字母、数字和特殊符号,密码长度必须在8-16位之间',
        strong: '密码必须包含字母、数字和特殊符号,密码长度必须在8-16位之间',
        medium: '密码必须为字母、数字和特殊符号任意两种的组合,密码长度必须在8-16位之间',
        weak: '密码必须包含字母,密码长度必须在6-16位之间'
    }
}
 
function validPwd(password) {
    //强度校验
    if (passwordStrength && passwordRules[passwordStrength]) {
        if (!new RegExp(passwordRules[passwordStrength]).test(password)) {
            return ERROR.passwordStrengthError[passwordStrength]
        }
    }
    return true
}
 
function getPwdRules(pwdName = 'password', rePwdName = 'password2') {
    const rules = {}
    rules[pwdName] = {
        rules: [{
                required: true,
                errorMessage: ERROR.normal.noPwd,
            },
            {
                validateFunction: function(rule, value, data, callback) {
                    const checkRes = validPwd(value)
                    if (checkRes !== true) {
                        callback(checkRes)
                    }
                    return true
                }
            }
        ]
    }
 
    if (rePwdName) {
        rules[rePwdName] = {
            rules: [{
                    required: true,
                    errorMessage: ERROR.normal.noRePwd,
                },
                {
                    validateFunction: function(rule, value, data, callback) {
                        if (value != data[pwdName]) {
                            callback(ERROR.normal.rePwdErr)
                        }
                        return true
                    }
                }
            ]
        }
    }
    return rules
}
 
export default {
    ERROR,
    validPwd,
    getPwdRules
}