From 8b78e8f209717869b991b0df40edd94fba65e0b0 Mon Sep 17 00:00:00 2001 From: tianyongbao Date: Wed, 17 Jun 2026 05:47:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E6=96=B0=E5=A2=9E=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E5=A4=8D=E6=9D=82=E5=BA=A6=E6=A0=A1=E9=AA=8C=EF=BC=8C?= =?UTF-8?q?=E6=B3=A8=E5=86=8C/=E4=BF=AE=E6=94=B9=E5=AF=86=E7=A0=81/?= =?UTF-8?q?=E6=96=B0=E5=BB=BA=E7=94=A8=E6=88=B7/=E9=87=8D=E7=BD=AE?= =?UTF-8?q?=E5=AF=86=E7=A0=81=E7=BB=9F=E4=B8=80=E4=B8=BA8-20=E4=BD=8D?= =?UTF-8?q?=E4=B8=94=E9=A1=BB=E5=90=AB=E5=A4=A7=E5=B0=8F=E5=86=99=E5=AD=97?= =?UTF-8?q?=E6=AF=8D/=E6=95=B0=E5=AD=97/=E7=89=B9=E6=AE=8A=E5=AD=97?= =?UTF-8?q?=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/validate.js | 74 ++++++++++++++++++++-- src/views/register.vue | 5 +- src/views/system/user/index.vue | 9 +-- src/views/system/user/profile/resetPwd.vue | 3 +- 4 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/utils/validate.js b/src/utils/validate.js index 702add4..5cf5c5c 100644 --- a/src/utils/validate.js +++ b/src/utils/validate.js @@ -1,9 +1,9 @@ /** - * 判断url是否是http或https + * 判断url是否是http或https * @param {string} path * @returns {Boolean} */ - export function isHttp(url) { +export function isHttp(url) { return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1 } @@ -12,7 +12,7 @@ * @param {string} path * @returns {Boolean} */ - export function isExternal(path) { +export function isExternal(path) { return /^(https?:|mailto:|tel:)/.test(path) } @@ -30,7 +30,8 @@ export function validUsername(str) { * @returns {Boolean} */ export function validURL(url) { - const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/ + const reg = + /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/ return reg.test(url) } @@ -66,7 +67,8 @@ export function validAlphabets(str) { * @returns {Boolean} */ export function validEmail(email) { - const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + const reg = + /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ return reg.test(email) } @@ -91,3 +93,65 @@ export function isArray(arg) { } return Array.isArray(arg) } + +// ============ 密码复杂度校验 ============ +// 规则(与 H5 端保持一致): +// 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 = /[^A-Za-z0-9\s]/ + +/** + * 校验密码复杂度,返回精确的失败原因 + * @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: '' } +} + +/** + * Element Plus (async-validator) 自定义校验函数 + * 用法:{ validator: passwordValidator, trigger: 'blur' } + * @param {object} rule + * @param {string} value + * @param {Function} callback + */ +export function passwordValidator(rule, value, callback) { + const { valid, message } = validatePassword(value) + if (valid) { + callback() + } else { + callback(new Error(message)) + } +} diff --git a/src/views/register.vue b/src/views/register.vue index c2336f6..54462e1 100644 --- a/src/views/register.vue +++ b/src/views/register.vue @@ -67,6 +67,7 @@