light-map.js 9.5 KB

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