historyLocation.vue 11.8 KB
<template>
	<v-full-layout>
		<template slot="breadcrumb">
			<el-breadcrumb-item>历史停留</el-breadcrumb-item>
		</template>
		<template slot="main">
			<div class="map-wrap" >
				<div id="map" class="map-container" v-loading="mapLoading"></div>
				<map-controll></map-controll>
				<div class="controll-wrap">
					<div class="search-box"  :class="resultBoxHidden?'active':''">
						<h3>历史停留</h3>
						<p class="info">{{name}}/{{standno}}/{{sn}}</p>
						<el-form ref="form" :model="form" :rules="rules" :inline="false" size="mini"  class="form">
					  		<el-form-item label="查询范围" prop="dateRange">
								<el-date-picker
								  style="width: 100%;"
							      v-model="form.dateRange"
							      type="daterange"
							      size="small"
							      :clearable="false"
							      :editable="false"
							      :unlink-panels="true"
							      :picker-options="{
							      	disabledDate(time){
							      		return time.getTime() > ((new Date()).getTime()-24*60*60*1000)
							      	}
							      }"
							      range-separator="至"
							      start-placeholder="开始日期"
							      end-placeholder="结束日期">
							    </el-date-picker>
								<div class="date-filter">
									<el-button size="mini" type="text" @click="pickDate(7)">前7天</el-button>
									<el-button size="mini" type="text" @click="pickDate(15)">前15天</el-button>
									<el-button size="mini" type="text" @click="pickDate(30)">前30天</el-button>
								</div>
				  			</el-form-item>
				  			<el-form-item label="查询时段" prop="timerange">
								<el-select v-model="form.timerange" placeholder="请选择" size="small" style="width: 280px">
							      <el-option label="上午(06:00~12:00)" value="1"></el-option>
							      <el-option label="下午(12:00~21:00)" value="2"></el-option>
							      <el-option label="夜晚(21:00~06:00)" value="3"></el-option>
							      <el-option label="全天(00:00~24:00)" value="0"></el-option>
							    </el-select>
				  			</el-form-item>
				  			<el-form-item label="停留时长" prop="stay">
					  			<el-row class="stay-ctrl pull-left">
					  				<el-col :span="8">
					  					<el-form-item prop="stayMin">
											<el-input v-model.trim="form.stayMin" placeholder="最小值"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="2" class="text-center">~</el-col>
									<el-col :span="8">
					  					<el-form-item prop="stayMax">
											<el-input v-model.trim="form.stayMax" placeholder="最大值"></el-input>
										</el-form-item>
									</el-col>
									<span class="unit">分钟</span>
					  			</el-row>
					  		</el-form-item>
				  			<el-form-item class="submit-el-form-item">
							    <el-button type="primary" @click="searchSubmit" size="small" :loading="isSearching">查询</el-button>
							</el-form-item>
				  		</el-form>
					</div>
					<div class="result-box" :class="resultBoxHidden?'':'active'">
						<div class="result-box-head">
							<el-button class="pull-left" size="mini" type="text" @click="resultBoxHidden=true;">返回“查询”</el-button>
							<el-button class="pull-right" size="mini" type="text" @click="exportExcel">导出Excel</el-button>
							<span>停留点({{resultList.length}})</span>
						</div>
						<div class="result-box-filter text-center">
							<el-button class="" size="mini" type="text" @click="sortByTime">停留时间<i class="fa fa-sort"></i></el-button>
							<el-button class="" size="mini" type="text" @click="sortByStay">停留时长<i class="fa fa-sort"></i></el-button>
						</div>
						<ul class="stay-list">
							<li class="stay-list-item" v-for="item in resultList" @click="openMapInfoWindow(item.idx)">
								<p class="addr">{{item.transferAddress}}</p>
								<p class="datetime"><span>{{item.begintime}}</span><span class="pull-right">{{item.duration}}</span></p>
							</li>
							<li v-show="!resultList.length">
								<p class="text-center">无数据</p>
							</li>
							<!-- <infinite-loading v-if="needInfinite" @infinite="infiniteHandler" ref="infiniteLoading">
								<span slot="no-more">没有更多数据了</span>
								<span slot="no-results">无结果</span>
							</infinite-loading> -->
						</ul>
					</div>
				</div>
			</div>
		</template>
	</v-full-layout>
