Files
cardsystem/pages/switchcity/switchcity.js
2021-05-21 15:02:18 +08:00

180 lines
4.4 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 获取城市
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
winHeight : 0, //可视高度
cityList : [], //城市列表
adcode : 0, //城市code
scrollTop : 0, //置顶高度
scrollTopId : '', //置顶id
searchLetter: [], //热门城市
regionShow : false, //区域显示开关
city : '', //城市名称
area : '', //区域名称
cityName : '', //定位城市名称
defaultcity : '', //定位区域名称
defaultregi : '', //区域名称
regiList : [], //区域列表
address : {
city_code: "",
area_code: ""
}
},
onLoad(e) {
wx.getLocation({
success: res => {
// 解析坐标
                getApp().qqmapsdk.reverseGeocoder({
                    location: {
                        latitude    : res.latitude,
                        longitude   : res.longitude
                    },
                    success: res=>{
                        this.setData({
defaultcity : res.result.ad_info.city,
defaultregi : res.result.ad_info.district
                        })
                    }
                })
},
fail: res => {}
})
if(e.area === '') e.area_code = '0'
this.setData({
address : {
area_code: e.area_code,
city_code: e.city_code,
area: e.area,
city: e.city
}
        })
// 获取全部城市
this.local();
},
/**
* 生命周期函数--监听页面加载
*/
onShow() {},
/**
* 全部城市
*/
local() {
wx.$api.index.newCity().then(res=>{
this.setData({
cityList : res.data
})
// 获取区级列表
this.regilist(this.data.address.city_code)
}).catch(err=>{
if(!err.login){
// 写入缓存
wx.setStorage({
key : 'token',
data : ''
})
}
})
},
/**
* 城市字母
*/
clickLetter (e) {
var showLetter = e.currentTarget.dataset.letter;
this.setData({
showLetter : showLetter,
isShowLetter: true,
scrollTopId : showLetter,
})
var that = this;
setTimeout(function () {
that.setData({
isShowLetter: false
})
}, 1000)
},
/**
* 选择城市
*/
bindCity(e) {
let stairid = e.currentTarget.dataset.citycode,
city = e.currentTarget.dataset.city
this.setData({
address: {
city_code: stairid,
city: city
}
})
// 获取区级列表
this.regilist(stairid)
},
/**
* 地区列表
*/
regilist(areaCode) {
wx.$api.index.newidxCity(areaCode).then(res=>{
res.data.unshift({ code: '0', name:'全部'})
this.setData({
regiList : res.data
})
})
},
/**
* 选择市区返回
*/
regionOpt (e) {
let area_name = e.currentTarget.dataset.city
var pages = getCurrentPages();
var prevPage = pages[pages.length - 2];
if(area_name === '全部') area_name = this.data.address.city
prevPage.setData({
city: area_name
})
wx.navigateBack({
delta: 1
})
},
/**
* 选择定位城市返回
*/
nowLocation () {
var pages = getCurrentPages();
var prevPage = pages[pages.length - 2];
prevPage.setData({
cityName: this.data.defaultcity,
city : this.data.defaultregi
})
wx.navigateBack({
delta: 1
})
},
/**
* 点击当前回到顶部
*/
hotCity () {
this.setData({
scrollTop: 0,
})
}
})