// pages/beerStore/beerStore.js const request = require("../../utils/request.js"); Page({ /** * 页面的初始数据 */ data: { bannerIndex: 0, bannerData: [], storeList: [], lat: null, lng: null }, toUrl(event) { let url = event.currentTarget.dataset.url; let jumpType = request.getUrlKey(url, 'jumpType'); if(jumpType == 'app') { let URL = decodeURIComponent(url) let jumpUrl = request.getUrlKey(URL, 'miniptogram') wx.navigateTo({ url: JSON.parse(jumpUrl).path }) }else if(jumpType == 'H5') { let jumpUrl = request.getUrlKey(url, 'url') wx.navigateTo({ url: '../webView/webView?url='+jumpUrl }) }else if(jumpType == 'miniptogram') { let path = request.getUrlKey(url, 'path') let appId = request.getUrlKey(url, 'appId') console.log(appId) console.log(path) wx.navigateToMiniProgram({ appId: appId, path: path, extraData: {}, envVersion: 'release', success(res) { // 打开成功 } }) } }, getBannerData() { let that = this; request.getData( 'app/index/banner/list',{bannerType: 'StoreList'}, res => { that.setData({ bannerData: res.data.data }) } ) }, getStoreData() { let that = this; request.getData( 'app/stores/list',{}, res => { that.setData({ storeList: res.data.data }) that.storeDataFun(); }, res => { console.log(res) } ) }, bindchange(event) { this.setData({ bannerIndex: event.detail.current }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { let that = this; wx.getLocation({ type: 'wgs84', success (res) { that.setData({ lat: res.latitude, lng: res.longitude }) that.getStoreData(); } }) this.getBannerData(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, storeDataFun() { let that = this; that.data.storeList.forEach(item=>{ item.distanceNum = that.getdiscountNum(that.data.lat, that.data.lng, item.lat, item.lng); item.distance = that.getdiscount(that.data.lat, that.data.lng, item.lat, item.lng); }) that.data.storeList = that.data.storeList.sort(function(a, b){ return a.distanceNum - b.distanceNum; }) that.setData({ storeList: that.data.storeList }) }, getdiscount(lat1,lng1,lat2,lng2){ var radLat1 = lat1*Math.PI / 180.0; var radLat2 = lat2*Math.PI / 180.0; var a = radLat1 - radLat2; var b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0; var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2))); s = s *6378.137 ; s = Math.round(s * 10000) / 10000; s = s * 1000; if (isNaN(s)) { return 0 + 'km'; } s = Math.floor(s/1000 * 100) / 100; s = s.toFixed(2) + 'km' // if (s > 1000) { // // 大于1000米时 // s = Math.floor(s/1000 * 100) / 100; // s = s.toFixed(2) + 'km' // } else { // // 小于1000米直接返回 // s = s.toFixed(2) + 'm' // } return s; }, getdiscountNum(lat1,lng1,lat2,lng2){ var radLat1 = lat1*Math.PI / 180.0; var radLat2 = lat2*Math.PI / 180.0; var a = radLat1 - radLat2; var b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0; var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2))); s = s *6378.137 ; s = Math.round(s * 10000) / 10000; s = s * 1000; return s.toFixed(2); } })