| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- const city = require('../../utils/util.js');
- const appInstance = getApp();
- Page({
- data: {
- searchLetter: [],
- showLetter: "",
- winHeight: 0,
- cityList: [],
- isShowLetter: false,
- scrollTop: 0,//置顶高度
- scrollTopId: '',//置顶id
- city: "定位中",
- currentCityCode: '',
- hotcityList: [{ cityCode: 110000, city: '北京市' }, { cityCode: 310000, city: '上海市' }, { cityCode: 440100, city: '广州市' }, { cityCode: 440300, city: '深圳市' }, { cityCode: 330100, city: '杭州市' }, { cityCode: 320100, city: '南京市' }, { cityCode: 420100, city: '武汉市' }, { cityCode: 120000, city: '天津市' }, { cityCode: 610100, 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,
- },
- //选择城市
- bindCity: function (e) {
- this.setData({
- condition: true, //选择区县修改为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
- wx.switchTab({
- url: '../index/index'
- })
- },
- onLoad: function (options) {
- 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();
- },
- //定位当前城市的函数
- 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=${appInstance.globalData.tencentMapKey}`,
- 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
- })
- }
- })
- }
- })
- },
- //获取当前选择城市的区县
- selectCounty: function () {
- let code = this.data.currentCityCode
- const that = this;
- wx.request({
- url: `https://apis.map.qq.com/ws/district/v1/getchildren?&id=${code}&key=${appInstance.globalData.tencentMapKey}`,
- success: function (res) {
- that.setData({
- countyList: res.data.result[0],
- })
- },
- fail: function () {
- console.log("请求区县失败,请重试");
- }
- })
- },
- //重新定位城市
- reGetLocation: function () {
- appInstance.globalData.defaultCity = this.data.city
- appInstance.globalData.defaultCounty = this.data.county
- //返回首页
- wx.switchTab({
- url: '../index/index'
- })
- },
- //点击热门城市回到顶部
- hotCity: function () {
- this.setData({
- scrollTop: 0,
- })
- },
- 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 = city.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
- }
- },
-
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- return {
- title: '很赞的全国城市切换器~',
- desc: '分享个小程序,希望你喜欢☺️~',
- success: function (res) {
- // 转发成功
- wx.showToast({
- title: "分享成功",
- duration: 1000,
- icon: "success"
- })
- }
- }
- }
- })
|