</template>
<script>
	import store from '../../vuex/store.js'
	import remoteLoad from 'utils/remoteLoad.js'
	import mapService from 'utils/mapService.js'
	import 'utils/mapEvent.js'
	import EventBus from '../../event-bus'
	import {services} from '../../api'
	import mock from '../../mock'

	import {CONSTANT} from '../../constant'
	import InfiniteLoading from 'vue-infinite-loading';

	const MONITOR_SERVICE =  services.monitor;
	import {exportExcel} from 'utils';
	import MapControll from './MapControll.vue'
	const BUSINESS_SERVICE =  services.business;

	export default{
		data(){
			let checkDaterange = (rule, vaule, cb)=>{
				const dateRange = this.form.dateRange
				if(!dateRange.length){
					cb(new Error('请选择日期'))
				}else{
					let min = dateRange[0];
					let max = dateRange[1];
					
					if(!moment.isMoment(min)){
						min = moment(min)
					}
					if(!moment.isMoment(max)){
						max = moment(max)
					}
					
					if(min.isBefore(moment(Object.assign({},max)).subtract(29, 'days'), 'day')){
						cb(new Error('日期范围不能超过30天'))
					}
				}
				cb()
			}
			let checkStay = (rule, vaule, cb)=>{
				const stayMin = this.form.stayMin;
				const stayMax = this.form.stayMax;

				if((stayMin && !/^(\d{1,4}|10000)$/.test(stayMin)) || (stayMax && !/^(\d{1,4}|10000)$/.test(stayMax))){
					cb(new Error('只能输入0~10000的正整数'))
				}else if(stayMin!='' && stayMax!='' && parseInt(stayMin)>parseInt(stayMax)){
					cb(new Error('最小值不可大于最大值'))
				}
				cb()
			}
			return {
				AMap: null,
				mapObj: null,
				mapLoading: false,
				isInitMap: false,
				searchText: '',
				searchType: 'sn',
				isSearching: false,
				resultBoxHidden: true,
				form: {
					dateRange: [moment().subtract(7, 'days'),moment().subtract(1, 'days')],
					timerange: '1',
					stayMin: '',
					stayMax: ''
				},
				rules: {
					dateRange: [{validator: checkDaterange}],
					stay: [{validator: checkStay}]
				},
				needInfinite: true,
				params: {},
				currentPage: 1,
				pageSize: 10,
				total: 100,
				maxPage: 10,
				resultList: [],
				sn: this.$route.query.sn,
				name: this.$route.query.name,
				standno: this.$route.query.standno,
				sortByTimeIsPositive: true,
				sortByStayIsPositive: true
			}
		},
		mounted(){
			store.dispatch('setFullsize', true)
			
		},
		methods: {
			initMap(){
				let check = ()=>{
					this.checkMapLoaded && window.clearTimeout(this.checkMapLoaded);

					return setTimeout(()=>{
				      	if('AMap' in window && typeof AMap.Map == 'function' && !this.mapObj){
				      		window.AMap = AMap;
				      		this.mapObj = mapService.init('map');
				      		
				      		//this.bindEvent();
							this.isInitMap = true;
							this.drawPoi()
						}else{
							this.checkMapLoaded = check()
						}
				    }, 100)
				}
				this.checkMapLoaded = check();
			},
			pickDate(beforeDays){
				this.form.dateRange = [moment().subtract(beforeDays, 'days'),moment().subtract(1, 'days')]
			},
			searchSubmit(){
				this.$refs.form.validate((valid, field)=>{
	    			if(valid){
	    				this.isSearching = true;
	    				
	    				let params = {
	    					sn: this.sn,
	    					sTime: moment(this.form.dateRange[0]).startOf('day').format('YYYY-MM-DD HH:mm:ss'),
							eTime: moment(this.form.dateRange[1]).endOf('day').format('YYYY-MM-DD HH:mm:ss'),
							times: this.form.timerange,
	    				}
	    				if(this.form.stayMax !== ''){
	    					params.maxtime = this.form.stayMax;
	    				}
	    				if(this.form.stayMin !== ''){
	    					params.mintime = this.form.stayMin;
	    				}
	    				EventBus.$emit('uiMap.historyLocation.destroy');
	    				MONITOR_SERVICE.getHistoryLocation(params).then((res)=>{
							if(res.returnCode == 0){
								const data = res.data;
								if(data.length){
									data.forEach((item, idx)=>{
										item.idx = idx;
									});
									
									this.resultList = data;
									EventBus.$emit('uiMap.historyLocation.draw', data);
									
									this.resultBoxHidden = false;
								}else{
									this.$message({
						              type: 'info',
						              message: '暂无数据'
						            });
								}
								this.isSearching = false;
							}
						});
	    			}else{
	    				return false;
	    			}
	    		});
			},
			infiniteHandler($state){
				if(this.currentPage> this.maxPage){
					$state.complete();
					return;
				}
				
				this.params.currentPage = this.currentPage;
				this.params.pageSize = this.pageSize;
				this.params.status = 1;


				this.currentPage++;
				$state.loaded();
				setTimeout(()=>{
					this.resultList = this.resultList.concat([{
						addr: '甘肃省酒泉市肃州区海马福仕达中瑞汽车',
						date: '2018-11-20 15:19:21',
						restTime: '25小时26分钟25秒'
					}])
				},10000)
				
				return;
				DEVICE_SERVICE.getDeviceList(this.params).then((res)=>{
					const data = res.data;
					if(data && data.length){
						const arr = []
						data.forEach((item) => {
							arr.push({
								label: item.name,
								value: item.id
							})
						})
						if(this.options[0] && 'label' in this.options[0]){
							this.options = this.options.concat(arr);
						}else{
							this.options = arr
						}
						
						this.total = res.recordsTotal;
						this.maxPage = Math.ceil(this.total/this.pageSize);
						this.currentPage++;
						this.$refs.infiniteLoading.$emit('$InfiniteLoading:loaded');
					}else{
						this.$refs.infiniteLoading.$emit('$InfiniteLoading:complete');
						this.options = []
					}
				})
			},
			sortByTime(){
				let sortArr = this.resultList.sort((a,b)=>{
					return moment(a.begintime).isBefore(moment(b.begintime))?(this.sortByTimeIsPositive?-1:1):(this.sortByTimeIsPositive?1:-1);
				});
				this.sortByTimeIsPositive = !this.sortByTimeIsPositive;
				this.resultList = [].concat(sortArr);
			},
			sortByStay(){
				let sortArr = this.resultList.sort((a,b)=>{
					let aTime = (new Date(a.endtime)).getTime() - (new Date(a.begintime)).getTime();
					let bTime = (new Date(b.endtime)).getTime() - (new Date(b.begintime)).getTime();
					return aTime>bTime?(this.sortByStayIsPositive?-1:1):(this.sortByStayIsPositive?1:-1);
				});
				this.sortByStayIsPositive = !this.sortByStayIsPositive;
				this.resultList = [].concat(sortArr);
			},
			exportExcel(){
				let arr = [];
				this.resultList.forEach((item)=>{
					item.address = item.transferAddress;
					arr.push(item);
				});
	            
                exportExcel('track/exportVMData', {
                    vmExportType: 6,
                    jsonStr: encodeURI(JSON.stringify(arr))
                }, 'post');
			},
			openMapInfoWindow(idx){
				EventBus.$emit('uiMap.historyLocation.openInfoWindow', idx)
			},
			drawPoi(){
				BUSINESS_SERVICE.getPOI({
					sn: this.sn
				}).then((res)=>{
					if(res.returnCode === '0'){
						if(res.data && res.data.length){
							res.data.forEach((item)=>{
								let marker = mapService.createPOIMarker(mapService.getLngLat(item.lng, item.lat), this.mapObj)
								let infoWindow = mapService.bindPOIInfoWindow(marker, this.mapObj, item)
							})
						}
					}
				})
			}
		},
		async created () {
			if(window.AMap){
				this.initMap();
			}else{
		      	await remoteLoad(`${CONSTANT.AMAP_URL}`).then(()=>{
		      		remoteLoad(`https://webapi.amap.com/ui/1.0/main.js?v=1.0.11`).then(()=>{
		      			this.initMap();
		      		})
		      	})
		      	
		    }
		   	EventBus.$on('uiMap.historyLocation.transferSuccess', (data)=>{
		   		this.resultList = [].concat(data);
		   	});
		   	
		},
		beforeDestroy(){
			

		},
		components: {
			MapControll
		}
	}
</script>
<style scoped>
	/*样式写在  scss/monitor.scss*/
	
</style>