index.js 7.8 KB
Newer Older
王绍森's avatar
王绍森 committed
1 2 3 4 5 6 7
import * as service from '../AffairPage/publicApiService';
import moment from 'moment';
import { isJSON } from '@/baseComponent/utils';
import { getDefaultValues } from '../AffairPage/destruction';
import { message } from 'antd';

const text = [
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	'BINARY',
	'BIT',
	'BLOB',
	'CHAR',
	'GEOMETRY',
	'GEOMETRYCOLLECTION',
	'JSON',
	'LINESTRING',
	'LONGBLOB',
	'LONGTEXT',
	'MEDIUMBLOB',
	'MEDIUMINT',
	'MEDIUMTEXT',
	'MULTILINESTRING',
	'MULTIPOINT',
	'MULTIPOLYGON',
	'POINT',
	'POLYGON',
	'REAL',
	'DECIMAL',
	'ENUM',
	'SET',
	'TEXT',
	'TINYBLOB',
	'TIBYTEXT',
	'VARBINARY',
	'VARCHAR',
王绍森's avatar
王绍森 committed
35 36 37 38 39
];

const number = ['BIGINT', 'DOUBLE', 'FLOAT', 'INT', 'INTEGER', 'NUMERIC', 'SMALLINT', 'TINYINT'];

const date = ['DATE', 'DATETIME', 'TIME', 'TIMESTAMP', 'YEAR'];
40

王绍森's avatar
王绍森 committed
41
const format = {
42 43 44 45 46 47
	// 欢哥那边定义好的 时间格式类型
	DATE: 'YYYY-MM-DD HH:mm',
	DATETIME: 'YYYY-MM-DD HH:mm:ss',
	TIME: 'HH:mm:ss',
	TIMESTAMP: 'HH:mm:ss',
	YEAR: 'YYYY',
王绍森's avatar
王绍森 committed
48
};
49
const taskNode = ['TASKNODE'];
王绍森's avatar
王绍森 committed
50 51 52 53 54

/**
 * 将时间格式转化为unix时间戳
 * */
const transLateTimeTOUnix = (value, dataType) => {
55 56 57 58 59
	if (date.indexOf(dataType) > -1 && value) {
		return moment(value).format('x');
	} else {
		return value;
	}
王绍森's avatar
王绍森 committed
60 61
};

62 63 64 65 66 67 68
const getFormArrayConfig = (header) => {
	let config = [];
	for (let item of header) {
		if (item.isPrimaryKey || item.isHidden || item.dataIndex === 'operationEdit') {
			// 主键
			continue;
			/* config.push(
王绍森's avatar
王绍森 committed
69 70 71 72 73 74 75 76
         {
           key: item.name,
           name: item.title,
           type: 'input',
           placeHolder: `请输入${item.name}`,
           required: !item.isNull,
         }
       );*/
77 78 79 80 81 82 83
		} else if (item.options && Array.isArray(item.options) && item.options.length) {
			config.push({
				key: item.name,
				name: item.title,
				type: 'select',
				required: !item.isNull,
				base52: item.base52,
84
				options: item.options,
85
			});
86 87 88 89 90 91 92
		} else if (text.indexOf(item.type) > -1) {
			config.push({
				key: item.name,
				name: item.title,
				type: 'input',
				placeHolder: `请输入${item.name}`,
				required: !item.isNull,
93
				base52: item.base52,
94 95 96 97 98 99 100 101 102
			});
		} else if (number.indexOf(item.type) > -1) {
			config.push({
				key: item.name,
				name: item.title,
				type: 'inputNumber',
				placeHolder: `请输入${item.name}`,
				required: !item.isNull,
				min: 0,
103 104
				base52: item.base52,
			});
105 106 107 108 109 110 111 112
		} else if (date.indexOf(item.type) > -1) {
			config.push({
				key: item.name,
				name: item.title,
				type: 'datePicker',
				placeHolder: `请输入${item.name}`,
				required: !item.isNull,
				format: format[item.type],
113 114
				base52: item.base52,
			});
115 116 117 118 119 120 121
		} else {
			config.push({
				key: item.name,
				name: item.title,
				type: 'input',
				placeHolder: `请输入${item.name}`,
				required: !item.isNull,
122 123
				base52: item.base52,
			});
124 125 126
		}
	}
	return config;
王绍森's avatar
王绍森 committed
127 128 129
};

const transLate = {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	TextArea: 'textarea',
	Radio: 'radioGroup',
	Label: 'text',
	Checkbox: 'checkBoxMutiple',
	Select: 'select',
	Switch: 'switch',
	DatePicker: 'datePicker',
	RangePicker: 'rangePicker',
	UploadCom: 'buttonUpload',
	ImgUploadCom: 'upload',
	VideoUploadCom: 'upload',
	Input: 'input',
	TableSelect: 'tableSelect', // 表格选择
	ChildForm: 'childForm', // 最终保存的数据还有问题
	InputNumber: 'inputNumber',
	PartForm: 'partForm', // 还未开发 相当于选了一个选项 表单字段变更
	Echart: 'echart', //  不做
	Description: 'input', // 'description', // 还未开发
	Table: 'table', // 表格 未开发
};

