search.js 7.9 KB
// pages/service/renting/search/search.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchValue: '',
        postList: [],
        searchHistory:[],
        currentModal: 0,
        currentPost: 0,
        hidden_img: false,
        hasMore: true,
        pageNumber: 0,
        avatar: [],
        is_write:2,//1:已填写,2:未填写
    },
    goPostDetail(e) {
        const id = +e.currentTarget.dataset.id;
        const type = +e.currentTarget.dataset.type;
        console.log('type', type);
        if(type === 1) { //1是活动,2是拼餐
            wx.navigateTo({
                url: '/pages/activity/detail/detail?id=' + id,
            })
        }else {
            wx.navigateTo({
                url: '/pages/meal/detail/detail?id=' + id,
            })
        }
    },
    inputFocus() {
        this.setData({postList:[]})
    },
    //输入搜索内容
    inputKey(e) {
        this.setData({searchValue: e.detail.value})
    },
    //点击搜索(搜索结果)
    // startSearch() {
    //     let self = this;
    //     if(!self.data.hasMore) return;
    //     self.setData({hasMore: false});
    //     let url = '/home/home/search';
    //     let params = {
    //         keyword: self.data.searchValue,
    //         page: self.data.pageNumber,
    //     };
    //     let header = {
    //         "XX-Token": wx.getStorageSync('token')
    //     };
    //     if(self.data.searchValue === '') {
    //         wx.showToast({title:'搜索内容不能为空~',icon: 'none'})
    //     }else {
    //         self.getSearchHistory();
    //         app.post(url, params, header).then((res) => {
    //             // console.log('搜索结果', res);
    //             if(+res.page < res.page_size) {
    //                 self.data.hasMore = true;
    //             }
    //             const list = res.data.map((item, index) => {
    //                 return {...item, showlabel_box: false, more:  item.more !== '' ? JSON.parse(item.more) : ''}
    //             });
    //             self.setData({
    //                 postList: self.data.postList.concat(list)
    //             });
    //             // console.log('搜索结果', self.data.postList);
    //         })
    //     }
    // },
    //点击量
    // hit(current) {
    //     let url = '/home/home/hits';
    //     let params = {
    //         id: this.data.postList[current].id,
    //     };
    //     let header = { "XX-Token": wx.getStorageSync('token') };
    //     app.post(url, params, header).then((res) => {
    //         // console.log('点击量',res);
    //         if(res.message == "操作成功") {
    //             const post_hits = this.data.postList[current].post_hits +1
    //             this.setData({
    //                 [`postList[${current}].post_hits`]: post_hits
    //             });
    //         }
    //     })
    // },
    //清空输入框
    clearInput() {
        this.setData({searchValue: ''})
    },
    //搜索结果
    startSearch() {
        let self = this;
        let url = '/portal/Active/findRes';
        let header = {
            "XX-Token": wx.getStorageSync('token')
        };
        let params = {
            token: wx.getStorageSync('token'),
            keyword:self.data.searchValue,
        };
        app.post(url, params, header).then((res) => {
            console.log('搜索历史列表', res);
            wx.hideLoading();
            if(+res.code === 1) {
                if(res.data.list.length === 0) {
                    wx.showToast({title:'暂无更多内容',icon:'none'})
                }else {
                    self.setData({
                        postList: res.data.list
                    });
                }
            }

        })
    },
    //点击热门搜索
    clickHot(e) {
        const current = e.currentTarget.dataset.index;
        this.setData({
            searchValue: this.data.hot[current].title,
        });
        wx.showLoading({title: '加载中',});
        this.startSearch();
        this.getSearchHistory()
    },
    //点击搜索历史
    clickHistory(e) {
        const current = e.currentTarget.dataset.index;
        this.setData({
            searchValue: this.data.history[current].title,
        });
        wx.showLoading({title: '加载中',});
        this.startSearch();
        this.getSearchHistory()
    },
    //清空历史搜索
    clearHistory() {
        let self = this;
        let url = '/portal/Active/delHistory';
        let params = {
            token: wx.getStorageSync('token'),
        };
        app.post(url, params, {}).then((res) => {
            console.log('清空搜索历史列表', res);
            wx.showLoading({title: '加载中',});
            if(+res.code === 1) {
                wx.showToast({title:res.message,icon:'none'})
            }
            self.getSearchHistory()
        })
    },

    // //查看文章详情
    // goPostDetail(e) {
    //     const current = e.currentTarget.dataset.index;
    //     const list = this.data.postList.map((item,index) => {
    //         return {...item, showlabel_box: false}
    //     });//进入详情时关闭所有弹框
    //     this.setData({
    //         postList: list,
    //         currentPost: current
    //     });
    //     this.hit(current);
    //     wx.navigateTo({
    //         url: '../../rentingDetail/rentingDetail?post_id=' + this.data.postList[current].id,
    //     })

    // },
    //获取热门搜索和搜索历史
    getSearchHistory() {
        const self = this;
        let url = '/portal/Active/find';
        let params = {
            // activeId: self.data.detail_id,
            token: wx.getStorageSync('token'),
        };
        // let header = {
        //     "token": wx.getStorageSync('token')
        // };
        app.post(url, params, {}).then((res) => {
            console.log('获取热门搜索和搜索历史', res);
            if (+res.code === 1) {
                wx.hideLoading();
                self.setData({
                    hot: res.data.hot,
                    history:res.data.history
                    // end_time: res.data.active.time,
                    // over_hours:res.data.active.over_time,//几小时截止
                });
                // self.countDown();
            }
            // wx.hideLoading()
        })

    },
    onLoad: function (options) {
        const self = this;
        self.setData({
            id: +options.id,
            order: +options.order,
            select_id: +options.select_id,
        });
        self.getSearchHistory();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // wx.showLoading({title: '加载中',});
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        const self = this;
        if (!self.data.hasMore) {
            wx.showToast({
                title: '没有更多数据了~',
                icon: 'none'
            });
            console.log('reach bottom', self.data.hasMore);
        } else {
            console.log('reach bottom', self.data.hasMore);
            self.data.pageNumber++;
            self.startSearch();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})