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.lengthitem.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 }