iClearExp.js 12.8 KB
1 2 3 4 5 6 7 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
import store from '../store'
import i18n from '@/assets/languages'
import { getTokenTime } from "@/api/system/user.js"
import { getEntryLock, unEntryLock, uploadEntryLock } from "@/api/entry-api.js";
import { getdownloadExlFileType } from "@/api/custom-api.js";
import { alertWarningMsg, alertSuccessMsg } from './index'


const baseURL = process.env.VUE_APP_BASE_API

// 日期格式化
export function parseTime(time, pattern) {
	if (arguments.length === 0 || !time) {
		return null
	}
	const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
	let date
	if (typeof time === 'object') {
		date = time
	} else {
		if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
			time = parseInt(time)
		} else if (typeof time === 'string') {
			time = time.replace(new RegExp(/-/gm), '/');
		}
		if ((typeof time === 'number') && (time.toString().length === 10)) {
			time = time * 1000
		}
		date = new Date(time)
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
		let value = formatObj[key]
		// Note: getDay() returns 0 on Sunday
		if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
		if (result.length > 0 && value < 10) {
			value = '0' + value
		}
		return value || 0
	})
	return time_str
}

// 表单重置
export function resetForm(refName) {
	if (this.$refs[refName]) {
		this.$refs[refName].resetFields();
	}
}

// 添加日期范围
export function addDateRange(params, dateRange, propName) {
	var search = params;
	search.params = {};
	if (null != dateRange && '' != dateRange) {
		if (typeof (propName) === "undefined") {
			search.params["beginTime"] = dateRange[0];
			search.params["endTime"] = dateRange[1];
		} else {
			search.params["begin" + propName] = dateRange[0];
			search.params["end" + propName] = dateRange[1];
		}
	}
	return search;
}

// 回显数据字典
export function selectDictLabel(datas, value) {
	var actions = [];
	Object.keys(datas).some((key) => {
		if (datas[key].dictValue == ('' + value)) {
			actions.push(datas[key].dictLabel);
			return true;
		}
	})
	return actions.join('');
}

// 回显数据字典(字符串数组)
export function selectDictLabels(datas, value, separator) {
	var actions = [];
	var currentSeparator = undefined === separator ? "," : separator;
	var temp = value.split(currentSeparator);
	Object.keys(value.split(currentSeparator)).some((val) => {
		Object.keys(datas).some((key) => {
			if (datas[key].dictValue == ('' + temp[val])) {
				actions.push(datas[key].dictLabel + currentSeparator);
			}
		})
	})
	return actions.join('').substring(0, actions.join('').length - 1);
}

// 通用下载方法
export function download(fileName) {
	window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
}

// 字符串格式化(%s )
export function sprintf(str) {
	var args = arguments, flag = true, i = 1;
	str = str.replace(/%s/g, function () {
		var arg = args[i++];
		if (typeof arg === 'undefined') {
			flag = false;
			return '';
		}
		return arg;
	});
	return flag ? str : '';
}

// 转换字符串,undefined,null等转化为""
export function praseStrEmpty(str) {
	if (!str || str == "undefined" || str == "null") {
		return "";
	}
	return str;
}

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function handleTree(data, id, parentId, children) {
	let config = {
		id: id || 'id',
		parentId: parentId || 'parentId',
		childrenList: children || 'children'
	};

	var childrenListMap = {};
	var nodeIds = {};
	var tree = [];

	for (let d of data) {
		let parentId = d[config.parentId];
		if (childrenListMap[parentId] == null) {
			childrenListMap[parentId] = [];
		}
		nodeIds[d[config.id]] = d;
		childrenListMap[parentId].push(d);
	}

	for (let d of data) {
		let parentId = d[config.parentId];
		if (nodeIds[parentId] == null) {
			tree.push(d);
		}
	}

	for (let t of tree) {
		adaptToChildrenList(t);
	}

	function adaptToChildrenList(o) {
		if (childrenListMap[o[config.id]] !== null) {
			o[config.childrenList] = childrenListMap[o[config.id]];
		}
		if (o[config.childrenList]) {
			for (let c of o[config.childrenList]) {
				adaptToChildrenList(c);
			}
		}
	}
	return tree;
}

//商品要素数据结构转Array结构
export function hscodeElementInfo(val) {
	let arr = []
	for (let i = 1; i <= 20; i++) {
		let element = {}
		if (val['element' + i]) {
			element = {
				index: i - 1,
				element: val['element' + i],
				flag: val['flag' + i],
				select: val['select' + i],
				selectFlag: val['selectFlag' + i],
				key: 'element' + (i - 1)
			}
			arr.push(element)
		} else {
			break
		}
	}
	return arr
}

