searchcity.js 3.0 KB
// pages/searchcity/searchcity.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityarr: ['东京', '东京', '东京', '东京'],
    index: 0,
    countryid: '',
    cityarr: [],
    hotcityarr: [],
    currentcity: '',
    cityid: '',
    current: false,
    keyword:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.setData({
      countryid: options.countryid,
      currentcity: options.cityname,
      cityid: options.cityid
    })
    this.getcity();
    this.gethotcity()

  },
  // 输入城市
  entercity(e){
    this.setData({
      keyword:e.detail.value
    })
  },

  search(){
    this.getcity()
  },


  // 获取全部城市
  getcity() {
    let that = this;
    var url = 'sundry/get_city';
    var params = {
      country_id: that.data.countryid,
      keyword:that.data.keyword

    }
    app.post(url, params).then((res) => {
      console.log(res);
      that.setData({
        cityarr: res
      })



    }).catch((err) => {

    })
  },
  // 获取当前城市
  gethotcity() {
    let that = this;
    var url = 'sundry/get_city';
    var params = {
      country_id: that.data.countryid,
      is_hot: 1
    }
    app.post(url, params).then((res) => {
      console.log(res);
      that.setData({
        hotcityarr: res
      })



    }).catch((err) => {

    })
  },

  selhotcity(e) {
    console.log(e)
    wx.setStorageSync('num', "")
    let id = e.currentTarget.dataset.id;
    console.log(id)
    let cityname = e.currentTarget.dataset.cityname;
    app.globalData.cityname=cityname;
    app.globalData.cityid = id
    this.setData({
      sel: e.currentTarget.dataset.index,
    })
     this.changecity(id);
    wx.navigateBack({
      checked:true
    })
  },

  // 当前城市切换
  selectcurrent(){
    wx.setStorageSync('num', "")
    let id=this.data.cityid;
    this.changecity(id);
    app.globalData.cityname = this.data.currentcity;
    app.globalData.cityid = id
    this.setData({
      current:true
    })
    wx.navigateBack({
      checked:true
    })
  },

  // 切换默认城市
  changecity(id) {
    console.log(id)
    let that = this;
    var url = 'sundry/edit_user_city';
    var params = {
      country_id: that.data.countryid,
      city_id: id
    }
    app.post(url, params).then((res) => {
      console.log(res);



    }).catch((err) => {

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})