From 41949ba4e330a3a35ef9bf998c6ea2423cf21718 Mon Sep 17 00:00:00 2001 From: lbw Date: Thu, 2 Mar 2023 11:27:53 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=20=E4=BC=98=E5=8C=96=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/login/index.ts | 4 +- src/utils/validate.ts | 97 +++++++++++----------- src/views/admin/user/form.vue | 88 +++++++++----------- src/views/login/component/authredirect.vue | 2 +- 4 files changed, 89 insertions(+), 102 deletions(-) diff --git a/src/api/login/index.ts b/src/api/login/index.ts index b56b94de..f4eba216 100644 --- a/src/api/login/index.ts +++ b/src/api/login/index.ts @@ -1,6 +1,6 @@ import request from '/@/utils/request'; import { Session } from "/@/utils/storage"; -import { rule } from "/@/utils/validate" +import { validateNull } from "/@/utils/validate" import { useUserInfo } from "/@/stores/userInfo"; /** @@ -99,7 +99,7 @@ export const checkToken = (refreshTime: number, refreshLock: boolean) => { params: { token: Session.get("token") } }) .then((response) => { - if (rule.validatenull(response) || response.code === 1) { + if (validateNull(response) || response.code === 1) { clearInterval(refreshTime) return } diff --git a/src/utils/validate.ts b/src/utils/validate.ts index 14cb3a69..3e436518 100644 --- a/src/utils/validate.ts +++ b/src/utils/validate.ts @@ -1,49 +1,8 @@ -/** - * @desc [自定义校验规则] - * @example - * import { validateRule } from "@/utils/validateRules"; - * rules: [ - * { validator: validateRule.emailValue, trigger: 'blur'} - * ] - */ - -export const getRegExp = function (validatorName) { - const commonRegExp = { - number: '/^[-]?\\d+(\\.\\d+)?$/', - letter: '/^[A-Za-z]+$/', - letterAndNumber: '/^[A-Za-z0-9]+$/', - mobilePhone: '/^[1][3-9][0-9]{9}$/', - letterStartNumberIncluded: '/^[A-Za-z]+[A-Za-z\\d]*$/', - noChinese: '/^[^\u4e00-\u9fa5]+$/', - chinese: '/^[\u4e00-\u9fa5]+$/', - email: '/^([-_A-Za-z0-9.]+)@([_A-Za-z0-9]+\\.)+[A-Za-z0-9]{2,3}$/', - url: '/^([hH][tT]{2}[pP]:\\/\\/|[hH][tT]{2}[pP][sS]:\\/\\/)(([A-Za-z0-9-~]+)\\.)+([A-Za-z0-9-~\\/])+$/', - } - - return commonRegExp[validatorName] -} - -const validateFn = function (validatorName, rule, value, callback, defaultErrorMsg) { - //空值不校验 - if (validatenull(value) || (value.length <= 0)) { - callback() - return - } - - const reg = eval(getRegExp(validatorName)) - - if (!reg.test(value)) { - let errTxt = rule.errorMsg || defaultErrorMsg - callback(new Error(errTxt)) - } else { - callback() - } -} - /** * 判断是否为空 + * @param val 数据 */ -const validatenull = (val: any) => { +export const validateNull = (val: any) => { if (typeof val === 'boolean') { return false } @@ -62,6 +21,7 @@ const validatenull = (val: any) => { } + export const rule = { /** * 校验 请输入中文、英文、数字包括下划线 @@ -107,7 +67,7 @@ export const rule = { validatePhone(rule: any, value: any, callback: any) { var isPhone = /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/ - if(value.indexOf('****') >= 0){ + if (value.indexOf('****') >= 0) { return callback() } @@ -118,9 +78,6 @@ export const rule = { } }, - - - /* 数字 */ number(rule, value, callback) { validateFn('number', rule, value, callback, '[' + rule.label + ']包含非数字字符') @@ -141,7 +98,6 @@ export const rule = { validateFn('mobilePhone', rule, value, callback, '[' + rule.label + ']手机号码格式有误') }, - /* 字母开头,仅可包含数字 */ letterStartNumberIncluded(rule, value, callback) { validateFn('letterStartNumberIncluded', rule, value, callback, '[' + rule.label + ']必须以字母开头,可包含数字') @@ -169,11 +125,10 @@ export const rule = { regExp(rule, value, callback) { //空值不校验 - if (validatenull(value) || (value.length <= 0)) { + if (validateNull(value) || (value.length <= 0)) { callback() return } - const pattern = eval(rule.regExp) if (!pattern.test(value)) { let errTxt = rule.errorMsg || '[' + rule.label + ']invalid value' @@ -185,4 +140,46 @@ export const rule = { } +/** + * @desc [自定义校验规则] + * @example + * import { validateRule } from "@/utils/validateRules"; + * rules: [ + * { validator: validateRule.emailValue, trigger: 'blur'} + * ] + */ + +export const getRegExp = function (validatorName) { + const commonRegExp = { + number: '/^[-]?\\d+(\\.\\d+)?$/', + letter: '/^[A-Za-z]+$/', + letterAndNumber: '/^[A-Za-z0-9]+$/', + mobilePhone: '/^[1][3-9][0-9]{9}$/', + letterStartNumberIncluded: '/^[A-Za-z]+[A-Za-z\\d]*$/', + noChinese: '/^[^\u4e00-\u9fa5]+$/', + chinese: '/^[\u4e00-\u9fa5]+$/', + email: '/^([-_A-Za-z0-9.]+)@([_A-Za-z0-9]+\\.)+[A-Za-z0-9]{2,3}$/', + url: '/^([hH][tT]{2}[pP]:\\/\\/|[hH][tT]{2}[pP][sS]:\\/\\/)(([A-Za-z0-9-~]+)\\.)+([A-Za-z0-9-~\\/])+$/', + } + return commonRegExp[validatorName] +} + +const validateFn = function (validatorName, rule, value, callback, defaultErrorMsg) { + //空值不校验 + if (validateNull(value) || (value.length <= 0)) { + callback() + return + } + + const reg = eval(getRegExp(validatorName)) + + if (!reg.test(value)) { + let errTxt = rule.errorMsg || defaultErrorMsg + callback(new Error(errTxt)) + } else { + callback() + } +} + + diff --git a/src/views/admin/user/form.vue b/src/views/admin/user/form.vue index 36d07ede..e3428f4c 100644 --- a/src/views/admin/user/form.vue +++ b/src/views/admin/user/form.vue @@ -1,7 +1,7 @@