local.html 7.73 KB
Newer Older
xinzhedeai's avatar
xinzhedeai committed
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
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>excel文件导入</title>
		<style type="text/css">
			html,.body{
				height: 100%;
				width: 100%;
				overflow: hidden;
				padding: 0;
			}
			.body{
				display: flex;
				justify-content: center;
				align-items: center;
				margin: 0;
			}
			.post-message-section {
				visibility: hidden;
			}

			.file-upload {
				position: relative;
				display: inline-block;
			}

			input[type="file"] {
				display: none;
				/* 隐藏原始输入框 */
			}

			label {
				width: 240px;
				height: 25px;
				line-height: 25px;
				display: inline-block;
				padding: 10px 15px;
				background-color: #007aff;
				color: white;
				border-radius: 32px;
				cursor: pointer;
				transition: background-color 0.3s;
				text-align: center;
				font-size: 16px;
			}
			header{
				position: absolute;
				top: 0;
				display: flex;
				justify-content: center;
				align-items: center;
				height: 44px;
				width: 100vw;
				color: #fff;
				padding-top: 25px;
				
			}
			header .title{
				font-size: 18px;
			}
			header img{
				position: absolute;
				left: 20px;
			}
			/* header::before{
				width: 16px;
				height: 30px;
				content: '';
				position: absolute;
				left: 20px;
				top: 20px;
				background-image: url('/static/image/home/return.png');
				background-repeat: no-repeat;
				background-size: cover;
			} */
			main{
				width: 90%;
				height: 80vh;
				background-color: #fff;
				border-radius: 10px;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				flex-direction: column;
				margin: 0 auto;
				gap: 30px 0;
			}
			.body{
				width: 100vw;
				height: calc(100vh-22px);
				padding-top: 22px;
				background: linear-gradient(180deg, #007AFF 0%, #419AFF 16%, #EFF1F4 43%);
				position: relative;
				display: flex;
				justify-content: center;
			}
		</style>
	</head>
	<body class="body">
		<header>
		  <img id="gobackBtn" src="../../static/image/home/return.png" style="width: 8px; height:15px;"/>
		  <span class="title">数据导入</span>
		  <!-- <span class="title"></span> -->
		</header>
		<main>
			<img src="../../static/image/upload/center.png" style="width: 207px; height:164px; margin-top: 159px;" />
			<div class="file-upload">
				<input type="file" id="file-input" @change="onFileChange" />
				<label for="file-input">选择系统文件并导入</label>
			</div>
		</main>
		<script type="text/javascript" src="vue.min.js"></script>
		<script type="text/javascript" src="webview.js"></script>
		<script src="xlsx.full.min.js"></script>
		<script type="text/javascript">
			console.log('XLSX', XLSX)

			const fileInfoDiv = document.getElementById('fileInfo');
			// document.getElementById('file-input').click();

			// 返回上一页
			document.getElementById('gobackBtn').addEventListener('click', function(event) {
				uni.postMessage({ // 往真机页面传递读取数据
					data: {
						type: 'goHome'
					}
				});
			})
	
			document.getElementById('file-input').addEventListener('change', function(event) {
				/**
				 * 1、选择excel文件
				 * 2、读取excel文件数据内容(只读取第一个sheet的表格数据值)
				 * 3、数据清洗,获取级联列表数据
				 * 4、
				 */
				console.log('onFileChange', JSON.stringify(event))
				const files = event.target.files; // 获取文件列表
				if (files.length > 0) {
					const file = files[0]; // 获取第一个文件对象

					const fileSize = (file.size / (1024)).toFixed(2); // 文件大小,单位MB
					const fileName = file.name;
					const fileType = file.type;
					const extendName = fileName.split('.')[1]
					if (extendName !== 'xlsx' && extendName !== 'xls') {
						uni.postMessage({ // 往真机页面传递读取数据
							data: {
								errorTip: '请选择xlsx、xls格式的文件进行导入'
							}
						});
						// alert('请选择xlsx、xls格式的文件进行导入');
						return
					}
					if (fileSize > 700) { // 9999条数据 ≈ 700kb
						// alert('文件过大,不能超过15MB');
						uni.postMessage({ // 往真机页面传递读取数据
							data: {
								errorTip: '文件过大,不能超过700kb'
							}
						});
						return
					}

					var reader = new FileReader();

					reader.readAsArrayBuffer(file);

					reader.onload = function(e) {
						var data = e.target.result;
						var workbook = XLSX.read(data);

						// console.log('读取数据workbook', JSON.stringify(workbook))

						const sheetName = workbook.SheetNames[0];
						const sheet = workbook.Sheets[sheetName];
						const jsonData = XLSX.utils.sheet_to_json(sheet);
						console.log('读取数据jsonData', JSON.stringify(jsonData))

						// console.log('sheet_to_row_object_array', XLSX.utils.sheet_to_row_object_array(sheet))
						// jsonData 数据格式 如dataArr
						const dataArr = jsonData

						// 用于存储重复项
						const duplicates = {};
						const keysToCheck = ['工程名称', '中段名称', '分段名称', '排位线名称', '炮孔编号'];
						
						// 导入数据必填项填写
						var checkErrorRows = []
						if(dataArr.length){
							dataArr.forEach((item, index)=>{
								for (let key in keysToCheck) {
									if(!item[keysToCheck[key]]){
										checkErrorRows.push(
										`第[ ${index+1+1} ]行,数据列[ ${keysToCheck[key]} ]不存在或值为空!`)
									}
								}
							})
							if(checkErrorRows.length){ // 校验失败,提示错误信息
								uni.postMessage({ // 往真机页面传递读取数据
									data: {
										errorTip: checkErrorRows.join('\n')
									}
								});
								return
							}
						}else{
							uni.postMessage({ // 往真机页面传递读取数据
								data: {
									errorTip: '导入文件内容为空'
								}
							});
							return
						}
						// 检查重复项
						dataArr.forEach((item, index) => {
							const key = keysToCheck.map(k => item[k]).join('|'); // 使用各属性的组合作为唯一标识
							if (!duplicates[key]) {
								duplicates[key] = []; // 初始化数组
							}
							duplicates[key].push(index + 1); // 保存重复项的行号
						});

						// 找出并提醒重复项
						const repeatingItems = Object.values(duplicates).filter(itemIndexes => itemIndexes.length > 1);

						if (repeatingItems.length > 0) {
							const messages = repeatingItems.map(item => `重复行: ${item.join(', ')}`).join('\n');
							console.log(`发现重复项:\n${messages}`);
						} else {
							console.log('没有发现重复项');
						}


						const tempArr = dataArr.map((item)=>{
							return {
								engineeringName: item['工程名称']+'',
								middleSectionName: item['中段名称']+'',
								segmentName: item['分段名称']+'',
								alignmentLineName: item['排位线名称']+'',
								boreholeNumber: item['炮孔编号']+'',
								throughHole: item['透孔'],
								designHoleDepth: item['设计孔深'],
								designInclination: item['设计倾角'],
							}
						})
						
						alert('导入数据成功,正在跳转首页')
						setTimeout(()=>{
							uni.postMessage({ // 往真机页面传递读取数据
								data: {
									dataArr: tempArr,
								}
							});
						}, 1000)

					};

					// alert(`
					//     <p>文件名: ${fileName}</p>
					//     <p>文件类型: ${fileType}</p>
					//     <p>文件大小: ${fileSize} KB</p>
					// `);

				} else {
					alert('文件导入失败')
				}
			})

			// 待触发 `UniAppJSBridgeReady` 事件后,即可调用 uni 的 API。
			document.addEventListener('UniAppJSBridgeReady', function() {
				uni.getEnv(function(res) {
					console.log('当前环境:' + JSON.stringify(res));
				});
			});
		</script>
	</body>
</html>