import moment from '../../utils/moment.min'; /** * 信息输入逻辑 * 包括输入信息存储、输入规范性检测、错误提示等 * @see "/data/sceneInfo.js" */ module.exports = Behavior({ properties: { form: { type: Array, observer(value) { this.setData({ msg: value }); }, }, }, data: { msg: [], info: {}, start: moment().format('YYYY年MM月DD日'), }, lifetimes: { attached: function () {}, }, methods: { selectChange(e) { const index = e.currentTarget.dataset.index; const options = e.detail.options; this.setData({ [`msg[${index}].options`]: options, }); this.onItemChange(); }, pickerChange(e) { console.log(e); const { index, mode } = e.currentTarget.dataset; if (mode == 'region') { const i = e.detail.value.concat(e.detail.code); this.setData({ [`msg[${index}].value`]: i, }); } else if (mode == 'selector') { const i = e.detail.value; this.setData({ [`msg[${index}].selectIndex`]: i, [`msg[${index}].value`]: this.data.msg[index].options[i].value, }); } else if (mode == 'date') { const i = e.detail.value; this.setData({ [`msg[${index}].value`]: i, }); } this.onItemChange(); }, changeMsg(e) { let id = e.detail.params.id; let value = e.detail.params.value; let msg = this.data.msg; let msgLength = msg.length; for (let i = 0; i < msgLength; i++) { if (msg[i].id === id) { let path = 'msg[' + i + '].value'; let nPath = 'msg[' + i + '].noValue'; this.setData({ [path]: value, [nPath]: false, }); } } this.onItemChange(); }, /** * 信息完整度检查 */ submitCheck() { //清空缓存的密码 this.lastPW = ''; let msg = this.data.msg; //确认信息完整度 for (let i = 0; i < msg.length; i++) { const m = msg[i]; //选填项直接略过 if (m.required == false) continue; //复选框和单选框(复选框的逻辑包含了单选框) if (m.type == 'checkbox' || m.type == 'radio') { // console.log('m.options', m.options); m.value = ''; for (let t = 0; t < m.options.length; t++) { const o = m.options[t]; if (o.checked) { if (m.value != '') { m.value += ';'; } if (o.hasRemark) { // console.log('o.remark', o.remark); if (o.remark == '') { this.setData({ [`msg[${i}].options[${t}].noValue`]: true, }); this.showErrorMsg(i, '备注信息不能为空'); return false; } else { m.value += o.remark; } } else { m.value += o.name; } } } if (m.value == '') { this.showErrorMsg(i); return false; } } //下拉选择框 else if (m.type == 'picker') { if (m.pickerMode == 'selector') { if (m.selectIndex == 0) { this.showErrorMsg(i, `${this.data.msg[i].name}未选择`); return false; } else { m.value = m.options[m.selectIndex].name; } } else if (m.pickerMode == 'region') { if (m.value.length == 0) { this.showErrorMsg(i, `${this.data.msg[i].name}未选择`); return false; } } } //输入框 //必填项空白检测 else if (m.value === '' || m.value === null || m.value === undefined) { this.showErrorMsg(i); return false; } //输入框下的密码类型检测 else if (m.type == 'password') { //密码一致性确认 if (this.lastPW == '') { this.lastPW = m.value; } else if (this.lastPW != m.value) { let error = '两次输入的密码不一致'; this.setData({ errorMsg: error, }); return false; } //密码复杂度确认 var pwError; var pw = m.value; var regex1 = /[a-zA-Z]/; var regex2 = /[0-9]/; var r = regex1.test(pw) && regex2.test(pw); console.log('regex:' + r); if (pw.length < 6) { pwError = '密码位数最少6位'; } else if (!r) { pwError = '密码必须包含字母和数字'; } if (pwError) { this.setData({ errorMsg: pwError, }); return false; } } } return true; }, showErrorMsg(index, err = `${this.data.msg[index].name}不可为空`) { this.setData({ errorMsg: err, [`msg[${index}].noValue`]: true, }); const id = '#' + this.data.msg[index].id; wx.createSelectorQuery() .select('.page') .boundingClientRect(res1 => { const top1 = res1.top; wx.createSelectorQuery() .in(this) .select(id) .boundingClientRect(res => { const h = res.top - top1 - 60; // console.log(h); wx.pageScrollTo({ // duration: 300, scrollTop: h, // selector: id, // offsetTop: -60, }); }) .exec(); }) .exec(); }, onSubmit: function () { console.log('onSubmit'); if (!this.submitCheck()) return; let msg = this.data.msg; if (msg) { const info = this.data.info; msg.forEach(m => { info[m.id] = m.value; }); this.setData({ info }); this.triggerEvent('onSubmit', info); if (typeof this._submit === 'function') this._submit(info); } }, onItemChange() { this.triggerEvent('itemChange', this.data.msg); }, }, });