light-map.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. mapTap: function () {
  156. this.setData({
  157. show: false
  158. });
  159. },
  160. /**
  161. * 点击其他用户定位
  162. */
  163. markerTap: function (e) {
  164. let that = this;
  165. console.info(e);
  166. let id = e.markerId;
  167. util.post({
  168. url: '/api/easemob/getUpUserAttribute/' + id,
  169. success: (res) => {
  170. wx.hideLoading();
  171. console.info(res);
  172. if (res.data.code != 200) {
  173. util.toast(res.data.msg);
  174. } else {
  175. that.setData({
  176. show: true,
  177. userPhoto: res.data.data.data.avatarurl,
  178. nickname: res.data.data.data.nickname,
  179. username: id,
  180. });
  181. }
  182. }
  183. });
  184. },
  185. /**
  186. * 群组列表
  187. */
  188. getGroupList: function () {
  189. var that = this;
  190. WebIM.conn.getGroup({
  191. limit: 50,
  192. success: function (res) {
  193. console.info('groupList', res);
  194. let arr = [];
  195. if (res.data && res.data.length > 0) {
  196. for (let i in res.data) {
  197. arr.push(res.data[i].groupid);
  198. }
  199. }
  200. that.setData({
  201. groupIdList: arr
  202. });
  203. },
  204. error: function (e) {
  205. console.info(e)
  206. }
  207. });
  208. },
  209. /**
  210. * 发送群组文本消息
  211. */
  212. sendGroupText: function (groupId, message) {
  213. // 生成本地消息id
  214. let id = WebIM.conn.getUniqueId();
  215. // 创建文本消息
  216. let msg = new WebIM.message('txt', id);
  217. let option = {
  218. // 消息内容
  219. msg: 'location',
  220. // 接收消息对象(群组id)
  221. to: groupId,
  222. // 群聊类型设置为群聊
  223. chatType: 'groupChat',
  224. // 扩展消息
  225. ext: message,
  226. // 对成功的相关定义,sdk会将消息id登记到日志进行备份处理
  227. success: function () {
  228. console.log('send room text success');
  229. disp.fire('em.chat.sendSuccess', id, 'location');
  230. },
  231. // 对失败的相关定义,sdk会将消息id登记到日志进行备份处理
  232. fail: function () {
  233. console.log('failed');
  234. }
  235. };
  236. msg.set(option);
  237. WebIM.conn.send(msg.body);
  238. },
  239. /**
  240. * 显示其他群组成员定位
  241. */
  242. showMapMarker: function (groupId) {
  243. let that = this;
  244. let myName = wx.getStorageSync("myUsername");
  245. console.info(groupId, myName);
  246. let historyChatMsgs = wx.getStorageSync("rendered_" + groupId + myName) || [];
  247. console.info('chatMsg:', historyChatMsgs);
  248. let chatMsg = wx.getStorageSync(groupId + myName) || [];
  249. console.info('chatMsg:', chatMsg);
  250. chatMsg = historyChatMsgs.concat(chatMsg);
  251. if (!chatMsg.length) return;
  252. chatMsg = chatMsg.slice(-20);
  253. console.info('chatMsg:', chatMsg);
  254. let psersonList = [];
  255. let markerList = [];
  256. for (let i in chatMsg) {
  257. if (chatMsg[i].info.from == myName) continue;
  258. if (chatMsg[i].msg.data[0].data != 'location') continue;
  259. if (psersonList.indexOf(chatMsg[i].info.from) >= 0) continue;
  260. psersonList.push(chatMsg[i].info.from);
  261. markerList.push({
  262. id: Number(chatMsg[i].info.from),
  263. latitude: chatMsg[i].msg.ext.latitude,
  264. longitude: chatMsg[i].msg.ext.longitude,
  265. iconPath: app.globalData.imgPath + '/map-user.png',
  266. width: 34,
  267. height: 40
  268. });
  269. that.setData({
  270. psersonList: psersonList,
  271. markers: markerList,
  272. });
  273. }
  274. },
  275. /**
  276. * 打开聊天
  277. */
  278. intoChatRoom: function () {
  279. let that = this;
  280. let myName = wx.getStorageSync("myUsername");
  281. let nameInfo = {
  282. myName: myName,
  283. your: that.data.username.toString()
  284. };
  285. wx.navigateTo({
  286. url: "../chat-detail/chat-detail?username=" + JSON.stringify(nameInfo)
  287. });
  288. }
  289. })