/* * @Author: 小明丶 * @Date: 2019-09-02 09:12:30 * @LastEditors: 小明丶 * @LastEditTime: 2019-09-02 09:18:52 * @Description: */ /** * Created by c.y * 2018/4/12 * form--提交数据组合--例如房抵贷 */ import Vue from 'vue'; import validate from './validator'; import IDValidator from './IDValidator'; // 把后台返回的null,转换为空字符串, infoObj需要检查 function changeNullToString (infoObj) { for (let i in infoObj) { infoObj[i] === null ? infoObj[i] = '' : infoObj[i] = infoObj[i]; } return infoObj; } // 组合后台返回省,市,区---来满足vux的地址选择插件需要的格式 // province省 city市 country区 function getAddressFormat (province, city, country) { let addressList = []; if (!validate.checkValEmpty(province)) { addressList.push(province); } if (!validate.checkValEmpty(city)) { addressList.push(city); } if (!validate.checkValEmpty(country)) { addressList.push(country); } return addressList; } // 判断地址类型的值,是否必填,并进行提示 // addressList, 代表需要校验的地址数组 function checkAddressRequired (addressList) { return validate.checkValEmpty(addressList[0]); } // 得到selector的列表 function getOptionsList (optionList) { let tmpList = []; if (optionList instanceof Array) { optionList.forEach(function (item) { tmpList.push({ key: item.code, value: item.name }); }); } return tmpList; } // 检查必填的字段以及错误提示, requiredList是检查规则, checkInfo要检查的对象 // context为this对象 function checkRequiredField (requiredList, checkInfo, context) { let allPassLabel = true; for (let i = 0; i < requiredList.length; i++) { // 如果配置了validator,那么就以validator进行验证,忽略其他验证(validator需要是函数) if (requiredList[i].validator && Object.prototype.toString.call(requiredList[i].validator) === '[object Function]') { if (!requiredList[i].validator(requiredList[i], checkInfo, context)) { allPassLabel = false; return false; } // 如果是地址的话 } else if (requiredList[i].type === 'address') { // 对于地址检查,是否为空,为空的话,返回true if (checkAddressRequired(context[requiredList[i].checkKey])) { checkValidateTip(requiredList[i], context); allPassLabel = false; return false; } // 如果是非地址的话,那么第一步检查是否为空 } else { // 第一步,先检查是否填写 if (validate.checkValEmpty(checkInfo[requiredList[i].key])) { checkValidateTip(requiredList[i], context); allPassLabel = false; return false; } // 如果有patter的话,直接使用pattern,即正则判断(pattern需要正则) if (requiredList[i].pattern && Object.prototype.toString.call(requiredList[i].pattern) === '[object RegExp]') { if(!requiredList[i].pattern.test(checkInfo[requiredList[i].key])) { checkRegularExpreTip(requiredList[i], context); allPassLabel = false; return false; } } // 对于特定类型,进行判断是否添加正确的类型 // 如果是名字类型 if (requiredList[i].type === 'name') { if (!validate.checkName(checkInfo[requiredList[i].key])) { checkRegularExpreTip(requiredList[i], context); allPassLabel = false; return false; } // 如果是手机号 } else if (requiredList[i].type === 'tel') { if (!validate.checkPhone(checkInfo[requiredList[i].key])) { checkRegularExpreTip(requiredList[i], context); allPassLabel = false; return false; } // 如果是身份证 } else if (requiredList[i].type === 'idCard') { if (!IDValidator.isValid(checkInfo[requiredList[i].key])) { checkRegularExpreTip(requiredList[i], context); allPassLabel = false; return false; } // 如果是座机 } else if (requiredList[i].type === 'lineTel') { if (!validate.checkLandline(checkInfo[requiredList[i].key])) { checkRegularExpreTip(requiredList[i], context); allPassLabel = false; return false; } // 如果是房屋楼层 } else if (requiredList[i].type === 'floor') { if (!validate.checkFloor(checkInfo[requiredList[i].key])) { checkRegularExpreTip(requiredList[i], context); allPassLabel = false; return false; } // 如果是银行卡 }else if (requiredList[i].type === 'bankCardNo') { if (!validate.bankCard(checkInfo[requiredList[i].key])) { checkRegularExpreTip(requiredList[i], context); allPassLabel = false; return false; } }else if (requiredList[i].type === 'isEmail') { if (!validate.checkEmail(checkInfo[requiredList[i].key])) { checkRegularExpreTip(requiredList[i], context); allPassLabel = false; return false; } } } } return allPassLabel; } // 错误提示, requiredItem为检查的对象,context为this对象 function checkValidateTip (requiredItem, context) { let _this = context; // tipType值为1为输入框,为2的话是下拉选择 if (requiredItem.tipType === 1) { // 如果进行正则验证的话,那么提示内容是正确,而一般的提示没有正确的这个提示字 // 如果没有type属性的话,那么就是默认的 Vue.prototype.$notify('请输入' + requiredItem.message, 'middle'); return false; } else if (requiredItem.tipType === 2) { Vue.prototype.$notify('请选择' + requiredItem.message, 'middle'); return false; } } // 错题提示,requiredItem为检查的对象,context为this对象 // 这个是正则匹配的检测或者其他特殊检测的提示 function checkRegularExpreTip (requiredItem, context) { Vue.prototype.$notify('请输入正确的' + requiredItem.message, 'middle'); return false; } // 封装提交数据 submitVal需要提交的val对象 function packageSubmitInfo (submitVal) { let submitInfo = {}; if (submitVal instanceof Object && submitVal !== null) { for (let i in submitVal) { // 对于非必填的话,其值为空的话,那么不传递给后台 if (!validate.checkValEmpty(submitVal[i])) { submitInfo[i] = submitVal[i]; } } } return submitInfo; } export default { getAddressFormat, getOptionsList, checkRequiredField, packageSubmitInfo, changeNullToString };