feat(auth): 新增密码复杂度校验,注册与修改密码统一为8-20位且须含大小写字母/数字/特殊字符

This commit is contained in:
tianyongbao
2026-06-16 22:53:25 +08:00
parent 1ff68c1f98
commit 973ad0fc58
3 changed files with 86 additions and 10 deletions

View File

@@ -33,7 +33,15 @@
</view> </view>
<view class="tip-item"> <view class="tip-item">
<text class="tip-dot"></text> <text class="tip-dot"></text>
<text class="tip-text">密码长度为 6-20 个字符</text> <text class="tip-text">密码长度为 8-20 个字符</text>
</view>
<view class="tip-item">
<text class="tip-dot"></text>
<text class="tip-text">必须同时包含大写字母小写字母数字和特殊字符</text>
</view>
<view class="tip-item">
<text class="tip-dot"></text>
<text class="tip-text">密码不能包含空格</text>
</view> </view>
<view class="tip-item"> <view class="tip-item">
<text class="tip-dot"></text> <text class="tip-dot"></text>
@@ -60,6 +68,7 @@
<script> <script>
import { register } from "@/api/login" import { register } from "@/api/login"
import { validatePassword } from "@/utils/validate"
export default { export default {
data() { data() {
return { return {
@@ -90,9 +99,8 @@
errorMessage: '密码不能为空', errorMessage: '密码不能为空',
}, },
{ {
minLength: 6, validateFunction: (rule, value) => validatePassword(value).valid,
maxLength: 20, errorMessage: '密码需 8-20 位,且须同时含大小写字母、数字和特殊字符'
errorMessage: '长度在 6 到 20 个字符'
} }
] ]
}, },

View File

@@ -22,15 +22,15 @@
<view class="tips-list"> <view class="tips-list">
<view class="tip-item"> <view class="tip-item">
<text class="tip-dot"></text> <text class="tip-dot"></text>
<text class="tip-text">密码长度为 6-20 个字符</text> <text class="tip-text">密码长度为 8-20 个字符</text>
</view> </view>
<view class="tip-item"> <view class="tip-item">
<text class="tip-dot"></text> <text class="tip-dot"></text>
<text class="tip-text">建议使用字母数字和符号组合</text> <text class="tip-text">必须同时包含大写字母小写字母数字和特殊字</text>
</view> </view>
<view class="tip-item"> <view class="tip-item">
<text class="tip-dot"></text> <text class="tip-dot"></text>
<text class="tip-text">不要使用容易被猜到的密码</text> <text class="tip-text">密码不能包含空格</text>
</view> </view>
</view> </view>
</view> </view>
@@ -45,6 +45,7 @@
<script> <script>
import { updateUserPwd } from "@/api/system/user" import { updateUserPwd } from "@/api/system/user"
import { validatePassword } from "@/utils/validate"
export default { export default {
data() { data() {
@@ -67,9 +68,8 @@
errorMessage: '新密码不能为空', errorMessage: '新密码不能为空',
}, },
{ {
minLength: 6, validateFunction: (rule, value) => validatePassword(value).valid,
maxLength: 20, errorMessage: '新密码需 8-20 位,且须同时含大小写字母、数字和特殊字符'
errorMessage: '长度在 6 到 20 个字符'
} }
] ]
}, },

68
src/utils/validate.js Normal file
View File

@@ -0,0 +1,68 @@
/**
* 密码复杂度校验工具
*
* 规则(企业级高档位):
* 1. 长度为 8-20 个字符
* 2. 不能包含空格
* 3. 必须同时包含:大写字母、小写字母、数字、特殊字符 四类
*
* 注册与修改密码共用,保证两处复杂度判断一致。
*/
// 密码长度范围
export const PASSWORD_MIN_LENGTH = 8
export const PASSWORD_MAX_LENGTH = 20
// 四类字符检测
const REG_UPPER = /[A-Z]/
const REG_LOWER = /[a-z]/
const REG_NUMBER = /[0-9]/
// 特殊字符:常见符号集合(! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
const REG_SPECIAL = /[!-/:-@\[-`{-~]/
/**
* 校验密码复杂度
* @param {string} password 待校验的密码
* @returns {{ valid: boolean, message: string }} 校验结果与错误提示
*/
export function validatePassword(password) {
if (password === undefined || password === null || password === '') {
return { valid: false, message: '密码不能为空' }
}
const value = String(password)
if (value.length < PASSWORD_MIN_LENGTH || value.length > PASSWORD_MAX_LENGTH) {
return { valid: false, message: `密码长度在 ${PASSWORD_MIN_LENGTH}${PASSWORD_MAX_LENGTH} 个字符` }
}
if (/\s/.test(value)) {
return { valid: false, message: '密码不能包含空格' }
}
// 必须同时包含大写字母、小写字母、数字、特殊字符
if (!REG_UPPER.test(value)) {
return { valid: false, message: '密码必须包含大写字母' }
}
if (!REG_LOWER.test(value)) {
return { valid: false, message: '密码必须包含小写字母' }
}
if (!REG_NUMBER.test(value)) {
return { valid: false, message: '密码必须包含数字' }
}
if (!REG_SPECIAL.test(value)) {
return { valid: false, message: '密码必须包含特殊字符' }
}
return { valid: true, message: '' }
}
/**
* uni-forms 自定义校验函数:校验密码复杂度
* 用法:
* { validateFunction: passwordComplexityValidator, errorMessage: '...' }
* 注意errorMessage 仅在 validateFunction 返回 false 时展示,
* 因此这里直接返回 validatePassword 的布尔结果,配合一个通用提示文案。
*
* @param {object} rule 规则对象(未使用)
* @param {string} value 当前输入值
* @returns {boolean} 是否通过校验
*/
export function passwordComplexityValidator(rule, value) {
return validatePassword(value).valid
}