Files
2025-06-13 21:16:12 +08:00

187 lines
3.5 KiB
JavaScript

// 表单校验规则由 schema2code 生成,不建议直接修改校验规则,而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema
const validator = {
"create_date": {
"rules": [
{
"format": "timestamp"
}
]
},
"device_uuid": {
"rules": [
{
"format": "string"
}
]
},
"ip": {
"rules": [
{
"format": "string"
}
]
},
"state": {
"rules": [
{
"format": "int"
}
]
},
"type": {
"rules": [
{
"format": "string"
},
{
"range": [
{
"value": "logout",
"text": "logout"
},
{
"value": "login",
"text": "login"
},
{
"value": "register",
"text": "register"
},
{
"value": "reset-pwd",
"text": "reset-pwd"
},
{
"value": "bind-mobile",
"text": "bind-mobile"
},
{
"value": "bind-weixin",
"text": "bind-weixin"
},
{
"value": "bind-qq",
"text": "bind-qq"
},
{
"value": "bind-apple",
"text": "bind-apple"
},
{
"value": "bind-alipay",
"text": "bind-alipay"
}
]
}
]
},
"ua": {
"rules": [
{
"format": "string"
}
]
},
"user_id": {
"rules": [
{
"required": true
},
{
"format": "string"
}
]
},
"username": {
"rules": [
{
"format": "string"
}
]
},
"email": {
"rules": [
{
"format": "string"
}
]
},
"mobile": {
"rules": [
{
"format": "string"
}
]
},
"appid": {
"rules": [
{
"format": "string"
}
]
}
}
const enumConverter = {
"type_valuetotext": {
"logout": "logout",
"login": "login",
"register": "register",
"reset-pwd": "reset-pwd",
"bind-mobile": "bind-mobile",
"bind-weixin": "bind-weixin",
"bind-qq": "bind-qq",
"bind-apple": "bind-apple",
"bind-alipay": "bind-alipay"
}
}
function filterToWhere(filter, command) {
let where = {}
for (let field in filter) {
let { type, value } = filter[field]
switch (type) {
case "search":
if (typeof value === 'string' && value.length) {
where[field] = new RegExp(value)
}
break;
case "select":
if (value.length) {
let selectValue = []
for (let s of value) {
selectValue.push(command.eq(s))
}
where[field] = command.or(selectValue)
}
break;
case "range":
if (value.length) {
let gt = value[0]
let lt = value[1]
where[field] = command.and([command.gte(gt), command.lte(lt)])
}
break;
case "date":
if (value.length) {
let [s, e] = value
let startDate = new Date(s)
let endDate = new Date(e)
where[field] = command.and([command.gte(startDate), command.lte(endDate)])
}
break;
case "timestamp":
if (value.length) {
let [startDate, endDate] = value
where[field] = command.and([command.gte(startDate), command.lte(endDate)])
}
break;
}
}
return where
}
export { validator, enumConverter, filterToWhere }