batch_upgrade.vue 13.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
<template>
  <div class="innnerBox">
	   <el-col :span="22">
		    <el-form ref="form" label-width="80px" inline>
			  <el-form-item label="运维设备">
				<span class="selectBox">
				   <el-select placeholder="请选择" :popper-append-to-body=false v-model="devSeled">
					 <el-option :value="item.value" :label='item.label' v-for="item in devOpt"></el-option>
					</el-select>
				</span>
			  </el-form-item>
			</el-form>
		</el-col>
		   <el-col :span="2">
			   <div style="margin-bottom: 6px;padding-top: 3px;">
					<el-button type="success" class="block" @click="batchUpdate">批量升级</el-button>
				</div>
		   </el-col>
		  <div  style="">
			  <el-table
			       :data="tableData"
			       height="574"
				   stripe
				   border
			      @selection-change="handleSelectionChange">
			      <el-table-column
			        type="selection"
			        align="center"
			        width="55">
			      </el-table-column>
			       <el-table-column
			         prop="device_name"
			          align="center"
			         label="设备名称">
			       </el-table-column>
			         <el-table-column
			         prop="in_ip"
			          align="center"
			         label="内网IP">
			       </el-table-column>
			       <el-table-column
			         prop="out_ip"
			          align="center"
			         label="外网IP">
			       </el-table-column>
			       <el-table-column
			         prop="software_version"
			          align="center"
			         label="当前版本">
			       </el-table-column>
			       <el-table-column
			         prop="RefinedFeature_text"
					   align="center"
			         label="升级状态">
					 <template slot-scope="scope">
						 <span :style="{ color: scope.row.styleC }" :class="{ 'span-progress': scope.row.spanClass }">{{ scope.row.state }}</span>
						 <strong id="bar" :style="{ width: scope.row.styleW }">{{ scope.row.fileText }}</strong> 
					 </template>
			       </el-table-column>
			       <el-table-column
			        align="center"
			         label="操作">
			         <template slot-scope="scope">
			         	<el-tooltip content="详情" placement="bottom" effect="light" :visible-arrow=false>
			         		<span class="icon-fanxing-xiugai editIcon"   @click="detailFun(scope.$index, scope.row)"></span>
			         	</el-tooltip>
			         </template>
			       </el-table-column>
			  </el-table>
				<div style="margin-top: 28px;">
					<el-pagination
					style="float: right;"
					  background
					  prev-text="上一页"
					  next-text="下一页"
					   :page-sizes="[30, 50, 100, 200]"
					  layout="total, prev, pager, next,sizes, jumper"
					  :current-page="page"
					  @size-change="handleSizeChange"
					  @current-change="handleCurrentChange"
					  :total="total">
					</el-pagination>
					<div style="clear: both;"></div>
				</div>
		  </div>
		  <el-dialog
		  	title="批量上传"
		  	:visible.sync="addVisible"
		  	width="450px">
		  	<div>
		  		<el-form label-position="left" label-width="120px">
		  			<el-form-item label="添加文件">
		  			<el-upload
		  				ref="upload"
		  				action="uploadUrl"
		  				:http-request="httpRequest"
		  				:multiple=false
						:limit="1"
						:on-exceed="handleExceed"
		  				name="file"
		  				:auto-upload="false">
		  				<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
		  				<!-- <div slot="tip" class="el-upload__tip">只能上传视频文件</div> -->
		  			</el-upload>
		  			</el-form-item>
		  		</el-form>
		  	</div>
		  	<span slot="footer" class="dialog-footer">
		  		<el-button @click="addVisible=false">取 消</el-button>
		  		<el-button type="primary" @click="save">上 传</el-button>
		  	</span>
		  </el-dialog>
  </div>
