// c:\Users\56817\Desktop\Working Space\3,TikTok-MiniPro\ttc\pages2\shopSelect\shopSelect.js let config = require("../../config/config.js"); let util = require("../../utils/util"); let Http = require("../../utils/HttpBasics"); let app = getApp(); const imgurl = require("../../utils/imgurl"); Page({ data: { isShop: true, position: 0, tagArr: [], cityArr: [], mallList: [], currentTag: "", currentCity: "", isLocation: false, isOrder: "", merOrderId: "", latitude: "", longitude: "", toView: "A" }, onLoad: function (options) { tt.showLoading({ title: '加载中...', }) console.log(options, 'options') const mallTenantId = tt.getStorageSync('mallTenantId'); if (mallTenantId) { this.setData({ mallTenantId }) } // const tagArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] // this.setData({ // tagArr // }) this.setData({ isOrder: options.isOrder || false, merOrderId: options.merOrderId || '', }) this.getLocation() }, getCouponChannelId(flag) { const that = this Http.get({ url: config.api.orderDetail, data: { orderId: that.data.merOrderId, mallTenantId: that.data.mallTenantId || '' } }).then(res => { const couponChannelId = res.data.orders[0].couponChannelId that.getCouponMerchant(couponChannelId, flag) that.getCityList(couponChannelId) }).catch(err => { }) }, //获取适用门店 getCouponMerchant(couponChannelId, flag) { console.log(flag, 'flag'); const that = this Http.get({ url: config.api.couponMerchant, data: { couponChannelId, city: that.data.currentCity, mallTenantId: that.data.mallTenantId || '' } }).then(res => { const keys = Object.keys(res.data) if (keys.length > 0) { const list = [] let i = 0 keys.forEach(item => { const arr = item.split('|') const obj = { tenantId: arr[0], mallName: arr[1], merchantVoList: res.data[item], expand: false } if (i == 0) { obj.expand = true } list.push(obj) i++ }) const mallList = [] // 过滤商管商户 list[0].merchantVoList.forEach(item => { if (!item.isAdmin) { mallList.push(item) } }) that.setData({ mallList }) console.log(that.data.mallList, 'mallList'); } else { that.setData({ mallList: [] }) console.log(that.data.mallList, 'mallList'); } if (flag) { that.dealMallList() } tt.hideLoading(); }) }, // 获取适用门店所在的城市list getCityList(couponChannelId) { tt.showLoading({ title: '加载中...', }) const that = this Http.get({ url: config.api.couponMerchantCity, data: { couponChannelId } }).then(res => { console.log(res, 'res'); const tagArr = Object.keys(res.data) tagArr.sort(function (a, b) { if (a < b) { return -1; } if (a > b) { return 1; } return 0; }); const cityArr = res.data that.setData({ tagArr, cityArr }) console.log(tagArr, 'tagArr'); console.log(cityArr, 'cityArr'); tt.hideLoading(); }).catch(err => { console.log(err, 'err'); }) }, /** * 获得经纬度 */ getLocation() { console.log('getLocation'); const that = this; tt.getLocation({ type: "wgs84", // isHighAccuracy: true, success: function (res) { console.log(res, 'getLocation') that.setData({ latitude: res.latitude, longitude: res.longitude, isLocation: true, currentCity: res.city }) if (that.data.isOrder) { const merOrderId = that.data.merOrderId that.getCouponMerchant(merOrderId, true) that.getCityList(merOrderId) } else { that.getCouponChannelId(true) } }, // 拒绝提供定位权限 fail: error => { if (that.data.isOrder) { const merOrderId = that.data.merOrderId that.getCouponMerchant(merOrderId, false) that.getCityList(merOrderId) } else { that.getCouponChannelId(false) } that.setData({ isShop: false, isLocation: false, mallList: [] }) tt.hideLoading(); }, complete: finish => { const city = that.data.currentCity if (!city) { tt.showToast({ title: "定位失败!", icon: "fail" }); } } }) }, dealMallList() { const that = this const latitudeNow = that.data.latitude const longitudeNow = that.data.longitude const mallLocationArr = [] const mallList = that.data.mallList mallList.forEach((item, index) => { const obj = { latitude: item.latitude || false, longitude: item.longitude || false, id: item.id, index: index } mallLocationArr.push(obj) }) console.log(mallLocationArr, 'mallLocationArr'); const tempArr = mallList const bloobArr = [] mallLocationArr.forEach((item, index) => { const distanceData = that.getDistances(latitudeNow, longitudeNow, item.latitude, item.longitude) tempArr[index].distance_str = distanceData.distance_str tempArr[index].distanceName = tempArr[index].merchantName + ' ' + distanceData.distance_str tempArr[index].distance = distanceData.distance bloobArr.push(tempArr[index]) }) const finalSortMallList = that.bloob(bloobArr) console.log(finalSortMallList, 'finalSortMallList'); that.setData({ mallList: finalSortMallList }) }, /** * @description:根据经纬度计算距离 * @param {*} locationInfo (lat1, lng1, lat2, lng2) * @return: distanceObj: { distance , distance_str } */ getDistances(lat1, lng1, lat2, lng2) { if (lat2 || lng2) { function rad(num) { return num * Math.PI / 180.0; } var radLat1 = rad(lat1); var radLat2 = rad(lat2); var a = radLat1 - radLat2; var b = rad(lng1) - rad(lng2); 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; var distance = s; var distance_str = ""; if (parseInt(distance) >= 1) { distance_str = distance.toFixed(2) + "km"; } else if (!arseInt(distance)) { return false } else { distance_str = (distance * 1000).toFixed(2) + "m"; } let objData = { distance: distance, distance_str: distance_str } return objData } else { let objData = { distance: Infinity, distance_str: '' } return objData } }, bloob(arr) { const tempArr = arr for (let i = 0; i < tempArr.length - 1; i++) { for (let j = 0; j < tempArr.length - 1 - i; j++) { if (tempArr[j].distance > tempArr[j + 1].distance) { let temp = tempArr[j]; tempArr[j] = tempArr[j + 1]; tempArr[j + 1] = temp; } } } return tempArr }, selectCity() { this.setData({ isShop: false }) }, selectShop() { this.setData({ isShop: true }) }, indexBarMove(e) { }, tagBind(e) { const that = this const id = e.target.id that.setData({ currentTag: id, toView: id }) tt.vibrateShort() }, goSetShop(e) { if (e.target.id == "goMap") return const item = e.currentTarget.dataset.item console.log(item, 'item'); app.globalData.shopItem = item tt.navigateBack(); }, //跳转地图页面 goMap(e) { const item = e.currentTarget.dataset.item const latitude = Number(item.latitude) const longitude = Number(item.longitude) if (!latitude && !latitude) { tt.showToast({ title: '抱歉,该门店暂不支持!', }); return } tt.openLocation({ latitude, longitude, scale: 18, success() { console.log("打开地图成功"); }, fail(err) { console.log("打开地图失败:", err.errMsg); }, }); }, goFindShop(e) { const name = e.currentTarget.dataset.name this.setData({ currentCity: name, mallList: [], isShop: true }) tt.showLoading({ title: '加载中...', }); const isLocation = this.data.isLocation if (this.data.isOrder) { const merOrderId = this.data.merOrderId this.getCouponMerchant(merOrderId, isLocation) this.getCityList(merOrderId) } else { this.getCouponChannelId(isLocation) } }, })