export function elementTostring(arr) {
	let str = ''
	if (arr.length > 0) {
		arr.forEach((item, idx) => {
			if (item.indexOf('品名') < 0) {
				idx != arr.length - 1 ? str = str + item + '|' : str = str + item
			}
		})
	}
	return str
}

export function numberRound(num, i) {
	// if (num.toString().indexOf('.') < 0) return Number(num)
	// else {
	// 	let arr = num.toString().split('.')
	// 	let decimal = Number(`0.${arr[1]}`).toPrecision(i)
	// 	if (decimal.toString().split('.')[0] != '0') {
	// 		num = Number(`${Number(arr[0]) + Number(decimal.toString().split('.')[0])}.${decimal.toString().split('.')[1]}`)
	// 	} else {
	// 		num = Number(`${arr[0]}.${decimal.toString().split('.')[1]}`)
	// 	}
	// 	return num
	// }

	if (num >= 0) {

		var tempNumber = parseInt((num * Math.pow(10, i) + 0.5)) / Math.pow(10, i);
		return tempNumber;

	} else {

		let numberRound1 = -num;

		var tempNumber = parseInt((numberRound1 * Math.pow(10, i) + 0.5)) / Math.pow(10, i);

		return -tempNumber;

	}

}

//千克、克拉、克、斤转换千克
export function detailUnitConversion(num1, num2, unit1, unit2) {
	let kg = 1//千克
	let carat = 5000//克拉
	let jin = 2//斤
	let gram = 1000//克

	let mass1 = 0
	let mass2 = 0

	function unitSwitch(num, unit) {
		if (unit == '千克') {
			return Number(num)
		} else if (unit == '克拉') {
			return numberRound(Number(num) / carat, 5)
		} else if (unit == '斤') {
			return numberRound(Number(num) / jin, 5)
		} else if (unit == '克') {
			return numberRound(Number(num) / gram, 5)
		} else {
			return 0
		}
	}

	mass1 = unitSwitch(num1, unit1)
	mass2 = unitSwitch(num2, unit2)
	return { mass1: mass1, mass2: mass2 }
}

//字典表添加
export function setDict() {
	let obj = {}
	store.dispatch('GetDistrict').then(res => {
		obj.district = res
	})
	store.dispatch('GetPort', { type: 'all' }).then(res => {
		obj.port = res
	})
	let arr = ['LicenseCode', 'VehicleWayCode', 'supervisionMode', 'natureExemption', 'ClinchType', 'NewPackageType', 'transactionUnit', 'Country', 'levyType', 'currencySystem', 'ENTRY_METHOD', 'pickUpCity', 'UPLOAD_FILE_METHOD']
	arr.forEach((item, index) => {
		store.dispatch('GetDict', item).then(res => {
			obj[item] = res
		})
	})
}

let messageHandler = {
	oldTitle: ''
}
let changeTitle = null

//网页标题文字闪动方法
export function promptMessage(val) {
	//功能未完善-如需启用该功能,请进行调试
	if (val == 'open') {
		sendNotificationFn(i18n.t('system.newTip'), 1500);
	} else if (val == 'close') {
		clearInterval(changeTitle)
		window.document.title = i18n.t('system.title');
		messageHandler.notice = null
		messageHandler.oldTitle = ''
	}
	setTimeout(() => {
		clearInterval(changeTitle)
	}, 10000);
}

//浏览器标签提示
function sendNotificationFn(message, time) {
	messageHandler.oldTitle = window.document.title; // 保存原有标题
	let changeVal = 1;
	changeTitle = setInterval(() => {
		if (changeVal) {
			window.document.title = message;
			changeVal = 0
		} else {
			window.document.title = messageHandler.oldTitle;
			changeVal = 1
		}
	}, time);
}

//浏览器消息提示
export function sendNotificationHandler(title, msg) {
	return new Promise((resolve, reject) => {
		if (window.Notification.permission == "granted") { // 判断是否有权限
			sendNotification(title, msg).then(res => {
				resolve(res)
			})
		} else if (window.Notification.permission != "denied") {
			window.Notification.requestPermission((permission) => { // 没有权限发起请求
				sendNotification(title, msg).then(res => {
					resolve(res)
				})
			});
		}
	})
}

//发送浏览器消息通知
function sendNotification(title, msg) {
	return new Promise((resolve, reject) => {
		//注意设置requireInteraction且不设置tag时会出现多条提示,不兼容的浏览器,通过不断创建对象进行保留提示
		let notification = new Notification(title, {
			body: msg,
			requireInteraction: true,
			tag: 'notification',
			icon: process.env.VUE_APP_BASE_API + '/favicon.ico',
		})
		Notification.requestPermission()
		notification.onclick = (event) => {
			notification.close();
			resolve('close')
		}
		notification.onclose = (event) => {
			closeNotification();
			resolve('close')
		}
	})
}

