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

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
// args.url = 'https://www.opsoul.com' + args.url;
// args.url = 'http://192.168.2.42:80' + args.url;
args.url = 'http://127.0.0.1:80' + args.url;
args.url = 'http://120.79.136.57' + args.url;
// args.url = 'http://127.0.0.1:80' + args.url;
if (args.data) {
args.data.deptId = globalData.deptId;
args.data.from = globalData.from;
@ -43,49 +43,73 @@ export default {
}
})
},
authAndGetLocation() {
async authAndGetLocation() {
let _this = this;
// 通过 wx.getSetting 先查询一下用户是否授权了 "scope.userLocation" 这个 scope
wx.getSetting({
success(res) {
if (!res.authSetting['scope.userLocation']) {
// 用户授权
wx.authorize({
scope: 'scope.userLocation',
success() {
// 用户已经同意, 后续调用此接口不会弹窗询问
_this.wxGetLocation();
},
fail() {
// 用户已经拒绝过授权
wx.openSetting({
success(res) {
if (res['scope.userLocation']) {
_this.wxGetLocation();
}
}
})
}
})
} else {
_this.wxGetLocation();
}
}
})
let wxGetSettinsRes = await wx.getSetting();
if (!wxGetSettinsRes.authSetting['scope.userLocation']) {
// 用户授权
let authRes = await wx.authorize({
scope: 'scope.userLocation'
})
if (authRes && authRes.errMsg.indexOf('ok') != -1) {
// 用户已经同意, 后续调用此接口不会弹窗询问
await _this.wxGetLocation();
} else {
// 用户已经拒绝过授权
let wxOpenSettingRes = await wx.openSetting()
if (wxOpenSettingRes && wxOpenSettingRes.authSetting && wxOpenSettingRes.authSetting['scope.userLocation']) {
await _this.wxGetLocation();
}
}
} else {
await _this.wxGetLocation();
}
},
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 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;
},
async login() {
let userInfo = await wx.getUserProfile({
@ -104,7 +128,7 @@ export default {
// 第一次从服务端获取用户信息
let wxGetUserRes = await this.qryUserInfo(openId);
// 获取失败则获取微信信息再调用注册接口
if (!wxGetUserRes.data) {
if (!wxGetUserRes.data && wxGetUserRes.code === 0) {
// 注册完成后再次从服务端获取用户信息
let registerRes = await this.registerUser({
openId: openId,

View File

@ -113,13 +113,15 @@
beforeDestroy() {
this.offBindEvent();
},
onReady() {
this.loadData();
async onReady() {
await this.$request.authAndGetLocation();
await this.loadData();
},
methods: {
async loadData() {
//
this.loadRegionList();
this.loadRegionList();
this.getCurAreaArr();
//
let hotGoodsRes = await this.$request.getHotGoods({
pageNum: 1,
@ -143,6 +145,15 @@
offBindEvent() {
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() {
let typeList = await this.$request.listByStep();
typeList = typeList.data;
@ -173,8 +184,6 @@
this.areaList.push(regionList);
this.areaList.push(subRegionList);
this.areaList.push(subSubRegionList);
//
this.searchInfo.area = [regionList[0], subRegionList[0], subSubRegionList[0]];
},
regionChange(e) {
this.areaMultiIndex = e.detail.value;

View File

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

View File

@ -135,11 +135,20 @@
async loadData() {
this.pageNum = this.$globalData.initPageNum;
this.pageSize = this.$globalData.initPageSize;
this.loadCategoryList();
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();
},
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 = {}) {
this.pageNum = this.$globalData.initPageNum;
this.workerInfos = [];

View File

@ -176,13 +176,23 @@
this.offBindEvent();
},
methods: {
async loadData(params) {
async loadData(params) {
this.loadDefaultAddress();
// this.pickedProductList = await this.$api.data('pickedProductList');
this.pickedProductList = params.pickedProductList;
this.loadTotalPrice();
this.timeRangeList = this.$globalData.timeRangeList;
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() {
// totalprice
this.pickedProductList.forEach((obj) => {

View File

@ -163,16 +163,25 @@
}
if (params.inputGoodsName) {
this.inputGoodsName = params.inputGoodsName;
}
}
this.loadData();
},
methods: {
loadData() {
async loadData() {
this.pageNum = this.$globalData.initPageNum;
this.pageSize = this.$globalData.initPageSize;
this.loadCategoryList();
this.loadRegionList();
this.loadRegionList();
//
await this.getCurAreaArr();
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 = {}) {
params.pageNum = this.pageNum;

View File

@ -127,7 +127,8 @@
}],
searchInfo: {},
areaList: [],
areaMultiIndex: [0, 0, 0],
areaMultiIndex: [0, 0, 0],
categoryList: [],
stickyTop: this.CustomBar
}
},
@ -144,9 +145,18 @@
this.shopInfo = res.data;
this.loadCategoryList();
await this.loadRegionList();
this.searchInfo.area = [this.areaList[0][0], this.areaList[1][0], this.areaList[2][0]]
//
await this.getCurAreaArr();
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 = {}) {
params = {
status: 0,