// pages/welfareMall/couponFitStore/index.js import activity from '../../../api/activity' Page({ /** * 页面的初始数据 */ data: { couponId: '', keyword: '', page: 1, pageSize: 10, storeList: [] }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (!options.couponId) { wx.reLaunch({ url: '/pages/welfareMall/index/index', }) } this.data.couponId = options.couponId const that = this wx.getSetting({ success(res) { // 1. scope.userLocation 为真, 代表用户已经授权 if (res.authSetting['scope.userLocation']) { // 1.1 使用 getlocation 获取用户 经纬度位置 wx.getLocation({ success(res){ // 1.2 获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置 console.log(res) const l = that.qqMapToBMap(res.longitude, res.latitude) that.setData({ latitude: l[1], longitude: l[0], }) that.loadStoreListData() }, fail() { that.loadStoreListData() } }) }else { // 2. 用户未授权的情况下, 打开授权界面, 引导用户授权. wx.authorize({ scope: "scope.userLocation", success(res) { wx.getLocation({ success(res){ // 2.2 获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置 // 2.3 将获取到的 经纬度传值给 getAddress 解析出 具体的地址 that.setData({ latitude: res.latitude, longitude: res.longitude, }) that.loadStoreListData() }, fail() { that.loadStoreListData() } }) } }) } } }) }, qqMapToBMap(lng, lat) { if (lng == null || lng == '' || lat == null || lat == '') return [lng, lat]; var x_pi = 3.14159265358979324; var x = parseFloat(lng); var y = parseFloat(lat); var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi); var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi); var lng = (z * Math.cos(theta) + 0.0065).toFixed(5); var lat = (z * Math.sin(theta) + 0.006).toFixed(5); return [lng, lat]; }, bMapToQQMap(lng, lat) { if (lng == null || lng == '' || lat == null || lat == '') return [lng, lat]; var x_pi = 3.14159265358979324; var x = parseFloat(lng) - 0.0065; var y = parseFloat(lat) - 0.006; var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi); var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi); var lng = (z * Math.cos(theta)).toFixed(7); var lat = (z * Math.sin(theta)).toFixed(7); return [lng, lat]; }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, onConfirm(e) { this.setData({ page: 1, keyword: e.detail.value, noMore: false, noResult: false, storeList: [] }) this.loadStoreListData() }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, onReachBottom: function () { if (this.data.lock || this.data.noMore) { return } this.data.lock = true this.data.page++ this.loadStoreListData(); }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, loadStoreListData: function() { activity.getStoreListByCoupon({ couponId: this.data.couponId, latitude: this.data.latitude || null, longitude: this.data.longitude || null, page: this.data.page, keyword: this.data.keyword, pageSize: 10, }).then(data => { if (data.code == 200) { this.dataMapToView(data.data) } this.data.lock = false }).catch(_ => { this.data.lock = false; console.log(_) }) }, dataMapToView(list) { if (!Array.isArray(list) || list.length == 0) { if (this.data.page == 1) { this.setData({ noResult: true }) } else { this.setData({ noMore: true }) } return } list.forEach(v => { v.distance = parseInt(v.distance * 100) / 100 if (v.distance > 1) { v.distanceStr = "距离" + v.distance + "km" } else { v.distanceStr = "距离" + (v.distance * 100) + "m" } }) this.data.storeList = this.data.storeList.concat(list) this.setData({ storeList: this.data.storeList }) }, callPhone(e) { const phone = e.currentTarget.dataset.phone if (phone) { wx.makePhoneCall({ phoneNumber: phone, }) } } })