//index.js //获取应用实例 const { request, serverName,imgServer } = require('../../utils/services'); const { Toast, qqmapsdk } = require('../../utils/util.js'); const { defaultImg, noExhibitionImg } = require('../../utils/images'); const WxParse = require('../../common/component/wxParse/wxParse.js'); const { newRequestFns, newServerName } = require('../../utils/newServices.js'); // const QQMapWX = require('../../common/component/mapSDK/qqmap-wx-jssdk.min.js'); const app = getApp(); const distItems = [ { name: '按距离排序', id: '距离排序', idx: 1 }, { name: '按热度排序', id: '热度排序', idx: 2 } ] const typeItems = [ { name: '展览中', id: '展览中', idx: 1 }, { name: '即将开始', id: '即将开始', idx: 2 }, { name: '已结束', id: '已结束', idx: 3 }, { name: '全部状态', id: '全部状态', idx: 0 } ] Page({ data: { navItem: distItems, indicatorDots: true, autoplay: true, interval: 5000, duration: 1000, activeIndex: 0, locationName: "北京", locationNameDesp:"", isLike: false, showConfirm: false, tag: "", exhibitionList: [], commodityImgs: [ '../../imgs/testImg/thumbSmallImg.jpg', '../../imgs/testImg/thumbSmallImg.jpg', ], type: 5, testImg: '../../imgs/testImg/fdkz.png', getLocationBtn: false, isShow: false, exhiNum:0, activeDist:'距离排序', activeType:'展览中' }, to_search_exhibition: function () { wx.navigateTo({ url: '../yuezhan/search/index', success: function (res) { }, fail: function (res) { }, complete: function (res) { }, }) }, onLoad: function () { this.getLocationName(); // this.getList(1) this.setData({ serverName, noExhibitionImg, defaultImg, imgServer }); }, onShareAppMessage: function () { }, onShow: function () { let { city, clickToSelect, collectedArr, collectedChange, latitude, longitude, locationName } = app.globalData; let { activeIndex, exhibitionList } = this.data if (clickToSelect) { // 使用 globalData 中的位置信息 let finalLocationName = locationName || city || "北京"; this.setData({ locationName: finalLocationName, exhibitionList: [], activeIndex: 0, type: 5 }); // 如果有完整的位置信息,也设置坐标 if (latitude && longitude) { this.setData({ latitude: latitude, longitude: longitude }); } // 确保 city 与 locationName 一致 if (finalLocationName && finalLocationName !== "北京") { app.globalData.city = finalLocationName; } else { app.globalData.city = "北京"; } if (activeIndex == 1) { this.getNearByList(1); } else { this.getList(1); } } if (collectedChange) { for (let i = 0; i < exhibitionList.length; i++) { for (let j = 0; j < collectedArr.length; j++) { if (collectedArr[j].collectedId == exhibitionList[i].id) { exhibitionList[i].hasCollect = collectedArr[j].status if (exhibitionList[i].hasCollect) { exhibitionList[i].collectionsCount += 1; } else { exhibitionList[i].collectionsCount -= 1; } if (exhibitionList[i].collectionsCount < 0) { exhibitionList[i].collectionsCount = 0 } } } } this.setData({ exhibitionList }) } app.globalData.clickToSelect = false; // app.globalData.collectedChange = false; }, addLike: function (e) { let { type, id, idx } = e.currentTarget.dataset; let exhibitionList = this.data.exhibitionList; let { collectedArr, collectedChange } = app.globalData, hasItem = true; Toast.showToast2('loading'); let loginSessionKey = wx.getStorageSync('token') || ""; // if (loginSessionKey){ newRequestFns['isCollect']({ loginSessionKey, exhibitionId: id, type: Number(type), }, "post", res => { if (res.data.code > -1) { for (let i = 0; i < collectedArr.length; i++) { if (collectedArr[i].collectedId && id == collectedArr[i].collectedId) { collectedArr[i] = { collectedId: id, status: res.data.data.hasCollect, } hasItem = false; } } if (hasItem) { collectedArr.push({ collectedId: id, status: res.data.data.hasCollect, }) } app.globalData.collectedArr = collectedArr; app.globalData.collectedChange = true; exhibitionList[idx].hasCollect = res.data.data.hasCollect this.setData({ exhibitionList: exhibitionList }) } }, err => { }, complete => { Toast.hideLoading(); }) // } }, showDrop: function (e) { let tag = e.target.dataset.id let name = e.target.dataset.test if (tag==='type'){ this.setData({ navItem: { type: tag, name: name, arr: typeItems }, isShow: !this.data.isShow }) } else{ this.setData({ navItem: { type: tag, name: name, arr: distItems }, isShow: !this.data.isShow }) } }, getLocationName: function () { // 检查 globalData 是否已有位置信息 // if (app.globalData.latitude && app.globalData.longitude && app.globalData.locationName) { // 使用 globalData 中的值 this.setData({ latitude: app.globalData.latitude, longitude: app.globalData.longitude, locationName: app.globalData.locationName, locationNameDesp: app.globalData.locationName }); app.globalData.city = app.globalData.locationName || '北京'; this.getList(1); return; // } // 没有完整位置信息,获取位置 // wx.getLocation({ // type: 'wgs84', // success: (res) => { // // 保存到 globalData // app.globalData.latitude = res.latitude; // app.globalData.longitude = res.longitude; // this.setData({ // latitude: res.latitude, // longitude: res.longitude // }) // //2、根据坐标获取当前位置名称,显示在顶部:腾讯地图逆地址解析 // qqmapsdk.reverseGeocoder({ // location: { // latitude: res.latitude, // longitude: res.longitude // }, // success: (res) => { // let { city: locationName } = res.result.address_component; // locationName = locationName.substring(0, 2); // // 保存到 globalData // app.globalData.city = locationName; // app.globalData.locationName = locationName; // this.setData({ // locationName, // locationNameDesp: locationName // }) // this.getList(1) // }, // fail: function (res) { // // 失败时设置默认值 // app.globalData.city = "北京"; // app.globalData.locationName = "北京"; // this.setData({ // locationName: "北京" // }) // }, // complete: function (res) { // } // }); // } // }) }, offlineRecommend: function () { // 检查 globalData 是否已有位置信息 if (app.globalData.latitude && app.globalData.longitude) { // 使用 globalData 中的经纬度 this.setData({ latitude: app.globalData.latitude, longitude: app.globalData.longitude }); // 处理 city 设置 if (app.globalData.locationName) { app.globalData.city = app.globalData.locationName; } else { app.globalData.city = "北京"; } } // else { // // 没有位置信息,获取位置 // wx.getLocation({ // type: 'wgs84', // success: (res) => { // // 保存到 globalData // app.globalData.latitude = res.latitude; // app.globalData.longitude = res.longitude; // this.setData({ // latitude: res.latitude, // longitude: res.longitude // }) // } // }) // } let { latitude: lat, longitude: lng } = this.data; newRequestFns["offlineRecommend"]({ lat, lng, }, "", res => { this.setData({ ExhibitionPopular: res.data.data }) }, err => { }, complete => { }) }, getList: function (page) { let { locationNameDesp, locationName, latitude, longitude, activeDist, activeType } = this.data; let loginSessionKey = wx.getStorageSync("token"); let sort = distItems.find(item => item.id === activeDist).idx let type = typeItems.find(item => item.id === activeType).idx this.setData({ loading: true }) newRequestFns["getExhibitionListOffline"]({ page: page, type: type, loginSessionKey, city: locationName, sort, type, lng: longitude, lat: latitude }, '', res => { let tempContent = this.data.exhibitionList ? this.data.exhibitionList : []; let { pageData: exhibitionList, total } = res.data.data; // 判断是否为最后一页(返回空数组表示没有更多数据) let isLastPage = !exhibitionList || exhibitionList.length === 0; if (isLastPage) { this.setData({ loading: false, lastPage: true, // 标记为最后一页 exhiNum: total || 0 }); wx.stopPullDownRefresh(); if (total <= 3) { this.offlineRecommend(); } return; } exhibitionList.forEach((currentValue) => { currentValue.product ? currentValue.product.link = escape(currentValue.product.link) : ''; }) // 拼接新数据 let newExhibitionList = tempContent.concat(exhibitionList); this.setData({ currentPage: page, lastPage: false, // 有数据时设为false loading: false, exhibitionList: newExhibitionList, exhiNum: total }); wx.stopPullDownRefresh(); // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that) if (total <= 3){ this.offlineRecommend(); } }, err => { }, complete => { }) }, getNearByList: function (page) { let { type, locationName, latitude, longitude } = this.data; let loginSessionKey = wx.getStorageSync("token"); console.log(locationName, latitude, longitude, 999) wx.getSetting({ success: res => { if (!res.authSetting['scope.userLocation']) { Toast.showToast('tip', "无法获取用户位置", () => { return; }) this.setData({ getLocationBtn: true }) } else { this.setData({ loading: true, getLocationBtn: false }) if (!longitude && !latitude) { // 检查 globalData 是否已有位置信息 if (app.globalData.latitude && app.globalData.longitude) { // 使用 globalData 中的经纬度 let longitude = app.globalData.longitude; let latitude = app.globalData.latitude; // 处理 city 设置 if (app.globalData.locationName) { app.globalData.city = app.globalData.locationName; } else { app.globalData.city = "北京"; } this.setData({ longitude, latitude }); newRequestFns["getNearByList"]({ page: page, loginSessionKey, city: locationName, lng: longitude, lat: latitude }, '', res => { let tempContent = this.data.exhibitionList ? this.data.exhibitionList : []; let { pageData: exhibitionList, total } = res.data.data; // 判断是否为最后一页(返回空数组表示没有更多数据) let isLastPage = !exhibitionList || exhibitionList.length === 0; if (isLastPage) { this.setData({ loading: false, lastPage: true }); wx.stopPullDownRefresh(); return; } // 拼接新数据 let newExhibitionList = tempContent.concat(exhibitionList); this.setData({ currentPage: page, lastPage: false, // 有数据时设为false loading: false, exhibitionList: newExhibitionList, }); wx.stopPullDownRefresh(); // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that) }, err => { }, complete => { }) } else { // 没有位置信息,获取位置 // wx.getLocation({ // success: res => { // // 保存到 globalData // app.globalData.latitude = res.latitude; // app.globalData.longitude = res.longitude; // let longitude = (this.longitude = res.longitude); // let latitude = (this.latitude = res.latitude); // newRequestFns["getNearByList"]({ // page: page, // loginSessionKey, // city: locationName, // lng: longitude, // lat: latitude // }, '', res => { // let tempContent = this.data.exhibitionList // ? this.data.exhibitionList // : []; // let { pageData: exhibitionList, total } = res.data.data; // // 判断是否为最后一页(返回空数组表示没有更多数据) // let isLastPage = !exhibitionList || exhibitionList.length === 0; // if (isLastPage) { // this.setData({ // loading: false, // lastPage: true // }); // wx.stopPullDownRefresh(); // return; // } // // 拼接新数据 // let newExhibitionList = tempContent.concat(exhibitionList); // this.setData({ // currentPage: page, // lastPage: false, // 有数据时设为false // loading: false, // longitude, // latitude, // exhibitionList: newExhibitionList, // }); // wx.stopPullDownRefresh(); // // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that) // }, err => { // }, complete => { // }) // }, // }) } } else { newRequestFns["getNearByList"]({ page: page, loginSessionKey, city: locationName, lng: longitude, lat: latitude }, '', res => { let tempContent = this.data.exhibitionList ? this.data.exhibitionList : []; let { pageData: exhibitionList, total } = res.data.data; // 判断是否为最后一页(返回空数组表示没有更多数据) let isLastPage = !exhibitionList || exhibitionList.length === 0; if (isLastPage) { this.setData({ loading: false, lastPage: true }); wx.stopPullDownRefresh(); return; } // 拼接新数据 let newExhibitionList = tempContent.concat(exhibitionList); this.setData({ currentPage: page, lastPage: false, // 有数据时设为false loading: false, exhibitionList: newExhibitionList, }); wx.stopPullDownRefresh(); }, err => { }, complete => { }) } } } }) }, closeDialog: function () { this.setData({ showConfirm: false }) }, loadMore: function () { let { activeIndex } = this.data if (!this.data.lastPage) { if (activeIndex == 1) { this.getNearByList(this.data.currentPage + 1); } else { this.getList(this.data.currentPage + 1); } } else { return; } }, onReachBottom: function () { if (!this.data.loading) { this.loadMore(); } }, tabClick: function (e) { console.log(111111) this.setData({ exhibitionList: [], activeIndex: e.currentTarget.id, type: e.currentTarget.dataset.tag, currentPage: 1 }); if (e.currentTarget.id == 1) { this.getNearByList(1); } else { this.getList(1); } }, onIsclose: function (e) { this.setData({ isShow: e.detail }) }, getActiveItem:function(e){ let { activeDist, activeType} = e.detail this.setData({ activeDist, activeType, exhibitionList:[] }) this.getList(1) }, to_search: function () { // 检查 globalData 是否已有位置信息 if (app.globalData.latitude && app.globalData.longitude && app.globalData.locationName) { // 已有位置信息,直接跳转 wx.navigateTo({ url: './search/index', success: function (res) { }, fail: function (res) { }, complete: function (res) { }, }) return; } // // 没有位置信息,显示弹窗 // wx.showModal({ // title: '获取你的位置信息', // content: '将获取你的具体位置信息,用于推荐你所在定位附近的展览', // confirmText: '同意', // cancelText: '拒绝', // success: (res) => { // if (res.confirm) { // // 用户点击同意,获取位置信息(使用 getLocationName 的逻辑) // wx.getLocation({ // type: 'wgs84', // success: (locationRes) => { // // 设置坐标到 globalData 和页面数据 // app.globalData.latitude = locationRes.latitude; // app.globalData.longitude = locationRes.longitude; // this.setData({ // latitude: locationRes.latitude, // longitude: locationRes.longitude // }); // // 使用腾讯地图逆地址解析获取城市名称 // qqmapsdk.reverseGeocoder({ // location: { // latitude: locationRes.latitude, // longitude: locationRes.longitude // }, // success: (geoRes) => { // let { city: locationName } = geoRes.result.address_component; // locationName = locationName.substring(0, 2); // // 保存到 globalData // app.globalData.city = locationName; // app.globalData.locationName = locationName; // this.setData({ // locationName, // locationNameDesp: locationName // }); // // 获取位置成功,跳转到搜索页面 // wx.navigateTo({ // url: './search/index', // success: function (res) { }, // fail: function (res) { }, // complete: function (res) { }, // }) // }, // fail: (geoErr) => { // // 逆地址解析失败,使用默认城市(北京),直接跳转 // app.globalData.city = "北京"; // app.globalData.locationName = "北京"; // this.setData({ // locationName: "北京" // }); // wx.navigateTo({ // url: './search/index', // success: function (res) { }, // fail: function (res) { }, // complete: function (res) { }, // }) // } // }); // }, // fail: (locationErr) => { // // 获取位置失败,保持默认城市(北京),直接跳转 // console.log('获取位置失败:', locationErr); // app.globalData.city = "北京"; // app.globalData.locationName = "北京"; // this.setData({ // getLocationBtn: true, // locationName: "北京" // }); // wx.navigateTo({ // url: './search/index', // success: function (res) { }, // fail: function (res) { }, // complete: function (res) { }, // }) // } // }) // } else if (res.cancel) { // // 用户点击拒绝,保持默认城市(北京),直接跳转 // app.globalData.city = "北京"; // app.globalData.locationName = "北京"; // wx.navigateTo({ // url: './search/index', // success: function (res) { }, // fail: function (res) { }, // complete: function (res) { }, // }) // } // }, // fail: function (res) { // // 弹窗失败,直接跳转 // wx.navigateTo({ // url: './search/index', // success: function (res) { }, // fail: function (res) { }, // complete: function (res) { }, // }) // } // }) } })