diff --git a/uni_modules/uni-id-pages/common/password.js b/uni_modules/uni-id-pages/common/password.js new file mode 100644 index 0000000000000000000000000000000000000000..e3f57b3b15ade519a5954c955812335e597d61e0 --- /dev/null +++ b/uni_modules/uni-id-pages/common/password.js @@ -0,0 +1,113 @@ +// 导入配置 +import config from '@/uni_modules/uni-id-pages/config.js' + +const passwordLength = config.password.length +const passwordStrength = config.password.strength + +let minPasswordLength = 6 +let maxPasswordLength = 20 +if (passwordLength) { + if (passwordLength[0]) { + minPasswordLength = passwordLength[0] + } + if (passwordLength[1]) { + maxPasswordLength = passwordLength[1] + } +} +// 密码强度表达式 +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: { + superstrong: '密码必须包含大小写字母、数字和特殊符号', + strong: '密码必须包含字母、数字和特殊符号', + medium: '密码必须为字母、数字和特殊符号任意两种的组合', + weak: '密码必须包含字母' + }, + passwordLengthError: { + normal: '密码长度必须在' + minPasswordLength + '-' + maxPasswordLength + '位之间', + minLimit: '密码长度不得少于' + minPasswordLength + '位', + maxLimit: '密码长度不得超过' + maxPasswordLength + '位' + } +} + +function validPwd(password) { + //强度校验 + if (passwordStrength && passwordRules[passwordStrength]) { + if (!new RegExp(passwordRules[passwordStrength]).test(password)) { + return ERROR.passwordStrengthError[passwordStrength] + } + } + //长度校验 + if (passwordLength) { + if (passwordLength[0] && password.length < passwordLength[0]) { + return ERROR.passwordLengthError.minLimit + } + if (passwordLength[1] && password.length > passwordLength[1]) { + return ERROR.passwordLengthError.maxLimit + } + } + + 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.password) { + callback(ERROR.normal.rePwdErr) + } + return true + } + } + ] + } + } + return rules +} + +export default { + ERROR, + minPasswordLength, + maxPasswordLength, + validPwd, + getPwdRules +} diff --git a/uni_modules/uni-id-pages/config.js b/uni_modules/uni-id-pages/config.js index c6ff66c144964f989ce8b4e87be30cce40444904..9c9658535c506cfd062c02166528116b26126baf 100644 --- a/uni_modules/uni-id-pages/config.js +++ b/uni_modules/uni-id-pages/config.js @@ -29,18 +29,30 @@ export default { "agreements": { "serviceUrl": "https://xxx", //用户服务协议链接 "privacyUrl": "https://xxx", //隐私政策条款链接 - // 哪些场景下显示,1.注册(包括登录并注册,如:微信登录、苹果登录、短信验证码登录)、2.登录(如:用户名密码登录) - "scope": [ - 'register', 'login' + // 哪些场景下显示,1.注册(包括登录并注册,如:微信登录、苹果登录、短信验证码登录)、2.登录(如:用户名密码登录) + "scope": [ + 'register', 'login' ] - }, - // 提供各类服务接入(如微信登录服务)的应用id - "appid":{ - "weixin":{ - // 微信公众号的appid,来源:登录微信公众号(https://mp.weixin.qq.com)-> 设置与开发 -> 基本配置 -> 公众号开发信息 -> AppID - "h5":"xxxxxx", - // 微信开放平台的appid,来源:登录微信开放平台(https://open.weixin.qq.com) -> 管理中心 -> 网站应用 -> 选择对应的应用名称,点击查看 -> AppID - "web":"xxxxxx" - } + }, + // 提供各类服务接入(如微信登录服务)的应用id + "appid": { + "weixin": { + // 微信公众号的appid,来源:登录微信公众号(https://mp.weixin.qq.com)-> 设置与开发 -> 基本配置 -> 公众号开发信息 -> AppID + "h5": "xxxxxx", + // 微信开放平台的appid,来源:登录微信开放平台(https://open.weixin.qq.com) -> 管理中心 -> 网站应用 -> 选择对应的应用名称,点击查看 -> AppID + "web": "xxxxxx" + } + }, + /** + * 密码强度 + * superstrong(超强:密码必须包含大小写字母、数字和特殊符号) + * strong(强: 密码必须包含字母、数字和特殊符号) + * medium (中:密码必须为字母、数字和特殊符号任意两种的组合) + * weak(弱:密码必须包含字母) + * 为空或false则不验证密码强度 + */ + "password": { + "strength": "strong", + "length": [6, 20] //密码长度,默认在6-20位之间 } -} +} diff --git a/uni_modules/uni-id-pages/package.json b/uni_modules/uni-id-pages/package.json index e67123e5ceeefd3d0213c1f42fb2d17e752e669d..f03e447ee1162265fb5a1b7e4f24f53fc1196280 100644 --- a/uni_modules/uni-id-pages/package.json +++ b/uni_modules/uni-id-pages/package.json @@ -47,7 +47,7 @@ "uni-load-more", "uni-popup", "uni-scss", - "uni-transition" + "uni-transition" ], "encrypt": [], "platforms": { diff --git a/uni_modules/uni-id-pages/pages/register/register.vue b/uni_modules/uni-id-pages/pages/register/register.vue index 9284adecca326f11beae8abccd63cc8d99ce8805..2433a88fdd47a16d5b7fa98d4055a2b376a8cd58 100644 --- a/uni_modules/uni-id-pages/pages/register/register.vue +++ b/uni_modules/uni-id-pages/pages/register/register.vue @@ -12,7 +12,7 @@ @@ -33,6 +33,7 @@