const getOpts = async (item) => {
	let options = [];
	if (item.optionType === 'reference') {
		options = await service.getOptions(
			item.dataId,
			item.filterSql,
			item.referenceCodeName,
			item.referenceNameName,
		);
	} else if (item.optionType === 'sql') {
		options = await service.getSqlOptions(item.dataId, item.sqlKey, item.valueName, item.labelName);
	} else {
		options = item.options;
	}
	return options;
王绍森's avatar
王绍森 committed
166 167
};

168
const getSearchCondition = async (id, appId = undefined) => {
169 170 171 172 173
	const searchHeaders = await service.getHead(id).then((res) => {
		if (res && res.length) {
			return res.filter((x) => {
				return x.isShowQuery !== false && x.isHidden === false;
			});
钟是志's avatar
钟是志 committed
174 175 176
		}else{
		  return [];
    }
177
	});
178
  // console.log(JSON.stringify(searchHeaders));
179
  for (let item of searchHeaders) { // H:\project\one_stop_front\src\pages\AdminSystem\pages\ddl\formList\index.js
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    if(item.referenceObjId && item.id && item.referenceCodeName && item.referenceNameName){ //通过引用去查枚举
      item.options = await service.getBatchOptions(item.id).then((res) => {
        if(res && res[item.id]){
          return res[item.id].filter((g) => {
            return !!g.name && !!g.row_id;
          }).map((y) => {
            return {
              key: y[item.referenceCodeName],
              name: y[item.referenceNameName],
            };
          });
        }else {
          return [];
        }
      });
    }

    if (item.isGroupQuery && item.id && (!item.options || !item.options.length)) {
198
			// 获取下拉搜索条件的枚举项
199 200 201 202
			item.options = await service.getGroupList({
        columnId: item.id,
        appId,
      }).then((res) => {
203
				if (res && Array.isArray(res)) {
204 205
					return res
						.filter((x) => {
206
							return x.value !== item.title && x.value;
207 208 209 210 211 212 213
						})
						.map((x) => {
							return {
								key: x.value,
								name: x.value,
							};
						});
214 215 216 217
				} else {
					return [];
				}
			});
218 219
		}
	}
220 221 222 223 224 225

	const condition = getFormArrayConfig(searchHeaders);

	condition.forEach((x) => {
		x.required = false;
	});
226
	return condition;
王绍森's avatar
王绍森 committed
227 228 229
};

const translateAddFields = async (data, tableInfo) => {
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	let key = [];
	let res = [];
	for (let item of data) {
		if (key.indexOf(item.key) > -1) {
			continue;
		} else {
			key.push(item.key);
		}
		item.type = transLate[item.comName] || 'input';
		if (typeof item.key === 'undefined' || !item.key) {
			item.key = item.c1;
		}
		switch (item.comName) {
			case 'Input':
				if (number.indexOf(item.dataType) > -1) {
					item.type = 'inputNumber';
				}
				break;
			case 'DatePicker':
				item.format = item.format || format[item.dataType];
250 251
				// console.log(item.format);
				if (item.format && item.format.indexOf('HH') > -1) {
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
					item.showTime = true;
				}
				break;
			case 'RangePicker':
				item.name = item.name || '起止时间';
				item.endKey = item.c2;
				item.format = item.format || format[item.dataType];
				if (item.format.indexOf('HH') > -1) {
					item.showTime = true;
				}
				break;
			case 'ChildForm':
				item.componentProps.thisFields = await translateAddFields(
					item.componentProps.thisFields,
					tableInfo,
				);
				break;
			case 'Radio':
				item.options = await getOpts(item);
				break;
			case 'Select':
				item.options = await getOpts(item);
				break;
			case 'Checkbox':
				item.options = await getOpts(item);
				item.options = item.options.map((x) => {
					return {
						label: x.name,
						value: x.key,
					};
				});
				break;
			case 'TableSelect':
				let info = await service.handleSqlfind(item.componentProps.sqlKey);
				if (isJSON(info.cols)) {
					info.cols = JSON.parse(info.cols);
				}
				item.componentProps.columnsData = info;
				item.componentProps.allValues = JSON.stringify(getDefaultValues(data, tableInfo));
				break;
			default:
				break;
		}
		res.push(item);
	}
	return res;
王绍森's avatar
王绍森 committed
298 299 300
};

const mustHaveValue = (configFields, data) => {
301 302 303 304 305 306 307 308 309 310 311 312 313
	for (let item of configFields) {
		if (item.required && !data[item.key] && data[item.key] !== false && data[item.key] !== 0) {
			message.warning(`${item.name}是必填项请填写`);
			return false;
		}
		if (item.required && typeof data[item.key] === 'object') {
			if (data[item.key].selects && JSON.stringify(data[item.key].selects) === '{}') {
				message.warning(`${item.name}是必填项请填写`);
				return false;
			}
		}
	}
	return true;
王绍森's avatar
王绍森 committed
314 315 316
};

export {
317 318 319 320 321 322 323 324 325 326
	text,
	number,
	date,
	getFormArrayConfig,
	translateAddFields,
	transLateTimeTOUnix,
	format,
	mustHaveValue,
	taskNode,
	getSearchCondition,
王绍森's avatar
王绍森 committed
327
};