提交 f3a5191a 编写于 作者: DCloud_JSON's avatar DCloud_JSON

添加密码增强登录功能

上级 21d313ce
// 导入配置
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
}
......@@ -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位之间
}
}
}
......@@ -47,7 +47,7 @@
"uni-load-more",
"uni-popup",
"uni-scss",
"uni-transition"
"uni-transition"
],
"encrypt": [],
"platforms": {
......
......@@ -12,7 +12,7 @@
</uni-forms-item>
<uni-forms-item name="password" v-model="formData.password" required>
<uni-easyinput :inputBorder="false" :focus="focusPassword" @blur="focusPassword = false"
class="input-box" maxlength="20" placeholder="请输入6-20位密码" type="password"
class="input-box" maxlength="20" :placeholder="'请输入' + passwordLength[0] + '-' + passwordLength[1] + '位密码'" type="password"
v-model="formData.password" trim="both" />
</uni-forms-item>
<uni-forms-item name="password2" v-model="formData.password2" required>
......@@ -33,6 +33,7 @@
<script>
import rules from './validator.js';
import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
import config from '@/uni_modules/uni-id-pages/config.js'
const uniIdCo = uniCloud.importObject("uni-id-co")
export default {
mixins: [mixin],
......@@ -52,6 +53,11 @@
focusPassword2:false
}
},
computed:{
passwordLength(){
return config.passwordLength
}
},
onReady() {
this.$refs.form.setRules(this.rules)
},
......
import passwordMod from '@/uni_modules/uni-id-pages/common/password.js'
export default {
"username": {
"rules": [{
......@@ -42,45 +43,14 @@ export default {
if (/^\d+$/.test(value)) {
callback('昵称不能为纯数字')
};
if(/[\u4E00-\u9FA5\uF900-\uFA2D]{1,}/.test(value)){
callback('昵称不能包含中文')
}
return true
}
}
],
"label": "昵称"
},
"password": {
"rules": [{
required: true,
errorMessage: '密码长度不少于6位',
},
{
minLength: 6,
maxLength: 20,
errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
}
],
"label": "密码"
},
"password2": {
"rules": [{
required: true,
errorMessage: '再次输入密码',
},
{
minLength: 6,
maxLength: 20,
errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
},
{
validateFunction: function(rule, value, data, callback) {
// console.log(value);
if (value != data.password) {
callback('两次输入密码不一致')
};
return true
}
}
],
"label": "确认密码"
}
...passwordMod.getPwdRules()
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册