index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // pages/user/localtion/index.js
  2. const {
  3. globalData: {
  4. util: {
  5. regeneratorRuntime,
  6. api: {
  7. allDept: url
  8. },
  9. request: {
  10. get
  11. },
  12. navigateBack,
  13. navigate_auth_native,
  14. telephone_native
  15. }
  16. }
  17. } = getApp();
  18. Page({
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. async onLoad() {
  23. this.p = get(url)
  24. },
  25. /**
  26. * 生命周期函数--监听页面初次渲染完成
  27. */
  28. onReady: function() {
  29. },
  30. /**
  31. * 生命周期函数--监听页面显示
  32. */
  33. onShow() {
  34. wx.getLocation({
  35. type: 'wgs84',
  36. success: async({
  37. latitude,
  38. longitude
  39. }) => {
  40. let items= await this.p //去分页后 去除花括号
  41. items.forEach(item => {
  42. let [x, y] = item.position.split(',')
  43. item.distance = this.getDistance(latitude, longitude, y, x) / 1000
  44. item.longlat = [x, y]
  45. if(item.deptName.indexOf("李楼")!=-1){
  46. item.img = '/images/address/lilouzheng.jpg'
  47. } else if (item.deptName.indexOf("白马寺") != -1) {
  48. item.img = '/images/address/baimasi.jpg'
  49. } else if (item.deptName.indexOf("关林") != -1) {
  50. item.img = '/images/address/guanlin.jpg'
  51. } else if (item.deptName.indexOf("古城") != -1) {
  52. item.img = '/images/address/gucheng.jpg'
  53. } else if (item.deptName.indexOf("科技园") != -1) {
  54. item.img = '/images/address/kejiyuan.jpg'
  55. } else if (item.deptName.indexOf("泉舜") != -1) {
  56. item.img = '/images/address/quanshun.jpg'
  57. } else if (item.deptName.indexOf("龙门街道") != -1) {
  58. item.img = '/images/address/longmenjiedao.jpg'
  59. } else if (item.deptName.indexOf("太康") != -1) {
  60. item.img = '/images/address/taikangdonglu.jpg'
  61. } else if (item.deptName.indexOf("学府") != -1) {
  62. item.img = '/images/address/xuefujie.jpg'
  63. } else if (item.deptName.indexOf("安乐") != -1) {
  64. item.img = '/images/address/anlezhenzhengfu.jpg'
  65. } else if (item.deptName.indexOf("开元") != -1) {
  66. item.img = '/images/address/kaiyuanlu.jpg'
  67. }else{
  68. item.img = '/images/address/luoyang.jpg'
  69. }
  70. })
  71. this.setData({
  72. items: items.sort((a, b) => a.distance - b.distance).map(({ deptName: name, img,deptLeader: tel, distance,longlat})=>({
  73. name, tel, distance, img,longlat
  74. })),
  75. })
  76. },
  77. fail(e) {
  78. wx.getSetting({
  79. success({
  80. authSetting
  81. }) {
  82. if (!authSetting['scope.userLocation']) {
  83. wx.showModal({
  84. title: '是否授权当前位置',
  85. content: '需要获取您的地理位置,请确认授权,否则地图功能将无法使用',
  86. success: function(tip) {
  87. if (tip.confirm) {
  88. wx.openSetting({
  89. success: function(data) {
  90. if (data.authSetting["scope.userLocation"] === true) {
  91. wx.showToast({
  92. title: '授权成功',
  93. icon: 'success',
  94. duration: 1000
  95. })
  96. }
  97. }
  98. })
  99. } else {
  100. // wx.showToast({
  101. // title: '授权失败',
  102. // icon: 'none',
  103. // duration: 1000
  104. // })
  105. navigateBack();
  106. }
  107. }
  108. })
  109. }
  110. }
  111. })
  112. }
  113. })
  114. },
  115. /**
  116. * 生命周期函数--监听页面隐藏
  117. */
  118. onHide: function() {
  119. },
  120. /**
  121. * 生命周期函数--监听页面卸载
  122. */
  123. onUnload: function() {
  124. },
  125. /**
  126. * 页面相关事件处理函数--监听用户下拉动作
  127. */
  128. onPullDownRefresh: function() {
  129. },
  130. /**
  131. * 页面上拉触底事件的处理函数
  132. */
  133. onReachBottom: function() {
  134. },
  135. /**
  136. * 用户点击右上角分享
  137. */
  138. onShareAppMessage: function() {
  139. },
  140. getDistance: function(lat1, lng1, lat2, lng2) {
  141. lat1 = lat1 || 0;
  142. lng1 = lng1 || 0;
  143. lat2 = lat2 || 0;
  144. lng2 = lng2 || 0;
  145. var rad1 = lat1 * Math.PI / 180.0;
  146. var rad2 = lat2 * Math.PI / 180.0;
  147. var a = rad1 - rad2;
  148. var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  149. var r = 6378137;
  150. return (r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))).toFixed(0)
  151. },
  152. map:function(e){
  153. wx.openLocation({
  154. latitude: parseFloat(e.currentTarget.dataset.w),
  155. longitude: parseFloat(e.currentTarget.dataset.j),
  156. name: e.currentTarget.dataset.name ? e.currentTarget.dataset.name:'',
  157. scale: 18
  158. })
  159. },
  160. navigate_auth_native,
  161. telephone_native
  162. })