纸通宝小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

190 lines
5.3 KiB

import qqmap from '../../utils/map';
import region2 from '../../utils/region2'
const app = getApp()
Component({
properties: {
styles: { //这个是可以自定义最外层的view的样式
type: String,
value: '',
observer: function (newval, oldval) {
// 监听改变
console.log(newval, oldval);
}
},
},
data: {
//下面是字母排序
letter: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
cityListId: '',
//下面是城市列表信息,这里只是模拟数据
citylist:region2,
//下面是热门城市数据,模拟数据
newcity: [{"id":110100,"cityName":"北京"}, {"id":310100,"cityName":"上海"}, {"id":440100,"cityName":"广州"},{"id":440300,"cityName":"深圳"}],//若还需要添加则在region2中查找
// citySel: '全国',
locateCity: '',
touchEndy:0,
rightheight:0,
topHeight:0,
dialogIsShow:false,
chooseCity:null,//缓存的城市
},
attached() {
var that=this
this.data.topHeight=app.globalData.CustomBar
wx.getStorage({
key: 'city',
success: function(res){
if(res.data.cityName){
that.setData({
chooseCity:res.data.cityName
})
}else{
that.setData({
chooseCity:''
})
}
},
fail: function(res) {
that.setData({
chooseCity:''
})
}
});
var query = this.createSelectorQuery()
query.select('#rightSortHeight').boundingClientRect(function (res) {
console.log(res);
that.setData({
rightheight:res.height
})
}).exec();
},
methods: {
//点击城市
cityTap(e) {
const val = e.currentTarget.dataset.val || '',
types = e.currentTarget.dataset.types || '',
Index = e.currentTarget.dataset.index || '',
that = this;
let city = this.data.citySel;
let cityId=''
switch (types) {
case 'locate':
//定位内容
city = this.data.locateCity;
break;
case 'national':
//全国
city = '全国';
break;
case 'new':
//热门城市
city = val.cityName;
cityId=val.id
break;
case 'list':
//城市列表
city = val.cityName;
cityId=val.id
break;
}
if (city) {
var cityData={
cityName: city,
cityId:cityId
}
wx.setStorage({
key: 'city',
data: cityData
}) //点击后给父组件可以通过bindcitytap事件,获取到cityname的值,这是子组件给父组件传值和触发事件的方法
this.triggerEvent('citytap', {
cityname: city,
cityId:cityId
});
} else {
console.log('还没有');
this.getLocate();
}
},
//点击城市字母
letterTap(e) {
const Item = e.currentTarget.dataset.item;
this.setData({
cityListId: Item
});
this.titleDialog(Item)
},
//调用定位
getLocate(){
let that=this;
new qqmap().getLocateInfo().then(function (val) {//这个方法在另一个文件里,下面有贴出代码
console.log(val);
if (val.indexOf('市') !== -1) {//这里是去掉“市”这个字
console.log(val.indexOf('市') - 1);
val = val.slice(0, val.indexOf('市'));
console.log(val);
}
that.setData({
locateCity: val,
});
//把获取的定位和获取的时间放到本地存储
wx.setStorageSync('locatecity', { city: val, time: new Date().getTime() });
});
},
//开始触摸事件
touchStart: function (e) {
this.data.touchEndy = e.touches[0].pageY-this.data.topHeight;
},
touchMove: function (e) {
this.data.touchEndy = e.touches[0].pageY-this.data.topHeight;
var lindex = parseInt(this.data.touchEndy / this.data.rightheight * 26);//根据前面分析获取手指触摸位置在letters中的index值
var value = this.data.letter[lindex];
this.titleDialog(value)
},
touchEnd: function (e) {
var lindex = parseInt(this.data.touchEndy / this.data.rightheight * 26);//根据前面分析获取手指触摸位置在letters中的index值
var value = this.data.letter[lindex];
if(value){
this.setData({
cityListId: value
});
}
this.titleDialog(value)
},
titleDialog: function (value) {
if(value){
this.setData({
dialongValue:value,
dialogIsShow:true
})
setTimeout(() => {
this.setData({
dialongValue:null,
dialogIsShow:false
})
}, 1500);
}
},
},
ready() {
console.log(getApp());
let that = this,
cityOrTime = wx.getStorageSync('locatecity')||{},
time = new Date().getTime(),
city='';
if (!cityOrTime.time||(time - cityOrTime.time > 1800000)){//每隔30分钟请求一次定位
this.getLocate();
}else{//如果未满30分钟,那么直接从本地缓存里取值
that.setData({
locateCity: cityOrTime.city
})
}
},
})