| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- // 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() {
- }
- })
|