ttk-app-core
Version:
@ttk/recat enterprise develop framework
161 lines (158 loc) • 6.95 kB
JavaScript
import * as pattern from '@/utils/pattern'
import { byteLength } from '@/utils'
import { slFloat, jeFloat, djFloat } from "@/apps/app-demo/invoice-manage/constant"
function spaceValidator (rule, value, cb) {
if (value && value.includes(" ")) {
cb("不允许空格")
} else {
cb()
}
}
function maxlengthValidator (rule, value, cb) {
if (value && rule.maxlength) {
if (byteLength(value) > rule.maxlength) {
let msg = `超出${rule.maxlength}个字符`
if (rule.message) {
msg = rule.message
}
cb(msg)
return
}
}
cb()
}
function floatValidator (rule, value, cb) {
if (value && rule.maxlength) {
value = value.replace(/^(-|\+)*\d+\.?/g, "") // 先去掉整数和小数点,获得小数位
if (value.length > rule.maxlength) {
cb(`超出${rule.maxlength}位小数`)
return
}
}
cb()
}
// 定义需要校验的字段和校验规则, 支持同一个字段多条规则和异步校验,详情可参考
// https://github.com/yiminghe/async-validator
export const rules = {
isEmail: [{ type: "any",
async dependValidator(fields, value, cb) {
if (!value || fields.gmfEmail == "") {
cb("", "gmfEmail")
} else if (pattern.email.test(fields.gmfEmail)) {
cb("", "gmfEmail")
} else {
cb("请正确填写邮箱", "gmfEmail")
}
}
}],
isPhone: [{ type: "any",
async dependValidator(fields, value, cb) {
if (!value || fields.gmfMobile == "") {
cb("", "gmfMobile")
} else if (pattern.phone.test(fields.gmfMobile)) {
cb("", "gmfMobile")
} else {
cb("请正确填写手机", "gmfMobile")
}
}
}],
gmfEmail: [{ validator: spaceValidator }, { validator: maxlengthValidator, maxlength: 80 }, { type: 'string', pattern: pattern.email, message: '请正确填写邮箱'}],
gmfMobile: [{ validator: spaceValidator }, { validator: maxlengthValidator, maxlength: 13 }, { type: 'string', pattern: pattern.phone, message: '请正确填写手机号'}],
ghdwmc: [{ validator: spaceValidator }, { validator: maxlengthValidator, maxlength: 100, message: "请输入不超过50位中文" }, { type: 'string', required: true, message: '请填写购买方名称'}],
ghdwdzdh: [{ validator: maxlengthValidator, maxlength: 100, message: "请输入不超过40位中文和20位数字" }],
ghdwyhzh: [{ validator: maxlengthValidator, maxlength: 100, message: "请输入不超过35位中文和30位数字" }],
ghdwsbh: [{ validator: spaceValidator }, { validator: maxlengthValidator, maxlength: 20, message: "请输入15-20位的数字或字母" }, { type: 'string', pattern: pattern.nsrsbh, message: '请输入15-20位的数字或字母'}, {
async dependValidator(fields, value, cb) {
if (fields.xhdwsbh == value) {
cb("购买方识别号不能跟销售方识别号一样")
} else {
cb()
}
}
}],
// hsslbs: [{ type: 'string', required: true, message: '必填项'}],
xhdwmc: [{ type: 'string', required: true, message: '请填写名称'}, { validator: spaceValidator }],
xhdwsbh: [{ type: 'string', required: true, message: '请正确填写纳税人识别号'}, { validator: spaceValidator }],
xhdwdzdh: [{ type: 'string', required: true, message: '请正确填写地址、电话'}],
// xhdwyhzh: [{ type: 'string', required: true, message: '请填写开户行及账号'}],
skr: [{ validator: spaceValidator }, { validator: maxlengthValidator, maxlength: 16, message: "请输入不超过8位中文" }, ],
fhr: [{ validator: spaceValidator }, { validator: maxlengthValidator, maxlength: 16, message: "请输入不超过8位中文" }, ],
// bz: [{ validator: (rule, value, cb) => {
// if (value.includes(" ")) {
// cb("不允许空格")
// }
// }}]
bz: [{ validator: maxlengthValidator, maxlength: 186, message: "请输入不超过93位中文" }, ]
}
export const balanceRules = {
tmpSalePrice: [{ validator: floatValidator, maxlength: jeFloat },{ type: 'string', pattern: pattern.money, required: true, message: '请正确填写金额'}, { validator: spaceValidator }],
kce: [{ validator: floatValidator, maxlength: jeFloat },{ type: 'string', pattern: pattern.money, required: true, message: '请正确填写金额'}, { validator: spaceValidator }],
}
export const goodsRules = {
spmc: [{ type: 'string', required: true, message: '请选择商品'}], // 当没选择商品时,提示请选择商品
ggxh: [{ validator: spaceValidator }, { validator: maxlengthValidator, maxlength: 40, message: "请输入不超过20位中文" }],
dw: [{ validator: spaceValidator }, { validator: maxlengthValidator, maxlength: 14, message: "请输入不超过7位中文" }],
spsl: [{validator: spaceValidator}, { validator: maxlengthValidator, maxlength: 16 }, { validator: floatValidator, maxlength: slFloat }, { type: "any", async dependValidator(fields, value, cb) {
if (!fields.dj && !value) {
cb()
cb('', 'dj')
} else if (pattern.moneySpe.test(value)) {
cb()
} else {
cb("请正确填写数字")
}
}}],
dj: [{validator: spaceValidator}, { validator: maxlengthValidator, maxlength: 16 },{ validator: floatValidator, maxlength: djFloat },{ type: "any", async dependValidator(fields, value, cb) {
if (!fields.je && !value) {
cb()
} else if (!fields.spsl && !value) {
cb('')
cb('', 'spsl')
} else if (pattern.moneySpe.test(value)) {
cb()
} else {
cb("请正确填写金额")
}
}}],
je: [{validator: spaceValidator}, { validator: maxlengthValidator, maxlength: 12 },{ validator: floatValidator, maxlength: jeFloat },{ type: "any", async dependValidator(fields, value, cb) {
if (fields.fphxz != 0) {
cb()
} else if (!value) {
cb("请正确填写金额")
} else if (pattern.money.test(value)) {
cb()
if (fields.zkType == 2 && fields.zk >= Number(value)) {
cb("不能大于等于商品金额", "zk")
} else if (fields.zkType == 2) {
cb("", "zk")
}
} else {
cb("请正确填写金额")
}
}}],
zk: [{validator: spaceValidator}, { validator: maxlengthValidator, maxlength: 16 }, { type:"any", async dependValidator(fields, value, cb) {
value = Number(value) // 先将转成数字,以便下面进行数字大小的比较,不转value的原因是正则对数字校验不准确
if (value == "") {
cb()
return
}
if (fields.zkType == 1) {
if (!pattern.rate.test(value)) {
cb("折扣率必须0到100之间")
} else {
cb()
}
} else {
if (!pattern.money.test(value)) {
cb("请正确填写金额")
} else if (value >= fields.je) {
cb("不能大于等于商品金额")
} else if (value <= 0) {
cb("不能小于0")
} else {
cb()
}
}
}
}]
}