import { value, switchs, date } from "./config"; import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog"; var _value = value; const app = getApp(); Component({ options: { addGlobalClass: true,//解决继承全局样式问题 }, lifetimes: { attached: function () { this.init(); } }, properties: { type: { type: String, value: "edit" }, key: String }, observers: { 'value': function (value) { let A = value[0][1]["value"] * 1 || 0; let B = (value[0][3]["value"] * 1 || 0) + (value[0][4]["value"] * 1 || 0) + (value[0][5]["value"] * 1 || 0) + (value[0][6]["value"] * 1 || 0) + (value[0][7]["value"] * 1 || 0); let C = value[0][9]["value"] * 1 || 0; if (A == 0 || C == 0) return false; value[0][8]["value"] = ((parseFloat(A - C + B) / parseFloat(A + B)) * 100).toFixed(2) + "%"; if (this.result && this.result == value[0][8]["value"]) return false; this.result = value[0][8]["value"]; this.setData({ value }) } }, data: { value, active: '', activeObject: {}, activeIndex: [], switchB: false, endDate: date(), switchs, submitLock: false, saveLock: false, btn: true, radioInputShow: false, }, methods: { //组件石林进入页面接节点树执行 async init () { try { // 商品请求 var laompany = []; let res = await wx.$request({ method: "get", url: "/order/productDict/" }); laompany = res.data.map(item => ({ key: item.id, value: item.name, month: item.month })); let { type, value, switchs } = this.data; switchs['laompany'] = laompany; var obj = {}; switch (type) { case 'watch': obj = this.watchFunc(value); break; default: // 针对商品操作 非观看模式 this.data.key && laompany.forEach((item, index) => { if (item.key == this.data.key) { value[0][10]["value"] = item.key; value[0][10]["index"] = index; } }) break; } this.setData({ value, ...obj, switchs }) } catch (err) { wx.$err(err) } }, // 查看模式 watchFunc (data) { data = data.map(itemA => itemA.map(itemB => { if (itemB.hasOwnProperty('value')) itemB['disabled'] = true; return itemB }) ) return { value: data, btn: false } }, // 提交 async submitFunc () { try { let { value, submitLock } = this.data, megs = [], data = {}, cvalue = [], tels = []; if (submitLock) return false; this.setData({ submitLock: true }); if (value[2][6]['value'] == 0) { value = [...value[0], ...value[1], ...value[2]]; cvalue = [..._value[0], ..._value[1], ..._value[2]]; } else { value = [...value[0], ...value[1], ...value[2], ...value[3]]; cvalue = [..._value[0], ..._value[1], ..._value[2], ..._value[3]]; } value.forEach((item, index) => { // 特殊格式 非必须 if (!item.hasOwnProperty('value') || item.norequire) { if (item.hasOwnProperty('value')) data[item.name] = item.value; return false }; // 正常流程 // 判断是否为空 if (!item.value) { megs.push(`${item.title}不能为空!`); return false; }; // 数字类型 if (item.type == 'number' && !/^\d+.?\d*$/.test(item.value)) { megs.push(`${item.title}必须是数字!`); return false; } // 正则判断 if (cvalue[index].reg && !cvalue[index].reg.test(item.value)) { megs.push(`${item.title}格式不正确!`); return false; }; // 号码不能相同 tel tstel ctel cttel if (item.name == 'tel' || item.name == 'tstel' || item.name == 'ctel' || item.name == 'cttel') (!tels.some(item.value) || item.vlaue == '') && tels.push(item.value) ; data[item.name] = item.value; }); (tels.length !== 4 && tels.length != 0) && megs.push(`手机号码不能重复`); if (megs.length != 0) { this.setData({ errorShow: true, megs, submitLock: false }); return false; }; data['category'] = 1; // 申请贷款期限 不用传 delete data['maturity']; await wx.$request({ method: "post", url: "/order/", data }); this.setData({ submitLock: false }); Dialog.alert({ message: "已提交!" }).then(res => wx.reLaunch({ url: '/pages/index/index' })) } catch (err) { wx.$err(err) } }, // 保存 async saveFunc () { try { let { value, saveLock } = this.data, data = {}; if (saveLock) return false; this.setData({ saveLock: true }); // 拦截 var requeire = ['name', 'tel'], megs = []; value = [...value[0], ...value[1], ...value[2], ...value[3]]; var cvalue = [..._value[0], ..._value[1], ..._value[2], ..._value[3]]; value.forEach((item, index) => { if (!item.hasOwnProperty('value')) return false; if (requeire.some(itemB => itemB == item.name)) { if (!item.value) { megs.push(`${item.title}不能为空!`) return false }; if (cvalue[index].reg && !cvalue[index].reg.test(item.value)) { megs.push(`${item.title}格式不正确!`); return false; }; } data[item.name] = item.value; }); data['category'] = 0; // 申请贷款期限 不用传 delete data['maturity']; if (megs.length != 0) { this.setData({ errorShow: true, megs, saveLock: false }); return false; }; await wx.$request({ method: "post", url: "/order/", data }); this.setData({ saveLock: false }) Dialog.alert({ message: "已保存!" }).then(res => wx.reLaunch({ url: '/pages/index/index' })) } catch (err) { wx.$err(err) } }, // 输入 inputFunc (e) { let { value } = this.data, { index } = e.currentTarget.dataset; value[index[0]][index[1]]['value'] = e.detail.value; console.log(value[index[0]][index[1]]['name'], e.detail.value) this.setData({ value }) }, // switch switchFunc (e) { let { value, switchs } = this.data, { index } = e.currentTarget.dataset; let { name } = value[index[0]][index[1]]; if (switchs.hasOwnProperty(name)) { this.setData({ switchB: true, active: name, activeIndex: index }) } }, // 隐藏单选框 hideModal () { this.setData({ switchB: false }) }, // 单选框赋值 changeFunc (e) { let { activeIndex, value, switchs } = this.data; let _value = e.detail.value.split(","); value[activeIndex[0]][activeIndex[1]]['value'] = _value[0] * 1; value[activeIndex[0]][activeIndex[1]]['index'] = _value[1] * 1; // 申请贷款产品 申请贷款期限关联 if (value[activeIndex[0]][activeIndex[1]]['name'] = 'laompany') { value[0][11]['value'] = switchs['laompany'][_value[1] * 1]['month'] + '个月'; } // 第一借款人与申请人关系特殊处理 if (value[activeIndex[0]][activeIndex[1]]['name'] == 'mstatus') { value[2][2] = { ...value[2][2], disabled: _value[0] == 1 ? true : false, value: _value[0] == 1 ? 0 : '', index: _value[0] == 1 ? 0 : '' } } if (value[activeIndex[0]][activeIndex[1]].hasOwnProperty('forlen')) { for (let i = 1; i < (value[activeIndex[0]][activeIndex[1]]['forlen'] + 1); i++) { value[activeIndex[0]][(activeIndex[1] + i)]['norequire'] = _value[0] == 0 ? false : true; } } this.setData({ value, switchB: false }) }, // 日期 DateChange (e) { let { value } = this.data, { index } = e.currentTarget.dataset; value[index[0]][index[1]]['value'] = e.detail.value; this.setData({ value }) }, } })