index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. const APP = getApp();
  2. let mapContext; //地图对象
  3. let clockRefresh = null;
  4. let continueRefresh = null;
  5. Page({
  6. data: {
  7. device: null, //设备信息
  8. location: null, //位置信息
  9. mapCenter: { //地图聚焦中心 默认北京
  10. latitude: 39.9,
  11. longitude: 116.6,
  12. },
  13. markers: [], //地图标记
  14. },
  15. // 监听页面加载
  16. onLoad: function () {
  17. // 获取地图对象
  18. mapContext = wx.createMapContext('map');
  19. this.setData({
  20. device: APP.getCurrentDevice()
  21. })
  22. },
  23. // 监听页面显示
  24. onShow: function () {
  25. const that = this;
  26. if (APP.getDeviceList().length < 1) {
  27. APP.Modal.tips("您还没有设备可以查看");
  28. mapContext.moveToLocation({});
  29. return;
  30. }
  31. //请求设备位置
  32. this.getLocation(APP.getCurrentDevice().deviceId, false);
  33. setTimeout(function () {
  34. that.intervalRefresh(APP.getCurrentDevice().deviceId);
  35. }, 5000)
  36. that.intervalSetContinue(APP.getCurrentDevice().deviceId);
  37. },
  38. // 监听页面隐藏
  39. onHide: function () {
  40. //清除定时
  41. clearInterval(clockRefresh);
  42. clearInterval(continueRefresh);
  43. clockRefresh = null;
  44. continueRefresh = null;
  45. },
  46. /**
  47. * 定时刷新
  48. */
  49. intervalRefresh: function (deviceId) {
  50. console.log("启动")
  51. const that = this;
  52. if (clockRefresh != null) return;
  53. clockRefresh = setInterval(function () {
  54. that.getLocationInterval(deviceId);
  55. }, 15000)
  56. },
  57. /**
  58. * 定时设置持续定位
  59. */
  60. intervalSetContinue: function (deviceId) {
  61. console.log("启动持续定位")
  62. const that = this;
  63. if (continueRefresh != null) return;
  64. that.setContinue(deviceId);
  65. continueRefresh = setInterval(function () {
  66. that.setContinue(deviceId);
  67. }, 240000)
  68. },
  69. /**
  70. * 设置持续定位
  71. */
  72. setContinue: function (deviceId) {
  73. const params = {
  74. deviceId: deviceId,
  75. highFreq: 1
  76. }
  77. APP.Post(APP.Url.setContinue, params).then(res => {}).catch(res => {
  78. console.log(res)
  79. })
  80. },
  81. /**
  82. * 聚焦中心
  83. */
  84. center: function () {
  85. if (this.data.location == null) return;
  86. this.moveTo(this.data.location.lonGcj, this.data.location.latGcj);
  87. },
  88. /**
  89. * 获取设备位置
  90. */
  91. getLocation: function (deviceId) {
  92. const that = this;
  93. const params = {
  94. deviceId: deviceId
  95. }
  96. APP.Get(APP.Url.getLocation, params, false).then(res => {
  97. const location = res.data;
  98. if (location == null) {
  99. APP.Modal.tips("该设备还没有上传位置信息");
  100. return;
  101. }
  102. //首次加载聚焦中心为当前位置
  103. that.moveTo(location.lonGcj, location.latGcj);
  104. that.setLocation(location);
  105. })
  106. },
  107. /**
  108. * 获取设备位置
  109. * 定时器
  110. */
  111. getLocationInterval: function (deviceId) {
  112. console.log("请求数据")
  113. const params = {
  114. deviceId: deviceId
  115. }
  116. APP.Get(APP.Url.getLocation, params, false).then(res => {
  117. this.setLocation(res.data);
  118. })
  119. },
  120. /**
  121. * 设置位置信息
  122. */
  123. setLocation: function (location) {
  124. if (location == null) return;
  125. //信号强度
  126. location.signal = this.formatSignal(location.signalnum);
  127. //定位时间
  128. location.latestTime = this.formatTime(location.uploadTime)
  129. this.setData({
  130. location: location
  131. })
  132. //添加地图标记
  133. this.addMarker(location.lonGcj, location.latGcj)
  134. },
  135. /**
  136. * 格式化时间显示
  137. */
  138. formatTime: function (date) {
  139. const diff = (new Date().getTime() - new Date(date).getTime()) / 1000;
  140. if (diff < 60) return "刚刚";
  141. if (diff < 3600) return parseInt(diff / 60) + "分钟前";
  142. if (diff < 86400) return parseInt(diff / 3600) + "小时前";
  143. if (diff < 31104000) return parseInt(diff / 86400) + "天前";
  144. return "1年前";
  145. },
  146. /**
  147. * 格式化电池信号量
  148. */
  149. formatSignal: function (num) {
  150. if (num > 90) return "信号良好";
  151. if (num > 60) return "信号正常";
  152. if (num > 30) return "信号较差";
  153. if (num == 0) return "无信号";
  154. return "信号差"
  155. },
  156. /**
  157. * 添加标记
  158. */
  159. addMarker: function (lng, lat) {
  160. this.setData({
  161. markers: [{
  162. id: 1,
  163. iconPath: "/imgs/marker.png",
  164. latitude: parseFloat(lat),
  165. longitude: parseFloat(lng),
  166. width: 35,
  167. height: 35
  168. }]
  169. })
  170. },
  171. /**
  172. * 移动地图焦点到某个点
  173. */
  174. moveTo: function (lng, lat) {
  175. mapContext.moveToLocation({
  176. latitude: parseFloat(lat), //纬度
  177. longitude: parseFloat(lng), //经度
  178. })
  179. }
  180. })