type-detail.js 9.6 KB
// pages/index/type-detail/type-detail.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        is_showSort: false,
        is_showSelect: false,
        currentSort: null,
        currentSelect: 0,
        start_time_date: '',//app.nowDate(),
        end_time_date: '',//app.nowDate(),
        cate: [],
        score: 1,//(1智能排序,2最具人气,3距离最近)
        sort: [{id: 2, name: '最具人气'}, {id: 3, name: '距离最近'}],//(1智能排序,2最具人气,3距离最近)
        select_id: '',//分类id
        is_all: true,
        list: [],
        page_number:1,
        hasMore:true,
        cityList: [],
        addr:'', //切换地址
    },
    //打开城市列表页面
    chooseCity() {
        // this.setData({is_show_city:!this.data.is_show_city})
        wx.navigateTo({url:'/pages/index/city-list/city-list'})
    },
    //获取城市列表
    // getCityList() {
    //     const self = this;
    //     let url = '/portal/Index/cityList';
    //     app.post(url, {}, {}).then((res) => {
    //         console.log('获取城市列表', res);
    //         if (+res.code === 1) {
    //             const arr = [];
    //             res.data.list.forEach((item) => {
    //                 arr.push(item.title)
    //             });
    //             self.setData({
    //                 city_picker_list: arr,
    //                 cityList:res.data.list,
    //             });
    //         }
    //     })
    // },
    //切换城市
    // cityPickerChange(e) {
    //     this.setData({
    //         current_city: e.detail.value,
    //         is_city_change: true
    //     });
    //     const lat = this.data.cityList[e.detail.value].latng.split(',')[1];
    //     const lng = this.data.cityList[e.detail.value].latng.split(',')[0];
    //     this.setData({latitude: lat, longitude: lng,addr:this.data.cityList[e.detail.value].title});
    //     console.log('lat', 'lng',lat,lng);
    //     this.getTypeList();
    // },
    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,
            })
        }
    },
    //搜索
    search() {
        wx.navigateTo({
            url: '/pages/search/search',
        })
    },
    //点击智能排序按钮
    clickSort() {
        let that = this;
        that.setData({
            // down: !that.data.down,
            is_showSort: !that.data.is_showSort,
            is_showSelect: false
            // showrent_mask: false
        })
    },
    //点击筛选按钮
    clickSelect() {
        let that = this;
        that.setData({
            // down: !that.data.down,
            is_showSelect: !that.data.is_showSelect,
            is_showSort: false,
            // showrent_mask: false
        })
    },
    //点击蒙层关闭排序和筛选框
    closeMask() {
        let that = this;
        that.setData({
            // showrent_mask: false,
            is_showSort: false,
            is_showSelect: false,
        });
        wx.showLoading({
            title: '加载中',
        });
        that.initList();
        that.getTypeList();
    },
    //保持筛选列表开启
    openMask() {
        this.setData({is_showSelect: true,})
    },
    //点击最具人气/距离最近
    chooseSort(e) {
        const current = e.currentTarget.dataset.index;
        this.setData({
            // sort_id: this.data.sort[current].id,
            currentSort: current,
            is_showSort: false,
            score: this.data.sort[current].id,//参数
            // down: !this.data.down,
        });
        // this.initPortalPost();
        wx.showLoading({
            title: '加载中',
        });
        this.initList();
        this.getTypeList()
    },
    //选择开始时间
    changeStartTime: function (e) {
        console.log('picker发送选择改变,携带值为', e.detail.value)
        this.setData({
            start_time_date: e.detail.value,
            is_showSelect: true,
            // is_birth_change: true,
        })
    },
    //选择结束时间
    changeEndTime: function (e) {
        console.log('picker发送选择改变,携带值为', e.detail.value)
        this.setData({
            end_time_date: e.detail.value,
            is_showSelect: true,
            // is_birth_change: true,
        })
    },
    //选择全部分类
    chooseAll() {
        this.setData({is_all: true, is_showSelect: false, current_type: null,select_id:'',});
        this.initList();
        this.getTypeList();
    },
    //选择分类
    chooseType(e) {
        const current = e.currentTarget.dataset.index;
        const id = e.currentTarget.dataset.id;
        console.log('select_id', id);
        this.setData({current_type: current, is_all: false, is_showSelect: false, is_showSort: false,select_id:id});
        wx.showLoading({
          title: '加载中',
        });
        this.initList();
        this.getTypeList();
    },
    initList() {
        this.setData({
            list: [],
            page_number:1,
            hasMore:true,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('options', options);
        this.setData({select_id:+options.id?+options.id:''});
        wx.setNavigationBarTitle({title:options.name})
        if(options.city !== 'null') {
            this.setData({city_params:JSON.parse(options.city)});
            console.log('load里的调用');
            // this.getTypeList();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    //获取首页分类列表
    getTypeList() {
        const self = this;
        console.log('city',self.data.city);
        let url = '/portal/Index/cate';
        let params = {
            lat: wx.getStorageSync('lat') !== ''?self.data.lat:self.data.latitude,
            lng: wx.getStorageSync('lng') !== ''?self.data.lng:self.data.longitude,
            addr: wx.getStorageSync('addr') !== ''?self.data.addr:self.data.addr,
            keyword: '',  //	浮点型	非必填	-	-	搜索关键字
            score: self.data.score,    //	浮点型	非必填	-	-	排序(1智能排序,2最具人气,3距离最近)
            startTime: self.data.start_time_date,//	浮点型	非必填	-	-	筛选开始时间
            endTime: self.data.end_time_date,  //	浮点型	非必填	-	-	筛选结束时间
            type: self.data.select_id,     //	浮点型	非必填	-	-	筛选类型
        };
        let header = {
            "token": wx.getStorageSync('token')
        };
        app.post(url, params, {}).then((res) => {
            console.log('获取分类列表', res);
            if (+res.code === 1) {
                self.setData({
                    list: res.data.active,
                    cate: res.data.cate,
                    start_time_date:'', //清空开始时间
                    end_time_date:'', //清空结束时间
                });
            }
            wx.hideLoading()
        })

    },
    getCity() {
        const self = this;
        let url = '/portal/Index/findCity';
        let params = {
            lat: self.data.latitude,
            lng: self.data.longitude,
        };
        let header = {
            "token": wx.getStorageSync('token')
        };
        app.post(url, params, {}).then((res) => {
            console.log('获取城市', res);
            if (+res.code === 1) {
                self.setData({
                    addr: res.data.city.city,
                });
                self.getTypeList();
            }
        })
    },
    //获取地理位置
    getLocation() {
        const self = this;
        if(wx.getStorageSync('lat') === '' &&
            wx.getStorageSync('lng') === '' &&
            wx.getStorageSync('addr') === '') {
            wx.getLocation({
                type: 'wgs84',
                altitude: 'true',
                success(res) {
                    console.log('获取地理位', res);
                    self.setData({latitude: res.latitude, longitude: res.longitude});
                    self.getCity();
                }
            })
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const self = this;
        self.getLocation();
        if(wx.getStorageSync('lat') !== '' &&
            wx.getStorageSync('lng') !== '' &&
            wx.getStorageSync('addr') !== '') {
            console.log('已选择城市',wx.getStorageSync('lat'),wx.getStorageSync('lng'),wx.getStorageSync('addr'));
            self.setData({
                is_city_change:true,
                lat:wx.getStorageSync('lat'),
                lng: wx.getStorageSync('lng'),
                addr:wx.getStorageSync('addr'),
            });
            self.getTypeList();
        }
        // self.getCityList();
    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

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

    }
})