city-list.js 4.7 KB
// pages/index/city-list/city-list.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 0,
        list:[],
        status:0,
        searchValue: '',
        searchHistory:[],

    },
    inputFocus() {
        // this.setData({postList:[]})
    },
    //输入搜索内容
    inputKey(e) {
        this.setData({searchValue: e.detail.value})
    },
    //搜索结果
    startSearch() {
        let self = this;
        let url = '/portal/Index/addrFind';
        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.length === 0) {
                    wx.showToast({title:'暂无更多内容',icon:'none'})
                }else {
                    const arr = [];
                    res.data.forEach((item)=> {
                        arr.push(item.title)
                    });
                    self.setData({
                        city_picker_list: arr,
                        cityList:res.data,
                    });
                }
            }

        })
    },
    //tab点击事件
    chargeTab(e) {
        const current = +e.currentTarget.dataset.current;
        this.setData({
            current: current,
        });
        if(current === 0) {
            this.getCityList();
        }else if(current === 1) {
            console.log(1);
            this.getAbroadList()
        }
        // this.getList();
    },
    //获取国内城市列表
    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) => {
                    if(item.type !== 5) {
                        arr.push(item.title)
                    }
                });
                // console.log('arr---cityList', arr,self.data.cityList);
                self.setData({
                    city_picker_list: arr,
                    cityList:res.data.list,
                });
            }
        })
    },
    //获取国外列表
    getAbroadList() {
        const self = this;
        let url = '/portal/Index/addr';
        let params = {
            token: wx.getStorageSync('token'),
        };
        app.post(url, params, {}).then((res) => {
            // console.log('获取国外列表', res);
            if (+res.code === 1) {
                const arr = [];
                res.data.forEach((item) => {
                    arr.push(item.title)
                });
                self.setData({
                    city_picker_list: arr,
                    cityList:res.data,
                });
            }
        })
    },
    //选择城市
    chooseCity(e) {
        wx.showLoading({title:'加载中',mask:true});
        const current = e.currentTarget.dataset.index;
        const lat = this.data.cityList[current].latng.split(',')[1];
        const lng = this.data.cityList[current].latng.split(',')[0];
        const addr = this.data.cityList[current].title;
        // console.log('addr', addr);
        this.setData({current_city:current});
        wx.setStorageSync('lat',lat);
        wx.setStorageSync('lng',lng);
        wx.setStorageSync('addr',addr);
        setTimeout(function () {
            wx.hideLoading();
            wx.navigateBack({delta:1})
        }, 1000)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCityList();
    },
    onPageScroll: function (res) {
        // console.log(res);
        // this.setData({scroll_top:res.scrollTop})
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})