utils.js 5.5 KB
Newer Older
钟是志's avatar
钟是志 committed
1 2 3 4
/***
 * 一站式正在使用此文件
 * 请谨慎使用
 * */
5 6 7
import React from 'react';
import moment from 'moment';
import { Icon, message, notification } from 'antd';
8
import { getOnestopKey } from '../../Services';
9 10 11 12 13 14 15

let messageTime = new Date().getTime() - 3000;

/**
 * 校验 开始时间必须在结束时间之前的函数
 * */
export function checkDate(endTime = '2019-01-01', startTime = '2018-12-31') {
16
	return moment(endTime).isAfter(moment(startTime));
17 18 19 20 21 22
}

/**
 * 去除字符串中的所有html 标签
 * */
export function matchReg(str) {
23 24
	let reg = /<\/?.+?\/?>/g;
	return str.replace(reg, '').replace(/&nbsp;/g, ' ');
25 26 27
}

export function htmlFormat(str) {
28 29 30 31 32 33 34
	if (typeof str !== 'string') {
		return '';
	}
	const newTxt = str.replace(/\s+([^<>]+)(?=<)/g, function(match) {
		return match.replace(/\s/g, '&nbsp;');
	});
	return newTxt;
35 36 37
}

export function countSpecialField(filedSpanBig, nameSpanBig) {
38 39 40 41 42 43 44 45 46 47 48 49 50
	let style = {};
	if (document.body.clientWidth > 1400) {
		if (filedSpanBig === 5) {
			// 当设置一行排列5个字段时 自定义宽度为20%
			style = { width: '20%' };
		}
		if (filedSpanBig === 1 && nameSpanBig === 2) {
			// 当一行显示一个字段 然后名字又特别长时 使用这个width
			style = { width: '6%' };
		}
	}
	return style;
}
51 52 53 54 55

/**
 * 深拷贝函数
 * */
export function deepCopy(obj, parent = null) {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	if (React.isValidElement(obj)) {
		return React.cloneElement(obj);
	}
	if (['boolean', 'string', 'number'].indexOf(typeof obj) > -1 || !obj) {
		return obj;
	}
	let result;
	if (obj.constructor === Array) {
		result = [];
	} else {
		result = {};
	}
	let keys = Object.keys(obj),
		key = null,
		temp = null,
		_parent = parent;
	// 该字段有父级则需要追溯该字段的父级
	while (_parent) {
		// 如果该字段引用了它的父级则为循环引用
		if (_parent.originalParent === obj) {
			// 循环引用直接返回同级的新对象
			return _parent.currentParent;
		}
		_parent = _parent.parent;
	}
	for (let i = 0; i < keys.length; i++) {
		key = keys[i];
		temp = obj[key];
		// 如果字段的值也是一个对象
		if (temp && typeof temp === 'object') {
			// 递归执行深拷贝 将同级的待拷贝对象与新对象传递给 parent 方便追溯循环引用
			result[key] = deepCopy(temp, {
				originalParent: obj,
				currentParent: result,
				parent: parent,
			});
		} else {
			result[key] = temp;
		}
	}
	return result;
97 98 99 100 101 102 103 104 105 106
}

/**
 * 获取表单元素中每个元素的值,
 * @param type
 * @param e
 * @param other
 * @returns {*|boolean}
 */
export function getFormElemValue(type, e, other) {
107
	let value = e;
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	switch (type) {
		case 'input':
			value = e.target.value;
			break;
		case 'checkbox':
			value = e.target.checked;
			break;
		case 'textarea':
			value = e.target.value;
			break;
		case 'buttonUpload':
			value = e.url;
			break;
		case 'upload':
			value = Array.isArray(e) ? e.join(',') : '';
			break;
		default:
			break;
	}
128

129
	return value;
130 131 132 133 134 135
}

/**
 * 生成随机字符串
 * */
export function randomStr() {
136 137 138
	return Math.random()
		.toString(36)
		.substr(2);
139 140 141
}

export function isJSON(str) {
142 143 144 145 146 147 148 149 150 151 152
	if (typeof str == 'string') {
		try {
			JSON.parse(str);
			if (typeof JSON.parse(str) === 'number') {
				return false;
			}
			return true;
		} catch (e) {
			return false;
		}
	}
153 154
}

钟是志's avatar
钟是志 committed
155
export function checkMustHaveValue(configFileds, data) {
156 157 158 159 160 161
	for (let item of configFileds) {
		if (!data[item.key] && data[item.key] !== false && data[item.key] !== 0) {
			return false;
		}
	}
	return true;
162 163 164
}

export function mustHaveValue(configFields, data) {
165 166 167 168 169 170 171 172 173 174 175 176
	for (let item of configFields) {
		if (!item.required) continue;
		if (Array.isArray(data[item.key]) && data[item.key].length < 1) {
			message.warning(`${item.name}是必填项请填写`);
			return false;
		}
		if (!data[item.key] && data[item.key] !== false && data[item.key] !== 0) {
			message.warning(`${item.name}是必填项请填写`);
			return false;
		}
	}
	return true;
177 178 179 180 181 182 183
}

/**
 * 判断数组中是否有重复元素
 * 通过数组排序,比较临近元素
 * */
export function isRepeat(ary) {
184 185 186 187 188 189 190 191 192 193 194
	if (ary.length <= 1) {
		return false;
	}
	let nary = ary.sort();
	for (let i = 0; i < ary.length - 1; i++) {
		if (nary[i] === nary[i + 1]) {
			return nary[i];
			// alert("数组重复内容:" + nary[i]);
		}
	}
	return false;
195 196 197
}

export function displayRender(label) {
198 199 200 201 202
	if (label && label.length) {
		return label[label.length - 1];
	} else {
		return '';
	}
203 204 205
}

export function isEmptyValue(value) {
206
	return typeof value === 'undefined' || value === null || value === '';
207 208 209 210
}

// 全局的通知消息组件
export function controlNotification(props) {
211 212 213 214 215 216 217 218 219 220
	const nowTime = new Date().getTime();
	if (nowTime - messageTime < 3000) {
		return;
	}
	messageTime = nowTime;
	notification.info({
		...props,
		icon: <Icon type="info-circle" style={{ color: '#fa8c16' }} />,
	});
	return true;
221
}
222

钟是志's avatar
钟是志 committed
223
export function setOneStopConfig(value) {
224 225 226 227
	if (typeof value !== 'string') {
		value = JSON.stringify(value);
	}
	localStorage.setItem('oneStopConfig', value);
228 229
}

钟是志's avatar
钟是志 committed
230
export function getOneStopConfig(key) {
231 232 233 234 235 236 237 238 239 240 241 242
	let configList = localStorage.getItem('oneStopConfig');
	if (configList && isJSON(configList)) {
		let data = JSON.parse(configList);
		if (data && typeof data === 'object') {
			if (typeof data === 'undefined') {
				return '';
			}
			return data[key] || false;
		}
	} else {
		return getOnestopKey(key);
	}
243
}
钟是志's avatar
钟是志 committed
244

245 246 247 248 249 250 251 252 253 254 255
export function diGuiTree(treeData = [], i = 0) {
	// for (let item of treeData) {
	//   if(i === 2){
	//     item.selectable = true;
	//   }
	//   if (item.children && item.children.length) {
	//     i += 1;
	//     diGuiTree(item.children, i);
	//   }
	// }
	return treeData;
钟是志's avatar
钟是志 committed
256
}