123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- 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
- }
- },
- })
|