beerStore.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // pages/beerStore/beerStore.js
  2. const request = require("../../utils/request.js");
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. bannerIndex: 0,
  9. bannerData: [],
  10. storeList: [],
  11. lat: null,
  12. lng: null
  13. },
  14. toUrl(event) {
  15. let url = event.currentTarget.dataset.url;
  16. let jumpType = request.getUrlKey(url, 'jumpType');
  17. if(jumpType == 'app') {
  18. let URL = decodeURIComponent(url)
  19. let jumpUrl = request.getUrlKey(URL, 'miniptogram')
  20. wx.navigateTo({
  21. url: JSON.parse(jumpUrl).path
  22. })
  23. }else if(jumpType == 'H5') {
  24. let jumpUrl = request.getUrlKey(url, 'url')
  25. wx.navigateTo({
  26. url: '../webView/webView?url='+jumpUrl
  27. })
  28. }else if(jumpType == 'miniptogram') {
  29. let path = request.getUrlKey(url, 'path')
  30. let appId = request.getUrlKey(url, 'appId')
  31. console.log(appId)
  32. console.log(path)
  33. wx.navigateToMiniProgram({
  34. appId: appId,
  35. path: path,
  36. extraData: {},
  37. envVersion: 'release',
  38. success(res) {
  39. // 打开成功
  40. }
  41. })
  42. }
  43. },
  44. getBannerData() {
  45. let that = this;
  46. request.getData(
  47. 'app/index/banner/list',{bannerType: 'StoreList'},
  48. res => {
  49. that.setData({
  50. bannerData: res.data.data
  51. })
  52. }
  53. )
  54. },
  55. getStoreData() {
  56. let that = this;
  57. request.getData(
  58. 'app/stores/list',{},
  59. res => {
  60. that.setData({
  61. storeList: res.data.data
  62. })
  63. that.storeDataFun();
  64. },
  65. res => {
  66. console.log(res)
  67. }
  68. )
  69. },
  70. bindchange(event) {
  71. this.setData({
  72. bannerIndex: event.detail.current
  73. })
  74. },
  75. /**
  76. * 生命周期函数--监听页面加载
  77. */
  78. onLoad(options) {
  79. let that = this;
  80. wx.getLocation({
  81. type: 'wgs84',
  82. success (res) {
  83. that.setData({
  84. lat: res.latitude,
  85. lng: res.longitude
  86. })
  87. that.getStoreData();
  88. }
  89. })
  90. this.getBannerData();
  91. },
  92. /**
  93. * 生命周期函数--监听页面初次渲染完成
  94. */
  95. onReady() {
  96. },
  97. /**
  98. * 生命周期函数--监听页面显示
  99. */
  100. onShow() {
  101. },
  102. /**
  103. * 生命周期函数--监听页面隐藏
  104. */
  105. onHide() {
  106. },
  107. /**
  108. * 生命周期函数--监听页面卸载
  109. */
  110. onUnload() {
  111. },
  112. /**
  113. * 页面相关事件处理函数--监听用户下拉动作
  114. */
  115. onPullDownRefresh() {
  116. },
  117. /**
  118. * 页面上拉触底事件的处理函数
  119. */
  120. onReachBottom() {
  121. },
  122. /**
  123. * 用户点击右上角分享
  124. */
  125. onShareAppMessage() {
  126. },
  127. storeDataFun() {
  128. let that = this;
  129. that.data.storeList.forEach(item=>{
  130. item.distanceNum = that.getdiscountNum(that.data.lat, that.data.lng, item.lat, item.lng);
  131. item.distance = that.getdiscount(that.data.lat, that.data.lng, item.lat, item.lng);
  132. })
  133. that.data.storeList = that.data.storeList.sort(function(a, b){
  134. return a.distanceNum - b.distanceNum;
  135. })
  136. that.setData({
  137. storeList: that.data.storeList
  138. })
  139. },
  140. getdiscount(lat1,lng1,lat2,lng2){
  141. var radLat1 = lat1*Math.PI / 180.0;
  142. var radLat2 = lat2*Math.PI / 180.0;
  143. var a = radLat1 - radLat2;
  144. var b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
  145. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
  146. Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
  147. s = s *6378.137 ;
  148. s = Math.round(s * 10000) / 10000;
  149. s = s * 1000;
  150. if (isNaN(s)) {
  151. return 0 + 'km';
  152. }
  153. s = Math.floor(s/1000 * 100) / 100;
  154. s = s.toFixed(2) + 'km'
  155. // if (s > 1000) {
  156. // // 大于1000米时
  157. // s = Math.floor(s/1000 * 100) / 100;
  158. // s = s.toFixed(2) + 'km'
  159. // } else {
  160. // // 小于1000米直接返回
  161. // s = s.toFixed(2) + 'm'
  162. // }
  163. return s;
  164. },
  165. getdiscountNum(lat1,lng1,lat2,lng2){
  166. var radLat1 = lat1*Math.PI / 180.0;
  167. var radLat2 = lat2*Math.PI / 180.0;
  168. var a = radLat1 - radLat2;
  169. var b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
  170. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
  171. Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
  172. s = s *6378.137 ;
  173. s = Math.round(s * 10000) / 10000;
  174. s = s * 1000;
  175. return s.toFixed(2);
  176. }
  177. })