function closeNotification() {
	if (changeTitle != null) {
		clearInterval(changeTitle)
		window.document.title = i18n.t('system.title');
		messageHandler.oldTitle = ''
	}
}

export function upDateTokenTime() {
	getTokenTime().then((res) => {
		if (res.code === '200') {
			// setTime('iClearLoginTime',)
		}
	}).catch((err) => {
		console.log(err)
	})
}

//查询运单琐
export function sreachEntryLock(id, lockFlag, bizType) {
	return new Promise((resolve, reject) => {
		getEntryLock({ consignmentId: id, lockFlag: lockFlag, bizType: bizType }).then((res) => {
			if (res.code === "200") {
				resolve(res);
			} else if (res.code === "41082" || res.code === '41084') {
				reject(res);
			} else {
				resolve(res);
			}
		}).catch((err) => {
			console.log(err)
			resolve(err)
		})
	})
}

//刷新运单琐
export function uploadLock(id, bizType) {
	return new Promise((resolve, reject) => {
		uploadEntryLock({ consignmentId: id, bizType: bizType }).then(() => {
			resolve()
		}).catch((err) => {
			console.log(err)
			reject()
		})
	})
}

//释放运单琐
export function removeEntryLock(id, bizType) {
	return new Promise((resolve, reject) => {
		unEntryLock({ consignmentId: id, bizType: bizType }).then((res) => {
			resolve()
		}).catch((err) => {
			console.log(err)
			reject()
		})
	})
}


export function tableFixedTypeChange(dom) {
	return new Promise((resolve, reject) => {
		this.$nextTick(() => {
			setTimeout(() => {
				if (dom.length == 0 || dom[0].clientWidth == dom[0].scrollWidth) {
					resolve(false)
				} else {
					resolve(true)
				}
			}, 50);
		});
	})
}

//获取文件下载进度
export function getDownLoadFileStatus(exportSign) {
	return new Promise((resolve, reject) => {
		let getType = setInterval(() => {
			getdownloadExlFileType(exportSign).then(res => {
				if (res.code == '200') {
					if (res.data.extra == 'true') {
						clearInterval(getType)
						store.dispatch('SetLoadingType', { type: false, text: '' })
						resolve()
					}
				} else {
					clearInterval(getType)
					store.dispatch('SetLoadingType', { type: false, text: '' })
					alertWarningMsg(res.message)
					reject()
				}
			}).catch((err) => {
				clearInterval(getType)
				store.dispatch('SetLoadingType', { type: false, text: '' })
				reject()
			})
		}, 1000);
	})

}

//默认口岸值转换
export function approvalPortType(portType) {
	let str = ''
	const portList = [
		{ type: 0, value: 'PEKIP' },
		{ type: 1, value: 'PVGIP' },
		{ type: 2, value: 'TSNIP' },
		{ type: 3, value: 'TAOIP' },
		{ type: 4, value: 'XMNIP' },
		{ type: 5, value: 'SCN' }
	]
	portList.forEach(item => {
		if (item.type == portType) {
			str = item.value
		}
	})
	return str
}

//批量获取配置信息
export function getSystemOption(code) {
	return new Promise((resolve, reject) => {
		// getPortUploadFileType({ codes: code }).then(res => {
		// 	if (res.code === '200') {
		// 		let arr = []
		// 		res.data.records.forEach(item => {
		// 			let obj = {
		// 				code: item.paramName,
		// 				value: item.paramValue
		// 			}
		// 			arr.push(obj)
		// 		})
		// 		resolve(arr)
		// 	} else {
		// 		reject()
		// 	}
		// }).catch((err) => {
		// 	console.log(err)
		// 	reject()
		// })
	})
}

//币制字典表禁用
export function currencySystemDisableType(code) {
	let arr = ['currencySystem_PHP', 'currencySystem_IDR', "currencySystem_TWD"]
	let status = false
	if (arr.includes(code)) {
		status = true
	}
	return status
}

//合成状态字典显示处理
export function imageTaskStatusShowType(code, name) {
	let str = ''
	if (code === '130046' || code == "attSynthesisFlagDicId") {
		str = name
	} else {
		if (name == '待合成') {
			str = '待推送'
		} else if (name == '合成报错') {
			str = '推送失败'
		} else if (name == '已合成') {
			str = '已推送'
		} else {
			str = name
		}
	}
	return str
}