index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. const APP = getApp();
  2. //地图对象
  3. let mapContext;
  4. //定时刷新位置信息对象
  5. let locationInterval = null;
  6. //持续定位定时任务对象
  7. let freqInterval = null;
  8. //倒计时定时任务对象
  9. let countDownInterval = null;
  10. Page({
  11. data: {
  12. schedule: 0,
  13. circle: false,
  14. loading: false,
  15. stateInfo: null, //设备状态信息
  16. markers: [], //地图标记
  17. },
  18. // 监听页面加载
  19. onLoad: function () {
  20. // 获取地图对象
  21. mapContext = wx.createMapContext('map');
  22. },
  23. // 监听页面显示
  24. onShow: function () {
  25. if (APP.globalData.openNum == null) {
  26. APP.Modal.tips("您还没有设备可以查看");
  27. mapContext.moveToLocation({});
  28. return;
  29. }
  30. //请求设备位置
  31. this.getLatestLocation(APP.globalData.openNum);
  32. //开启定时任务-定时刷新位置信息
  33. this.intervalRefresh(APP.globalData.openNum);
  34. //开启定时任务-高频定位模式
  35. this.intervalSetContinue(APP.globalData.openNum);
  36. },
  37. // 监听页面隐藏
  38. onHide: function () {
  39. //清除定时
  40. clearInterval(locationInterval);
  41. clearInterval(freqInterval);
  42. locationInterval = null;
  43. freqInterval = null;
  44. },
  45. /**
  46. * 定时刷新
  47. */
  48. intervalRefresh: function (openNum) {
  49. console.log("启动定时刷新位置")
  50. const that = this;
  51. if (locationInterval == null) {//不为空说明已经被初始化,则不做处理
  52. that.countDown(14);
  53. locationInterval = setInterval(function () {
  54. APP.Get(APP.Url.getLatestLocation, { openNum: openNum }, false).then(res => {
  55. that.countDown(14);
  56. that.updateStateInfo(res.data);
  57. })
  58. }, 15000)
  59. }
  60. },
  61. /**
  62. * 定时开启高频定位
  63. */
  64. intervalSetContinue: function (openNum) {
  65. console.log("启动持续定位")
  66. const that = this;
  67. if (freqInterval == null) {
  68. that.startHighFreq(openNum)
  69. freqInterval = setInterval(function () {
  70. that.startHighFreq(openNum);
  71. }, 240000)
  72. }
  73. },
  74. /**
  75. * 开启高频定位
  76. */
  77. startHighFreq: function (openNum) {
  78. const params = {
  79. openNum: openNum,
  80. highFreq: 1
  81. }
  82. APP.Post(APP.Url.setContinue, params).then(res => {
  83. })
  84. },
  85. /**
  86. * 获取设备位置
  87. */
  88. getLatestLocation(openNum) {
  89. const that = this;
  90. const params = {
  91. openNum: openNum
  92. }
  93. APP.Get(APP.Url.getLatestLocation, params, false).then(res => {
  94. const detail = res.data;
  95. if (detail == null) {
  96. APP.Modal.tips("该设备还没有上传位置信息");
  97. return;
  98. }
  99. //首次加载聚焦中心为当前位置
  100. mapContext.moveToLocation({
  101. latitude: detail.latGcj, //纬度
  102. longitude: detail.lonGcj, //经度
  103. })
  104. //更新位置数据
  105. that.updateStateInfo(detail);
  106. })
  107. },
  108. /**
  109. * 设置位置等状态信息
  110. */
  111. updateStateInfo(stateInfo) {
  112. if (stateInfo == null) return;
  113. //定位时间
  114. stateInfo.uploadTimeFormat = this.formatTime(stateInfo.uploadTime)
  115. this.setData({
  116. stateInfo: stateInfo,
  117. markers: [{
  118. id: 1,
  119. iconPath: "/imgs/marker.png",
  120. latitude: parseFloat(stateInfo.latGcj),
  121. longitude: parseFloat(stateInfo.lonGcj),
  122. width: 35,
  123. height: 35
  124. }]
  125. })
  126. },
  127. /**
  128. * 格式化时间显示
  129. */
  130. formatTime: function (date) {
  131. const diff = (new Date().getTime() - new Date(date).getTime()) / 1000;
  132. if (diff < 60) return "刚刚";
  133. if (diff < 3600) return parseInt(diff / 60) + "分钟前";
  134. if (diff < 86400) return parseInt(diff / 3600) + "小时前";
  135. if (diff < 31104000) return parseInt(diff / 86400) + "天前";
  136. return "1年前";
  137. },
  138. /**
  139. * 倒计时
  140. */
  141. countDown(second) {
  142. const that = this;
  143. if (countDownInterval == null) {
  144. //系数
  145. let factor = 100 / second;
  146. //初始化步数
  147. let step = 1;
  148. //初始化进度条进度
  149. this.setData({
  150. schedule: 0,
  151. circle: true,
  152. loading: false
  153. })
  154. //定时器
  155. countDownInterval = setInterval(() => {
  156. //如果到达指定步数,则关闭定时
  157. if (step >= second) {
  158. clearInterval(countDownInterval);
  159. countDownInterval = null;
  160. this.setData({
  161. schedule: 0,
  162. circle: false,
  163. loading: true
  164. })
  165. }
  166. //进度条增加
  167. that.setData({
  168. schedule: factor * step
  169. })
  170. //步数增加
  171. step = step + 1;
  172. }, 1000);
  173. }
  174. },
  175. /**
  176. * 刷新
  177. */
  178. refresh() {
  179. wx.showLoading({
  180. title: '正在刷新',
  181. })
  182. setTimeout(function () {
  183. wx.hideLoading();
  184. APP.Modal.tips("刷新成功");
  185. }, 600)
  186. },
  187. call() {
  188. APP.Modal.tips("功能暂未开通");
  189. },
  190. openMapApp() {
  191. const params = {
  192. longitude: this.data.stateInfo.lonGcj,
  193. latitude: this.data.stateInfo.latGcj,
  194. destination: "手机所在位置"
  195. }
  196. mapContext.openMapApp(params);
  197. }
  198. })