123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- const APP = getApp();
- let mapContext; //地图对象
- //定时刷新位置信息对象
- let clockRefresh = null;
- //持续定位定时任务对象
- let continueRefresh = null;
- Page({
- data: {
- stateInfo: null, //设备状态信息
- markers: [], //地图标记
- },
- // 监听页面加载
- onLoad: function () {
- // 获取地图对象
- mapContext = wx.createMapContext('map');
- },
- // 监听页面显示
- onShow: function () {
- if (APP.globalData.openNum == null) {
- APP.Modal.tips("您还没有设备可以查看");
- mapContext.moveToLocation({});
- return;
- }
- //请求设备位置
- this.getLatestState(APP.globalData.openNum);
- //开启定时任务-定时刷新位置信息
- this.intervalRefresh(APP.globalData.openNum);
- //开启定时任务-高频定位模式
- this.intervalSetContinue(APP.globalData.openNum);
- },
- // 监听页面隐藏
- onHide: function () {
- //清除定时
- clearInterval(clockRefresh);
- clearInterval(continueRefresh);
- clockRefresh = null;
- continueRefresh = null;
- },
- /**
- * 定时刷新
- */
- intervalRefresh: function (openNum) {
- console.log("启动定时刷新位置")
- const that = this;
- if (clockRefresh != null) return;
- clockRefresh = setInterval(function () {
- that.getLatestStateInterval(openNum);
- }, 15000)
- },
- /**
- * 定时设置持续定位
- */
- intervalSetContinue: function (openNum) {
- console.log("启动持续定位")
- const that = this;
- if (continueRefresh != null) return;
- that.setContinue(openNum);
- continueRefresh = setInterval(function () {
- that.setContinue(openNum);
- }, 240000)
- },
- /**
- * 设置持续定位
- */
- setContinue: function (openNum) {
- const params = {
- openNum: openNum,
- highFreq: 1
- }
- APP.Post(APP.Url.setContinue, params).then(res => {
- })
- },
- /**
- * 获取设备位置
- */
- getLatestState: function (openNum) {
- const that = this;
- const params = {
- openNum: openNum
- }
- APP.Get(APP.Url.getLatestState, params, false).then(res => {
- const stateInfo = res.data;
- if (stateInfo == null) {
- APP.Modal.tips("该设备还没有上传位置信息");
- return;
- }
- //首次加载聚焦中心为当前位置
- mapContext.moveToLocation({
- latitude: parseFloat(stateInfo.latGcj), //纬度
- longitude: parseFloat(stateInfo.lonGcj), //经度
- })
- that.setStateInfo(stateInfo);
- })
- },
- /**
- * 获取设备位置
- * 定时器
- */
- getLatestStateInterval: function (openNum) {
- const params = {
- openNum: openNum
- }
- APP.Get(APP.Url.getLatestState, params, false).then(res => {
- this.setStateInfo(res.data);
- })
- },
- /**
- * 设置位置等状态信息
- */
- setStateInfo: function (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年前";
- },
- })
|