// pages/home/pages/location/index.js var ampFile = require("../../../utils/amap-wx.js"); var app = getApp(); var util = require("../../../utils/util.js") Page({ /** * 页面的初始数据 */ data: { city: [],//热门城市 currentCity: '武汉市',//当前城市 search: '', list:[],// city list }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { let _this = this; }, onReady: function () { this.loadData(); }, //选择城市 bindtap(e) { let cityName = e.currentTarget.dataset.detail.name ? e.currentTarget.dataset.detail.name : '' let cityId = e.currentTarget.dataset.id // console.log(e.target.dataset.detail.name) // console.log(e) wx.reLaunch({ url: `/pages/home/index/index?cityName=${cityName}&cityId=${cityId}`, // url: '/pages/home/index/index?id=' + e.currentTarget.dataset.id, }) }, loadData: function () { app._post_form('two/citylist', '', null, (res)=> { // console.log(res) if(res && res.msg === 'success') { let citys = res.city, hotCitys = res.hot; // fommat city data this.handleFormatCityData({array: hotCitys, type: 'hot'}) this.handleFormatCityData({ array: citys, type: 'normal' }) } }) }, /** * format city data * @params array [array] 城市数组 * @params type [string] 城市类型(热门 | 普通) */ handleFormatCityData({array,type}) { if(!array.length) return switch(type) { case 'hot': let arrayItem = { title: '热门城市', type, item: [] }, subItems = []; array.forEach(item=>{ let subItem = {}; subItem.key = '热门' subItem.name = item.name subItem.id=item.id subItems.push(subItem) }) arrayItem.item = subItems // console.log(arrayItem) this.setData({ list: [arrayItem] }) break; default: let _array = []; array.forEach(item => { let _arrayItem = {} _arrayItem.title = item.letter let arrayItem = [] try { item.city.forEach(ite=>{ let subItem = {}; subItem.key = item.letter subItem.name = ite.name subItem.id=ite.id arrayItem.push(subItem) }) _arrayItem.item = arrayItem // console.log(_arrayItem) } catch(e) { // console.log(e) } _array.push(_arrayItem) }) this.setData({ list: [...this.data.list, ..._array] }) break; } }, //重新定位 relocation() { } })