获取当前定位,订单确认页面自动带出默认地址

This commit is contained in:
donqi 2022-07-21 14:30:52 +08:00
parent 12221e22fe
commit c68e810350
7 changed files with 127 additions and 59 deletions

View File

@ -11,8 +11,8 @@ export default {
}) })
// request 触发前拼接 url // request 触发前拼接 url
// args.url = 'https://www.opsoul.com' + args.url; // args.url = 'https://www.opsoul.com' + args.url;
// args.url = 'http://192.168.2.42:80' + args.url; args.url = 'http://120.79.136.57' + args.url;
args.url = 'http://127.0.0.1:80' + args.url; // args.url = 'http://127.0.0.1:80' + args.url;
if (args.data) { if (args.data) {
args.data.deptId = globalData.deptId; args.data.deptId = globalData.deptId;
args.data.from = globalData.from; args.data.from = globalData.from;
@ -43,49 +43,73 @@ export default {
} }
}) })
}, },
authAndGetLocation() { async authAndGetLocation() {
let _this = this; let _this = this;
// 通过 wx.getSetting 先查询一下用户是否授权了 "scope.userLocation" 这个 scope // 通过 wx.getSetting 先查询一下用户是否授权了 "scope.userLocation" 这个 scope
wx.getSetting({ let wxGetSettinsRes = await wx.getSetting();
success(res) { if (!wxGetSettinsRes.authSetting['scope.userLocation']) {
if (!res.authSetting['scope.userLocation']) {
// 用户授权 // 用户授权
wx.authorize({ let authRes = await wx.authorize({
scope: 'scope.userLocation', scope: 'scope.userLocation'
success() { })
if (authRes && authRes.errMsg.indexOf('ok') != -1) {
// 用户已经同意, 后续调用此接口不会弹窗询问 // 用户已经同意, 后续调用此接口不会弹窗询问
_this.wxGetLocation(); await _this.wxGetLocation();
},
fail() {
// 用户已经拒绝过授权
wx.openSetting({
success(res) {
if (res['scope.userLocation']) {
_this.wxGetLocation();
}
}
})
}
})
} else { } else {
_this.wxGetLocation(); // 用户已经拒绝过授权
let wxOpenSettingRes = await wx.openSetting()
if (wxOpenSettingRes && wxOpenSettingRes.authSetting && wxOpenSettingRes.authSetting['scope.userLocation']) {
await _this.wxGetLocation();
} }
} }
} else {
await _this.wxGetLocation();
}
},
async wxGetLocation() {
console.log("获取定位")
let res = await wx.getLocation({
type: 'gcj02'
})
let areaRes = await this.baiduGetLoacation(res.latitude, res.longitude);
areaRes = areaRes.data;
if (areaRes) {
let areaArr = [areaRes.provinceArea, areaRes.cityArea, areaRes.countryArea];
let curLocation = {
area: areaArr,
updateTimes: new Date().getTime()
}
uni.setStorageSync('curLocation', curLocation);
}
},
async getCurArea() {
let curLocation = uni.getStorageSync('curLocation');
if (curLocation && curLocation.updateTimes) {
let curTimes = new Date().getTime();
let deltaSeconds = curTimes - Number(curLocation.updateTimes);
if (deltaSeconds >= 15 * 60 * 1000) {
// 设定重新获取定位的时间间隔为15分钟
await this.wxGetLocation();
curLocation = uni.getStorageSync('curLocation');
}
} else {
await this.wxGetLocation();
curLocation = uni.getStorageSync('curLocation');
}
if (curLocation && curLocation.area) {
return curLocation.area;
}
return null;
},
async baiduGetLoacation(latitude, longitude) {
let res = await uni.request({
url: '/tool/baidu/getLocation',
method: 'POST',
data: {
location: latitude + ',' + longitude
}
}) })
}, return res[1].data;
wxGetLocation() {
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude
const longitude = res.longitude
console.log(res)
// uni.setStorageSync('userLocation', res);
},
complete(res) {
console.log(res)
}
})
}, },
async login() { async login() {
let userInfo = await wx.getUserProfile({ let userInfo = await wx.getUserProfile({
@ -104,7 +128,7 @@ export default {
// 第一次从服务端获取用户信息 // 第一次从服务端获取用户信息
let wxGetUserRes = await this.qryUserInfo(openId); let wxGetUserRes = await this.qryUserInfo(openId);
// 获取失败则获取微信信息再调用注册接口 // 获取失败则获取微信信息再调用注册接口
if (!wxGetUserRes.data) { if (!wxGetUserRes.data && wxGetUserRes.code === 0) {
// 注册完成后再次从服务端获取用户信息 // 注册完成后再次从服务端获取用户信息
let registerRes = await this.registerUser({ let registerRes = await this.registerUser({
openId: openId, openId: openId,

View File

@ -113,13 +113,15 @@
beforeDestroy() { beforeDestroy() {
this.offBindEvent(); this.offBindEvent();
}, },
onReady() { async onReady() {
this.loadData(); await this.$request.authAndGetLocation();
await this.loadData();
}, },
methods: { methods: {
async loadData() { async loadData() {
// //
this.loadRegionList(); this.loadRegionList();
this.getCurAreaArr();
// //
let hotGoodsRes = await this.$request.getHotGoods({ let hotGoodsRes = await this.$request.getHotGoods({
pageNum: 1, pageNum: 1,
@ -143,6 +145,15 @@
offBindEvent() { offBindEvent() {
uni.$off('index_showProductDetail'); uni.$off('index_showProductDetail');
}, },
async getCurAreaArr() {
let areaArr = await this.$request.getCurArea();
if (areaArr) {
this.searchInfo.area = areaArr;
} else {
//
this.searchInfo.area = [regionList[0], subRegionList[0], subSubRegionList[0]];
}
},
async loadCategoryList() { async loadCategoryList() {
let typeList = await this.$request.listByStep(); let typeList = await this.$request.listByStep();
typeList = typeList.data; typeList = typeList.data;
@ -173,8 +184,6 @@
this.areaList.push(regionList); this.areaList.push(regionList);
this.areaList.push(subRegionList); this.areaList.push(subRegionList);
this.areaList.push(subSubRegionList); this.areaList.push(subSubRegionList);
//
this.searchInfo.area = [regionList[0], subRegionList[0], subSubRegionList[0]];
}, },
regionChange(e) { regionChange(e) {
this.areaMultiIndex = e.detail.value; this.areaMultiIndex = e.detail.value;

View File

@ -105,9 +105,6 @@
this.inParam = option; this.inParam = option;
this.loadData(option); this.loadData(option);
}, },
onShow() {
this.$request.authAndGetLocation();
},
onShareAppMessage(e) { onShareAppMessage(e) {
let shareInfo = null; let shareInfo = null;
if (e && e.target && e.target.dataset) { if (e && e.target && e.target.dataset) {

View File

@ -135,11 +135,20 @@
async loadData() { async loadData() {
this.pageNum = this.$globalData.initPageNum; this.pageNum = this.$globalData.initPageNum;
this.pageSize = this.$globalData.initPageSize; this.pageSize = this.$globalData.initPageSize;
this.loadCategoryList();
await this.loadRegionList(); await this.loadRegionList();
this.searchInfo.area = [this.areaList[0][0], this.areaList[1][0], this.areaList[2][0]] //
await this.getCurAreaArr();
this.loadCategoryList();
this.searchGoods(); this.searchGoods();
}, },
async getCurAreaArr() {
let areaArr = await this.$request.getCurArea();
if (areaArr) {
this.searchInfo.area = areaArr;
} else {
this.searchInfo.area = [this.areaList[0][0], this.areaList[1][0], this.areaList[2][0]]
}
},
async reloadData(params = {}) { async reloadData(params = {}) {
this.pageNum = this.$globalData.initPageNum; this.pageNum = this.$globalData.initPageNum;
this.workerInfos = []; this.workerInfos = [];

View File

@ -177,12 +177,22 @@
}, },
methods: { methods: {
async loadData(params) { async loadData(params) {
this.loadDefaultAddress();
// this.pickedProductList = await this.$api.data('pickedProductList'); // this.pickedProductList = await this.$api.data('pickedProductList');
this.pickedProductList = params.pickedProductList; this.pickedProductList = params.pickedProductList;
this.loadTotalPrice(); this.loadTotalPrice();
this.timeRangeList = this.$globalData.timeRangeList; this.timeRangeList = this.$globalData.timeRangeList;
this.timeRange = this.timeRangeList[0]; this.timeRange = this.timeRangeList[0];
}, },
async loadDefaultAddress() {
let res = await this.$request.getAddressList({
customerId: this.$request.getCurUserInfo().customerId,
isDefault: 1
});
if (res && res.data &&res.data.length) {
this.formInfo.defaultAddress = res.data[0];
}
},
loadTotalPrice() { loadTotalPrice() {
// totalprice // totalprice
this.pickedProductList.forEach((obj) => { this.pickedProductList.forEach((obj) => {

View File

@ -167,13 +167,22 @@
this.loadData(); this.loadData();
}, },
methods: { methods: {
loadData() { async loadData() {
this.pageNum = this.$globalData.initPageNum; this.pageNum = this.$globalData.initPageNum;
this.pageSize = this.$globalData.initPageSize; this.pageSize = this.$globalData.initPageSize;
this.loadCategoryList(); this.loadCategoryList();
this.loadRegionList(); this.loadRegionList();
//
await this.getCurAreaArr();
this.loadProductData(); this.loadProductData();
}, },
async getCurAreaArr() {
let areaArr = await this.$request.getCurArea();
if (areaArr) {
this.chosenArea = areaArr;
this.taskConditions[0].value = this.chosenArea[2].areaId;
}
},
async loadProductData(params = {}) { async loadProductData(params = {}) {
params.pageNum = this.pageNum; params.pageNum = this.pageNum;
params.pageSize = this.pageSize; params.pageSize = this.pageSize;

View File

@ -128,6 +128,7 @@
searchInfo: {}, searchInfo: {},
areaList: [], areaList: [],
areaMultiIndex: [0, 0, 0], areaMultiIndex: [0, 0, 0],
categoryList: [],
stickyTop: this.CustomBar stickyTop: this.CustomBar
} }
}, },
@ -144,9 +145,18 @@
this.shopInfo = res.data; this.shopInfo = res.data;
this.loadCategoryList(); this.loadCategoryList();
await this.loadRegionList(); await this.loadRegionList();
this.searchInfo.area = [this.areaList[0][0], this.areaList[1][0], this.areaList[2][0]] //
await this.getCurAreaArr();
this.searchGoods(); this.searchGoods();
}, },
async getCurAreaArr() {
let areaArr = await this.$request.getCurArea();
if (areaArr) {
this.searchInfo.area = areaArr;
} else {
this.searchInfo.area = [this.areaList[0][0], this.areaList[1][0], this.areaList[2][0]];
}
},
async loadProductData(params = {}) { async loadProductData(params = {}) {
params = { params = {
status: 0, status: 0,