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

    /**
     * 页面的初始数据
     */
    data: {
        current: 0,
        list:[],
        status:0,
    },
    //tab点击事件
    chargeTab(e) {
        const current = +e.currentTarget.dataset.current;
        this.setData({
            current: current,
        });
        if(current === 0) {
            this.getCityList()
        }else if(current === 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) => {
                    arr.push(item.title)
                });
                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,
                });
            }
        })
    },
    //选择国内城市
    chooseDomesticCity(e) {
        const current = e.currentTarget.dataset.index;
        wx.showLoading({title:'加载中',mask:true});
        this.setData({is_show_city:false});
        const lat = this.data.cityList[current].latng.split(',')[1];
        const lng = this.data.cityList[current].latng.split(',')[0];
        this.setData({lat: lat, lng: lng,addr:this.data.cityList[current].title});
        // console.log('lat', 'lng',lat,lng);
        // this.getIndex();
    },
    //选择国外城市
    chooseAbroadCity(e) {
        const current = e.currentTarget.dataset.index;
        wx.showLoading({title:'加载中',mask:true});
        this.setData({
            // current_city: e.detail.value,
            is_city_change: true,
            is_show_city:false
        });
        const lat = this.data.AbroadCityList[current].latng.split(',')[1];
        const lng = this.data.AbroadCityList[current].latng.split(',')[0];
        this.setData({lat: lat, lng: lng,addr:this.data.AbroadCityList[current].title});
        console.log('lat', 'lng',lat,lng);
        // this.getIndex();
    },
    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();
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})