123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- // 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() {
- wx.navigateBack({
- delta: -1,
- })
- }
- })
- }else {
- // 2. 用户未授权的情况下, 打开授权界面, 引导用户授权.
- wx.openSetting({
- success(res) {
- // 2.1 如果二次授权允许了 userLocation 权限, 就再次执行获取位置的接口
- if (res.authSetting["scope.userLocation"]) {
- wx.getLocation({
- success(res){
- // 2.2 获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置
- // 2.3 将获取到的 经纬度传值给 getAddress 解析出 具体的地址
- that.setData({
- latitude: res.latitude,
- longitude: res.longitude,
- })
- that.loadStoreListData()
- },
- fail() {
- wx.navigateBack({
- delta: -1,
- })
- }
- })
- }
- }
- })
- }
- }
- })
- },
- 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,
- longitude: this.data.longitude,
- 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: list
- })
- },
- callPhone(e) {
- const phone = e.currentTarget.dataset.phone
- if (phone) {
- wx.makePhoneCall({
- phoneNumber: phone,
- })
- }
- }
- })
|