light-map.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // pages/light-map/light-map.js
  2. const app = getApp();
  3. const util = require('../../utils/util');
  4. const WebIM = require("../../utils/WebIM.js")["default"];
  5. const disp = require("../../utils/broadcast");
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. imgPath: app.globalData.imgPath,
  12. longitude: 106.51778136583812,
  13. latitude: 29.617295391392194,
  14. scale: 16,
  15. markers: [],
  16. show: false,
  17. username: '刘元云',
  18. roleName: '工作人员'
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. let that = this;
  25. wx.getSetting({
  26. success: function (res) {
  27. console.info(res);
  28. if (!res.authSetting['scope.userLocation']) {
  29. wx.authorize({
  30. scope: 'scope.userLocation',
  31. success: function (res) {
  32. wx.getLocation({
  33. type: 'gcj02',
  34. success: function (res) {
  35. console.info('getLocation', res);
  36. that.setData({
  37. latitude: res.latitude,
  38. longitude: res.longitude
  39. });
  40. }
  41. });
  42. },
  43. fail: function (e) {
  44. console.info(e);
  45. wx.showToast({
  46. title: '用户未授权',
  47. icon: 'error'
  48. });
  49. }
  50. });
  51. } else {
  52. wx.getLocation({
  53. type: 'gcj02',
  54. success: function (res) {
  55. console.info('getLocation', res);
  56. that.setData({
  57. latitude: res.latitude,
  58. longitude: res.longitude
  59. });
  60. }
  61. });
  62. }
  63. }
  64. });
  65. that.getGroupList();
  66. // for (let i in that.data.groupIdList) {
  67. // let obj = {
  68. // latitude: result.latitude,
  69. // longitude: result.longitude
  70. // }
  71. // 发送当前用户的实时定位信息
  72. // that.sendGroupText(that.data.groupIdList[i], obj);
  73. // 显示其他用户的实时定位信息
  74. // that.showMapMarker(that.data.groupIdList[i]);
  75. // }
  76. },
  77. /**
  78. * 生命周期函数--监听页面初次渲染完成
  79. */
  80. onReady: function () {
  81. this.mapCtx = wx.createMapContext('map', this);
  82. },
  83. /**
  84. * 生命周期函数--监听页面显示
  85. */
  86. onShow: function () {
  87. let that = this;
  88. //需要基础库: 2.8.0
  89. wx.startLocationUpdate({
  90. type: 'gcj02',
  91. success: (res) => {
  92. console.info('startLocationUpdate', res);
  93. if (res.errMsg == 'startLocationUpdate:ok') {
  94. //需要基础库: 2.8.1
  95. wx.onLocationChange((result) => {
  96. console.info('onLocationChange', result);
  97. that.setData({
  98. latitude: result.latitude,
  99. longitude: result.longitude
  100. });
  101. for (let i in that.data.groupIdList) {
  102. let obj = {
  103. latitude: result.latitude,
  104. longitude: result.longitude
  105. }
  106. // 显示其他用户的实时定位信息
  107. that.showMapMarker(that.data.groupIdList[i]);
  108. // 发送当前用户的实时定位信息
  109. that.sendGroupText(that.data.groupIdList[i], obj);
  110. }
  111. });
  112. }
  113. },
  114. });
  115. },
  116. /**
  117. * 生命周期函数--监听页面隐藏
  118. */
  119. onHide: function () {
  120. wx.offLocationChange((result) => {
  121. console.info('offLocationChange', result);
  122. });
  123. wx.stopLocationUpdate({
  124. success: (res) => {
  125. console.info('stopLocationUpdate', res);
  126. },
  127. });
  128. },
  129. /**
  130. * 生命周期函数--监听页面卸载
  131. */
  132. onUnload: function () {
  133. wx.offLocationChange((result) => {
  134. console.info('offLocationChange', result);
  135. });
  136. wx.stopLocationUpdate({
  137. success: (res) => {
  138. console.info('stopLocationUpdate', res);
  139. },
  140. });
  141. },
  142. /**
  143. * 页面相关事件处理函数--监听用户下拉动作
  144. */
  145. onPullDownRefresh: function () {
  146. },
  147. /**
  148. * 页面上拉触底事件的处理函数
  149. */
  150. onReachBottom: function () {
  151. },
  152. /**
  153. * 用户点击右上角分享
  154. */
  155. onShareAppMessage: function () {
  156. },
  157. markerTap: function (e) {
  158. console.info(e);
  159. let id = e.markerId;
  160. util.post({
  161. url: '/api/easemob/getUpUserAttribute/' + id,
  162. success: (res) => {
  163. wx.hideLoading();
  164. console.info(res);
  165. }
  166. });
  167. },
  168. /**
  169. * 群组列表
  170. */
  171. getGroupList: function () {
  172. var that = this;
  173. WebIM.conn.getGroup({
  174. limit: 50,
  175. success: function (res) {
  176. console.info('groupList', res);
  177. let arr = [];
  178. if (res.data && res.data.length > 0) {
  179. for (let i in res.data) {
  180. arr.push(res.data[i].groupid);
  181. }
  182. }
  183. that.setData({
  184. groupIdList: arr
  185. });
  186. },
  187. error: function (e) {
  188. console.info(e)
  189. }
  190. });
  191. },
  192. /**
  193. * 发送群组文本消息
  194. */
  195. sendGroupText: function (groupId, message) {
  196. // 生成本地消息id
  197. let id = WebIM.conn.getUniqueId();
  198. // 创建文本消息
  199. let msg = new WebIM.message('txt', id);
  200. let option = {
  201. // 消息内容
  202. msg: 'location',
  203. // 接收消息对象(群组id)
  204. to: groupId,
  205. // 群聊类型设置为群聊
  206. chatType: 'groupChat',
  207. // 扩展消息
  208. ext: message,
  209. // 对成功的相关定义,sdk会将消息id登记到日志进行备份处理
  210. success: function () {
  211. console.log('send room text success');
  212. // disp.fire('em.chat.sendSuccess', id, 'location');
  213. },
  214. // 对失败的相关定义,sdk会将消息id登记到日志进行备份处理
  215. fail: function () {
  216. console.log('failed');
  217. }
  218. };
  219. msg.set(option);
  220. WebIM.conn.send(msg.body);
  221. },
  222. /**
  223. * 显示其他群组成员定位
  224. */
  225. showMapMarker: function (groupId) {
  226. let that = this;
  227. let myName = wx.getStorageSync("myUsername");
  228. console.info(groupId, myName);
  229. let historyChatMsgs = wx.getStorageSync("rendered_" + groupId + myName) || [];
  230. let chatMsg = wx.getStorageSync(groupId + myName) || [];
  231. chatMsg = historyChatMsgs.concat(chatMsg);
  232. if (!chatMsg.length) return;
  233. chatMsg = chatMsg.slice(-20);
  234. console.info('chatMsg:', chatMsg);
  235. let psersonList = [];
  236. let markerList = [];
  237. for (let i in chatMsg) {
  238. if (chatMsg[i].info.from == myName) continue;
  239. if (chatMsg[i].msg.data[0].data != 'location') continue;
  240. if (psersonList.indexOf(chatMsg[i].info.from) >= 0) continue;
  241. psersonList.push(chatMsg[i].info.from);
  242. markerList.push({
  243. id: Number(chatMsg[i].info.from),
  244. latitude: chatMsg[i].msg.ext.latitude,
  245. longitude: chatMsg[i].msg.ext.longitude,
  246. iconPath: app.globalData.imgPath + '/map-user.png',
  247. width: 34,
  248. height: 40
  249. });
  250. that.setData({
  251. psersonList: psersonList,
  252. markers: markerList,
  253. });
  254. }
  255. }
  256. })