const APP = getApp(); //地图对象 let mapContext; //定时刷新位置信息对象 let locationInterval = null; //持续定位定时任务对象 let freqInterval = null; //倒计时定时任务对象 let countDownInterval = null; Page({ data: { schedule: 0, circle: false, loading: false, stateInfo: null, //设备状态信息 markers: [], //地图标记 }, // 监听页面加载 onLoad: function () { // 获取地图对象 mapContext = wx.createMapContext('map'); }, // 监听页面显示 onShow: function () { if (APP.globalData.openNum == null) { APP.Modal.tips("您还没有设备可以查看"); mapContext.moveToLocation({}); return; } //请求设备位置 this.getLatestLocation(APP.globalData.openNum); //开启定时任务-定时刷新位置信息 this.intervalRefresh(APP.globalData.openNum); //开启定时任务-高频定位模式 this.intervalSetContinue(APP.globalData.openNum); }, // 监听页面隐藏 onHide: function () { //清除定时 clearInterval(locationInterval); clearInterval(freqInterval); locationInterval = null; freqInterval = null; }, /** * 定时刷新 */ intervalRefresh: function (openNum) { console.log("启动定时刷新位置") const that = this; if (locationInterval == null) {//不为空说明已经被初始化,则不做处理 that.countDown(14); locationInterval = setInterval(function () { APP.Get(APP.Url.getLatestLocation, { openNum: openNum }, false).then(res => { that.countDown(14); that.updateStateInfo(res.data); }) }, 15000) } }, /** * 定时开启高频定位 */ intervalSetContinue: function (openNum) { console.log("启动持续定位") const that = this; if (freqInterval == null) { that.startHighFreq(openNum) freqInterval = setInterval(function () { that.startHighFreq(openNum); }, 240000) } }, /** * 开启高频定位 */ startHighFreq: function (openNum) { const params = { openNum: openNum, highFreq: 1 } APP.Post(APP.Url.setContinue, params).then(res => { }) }, /** * 获取设备位置 */ getLatestLocation(openNum) { const that = this; const params = { openNum: openNum } APP.Get(APP.Url.getLatestLocation, params, false).then(res => { const detail = res.data; if (detail == null) { APP.Modal.tips("该设备还没有上传位置信息"); return; } //首次加载聚焦中心为当前位置 mapContext.moveToLocation({ latitude: detail.latGcj, //纬度 longitude: detail.lonGcj, //经度 }) //更新位置数据 that.updateStateInfo(detail); }) }, /** * 设置位置等状态信息 */ updateStateInfo(stateInfo) { if (stateInfo == null) return; //定位时间 stateInfo.uploadTimeFormat = this.formatTime(stateInfo.uploadTime) this.setData({ stateInfo: stateInfo, markers: [{ id: 1, iconPath: "/imgs/marker.png", latitude: parseFloat(stateInfo.latGcj), longitude: parseFloat(stateInfo.lonGcj), width: 35, height: 35 }] }) }, /** * 格式化时间显示 */ formatTime: function (date) { const diff = (new Date().getTime() - new Date(date).getTime()) / 1000; if (diff < 60) return "刚刚"; if (diff < 3600) return parseInt(diff / 60) + "分钟前"; if (diff < 86400) return parseInt(diff / 3600) + "小时前"; if (diff < 31104000) return parseInt(diff / 86400) + "天前"; return "1年前"; }, /** * 倒计时 */ countDown(second) { const that = this; if (countDownInterval == null) { //系数 let factor = 100 / second; //初始化步数 let step = 1; //初始化进度条进度 this.setData({ schedule: 0, circle: true, loading: false }) //定时器 countDownInterval = setInterval(() => { //如果到达指定步数,则关闭定时 if (step >= second) { clearInterval(countDownInterval); countDownInterval = null; this.setData({ schedule: 0, circle: false, loading: true }) } //进度条增加 that.setData({ schedule: factor * step }) //步数增加 step = step + 1; }, 1000); } }, /** * 刷新 */ refresh() { wx.showLoading({ title: '正在刷新', }) setTimeout(function () { wx.hideLoading(); APP.Modal.tips("刷新成功"); }, 600) }, call() { APP.Modal.tips("功能暂未开通"); }, openMapApp() { const params = { longitude: this.data.stateInfo.lonGcj, latitude: this.data.stateInfo.latGcj, destination: "手机所在位置" } mapContext.openMapApp(params); } })