index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const APP = getApp();
  2. let mapContext; //地图对象
  3. //定时刷新位置信息对象
  4. let clockRefresh = null;
  5. //持续定位定时任务对象
  6. let continueRefresh = null;
  7. Page({
  8. data: {
  9. stateInfo: null, //设备状态信息
  10. markers: [], //地图标记
  11. },
  12. // 监听页面加载
  13. onLoad: function () {
  14. // 获取地图对象
  15. mapContext = wx.createMapContext('map');
  16. },
  17. // 监听页面显示
  18. onShow: function () {
  19. if (APP.globalData.openNum == null) {
  20. APP.Modal.tips("您还没有设备可以查看");
  21. mapContext.moveToLocation({});
  22. return;
  23. }
  24. //请求设备位置
  25. this.getLatestState(APP.globalData.openNum);
  26. //开启定时任务-定时刷新位置信息
  27. this.intervalRefresh(APP.globalData.openNum);
  28. //开启定时任务-高频定位模式
  29. this.intervalSetContinue(APP.globalData.openNum);
  30. },
  31. // 监听页面隐藏
  32. onHide: function () {
  33. //清除定时
  34. clearInterval(clockRefresh);
  35. clearInterval(continueRefresh);
  36. clockRefresh = null;
  37. continueRefresh = null;
  38. },
  39. /**
  40. * 定时刷新
  41. */
  42. intervalRefresh: function (openNum) {
  43. console.log("启动定时刷新位置")
  44. const that = this;
  45. if (clockRefresh != null) return;
  46. clockRefresh = setInterval(function () {
  47. that.getLatestStateInterval(openNum);
  48. }, 15000)
  49. },
  50. /**
  51. * 定时设置持续定位
  52. */
  53. intervalSetContinue: function (openNum) {
  54. console.log("启动持续定位")
  55. const that = this;
  56. if (continueRefresh != null) return;
  57. that.setContinue(openNum);
  58. continueRefresh = setInterval(function () {
  59. that.setContinue(openNum);
  60. }, 240000)
  61. },
  62. /**
  63. * 设置持续定位
  64. */
  65. setContinue: function (openNum) {
  66. const params = {
  67. openNum: openNum,
  68. highFreq: 1
  69. }
  70. APP.Post(APP.Url.setContinue, params).then(res => {
  71. })
  72. },
  73. /**
  74. * 获取设备位置
  75. */
  76. getLatestState: function (openNum) {
  77. const that = this;
  78. const params = {
  79. openNum: openNum
  80. }
  81. APP.Get(APP.Url.getLatestState, params, false).then(res => {
  82. const stateInfo = res.data;
  83. if (stateInfo == null) {
  84. APP.Modal.tips("该设备还没有上传位置信息");
  85. return;
  86. }
  87. //首次加载聚焦中心为当前位置
  88. mapContext.moveToLocation({
  89. latitude: parseFloat(stateInfo.latGcj), //纬度
  90. longitude: parseFloat(stateInfo.lonGcj), //经度
  91. })
  92. that.setStateInfo(stateInfo);
  93. })
  94. },
  95. /**
  96. * 获取设备位置
  97. * 定时器
  98. */
  99. getLatestStateInterval: function (openNum) {
  100. const params = {
  101. openNum: openNum
  102. }
  103. APP.Get(APP.Url.getLatestState, params, false).then(res => {
  104. this.setStateInfo(res.data);
  105. })
  106. },
  107. /**
  108. * 设置位置等状态信息
  109. */
  110. setStateInfo: function (stateInfo) {
  111. if (stateInfo == null) return;
  112. //定位时间
  113. stateInfo.uploadTimeFormat = this.formatTime(stateInfo.uploadTime)
  114. this.setData({
  115. stateInfo: stateInfo,
  116. markers: [{
  117. id: 1,
  118. iconPath: "/imgs/marker.png",
  119. latitude: parseFloat(stateInfo.latGcj),
  120. longitude: parseFloat(stateInfo.lonGcj),
  121. width: 35,
  122. height: 35
  123. }]
  124. })
  125. },
  126. /**
  127. * 格式化时间显示
  128. */
  129. formatTime: function (date) {
  130. const diff = (new Date().getTime() - new Date(date).getTime()) / 1000;
  131. if (diff < 60) return "刚刚";
  132. if (diff < 3600) return parseInt(diff / 60) + "分钟前";
  133. if (diff < 86400) return parseInt(diff / 3600) + "小时前";
  134. if (diff < 31104000) return parseInt(diff / 86400) + "天前";
  135. return "1年前";
  136. },
  137. })