import request from '@/components/thrid/em-element-ui/src/tools/request'; import { isTypeof, utilsTrim, formatDate, axiosReqParams, responseKeyToValue, mergeRequestParams, LOCAL_STORAGE_CURR } from '@/components/thrid/em-element-ui/src/tools/utils'; //form表单提交数据解释 export function submitQueryData(_this, paramType = true) { let res = {}, rowObj = _this.rowData, // 当前操作数据 btnType = rowObj._btn && rowObj._btn.type ? rowObj._btn.type : '', // 操作按钮 oldRow = _this.rowData._oldData || {}, // 操作前数据 currEvent = _this.queryURL.hasOwnProperty(btnType) ? _this.queryURL[btnType] || {} : {}, // 操作事件的对象 isInFormData = currEvent.hasOwnProperty('indata') ? currEvent.indata : true, // 是否是提交所有表单数据,默认true isparams = _this.options.hasOwnProperty('isparams') ? _this.options.isparams : false; // disabled行输入框是否提交参数 // 用户自带参数 if (paramType && rowObj._params && isTypeof(rowObj._params) === 'object') { for (let [pk, pv] of Object.entries(rowObj._params)) { res[pk] = pv; } } _this.pageColumn.forEach(item => { let postValue = '', field = item.field, isCompares = true, inputType = _this.inputType ? _this.inputType : 'show', fieldKey = item[inputType] && item[inputType].field ? item[inputType].field : item.field, subFieldKey = item[inputType] && item[inputType].subField ? item[inputType].subField : '', noShow = item[inputType] && item[inputType].noShow ? item[inputType].noShow : 0, is_all_form_data = isInFormData || item.hasOwnProperty(inputType), is_field = !((btnType == 'add' && (noShow & 1) == 1) || (btnType == 'edit' && (noShow & 2) == 2)), is_disabled = isparams === false ? true : ((isparams & 1) == 1 ? true : (btnType == 'add' ? true : (item[inputType] && item[inputType].isDisabled ? false : true))), is_auto_push = isparams === false ? true : ((isparams & 2) == 2 ? true : (item.hasOwnProperty('isAutoPush') ? item.isAutoPush : true)), is_submit = isTypeof(item.ispush) == 'undefined' ? true : item.ispush; //is_submit代表是否提交改字段, is_field控制列表字段在不同操作事件中显示情况 if (is_submit && is_field && is_auto_push && is_disabled && is_all_form_data) { let currValue = rowObj[field]; if (isTypeof(currValue) == 'object') { postValue = (item[inputType] && item[inputType].dataType === 'object') ? currValue : JSON.stringify(currValue); } else if (isTypeof(currValue) == 'array') { // 多字段参数 if (item[inputType] && item[inputType].mulField && isTypeof(item[inputType].mulField) === 'object') { // 多选下拉列表对象参数 if (item[inputType].type == 'select' && item[inputType].multiple && currValue.length > 0) { let tmpDataArray = []; currValue.forEach(selectItem => { let tmpDataObject = {}, tmpFieldArray = isTypeof(selectItem) == 'string' ? selectItem.split(item[inputType].sign ? item[inputType].sign : '#0') : selectItem; for (let [kk, vv] of Object.entries(item[inputType].mulField)) { let fieldType = isTypeof(tmpFieldArray[vv]), tmpFieldValue = tmpFieldArray[vv] ? (fieldType === 'object' ? JSON.stringify(tmpFieldArray[vv]) : (fieldType === 'array' ? JSON.stringify(tmpFieldArray[vv]) : (fieldType === 'undefined' ? '' : tmpFieldArray[vv]))) : ''; tmpDataObject[kk] = tmpFieldValue; } tmpDataArray.push(tmpDataObject); }) postValue = tmpDataArray; } else { // 多字段单个字符串传参 for (let [kk, vv] of Object.entries(item[inputType].mulField)) { let fieldType = isTypeof(currValue[vv]), tmpFieldValue = currValue[vv] ? (fieldType === 'object' ? JSON.stringify(currValue[vv]) : (fieldType === 'array' ? JSON.stringify(currValue[vv]) : (fieldType === 'undefined' ? '' : currValue[vv]))) : ''; formInputDataCache(item, inputType, kk, tmpFieldValue); formDataParams(res, kk, tmpFieldValue, item, inputType, isCompares, subFieldKey, rowObj); } return false; } } else if (item[inputType] && item[inputType].type == 'file') { if (item[inputType].iType == 'string') { let tmpFileParams = [] currValue.forEach(fileParams => { tmpFileParams.push(fileParams[(item[inputType].paramField ? item[inputType].paramField : 'url')]) }) postValue = item[inputType].hasOwnProperty('sign') ? tmpFileParams.join(item[inputType].sign) : tmpFileParams.join(',') } else { postValue = currValue } } else if (item[inputType] && (item[inputType].type == 'checkbox-group' || item[inputType].type == 'input-range')) { if (item[inputType].iType == 'string') { postValue = item[inputType].hasOwnProperty('sign') ? currValue.join(item[inputType].sign) : currValue.join(',') } else { postValue = currValue } } else if (item[inputType] && (item[inputType].type == 'cascader' || item[inputType].type == 'select')) { if (item[inputType] && item[inputType].iType == 'string') { if (item[inputType].hasOwnProperty('sign')) { postValue = item[inputType].sign ? currValue.join(item[inputType].sign) : currValue.join(''); } else { postValue = currValue.join(); } } else { postValue = item[inputType].dataType === 'array' ? currValue : JSON.stringify(currValue); } } else { postValue = item[inputType].dataType === 'array' ? currValue : JSON.stringify(currValue); } } else if (isTypeof(currValue) == 'boolean') { postValue = currValue; } else if (typeof (currValue) == 'undefined') { postValue = ''; } else { if (item[inputType] && item[inputType].mulField && isTypeof(item[inputType].mulField) === 'object') { for (let [kk, vv] of Object.entries(item[inputType].mulField)) { let tmpFieldArray = isTypeof(currValue) == 'string' ? currValue.split(item[inputType].sign ? item[inputType].sign : '#0') : currValue, tmpFieldValue = tmpFieldArray[vv] ? tmpFieldArray[vv] : ''; formInputDataCache(item, inputType, kk, tmpFieldValue) formDataParams(res, kk, tmpFieldValue, item, inputType, isCompares, subFieldKey, rowObj); } return false; } else if (item[inputType] && item[inputType].valueField) { let mapValue = rowObj[item[inputType].valueField] postValue = mapValue if (item[inputType].props) { let tmpMapValue = item[inputType].props && item[inputType].props.hasOwnProperty('index') ? (mapValue && mapValue[item[inputType].props.index]) : mapValue; postValue = item[inputType].props.key && tmpMapValue && tmpMapValue.hasOwnProperty(item[inputType].props.key) ? tmpMapValue[item[inputType].props.key] : '' } } else { postValue = currValue; } } if (item[inputType] && item[inputType].isCompare && postValue == oldRow[field]) { isCompares = false } formInputDataCache(item, inputType, fieldKey, postValue); formDataParams(res, fieldKey, postValue, item, inputType, isCompares, subFieldKey, rowObj); // 提交字段对应多个字段 if (item[inputType] && item[inputType].fieldList && isTypeof(item[inputType].fieldList) == 'array') { let tmpMergeValue = []; item[inputType].fieldList.forEach(currField => { res[currField] ? tmpMergeValue.push(res[currField]) : tmpMergeValue.push(rowObj[currField].toString()) }) res[fieldKey] = tmpMergeValue.join(item[inputType].sign ? item[inputType].sign : '') formInputDataCache(item, inputType, fieldKey, res[fieldKey]); } } }); return res; } // 缓存编辑的字段 export function formInputDataCache(item, inputType, field, value) { value = item[inputType] && (!item[inputType].hasOwnProperty('trim') || (item[inputType].hasOwnProperty('trim') && item[inputType].trim === true)) ? utilsTrim(value) : value; if (item[inputType] && item[inputType].isCache) { let tmpObj = {}, tmpLocalData = [], key = LOCAL_STORAGE_CURR + (item[inputType].cacheField ? item[inputType].cacheField : field); if (localStorage.getItem(key)) { tmpLocalData = JSON.parse(localStorage.getItem(key)); tmpLocalData = isTypeof(tmpLocalData) == 'array' ? tmpLocalData : []; } tmpLocalData.unshift({ value }); //数组去重 tmpLocalData = tmpLocalData.reduce((cur, next) => { tmpObj[next.value] ? "" : tmpObj[next.value] = true && cur.push(next); return cur; }, []); localStorage.setItem(key, JSON.stringify(tmpLocalData)); } } export function formDataParams(data, key, value, item, inputType, isCompares, subKey = '', objRow = {}) { if (item[inputType] && item[inputType].dataType) { let dataType = isTypeof(item[inputType].dataType) === 'string' ? item[inputType].dataType : (item[inputType].dataType.hasOwnProperty(key) ? item[inputType].dataType[key] : '') // 根据不同dataType转变value值 switch (dataType) { case 'number': value = Number(value); break; case 'boolean': value = value ? true : false; break; case 'date': value = formatDate(value, 'yyyy-MM-dd'); break; case 'datetime': value = formatDate(value, 'yyyy-MM-dd hh:mm:ss'); break; case 'object': value = value === '' ? null : value; break; } } value = item[inputType] && (!item[inputType].hasOwnProperty('trim') || (item[inputType].hasOwnProperty('trim') && item[inputType].trim === true)) ? utilsTrim(value) : value; // 封装参数格式 if (item[inputType] && item[inputType].parent) { let parent = item[inputType].parent if (isTypeof(parent) === 'array') { const parentList = [...item[inputType].parent]; parent = parentList.pop() parentList.forEach(dataItem => { if (!data.hasOwnProperty(dataItem)) { data[dataItem] = {}; } data = data[dataItem]; }) } if (!data.hasOwnProperty(parent)) { data[parent] = {}; } item[inputType].isNode && (data[key] = value) isCompares && (data[parent][key] = value); if (subKey) { data[parent][subKey] = objRow[subKey]; } if (item[inputType].isNode) { data[key] = value; subKey && (data[subKey] = objRow[subKey]); } } else { isCompares && (data[key] = value); if (subKey) { data[subKey] = objRow[subKey]; } } return data; } ///提交edit form表单 export function submitEditFormInfo(_this, formName) { _this.$refs[formName].validate((valid) => { if (valid) { let queryData = submitQueryData(_this); request(axiosReqParams(_this.rowData, mergeRequestParams(queryData, _this.publicParams), _this.rowData._axios, _this.options)).then((response) => { let result = responseKeyToValue(response, _this, _this, _this.rowData._btn.type); if (result !== false) { _this.$emit('reload'); _this.$emit('clear', 'edit'); } }).catch((err) => { }); } }); } ///dialog弹出按钮事件处理 export function otherEvent(_this, item, formName) { if (_this.queryURL.hasOwnProperty(item.type)) { _this.$refs[formName].validate((valid) => { if (valid) { let queryData = submitQueryData(_this, false), data = _this.rowData._params && _this.rowData._params.data ? (_this.rowData._params.data || {}) : {}, queryTmpData = Object.assign({}, data, queryData); request(axiosReqParams(_this.rowData, mergeRequestParams(queryTmpData, _this.publicParams), _this.rowData._axios, _this.options)).then((response) => { let result = responseKeyToValue(response, _this, _this, _this.rowData._btn.type); if (result !== false) { _this.$emit('reload'); _this.$emit('clear', 'edit'); } }).catch((err) => { }); } }); } else { _this.$emit('onListEvent', item, _this.rowData); } } // dialog弹出按钮事件处理 export function otherDialogEvent(_this, item) { if (_this.queryURL.hasOwnProperty(item.type)) { submitRemoveEvent(_this) } else { _this.$emit('onListEvent', item, _this.rowData); } } //提交确认按钮,做rules验证 export function submitRemoveEvent(_this) { let rules = _this.rowData._rules; if (rules) { if (rules.isRules) { request(axiosReqParams(rules, mergeRequestParams(rules.params, _this.publicParams), rules._axios, _this.options)).then((response) => { if (response.data && response.data.total > 0) { _this.$message({ type: 'error', message: rules.message }); } else { submitRemoveFormInfo(_this); } }); } else { submitRemoveFormInfo(_this); } } else { submitRemoveFormInfo(_this); } } ///提交删除操作 export function submitRemoveFormInfo(_this) { let item = Object.assign({}, _this.rowData), data = _this.rowData._params.data; item._url = _this.rowData._url ? _this.rowData._url : _this.rowData.url; item._method = _this.rowData._params.method; request(axiosReqParams(item, mergeRequestParams(data, _this.publicParams), _this.rowData._axios, _this.options)).then((response) => { let result = responseKeyToValue(response, _this, _this, _this.rowData._btntype); _this.$emit('reload'); _this.subClearBtn(); }).catch((err) => { }); }