123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- // pages/yuezhan/search/index.js
- const { request, serverName, imgServer } = require('../../../utils/services');
- const {
- newRequestFns,
- newServerName
- } = require('../../../utils/newServices.js');
- const { Toast } = require('../../../utils/util');
- const { defaultImg, noExhibitionImg, noExhibitionHallImg } = require('../../../utils/images');
- const app = getApp();
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- tabs: ['展览', '展馆'],
- testImg: '../../../imgs/testImg/thumbSmallImg2.jpg',
- avatar: '../../../imgs/testImg/1.jpg',
- activeIndex:0,
- animationData: "",
- isLike:false,
- loading:false,
- currentPage:1,
- exhibitionList:[],
- pavilionList:[],
- totalElements:''
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- this.setData({
- searchText: options.searchText,
- serverName,
- imgServer
- });
-
- // 检查 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 = "北京";
- }
-
- this.loadData();
- }
- // this.getKeywordCode()
-
- this.setData({
- noExhibitionImg,
- noExhibitionHallImg,
- defaultImg
- })
- },
- keywordSearch:function(e){
- let { keyword } = e.currentTarget.dataset;
- this.setData({
- searchText: keyword
- })
- console.log("s",e)
- this.loadData();
- },
- getKeywordCode:function(){
- newRequestFns["getKeywordCode"]({}, "", res => {
- if (res.data.code > -1) {
- let {data:keyword} = res.data;
- this.setData({
- keyword
- })
- wx.stopPullDownRefresh();
- console.log(res)
- }
- }, err => {
- }, complete => {
- })
- },
- loadData: function () {
- // 通过activeIndex 判断应该load哪部分的数据
- const { activeIndex } = this.data;
- this.setData({
- exhibitionList: [],
- pavilionList: []
- });
- if (activeIndex == 0) {
- this.getExhibitionSearch(1);
- if (this.data.exhibitionList.length < 5){
- this.popular();
- }
- }
- else if (activeIndex == 1) {
- this.getPavilionSearch(1);
- }
- else {
- return
- }
- },
- addLike: function (e) {
- let { type, id, idx } = e.currentTarget.dataset;
- console.log(idx, type, id)
- let { collectedArr, collectedChange } = app.globalData, hasItem = true;
- let exhibitionList = this.data.exhibitionList;
- console.log('type', type)
- 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) {
- console.log("true")
- 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();
- })
- // }
- console.log(this.data.exhibitionList)
- },
- getExhibitionSearch:function(page){
- let name = this.data.searchText||"";
- this.setData({
- loading: true
- })
- let { latitude: lat, longitude: lng } = this.data;
- newRequestFns["getExhibitionSearch"]({
- name,
- lat,
- lng,
- page
- },"",res=>{
- if(res.data.code>-1){
- 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, // 标记为最后一页
- totalElements: total || 0
- });
- wx.stopPullDownRefresh();
- return;
- }
- exhibitionList.forEach((currentValue) => {
- currentValue.distance = Math.ceil(currentValue.distance);
- currentValue.product ? currentValue.product.link = escape(currentValue.product.link) : '';
- })
- // 拼接新数据
- let newExhibitionList = tempContent.concat(exhibitionList);
- // 去重
- newExhibitionList = newExhibitionList.reduce((acc, curr) => {
- if (!acc.some(item => item.id === curr.id)) {
- acc.push(curr);
- }
- return acc;
- }, []);
- this.setData({
- currentPage: page,
- lastPage: false, // 有数据时设为false
- loading: false,
- exhibitionList: newExhibitionList,
- totalElements: total
- });
- console.log(exhibitionList)
- wx.stopPullDownRefresh();
- console.log(res)
- }
- },err=>{
-
- }, complete => {
- })
- },
- getPavilionSearch: function (page) {
- let name = this.data.searchText || "";
- this.setData({
- loading: true
- })
- let { latitude: lat, longitude: lng } = this.data;
- newRequestFns["getPavilionSearch"]({
- name,
- lat,
- lng,
- page
- }, "", res => {
- if (res.data.code > -1) {
- let tempContent = this.data.pavilionList
- ? this.data.pavilionList
- : [];
- let { pageData: pavilionList, total } = res.data.data;
- // 判断是否为最后一页(返回空数组表示没有更多数据)
- let isLastPage = !pavilionList || pavilionList.length === 0;
- if (isLastPage) {
- this.setData({
- loading: false,
- lastPage: true
- });
- wx.stopPullDownRefresh();
- return;
- }
- // 拼接新数据
- let newPavilionList = tempContent.concat(pavilionList);
- // 去重
- newPavilionList = newPavilionList.reduce((acc, curr) => {
- if (!acc.some(item => item.id === curr.id)) {
- acc.push(curr);
- }
- return acc;
- }, []);
- this.setData({
- currentPage: page,
- lastPage: false, // 有数据时设为false
- loading: false,
- pavilionList: newPavilionList,
- });
- wx.stopPullDownRefresh();
- console.log(res)
- }
- }, err => {
- }, complete => {
- })
- },
- searchByText: function () {
- this.loadData()
- },
- bindInput: function (event) {
- var obj = {},
- key = event.target.dataset['key'];
- // Toast.showToast('success', event.detail.value);
- obj[key] = event.detail.value;
- this.setData(obj);
- console.log(this.data.searchText)
- },
- loadMore: function () {
- const { activeIndex } = this.data;
- // const loadDataFn =
- // activeIndex == 0 ? this.loadOneToOrderData : this.loadClassroomData;
- let loadDataFn;
- if ( activeIndex == 0) {
- loadDataFn = this.getExhibitionSearch;
- }
- else if ( activeIndex == 1) {
- loadDataFn = this.getPavilionSearch;
- }
- else {
- return
- }
- if (!this.data.lastPage) {
- this.setData({
- loading: true
- });
- console.log(this.data.currentPage + 1)
- loadDataFn(this.data.currentPage + 1);
- } else {
- return;
- }
- },
- popular : function(){
- let { latitude: lat, longitude: lng } = this.data;
- newRequestFns["recommend"]({
- lat,
- lng,
- }, "", res => {
- this.setData({
- ExhibitionPopular: res.data.data
- })
- }, err => {
- }, complete => {
- })
- },
- onReachBottom: function () {
- if (!this.data.loading) {
- this.loadMore();
- console.log('reach Bottom');
- }
- },
- tabClick: function (e) {
- let { activeIndex } = this.data;
- this.setData({
- loading: true,
- activeIndex: e.currentTarget.id
- })
- this.loadData();
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
-
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- let { collectedArr, collectedChange } = app.globalData;
- let { exhibitionList } = this.data
- let isLogin = app.globalData.isLogin;
- // this.setData({
- // exhibitionList: []
- // });
- // this.getBanner();
- // this.getList(1);
- this.setData({
- isLogin
- })
- 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;
- }
- }
- }
- // console.log(collectedArr.status, collectedArr.collectedId)
- }
- this.setData({
- exhibitionList,
- })
- }
- // app.globalData.clickToSelect = false;
- // app.globalData.collectedChange = false;
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
-
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
-
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- this.loadData();
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
-
- }
- })
|