</template>
<script>
	export default {
		data(){
			return{
				uploadUrl:'',
				file:[],
				addVisible:false,
				tableData:[],
				total:0,
				page:1,
				pageSize:30,
				selectcheck:[],
				devOpt:[],
				devSeled:'',
				flag:true,
				updating:false
			}
		},
		components:{},
		mounted(){
			this.getDevs();
		},
		methods:{
			handleExceed(files, fileList) {
				this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
			},
			// 自定义的上传函数
			httpRequest(param) {
				console.log(param)
				if(param.filename) {
					let modalFileExt = param.filename.slice(-7);
					if(modalFileExt !== '.tar.gz') {
						this.$message({
							type: 'warning',
							message: '仅支持"*.tar.gz"文件'
						})
						return false;
					}
				}else{
					this.$message({
						type: 'warning',
						message: '请选择文件'
					})
					return false;
				}
					this.flag = true
					for (let i = 0; i < this.selectcheck.length; i++) {
						const upgradeSoftWare = this.selectcheck[i].upgradeSoftWare;
						let isOk = false
						for (let j = 0; j < upgradeSoftWare.length; j++) {
							const ele = upgradeSoftWare[j];
							let ary =  param.filename.split('_')
							console.log(ele.softWareName==ary[0])
							console.log(ele.platType==ary[1])
							if (ele.platType==ary[1]&&ele.softWareName==ary[0]) {
								isOk = true
								break;
							}
						}
						console.log('isok'+isOk)
						if (!isOk) {
							this.flag = false
							break
						}
					}
					if(flag){
						this.file=[];
					// 一般情况下是在这里创建FormData对象,但我们需要上传多个文件,为避免发送多次请求,因此在这里只进行文件的获取,param可以拿到文件上传的所有信息
						this.file.push(param.file)
					}else{
						this.$alert('该升级包不能应用于所有选中的节点,请重新选择', '提示', {
							confirmButtonText: '确定',
						});
						return false
					}
				
			},
			save(){
				this.$refs.upload.submit(); // 这里是执行文件上传的函数,其实也就是获取我们要上传的文件
				// 最重要的就是这段代码
				if(this.flag){
// 						var upData = new FormData() // 首先创建FormData对象
// 							this.file.forEach(function (file) {
// 								upData.append('file', file); // 因为要上传多个文件,所以需要遍历一下才行
// 							upData.append('name', file.name);
// 							upData.append('vchan_type', 'vfile');
// 							upData.append('vchan_refid', new Date().getTime());
// 							})
// 					this.$api.resource.uploadFile(upData,this.devsId).then(res=>{
// 						console.log(res)
// 					})
console.log('上传')
				}
					
				
			},
			handleSelectionChange(obj){
			   this.selectcheck = obj;
			},
			handleSizeChange(val) {
				this.pageSize=val;
				this.getData();
			},
			handleCurrentChange(val) {
				this.page=val;
				this.getData();
			  },
			query(){
				this.getData();
			},
			getDevs(){
				this.$api.resource.devs().then(res=>{
					if(res.length > 0) {
						this.devOpt = res.map((v, i) => {
							return {
								value: v.dev_unid,
								label: '运维设备' + (++i)
							}
						});
						this.devSeled = this.devOpt[0].value;
						this.getData();
					} else {
						// this.$message({
						// 	message: '运维为空!',
						// 	type: 'error'
						// })
						console.log('未发现运维!')
						return false;
					}
				})
			},
			getData(){
				this.tableData=[];
				let offset = (this.page - 1) * this.pageSize;
				let search_params = {
					limit: this.pageSize,
					offset: offset,
					is_leaf:0
				}
				this.$api.resource.getDevsName(search_params,this.devSeled
				).then((res)=>{
					this.total=res.total_num;
					this.tableData = res.list_data;
						this.tableData.forEach(info => {
							info.styleC = '#fff';	// 升级状态文字颜色
							info.spanClass = false;	// 升级状态文字class
							info.state = '暂无';	// 升级状态文字
							info.styleW = '0%';	// 升级状态进度条宽度
							info.fileText = '';	// 升级状态进度条文字
							info.unchangeCount = 90;
							info.progressQueryErrorTime = {};	// 记录失败设备
						})
				}).catch((err)=>{
					
				})
			},
			
			
			/* 开始升级 */
			modalUpdateClick: function () {
				//区别备注
				if(this.updating) {
					this.$message({
						message: '程序正在升级中..',
						type: 'warning'
					});
					return;
				}
						this.updating = true;
						let localUpd = JSON.parse(window.localStorage.getItem('upgradeInfo'));
						//获取选中id组
						let localDevId = JSON.parse(window.localStorage.getItem('deviceArr'));
						console.log('升级之前看有没有未完成升级的:', localUpd)
						if(localUpd && localDevId) {
							this.queryProgress(localUpd);
							this.$message({
								type: 'info',
								message: '有设备正在升级中...请等待升级完成再进行下次升级!'
							})
							return;
						}
						// 筛选正在升级的...
						// 保存一份升级设备ID
						window.localStorage.setItem('deviceArr', JSON.stringify(this.checkedIndex));
						let UpdateArr = JSON.parse(window.localStorage.getItem('deviceArr'));
						console.log('本地UpdateArr:', UpdateArr)
						this.updateEle = false;
						// 初始化状态
						for(var i = 0, len = UpdateArr.length; i < len; i++) {
							this.modalUpdInfo.forEach(ele => {
								if(ele.device_id === UpdateArr[i]) {
									// console.log('筛选相同元素:', ele);
									ele.spanClass = true;
									ele.state = '暂无进度';
									ele.styleW = '1%';
									ele.styleC = '#fff'
								}
							})
						}
						this.updateEle = true;
						// 上传文件
						let modalFormData = new FormData();
						let ele_files = document.getElementById('modalUpgradeFile');
						modalFormData.append('list[0].file', ele_files.files[0]);
						let fxChildIds = UpdateArr.toString();
						let modalFormConfig = {
							headers: {
								'Content-Type': 'multipart/form-data',
								'authorization': window.localStorage.getItem('atoken')
							},
							onUploadProgress: progressEvent => {
								var complete = (progressEvent.loaded / progressEvent.total * 100 | 0) + '%';
								// console.log('complete:', complete)
								this.updateEle = false;
								for(var i = 0, len = UpdateArr.length; i < len; i++) {
									this.modalUpdInfo.forEach(ele => {
										if(ele.device_id === UpdateArr[i]) {
											// console.log('筛选相同元素:', ele);
											ele.fileText = complete;
											ele.styleW = complete;
										}
									})
								}
								this.updateEle = true;
							}
						};
						modalFormData.append('list[0].fx_device_ids', fxChildIds);
						let childUpdUrl = this.IP + '/api/v1/devconf_fx/devs/'+ this.dev_unid +'/fx_devs/upfile';
						this.updateEle = false;
						for(var i = 0, len = UpdateArr.length; i < len; i++) {
							this.modalUpdInfo.forEach(ele => {
								if(ele.device_id === UpdateArr[i]) {
									ele.state = '文件上传中';
								}
							})
						}
						// 发送http请求
						this.modalUpdHttp(childUpdUrl, modalFormData, modalFormConfig, fxChildIds);
						this.updateEle = true;
			},
			/* 升级程序 */
			modalUpdHttp: function (childUpdUrl, modalFormData, myFormConfig, fxChildIds) {
				this.$http.post(childUpdUrl, modalFormData, myFormConfig, fxChildIds)
				.then(res => {
					let childUpdRes = res.data;
					if(childUpdRes.ecode != 200) {
						this.updateEle = false;
						this.$message({
							type: 'error',
							message: '文件上传失败!'
						});
						for(var i = 0, len = fxChildIds.split(',').length; i < len; i++) {
							this.updInfo.forEach(ele => {
								if(ele.device_id === fxChildIds.split(',')[i]) {
									ele.state = '文件上传失败!';
									ele.spanClass = false;
									ele.styleW = '0%';
									ele.styleC = '#0f0'
									ele.fileText = ''
								}
							})
						}
						console.log('文件上传失败!' + childUpdRes.enote);
						this.updateEle = true;
					} else {
						this.updateEle = false;
						for(var i = 0, len = fxChildIds.split(',').length; i < len; i++) {
							this.updInfo.forEach(ele => {
								if(ele.device_id === fxChildIds.split(',')[i]) {
									ele.state = '文件上传成功!';
									ele.spanClass = false;
									ele.styleW = '0%';
									ele.styleC = '#fff'
									ele.fileText = '';
								}
							})
						}
						// 保存在本地
						window.localStorage.setItem('upgradeInfo', JSON.stringify(fxChildIds));
						this.updFlag = true;
						this.queryProgress(fxChildIds);
						this.updateEle = true;
					}
				})
			},
			/* 查询升级进度 */
			queryProgress(dev_ids) {
				console.log('升级的设备ID:', dev_ids);
				if(dev_ids === '' || dev_ids == undefined) {
					// let localUpdInfo = window.localStorage.getItem('upgradeInfo');
					// if(localUpdInfo || localUpdInfo == undefined){
						window.localStorage.removeItem('upgradeInfo');
					// }
					clearTimeout(this.updTimer);
					this.updFlag = false;
					console.log('暂无升级的设备');
					return;
				} else {
					this.$http({
						method: 'get',
						url: this.IP + '/api/v1/devconf_fx/devs/' + this.dev_unid + '/fx_devs/upgrade/progress?s=' + new Date().getTime(),
						headers: {
							'authorization': window.localStorage.getItem('atoken')
						},
						params: {
							fx_device_ids: dev_ids
						}
					}).then(res => {
						let progressRes = res.data;
						console.log('progressRes:', progressRes)
						this.newIds = dev_ids;
						this.updTab(this.newIds, progressRes);
						this.updTimer = setTimeout(() => {
							// console.log('查询进度开关: ' + this.updFlag)
							if(this.updFlag) {
								// console.log('更新查询进度的设备ID有:', this.newIds)
								this.queryProgress(this.newIds);
							}
						},1000);
					}).catch(err => {
						console.log('查询升级进度异常:', err.message);
					});
				}
			},
			detailFun(index,row){
				
			},
			batchUpdate(){
				if(this.selectcheck.length==0){
					this.$message({
						type: 'warning',
						message: '请选择需要升级的行!'
					})
					return false
				}
				this.addVisible=true;
			},
		},
	}
</script>
<style lang="scss" scoped>
</style>