2025-06-06 15:07:26 +08:00

209 lines
6.1 KiB
JavaScript

import { doNavigateWithUser } from '../../utils/helper';
import apiAddr from '../../api/base';
const app = getApp();
Component({
/**
* 组件的属性列表
*/
properties: {
},
/**
* 组件的初始数据
*/
data: {
qqmap_key: app.initQQMapKey(),
address: '定位中',
selectKeyWord: '',
shopArea: [],
lat: '',
lng: '',
mapObj: {},
},
methods: {
goPagesLink() {
doNavigateWithUser({
url: `/pages/index/shopcity`
});
},
// 获取地址
getAddress() {
let that = this;
let userlocat = wx.getStorageSync('userlocat');
let nowtime = Date.parse(new Date()) / 1000;
if (!userlocat || userlocat.overtime < nowtime || !userlocat.city) {
wx.showLoading({
title: '定位中...'
});
wx.getLocation({
success: (res) => {
console.log(res);
let overtime = Date.parse(new Date()) / 1000 + 7200;
this.setData({
lat: res.latitude,
lng: res.longitude,
})
if (this.data.qqmap_key) {
// 微信逆地址解析
wx.request({
url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + res.latitude + ',' + res.longitude + '&key=' + this.data.qqmap_key + '&get_poi=0',
success: function (res) {
// console.log('cityres', res);
var city = res.data.result.address_component.city;
// console.log(city);
if (city) {
that.setData({
address: city
})
let area = that.data.shopArea;
// console.log(area);
if (typeof area[city] !== 'undefined') {
// setData(that,'area_id',area[city]['area_id']);
}
}
userlocat = {
userlat: that.data.lat,
userlng: that.data.lng,
overtime: overtime,
city: city,
};
wx.setStorageSync('userlocat', userlocat);
wx.hideLoading();
// that.setData({
// pageNo: 0, // 当前页数
// shopList: [],
// })
// that.getList();
}
})
} else {
console.log('11111 走到这列了');
let sendData = {
lat: res.latitude,
lng: res.longitude,
};
// 后端地图接口
wx.request({
url: apiAddr.get_maps_geocoder,
method: 'post',
header: {
'Content-type': 'application/x-www-form-urlencoded'
},
dataType: 'json',
data: sendData,
success: function (res) {
// console.log('cityres2', res);
var city = res.data.city;
// console.log(city);
if (city) {
that.setData({
address: city
})
let area = that.data.shopArea;
if (typeof area[city] !== 'undefined') {
// setData(that,'area_id',area[city]['area_id']);
}
}
userlocat = {
userlat: that.data.lat,
userlng: that.data.lng,
overtime: overtime,
city: city,
};
wx.setStorageSync('userlocat', userlocat);
wx.hideLoading();
}
})
}
// 缓存
wx.request({
url: apiAddr.setcookie_location,
method: 'post',
header: {
'Content-type': 'application/x-www-form-urlencoded'
},
dataType: 'json',
data: userlocat,
success: (result) => {
// console.log(result);
},
})
},
// fail: () => wx.showToast({ title: '请点击右上角三个点并开启位置权限', icon: 'none', mask: true })
})
} else {
// console.log('userlocat', userlocat);
that.setData({
address: userlocat.city
})
}
},
searchInput(e) {
console.log('e',e);
let _this = this;
_this.setData({
selectKeyWord: e.detail.value
})
_this.triggerEvent('search', e.detail.value);
},
// 获取店铺区域列表
getShopAreaList() {
let shopArea = wx.getStorageSync('shopArea');
let nowtime = Date.parse(new Date()) / 1000;
if (!shopArea || shopArea.overtime < nowtime) {
wx.request({
url: apiAddr.get_community_area_list,
method: 'get',
header: {
'Content-type': 'application/x-www-form-urlencoded'
},
dataType: 'json',
success: (result) => {
let {
data: {
list
}
} = result;
if (list) {
this.setData({
shopArea: list
})
let overtime = Date.parse(new Date()) / 1000 + 7200;
shopArea = {
list: list,
overtime: overtime
};
wx.setStorageSync('shopArea', shopArea);
}
},
})
} else {
this.setData({
shopArea: shopArea.list
})
// setData(this, 'shopArea', shopArea.list);
}
},
},
lifetimes: {
attached: function () {
// 组件被添加到页面节点树时执行
this.getAddress();
this.getShopAreaList();
},
},
})