const util = require('../../../utils/util') const event = require('../../../utils/event') const storage = require('../../../utils/storage') const city = require("../../../const/city.js") const app = getApp() var hotList = [] Page({ data: { type: 0, value: '', height: app.globalData.fragmentHeight - 100, nowCity: null,//当前定位城市 localCity: null,//当前定位城市 hotCity: [], // 热门城市 lists:[],//城市列表 searchResult:[],//查找列表 titleHeight: 240, indexHeight: 0, itemHeight: 0, scrollViewId: '', // scroll-view滚动到的子元素的id 锚点 touchmove: false, // 是否在索引表上滑动 touchmoveIndex: -1 }, onLoad: function (options) { if(options.type){ if(Number(options.type) === 1){ util.showToast('请选择一个城市,方便查看情报信息') } this.data.type = Number(options.type) } var height = app.globalData.fragmentHeight - 100 this.data.indexHeight = height - 48 if(this.data.indexHeight >= 1200){ this.data.indexHeight = 1200 } var titleHeight = this.px2rpx(app.globalData.CustomBar) + 116 var itemHeight = parseInt(this.data.indexHeight / 24) var localCity = null if(app.nowLocation && !util.isEmpty(app.nowLocation.cityName)){ localCity = app.nowLocation } var nowCity = null if(app.nowCity){ nowCity = app.nowCity.cityName } var hotstring = storage.get('ztb-hot-list') if(!util.isEmpty(hotstring)){ hotList = JSON.parse(hotstring) } this.setData({ height, itemHeight, indexHeight: this.data.indexHeight, titleHeight, localCity, nowCity, lists: city.list, hotCity: hotList, type: this.data.type }) }, onChange({detail}){ this.setData({ value: detail },()=>{ this.searchCity() }) }, closeInput(e){ this.setData({ value: '' }) }, searchCity(){ let result = [] city.list.forEach((item1, index1) => { item1.data.forEach((item2, index2) => { if (item2.keyword.indexOf(this.data.value.toLocaleUpperCase()) !== -1) { result.push(item2) } }) }) this.setData({ searchResult: result }) }, rpx2px: function(rpx) { return rpx / 750 * app.globalData.windowWidth }, px2rpx: function(px) { return px * 750 / app.globalData.windowWidth }, touchStart: function(e) { this.setData({ touchmove: true }) let pageY = this.px2rpx(e.touches[0].pageY) let index = Math.floor((pageY - this.data.titleHeight) / this.data.itemHeight) let item = this.data.lists[index] if (item) { this.setData({ scrollViewId: item.letter == '#' ? 'E_' : item.letter, touchmoveIndex: index }) } }, touchMove: function(e) { let pageY = this.px2rpx(e.touches[0].pageY) let index = Math.floor((pageY - this.data.titleHeight) / this.data.itemHeight) let item = this.data.lists[index] if (item) { this.setData({ scrollViewId: item.letter, touchmoveIndex: index }) } }, touchEnd: function() { this.setData({ touchmove: false, touchmoveIndex: -1 }) }, lookNow: function(){ var latitude = parseFloat(app.nowLocation.latitude.toFixed(6)) var longitude = parseFloat(app.nowLocation.longitude.toFixed(6)) this.selectCity({code: app.nowLocation.cityCode, name:app.nowLocation.cityName, latitude, longitude }) }, lookItem: function(e){ var item = null if(util.isEmpty(this.data.value)){ if(!util.isEmpty(e.currentTarget.dataset.page)){ item = this.data.lists[e.currentTarget.dataset.page].data[e.currentTarget.dataset.index] } else { item = this.data.hotCity[e.currentTarget.dataset.index] } } else { item = this.data.searchResult[e.currentTarget.dataset.index] } if(this.data.type == 2){ const channel = this.getOpenerEventChannel() channel.emit('onCallback', { what: 120, detail: item }) wx.navigateBack() return } this.selectCity(item) }, selectCity: function(item){ if(app.nowCity) { if(app.nowCity.cityCode == item.code){ wx.navigateBack() return } app.nowCity.cityName = item.name.replace('市', '') app.nowCity.cityCode = item.code if(item.latitude){ app.nowCity.latitude = item.latitude app.nowCity.longitude = item.longitude } } else { app.nowCity = { cityName: item.name.replace('市', ''), cityCode: item.code } if(item.latitude){ app.nowCity.latitude = item.latitude app.nowCity.longitude = item.longitude } } var flag = false for (let index = 0; index < hotList.length; index++) { if(hotList[index].code == item.code){ flag = true break } } if(!flag){ hotList.push(item) storage.put('ztb-hot-list', JSON.stringify(hotList)) } // storage.put('ztb-now-city', JSON.stringify(app.nowCity)) event.emit('EventMessage', { what: 444, desc: 'nowCity' }) wx.navigateBack() }, onShareAppMessage: function () { return { title: '查看附近纸厂车辆排队和扣点', imageUrl: 'https://pic.imgdb.cn/item/61bc42cd2ab3f51d9161074e.png', path: `pages/index/index` } } })