import Message from 'tdesign-miniprogram/message/index';
|
|
/**
|
* 信息输入逻辑
|
* 包括输入信息存储、输入规范性检测、错误提示等
|
*/
|
export const useFormCheck = Behavior({
|
data: {
|
msg: [],
|
info: {},
|
},
|
lifetimes: {
|
attached: function () {},
|
},
|
methods: {
|
selectChange(e) {
|
const index = e.currentTarget.dataset.index;
|
const options = e.detail.options;
|
this.setData({
|
[`msg[${index}].options`]: options,
|
});
|
},
|
|
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,
|
});
|
}
|
},
|
|
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 + '].status';
|
this.setData({
|
[path]: value,
|
[nPath]: 'success',
|
});
|
}
|
}
|
},
|
|
/**
|
* 信息完整度检查
|
*/
|
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}].status`]: 'error',
|
});
|
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) {
|
this.showErrorMsg(i, '两次输入的密码不一致')
|
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.showErrorMsg(i, pwError)
|
return false;
|
}
|
}
|
}
|
|
return true;
|
},
|
|
showErrorMsg(index, err = `${this.data.msg[index].name}不可为空`) {
|
// Message.error({
|
// context: this,
|
// offset: [20, 32],
|
// duration: 5000,
|
// content: err,
|
// });
|
if (index != undefined) {
|
this.setData({
|
[`msg[${index}].status`]: 'error',
|
[`msg[${index}].tips`]: err,
|
});
|
}
|
},
|
|
onSubmit: function () {
|
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);
|
}
|
}
|
},
|
},
|
});
|