96 lines
2.4 KiB
JavaScript
96 lines
2.4 KiB
JavaScript
const formatTime = date => {
|
|
const year = date.getFullYear()
|
|
const month = date.getMonth() + 1
|
|
const day = date.getDate()
|
|
const hour = date.getHours()
|
|
const minute = date.getMinutes()
|
|
const second = date.getSeconds()
|
|
|
|
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
|
|
}
|
|
|
|
const formatNumber = n => {
|
|
n = n.toString()
|
|
return n[1] ? n : `0${n}`
|
|
}
|
|
|
|
const validateForm = (rules,page)=>{
|
|
const result = [];
|
|
for(var key in rules){
|
|
((rules[key] instanceof Array)?rules[key]:[rules[key]]).map((item)=>{
|
|
let valid = true;
|
|
let value = page.data[key];
|
|
if(typeof value=='string'){
|
|
value = value.trim();
|
|
}
|
|
//非空
|
|
if(item.required){
|
|
if(!value){
|
|
valid = false;
|
|
}
|
|
}else if(item.length){
|
|
//绝对长度
|
|
if(value.length!=item.length){
|
|
valid = false;
|
|
}
|
|
}else if(item.type=='phone'){
|
|
if(value.length!=11){
|
|
valid = false;
|
|
}
|
|
}else if(item.maxLength||item.minLength){
|
|
if(value.length>(item.maxLength||Infinity)||value.length<item.minLength||0){
|
|
valid = false
|
|
}
|
|
}else if(item.min){
|
|
if(value<item.min){
|
|
valid = false;
|
|
}
|
|
}
|
|
if(valid){
|
|
page.setData({
|
|
[`${key}Message`]:''
|
|
});
|
|
}else{
|
|
if(item.message){
|
|
page.setData({
|
|
[`${key}Message`]:item.message
|
|
});
|
|
}
|
|
result.push({
|
|
[key]:item.message,autoFocus:item.autoFocus,key:key,shake:item.shake
|
|
})
|
|
}
|
|
})
|
|
}
|
|
const focusInput = result.find((item)=>item.autoFocus);
|
|
if(focusInput){
|
|
page.setData({
|
|
[`${focusInput.key}Focus`]:true
|
|
})
|
|
}
|
|
const shakeInput = result.find((item)=>item.shake);
|
|
if(shakeInput){
|
|
if(!shakeInput.animation){
|
|
shakeInput.animation = wx.createAnimation({
|
|
duration: 20,
|
|
})
|
|
}
|
|
shakeInput.animation.translateX(10).step();
|
|
shakeInput.animation.translateX(-10).step();
|
|
shakeInput.animation.translateX(10).step();
|
|
shakeInput.animation.translateX(-10).step();
|
|
shakeInput.animation.translateX(5).step();
|
|
shakeInput.animation.translateX(0).step();
|
|
// needSetData[`${key}Animation`] = item.animation.export();
|
|
page.setData({
|
|
[`${shakeInput.key}Animation`]:shakeInput.animation.export()
|
|
})
|
|
}
|
|
return result;
|
|
}
|
|
|
|
module.exports = {
|
|
formatTime,
|
|
validateForm
|
|
}
|