// 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= originalList: [], // 原始数据 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { let _this = this; }, onReady: function () { this.loadData(); }, // 基础搜索功能 searchMt(e) { // this.search = e.detail.value; // console.log(e.detail.value); if (e.detail.value) { this.setData({ search: e.detail.value }) this.handleList(); } else { this.setData({ search: '', list: this.data.originalList }); } // console.log(this.data.originalList); }, handleList() { let list = []; this.data.originalList.forEach(item => { // if (item.name.indexOf(this.data.search) > -1) { // list.push(item); // } if (item.title == '热门城市') { list.push(item); } else { let itemList = []; item.item.forEach(v => { if (v.name.indexOf(this.data.search) > -1) { itemList.push(v); } }); if (itemList.length) { list.push({ title: item.title, item: itemList }); } } }); // console.log(list); this.setData({ list }); }, //选择城市 bindtap(e) { let cityName = e.currentTarget.dataset.detail.name ? e.currentTarget.dataset.detail.name : '' let cityId = e.currentTarget.dataset.cityid // console.log(e.target.dataset.detail.name) console.log(e.currentTarget.dataset) 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('hotCityList', '', {}, (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 subItem.cityId = item.cityId 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 subItem.cityId = ite.cityId arrayItem.push(subItem) }) _arrayItem.item = arrayItem // console.log(_arrayItem) } catch(e) { // console.log(e) } _array.push(_arrayItem) }) let list = [...this.data.list, ..._array]; this.setData({ list: list, originalList: list, }) break; } }, //重新定位 relocation() { } })