From c68e8103503cb7ee44651d5acee38ae57c8d5e8f Mon Sep 17 00:00:00 2001 From: donqi Date: Thu, 21 Jul 2022 14:30:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D=E5=AE=9A?= =?UTF-8?q?=E4=BD=8D=EF=BC=8C=E8=AE=A2=E5=8D=95=E7=A1=AE=E8=AE=A4=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E8=87=AA=E5=8A=A8=E5=B8=A6=E5=87=BA=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/js/request.js | 110 +++++++++++++++++----------- pages/index/home.vue | 19 +++-- pages/index/index.vue | 3 - pages/index/worker-home.vue | 13 +++- pages/order/order-detail.vue | 12 ++- pages/product/filtered-products.vue | 15 +++- pages/product/shop-detail.vue | 14 +++- 7 files changed, 127 insertions(+), 59 deletions(-) diff --git a/common/js/request.js b/common/js/request.js index eeca8aa..9deb810 100644 --- a/common/js/request.js +++ b/common/js/request.js @@ -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, diff --git a/pages/index/home.vue b/pages/index/home.vue index e2af360..7770408 100644 --- a/pages/index/home.vue +++ b/pages/index/home.vue @@ -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; diff --git a/pages/index/index.vue b/pages/index/index.vue index eed3b99..3ec8325 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -104,9 +104,6 @@ } this.inParam = option; this.loadData(option); - }, - onShow() { - this.$request.authAndGetLocation(); }, onShareAppMessage(e) { let shareInfo = null; diff --git a/pages/index/worker-home.vue b/pages/index/worker-home.vue index 2b83151..73d33e6 100644 --- a/pages/index/worker-home.vue +++ b/pages/index/worker-home.vue @@ -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 = []; diff --git a/pages/order/order-detail.vue b/pages/order/order-detail.vue index 4770c4a..d6d14c4 100644 --- a/pages/order/order-detail.vue +++ b/pages/order/order-detail.vue @@ -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) => { diff --git a/pages/product/filtered-products.vue b/pages/product/filtered-products.vue index 87568c7..71da50a 100644 --- a/pages/product/filtered-products.vue +++ b/pages/product/filtered-products.vue @@ -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; diff --git a/pages/product/shop-detail.vue b/pages/product/shop-detail.vue index 7e0414a..d0d8492 100644 --- a/pages/product/shop-detail.vue +++ b/pages/product/shop-detail.vue @@ -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,