const city = require('../../utils/city.js'); const cityObjs = require('../../utils/city.js'); const config = require('../../utils/config.js'); const appInstance = getApp(); const app = getApp() Page({ data: { searchLetter: [], showLetter: "", winHeight: 0, cityList: [], isShowLetter: false, scrollTop: 0,//置顶高度 scrollTopId: '',//置顶id city: "定位中", currentCityCode: '', hotcityList: [{ cityCode: 110000, city: '北京市' }, { cityCode: 310000, city: '上海市' }, { cityCode: 120000, city: '天津市' }, { cityCode: 320500, city: '苏州市' }, { cityCode: 330100, city: '杭州市' }, { cityCode: 320100, city: '南京市' }, { cityCode: 420100, city: '武汉市' }, { cityCode: 440100, city: '广州市' }, { cityCode: 440300, city: '深圳市' },], commonCityList: [{ cityCode: 110000, city: '北京市' }, { cityCode: 310000, city: '上海市' }], countyList: [{ cityCode: 110000, county: 'A区' }, { cityCode: 310000, county: 'B区' }, { cityCode: 440100, county: 'C区' }, { cityCode: 440300, county: 'D区' }, { cityCode: 330100, county: 'E县' }, { cityCode: 320100, county: 'F县' }, { cityCode: 420100, county: 'G县' }], inputName: '', completeList: [], county: '', condition: false, }, onLoad: function () { // 生命周期函数--监听页面加载 const searchLetter = city.searchLetter; const cityList = city.cityList(); const sysInfo = wx.getSystemInfoSync(); const winHeight = sysInfo.windowHeight; const itemH = winHeight / searchLetter.length; let tempArr = []; searchLetter.map( (item, index) => { let temp = {}; temp.name = item; temp.tHeight = index * itemH; temp.bHeight = (index + 1) * itemH; tempArr.push(temp) } ); this.setData({ winHeight: winHeight, itemH: itemH, searchLetter: tempArr, cityList: cityList }); this.getLocation(); let _this = this; _this.setData({ navH: app.globalData.navHeight }) }, onReady: function () { // 生命周期函数--监听页面初次渲染完成 }, onShow: function () { // 生命周期函数--监听页面显示 }, onHide: function () { // 生命周期函数--监听页面隐藏 }, onUnload: function () { // 生命周期函数--监听页面卸载 }, onPullDownRefresh: function () { // 页面相关事件处理函数--监听用户下拉动作 }, onReachBottom: function () { // 页面上拉触底事件的处理函数 }, clickLetter: function (e) { const showLetter = e.currentTarget.dataset.letter; this.setData({ toastShowLetter: showLetter, isShowLetter: true, scrollTopId: showLetter, }) const that = this; setTimeout(function () { that.setData({ isShowLetter: false }) }, 500) }, reGetLocation: function () { appInstance.globalData.defaultCity = this.data.city appInstance.globalData.defaultCounty = this.data.county //返回首页 wx.switchTab({ url: '../index/index' }) }, //选择城市 bindCity: function (e) { this.setData({ condition: true, city: e.currentTarget.dataset.city, currentCityCode: e.currentTarget.dataset.code, scrollTop: 0, completeList: [], }) this.selectCounty() appInstance.globalData.defaultCity = this.data.city appInstance.globalData.defaultCounty = '' }, bindCounty: function (e) { this.setData({ county: e.currentTarget.dataset.city }) appInstance.globalData.defaultCounty = this.data.county var pages = getCurrentPages(); var currPage = pages[pages.length - 1]; var prevPage = pages[pages.length - 2]; prevPage.setData({ city: this.data.city, county: e.currentTarget.dataset.city }) wx.navigateBack({ delta: 1 }) }, //点击热门城市回到顶部 hotCity: function () { this.setData({ scrollTop: 0, }) }, bindScroll: function (e) { }, selectCounty: function () { var code = ""; var codet = String(this.data.currentCityCode).substring(0,3) if (codet == '110' || codet == '120' || codet == '500' || codet == '310') { code = codet + "000" } else { code = String(this.data.currentCityCode) } const that = this; wx.request({ url: `https://apis.map.qq.com/ws/district/v1/getchildren?&id=${code}&key=${config.key}`, success: function (res) { that.setData({ countyList: res.data.result[0], }) }, fail() { app.timeOut() } }) }, getLocation: function () { this.setData({ county: '' }) const that = this; wx.getLocation({ type: 'wgs84', success: function (res) { let latitude = res.latitude let longitude = res.longitude wx.request({ url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}`, success: res => { that.setData({ city: res.data.result.ad_info.city, currentCityCode: res.data.result.ad_info.adcode, county: res.data.result.ad_info.district }) that.selectCounty(); }, fail() { app.timeOut() } }) }, fail() { app.timeOut() } }) }, bindBlur: function (e) { this.setData({ inputName: '' }) }, bindKeyInput: function (e) { this.setData({ inputName: e.detail.value }) this.auto() }, auto: function () { let inputSd = this.data.inputName.trim() let sd = inputSd.toLowerCase() let num = sd.length const cityList = cityObjs.cityObjs let finalCityList = [] let temp = cityList.filter( item => { let text = item.short.slice(0, num).toLowerCase() return (text && text == sd) } ) //在城市数据中,添加简拼到“shorter”属性,就可以实现简拼搜索 let tempShorter = cityList.filter( itemShorter => { if (itemShorter.shorter) { let textShorter = itemShorter.shorter.slice(0, num).toLowerCase() return (textShorter && textShorter == sd) } return } ) let tempChinese = cityList.filter( itemChinese => { let textChinese = itemChinese.city.slice(0, num) return (textChinese && textChinese == sd) } ) if (temp[0]) { temp.map( item => { let testObj = {}; testObj.city = item.city testObj.code = item.code finalCityList.push(testObj) } ) this.setData({ completeList: finalCityList, }) } else if (tempShorter[0]) { tempShorter.map( item => { let testObj = {}; testObj.city = item.city testObj.code = item.code finalCityList.push(testObj) } ); this.setData({ completeList: finalCityList, }) } else if (tempChinese[0]) { tempChinese.map( item => { let testObj = {}; testObj.city = item.city testObj.code = item.code finalCityList.push(testObj) }) this.setData({ completeList: finalCityList, }) } else { return } }, })