From 0e89111f6df4de31fa1258304e90442a885883eb Mon Sep 17 00:00:00 2001 From: lfs3 Date: Wed, 4 Sep 2019 15:21:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=B9=E6=A1=86=E9=97=AE=E9=A2=98=EF=BC=8Cbu?= =?UTF-8?q?g=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/models/wxLocation.js | 112 ++++++++-------- src/models/wxShare.js | 5 +- src/view/choose-community.vue | 59 +++++---- src/view/home.vue | 240 +++++++++++++++++++++------------- 4 files changed, 241 insertions(+), 175 deletions(-) diff --git a/src/models/wxLocation.js b/src/models/wxLocation.js index fe14bfd..492ff2c 100644 --- a/src/models/wxLocation.js +++ b/src/models/wxLocation.js @@ -2,68 +2,74 @@ import Vue from "vue"; import wx from "weixin-js-sdk"; import axios from "axios"; import wxShareModel from "../models/wxShare-model.js"; +let instance = axios.create(); //引入BMap import BMap from "BMap"; - Vue.prototype.wxLocation = function () { - wxShareModel.getWXCheck().then(response => { - if (response.code != 0) { - return; - } - var jsApiSignConfig = response.response.jsApiSignConfig; - wx.config({ - debug: false, // 开启调试模式,开发时可以开启 - appId: jsApiSignConfig.appId, // 必填,公众号的唯一标识 由接口返回 - timestamp: jsApiSignConfig.timestamp, // 必填,生成签名的时间戳 由接口返回 - nonceStr: jsApiSignConfig.nonceStr, // 必填,生成签名的随机串 由接口返回 - signature: jsApiSignConfig.signature, // 必填,签名 由接口返回 - jsApiList: [ - 'checkJsApi', 'openLocation', 'getLocation' - ], - }); - wx.checkJsApi({ - jsApiList: ['getLocation'], - success: function (res) { - if (res.checkResult.getLocation == false) { - alert('你的微信版本太低,不支持微信JS接口,请升级到最新的微信版本!'); - return; - } + return new Promise((resolve, reject) => { + wxShareModel.getWXCheck().then(response => { + if (response.code != 0) { + return; } - }); - wx.ready(() => { - // 首次进入页面进会弹窗确认地理位置 - if (localStorage.getItem("latitude") == null) { - wx.getLocation({ - type: "gcj02", // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02' + var jsApiSignConfig = response.response.jsApiSignConfig; + wx.config({ + debug: false, // 开启调试模式,开发时可以开启 + appId: jsApiSignConfig.appId, // 必填,公众号的唯一标识 由接口返回 + timestamp: jsApiSignConfig.timestamp, // 必填,生成签名的时间戳 由接口返回 + nonceStr: jsApiSignConfig.nonceStr, // 必填,生成签名的随机串 由接口返回 + signature: jsApiSignConfig.signature, // 必填,签名 由接口返回 + jsApiList: [ + 'checkJsApi', 'openLocation', 'getLocation' + ], + }); + wx.checkJsApi({ + jsApiList: ['getLocation'], success: function (res) { - console.log('res:' + res); - // alert('res.latitude:' + res.latitude + '------res.longitude:' + res.longitude); - localStorage.setItem("latitude", res.latitude); - localStorage.setItem("longitude", res.longitude); - $.ajax({ - url: 'http://api.map.baidu.com/geocoder/v2/?ak=a79bFu054xIbE23jlynkSXX20F9yGD4G&callback=renderReverse&location=' + res.latitude + ',' + res.longitude + '&output=json&pois=1', - type: "get", - dataType: "jsonp", - jsonp: "callback", - success: function (data) { - // alert('data.result.location.lng:' + data.result.location.lng + '-----data.result.location.lat:' + data.result.location.lat); - localStorage.setItem("latitude", data.result.location.lat); - localStorage.setItem("longitude", data.result.location.lng); + if (res.checkResult.getLocation == false) { + alert('你的微信版本太低,不支持微信JS接口,请升级到最新的微信版本!'); + return ""; + } + } + }); + wx.ready(() => { + // 首次进入页面进会弹窗确认地理位置 + if (localStorage.getItem("latitude") == null) { + wx.getLocation({ + type: "gcj02", // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02' + success: function (res) { + console.log('res:' + res); + // alert('res.latitude:' + res.latitude + '------res.longitude:' + res.longitude); + // localStorage.setItem("latitude", res.latitude); + // localStorage.setItem("longitude", res.longitude); + $.ajax({ + url: 'http://api.map.baidu.com/geocoder/v2/?ak=a79bFu054xIbE23jlynkSXX20F9yGD4G&callback=renderReverse&location=' + res.latitude + ',' + res.longitude + '&output=json&pois=1', + type: "get", + dataType: "jsonp", + jsonp: "callback", + success: function (data) { + // alert('data.result.location.lng:' + data.result.location.lng + '-----data.result.location.lat:' + data.result.location.lat); + localStorage.setItem("latitude", data.result.location.lat); + localStorage.setItem("longitude", data.result.location.lng); + resolve("定位成功") + } + }); + }, + cancel: function () { + alert("拒绝定位"); + resolve("定位失败") } }); - }, - cancel: function () { - alert("拒绝定位"); + } else { + resolve("已经有定位") } - }); - } else { - - } - // alert("lat:" + localStorage.getItem("latitude") + ";lng:" + localStorage.getItem("longitude")); - wx.error(function (res) { - alert("wx-js初始化: " + res); + // alert("lat:" + localStorage.getItem("latitude") + ";lng:" + localStorage.getItem("longitude")); + wx.error(function (res) { + alert("wx-js初始化: " + res); + resolve("定位失败") + }); }); }); - }); + }) } + diff --git a/src/models/wxShare.js b/src/models/wxShare.js index 8743ccb..e32aaf4 100644 --- a/src/models/wxShare.js +++ b/src/models/wxShare.js @@ -112,7 +112,7 @@ wxShareModel.getWXCheck().then(response =>{ localStorage.setItem("longitude", res.longitude); }, cancel: function () { - alter("拒绝定位"); + alert("拒绝定位"); } }); } else { @@ -120,7 +120,8 @@ wxShareModel.getWXCheck().then(response =>{ } // alert("lat:" + localStorage.getItem("latitude") + ";lng:" + localStorage.getItem("longitude")); wx.error(function (res) { - alter("wx-js初始化: " + res); + console.log("--------wx") + alert("wx-js初始化: " + res); }); }); }); diff --git a/src/view/choose-community.vue b/src/view/choose-community.vue index beddff8..77b643f 100644 --- a/src/view/choose-community.vue +++ b/src/view/choose-community.vue @@ -124,8 +124,8 @@ export default { if (data.addressComponents) { const result = data.addressComponents; const location = { - creditLongitude: r.point.lat, // 经度 - creditLatitude: r.point.lng, // 纬度 + creditLongitude: r.point.lng, // 经度 + creditLatitude: r.point.lat, // 纬度 creditProvince: result.province || "", // 省 creditCity: result.city || "", // 市 creditArea: result.district || "", // 区 @@ -173,8 +173,8 @@ export default { }, //确定时保存记录 confirm(list) { - console.log("list:" + list); - // let addressArr = list.split(","); + console.log("list:" + list); + // let addressArr = list.split(","); let arr = list[2].split("&") || ""; this.districtId = arr[0]; this.defaultCity = arr[1]; @@ -182,7 +182,7 @@ export default { console.log("arr:" + this.defaultCity); // localStorage.setItem("cityName", list); this.show = false; - this.pageNum = 1; + this.pageNum = 0; this.busy = false; this.loadMoreHide = false; this.loadMoreTips = false; @@ -266,26 +266,27 @@ export default { item.cityId + "&" + item.cityName, item.districtId + "&" + item.districtName + "&" + item.merchantNo ]; - if (isEmpty(this.templateNo)) { - localStorage.setItem("cityName", this.addressValue.slice()); - localStorage.setItem( - "estate", - item.housingEstateId + "&" + item.housingEstateName - ); - this.estateId = item.housingEstateId; - countPartner(); - this.$router.push({ - name: "首页" - }); - } else { - this.$vux.confirm.show({ - title: "选择小区", - content: "确定选择" + item.housingEstateName, - confirmText: "确定", - cancelText: "取消", - onCancel: () => {}, - onConfirm: () => { + + this.$vux.confirm.show({ + title: "选择小区", + content: "确定选择:" + item.cityName+item.districtName +"【" + item.housingEstateName + "】", + confirmText: "确定", + cancelText: "取消", + onCancel: () => {}, + onConfirm: () => { + this.estateId = item.housingEstateId; + if (isEmpty(this.templateNo)) { + localStorage.setItem("cityName", this.addressValue.slice()); + localStorage.setItem( + "estate", + item.housingEstateId + "&" + item.housingEstateName + ); this.estateId = item.housingEstateId; + countPartner(); + this.$router.push({ + name: "首页" + }); + } else { chooseCommunityApi .getGoodsTemlate({ templateNo: this.templateNo, @@ -309,7 +310,10 @@ export default { content: item.housingEstateName + "暂无提供此商品服务", confirmText: "确定", onConfirm: () => { - localStorage.setItem("cityName", this.addressValue.slice()); + localStorage.setItem( + "cityName", + this.addressValue.slice() + ); localStorage.setItem( "estate", item.housingEstateId + "&" + item.housingEstateName @@ -325,8 +329,9 @@ export default { this.showLoading = false; }); } - }); - } + } + }); + // this.$router.push({ // name: "首页" // }); diff --git a/src/view/home.vue b/src/view/home.vue index a6ebe20..1f68550 100644 --- a/src/view/home.vue +++ b/src/view/home.vue @@ -171,7 +171,8 @@ export default { show: false, location: {}, uniqueId: "", - defaultCity: "天河区", //区名 默认值 不需要去掉 + defaultDistrict: "天河区", + defaultCity: "广州市", //区名 默认值 不需要去掉 defaultCommunity: "请选择小区", merchantNo: 2, //商户编号 默认值 不需要去掉 districtId: "440106000", //区id 默认值 不需要去掉 @@ -319,11 +320,12 @@ export default { lng: this.lng }) .then(res => { + console.log("----------getAddressInfo"); if (res.response.allAddress && res.response.userAddress) { this.addressData = res.response.allAddress; this.flag = true; let data = res.response; - //type:1-定位失败,0-不弹窗,1-弹窗无服务商,2-弹窗有服务商 + //type:-1定位失败,0-不弹窗,1-弹窗无服务商,2-弹窗有服务商,3-小区无效 if (data.userAddress.type == 1) { this.address = data.userAddress.cityName + @@ -332,54 +334,23 @@ export default { : ""); this.alertShow = true; } else if (data.userAddress.type == 2) { - return new Promise((resolve, reject) => { - chooseCommunityApi - .getEstateList({ - draw: 0, - lat: this.lat, - lng: this.lng, - num: 1 - }) - .then(res => { - //获取定位地址 - if (res.dataList.length > 0) { - var addreRes = res.dataList[0]; - this.$vux.confirm.show({ - title: "自动定位小区:", - content: addreRes.housingEstateName, - cancelText: "切换地址", - onCancel: () => { - this.chooseCommunity(); - }, - onConfirm: () => { - this.defaultLocalEstate(addreRes); - this.getGoodsList(); - } - }); - } else { - this.$vux.confirm.show({ - title: "自动定位地址:", - content: - data.userAddress.cityName + - (data.userAddress.districtName - ? data.userAddress.districtName - : ""), - confirmText: "选择小区", - cancelText: "切换地址", - onCancel: () => { - this.chooseCommunity(); - }, - onConfirm: () => { - this.defaultLocalEstate(data.estateInfo); - this.chooseCommunity(); - } - }); - } - this.showLoading = false; - }) - .catch(error => { - this.showLoading = false; - }); + this.showLoading = false; + this.$vux.confirm.show({ + title: "自动定位小区", + content: + data.estateInfo.cityName + + data.estateInfo.districtName + + "【" + + data.estateInfo.housingEstateName + + "】", + cancelText: "切换", + onCancel: () => { + this.chooseCommunity(); + }, + onConfirm: () => { + this.defaultLocalEstate(data.estateInfo); + this.getGoodsList(); + } }); } else if (data.userAddress.type == 0) { this.getGoodsList(); @@ -393,6 +364,20 @@ export default { this.chooseCommunity(); } }); + } else if (data.userAddress.type == 3) { + localStorage.removeItem("estate"); + this.$vux.alert.show({ + title: + this.defaultCity + + this.defaultDistrict + + "【" + + this.defaultCommunity + + "】" + + "暂无开通服务", + onHide: () => { + this.chooseCommunity(); + } + }); } resolve(); } @@ -413,9 +398,10 @@ export default { this.addressValue = arr; this.provinceId = arr[0].split("&")[0]; this.cityId = arr[1].split("&")[0]; - let cityNameArr = arr[2].split("&"); - this.districtId = cityNameArr[0]; - this.defaultCity = cityNameArr[1]; + this.defaultCity = arr[1].split("&")[1]; + let districtArr = arr[2].split("&"); + this.districtId = districtArr[0]; + this.defaultDistrict = districtArr[1]; // this.merchantNo = cityNameArr[2]; this.merchantNo = 2; } @@ -428,6 +414,7 @@ export default { }, getData() { let get = async () => { + this.getId(); await this.getAddressInfo(); // await this.getGoodsList(); }; @@ -438,19 +425,33 @@ export default { this.chooseCommunity(); }, getLocation() { + // let get = async () => { //判断是否在微信环境下操作 var ua = window.navigator.userAgent.toLowerCase(); //通过正则表达式匹配ua中是否含有MicroMessenger字符串 - let _this = this; if (ua.match(/MicroMessenger/i) == "micromessenger") { //微信环境 - this.wxLocation({}); + this.wxLocation().then(result => { + console.log("wxLocationresult:" + result); + this.getId(); + this.getAddressInfo(); + }); + // this.getData().then(result => { + // this.wxLocation({}) + // }); } else { // TODO 非微信环境用百度api this.getCity(); } + // }; + // return get(); + }, + getWXLocation() { + let get = async () => { + await this.wxLocation({}); + }; + return get(); }, - getCity() { let _this = this; var geolocation = new BMap.Geolocation(); @@ -467,8 +468,8 @@ export default { if (data.addressComponents) { const result = data.addressComponents; const location = { - creditLongitude: r.point.lat, // 经度 - creditLatitude: r.point.lng, // 纬度 + creditLongitude: r.point.lng, // 经度 + creditLatitude: r.point.lat, // 纬度 creditProvince: result.province || "", // 省 creditCity: result.city || "", // 市 creditArea: result.district || "", // 区 @@ -480,20 +481,22 @@ export default { localStorage.setItem("longitude", location.creditLongitude); _this.lat = location.creditLatitude; _this.lng = location.creditLongitude; - // alert( - // "省-" + - // location.creditProvince + - // ";市-" + - // location.creditCity + - // ";区-" + - // location.creditArea + - // ";街道-" + - // location.creditStreet + - // ";lat-" + - // location.creditLatitude + - // ";lng-" + - // location.creditLongitude - // ); + alert( + "省-" + + location.creditProvince + + ";市-" + + location.creditCity + + ";区-" + + location.creditArea + + ";街道-" + + location.creditStreet + + ";lat-" + + location.creditLatitude + + ";lng-" + + location.creditLongitude + ); + _this.getId(); + _this.getAddressInfo(); } }); // } @@ -505,12 +508,15 @@ export default { ? estateRes.provinceId : this.provinceId; this.cityId = estateRes.cityId ? estateRes.cityId : this.cityId; + this.cityName = estateRes.cityName + ? estateRes.cityName + : this.defaultCity; this.districtId = estateRes.districtId ? estateRes.districtId : this.districtId; - this.defaultCity = estateRes.districtName + this.defaultDistrict = estateRes.districtName ? estateRes.districtName - : this.defaultCity; + : this.defaultDistrict; this.merchantNo = estateRes.merchantNo; this.addressValue = [ this.provinceId + "&" + estateRes.provinceName, @@ -542,6 +548,12 @@ export default { // this.getdefaultAddress(); // this.getAddress(); // this.getLocation(); + // this.$vux.confirm.show({ + // content: "当前小区为:天河区丽江新村,是否切换为:天河区骏景花园", + // confirmText: "确定", + + // onConfirm: () => {} + // }); this.getId(); let eId = this.$route.query.estateId; console.log("eId----" + eId + "estateId---" + this.estateId); @@ -568,20 +580,36 @@ export default { this.showLoading = false; if (isEmpty(res.response)) { //链接小区无效 - this.chooseCommunity(); + this.$vux.alert.show({ + title: + this.defaultCity + + this.defaultDistrict + + "【" + + this.defaultCommunity + + "】" + + "暂无开通服务", + onHide: () => { + this.chooseCommunity(); + } + }); } else { //链接小区有效 var linkEstate = res.response; this.$vux.confirm.show({ title: "当前小区", - content: linkEstate.housingEstateName, - confirmText: "确定", - cancelText: "切换", + content: + linkEstate.cityName + + linkEstate.districtName + + "【" + + linkEstate.housingEstateName + + "】", + confirmText: "切换", + cancelText: "确定", onCancel: () => { - this.chooseCommunity(); + this.defaultLocalEstate(linkEstate); }, onConfirm: () => { - this.defaultLocalEstate(linkEstate); + this.chooseCommunity(); } }); } @@ -590,8 +618,19 @@ export default { this.showLoading = false; }); } else { - this.showLoading = false; - this.chooseCommunity(); + this.$vux.alert.show({ + title: + this.defaultCity + + this.defaultDistrict + + "【" + + this.defaultCommunity + + "】" + + "暂无开通服务", + onHide: () => { + this.showLoading = false; + this.chooseCommunity(); + } + }); } } else { var loclEstate = res.response; @@ -617,10 +656,20 @@ export default { //链接小区有效 var linkEstate = res.response; this.$vux.confirm.show({ - title: "切换小区", - content: linkEstate.housingEstateName, + content: + "当前小区为:" + + this.defaultCity + + this.defaultDistrict + + "【" + + this.defaultCommunity + + "】,是否切换为:" + + linkEstate.cityName + + linkEstate.districtName + + "【" + + linkEstate.housingEstateName + + "】,是否切换为:", confirmText: "不切换", - cancelText: "切换", + cancelText: "确定", onCancel: () => { this.defaultLocalEstate(linkEstate); this.getGoodsList(); @@ -661,16 +710,21 @@ export default { var linkEstate = res.response; this.$vux.confirm.show({ title: "当前小区", - content: linkEstate.housingEstateName, - confirmText: "确定", - cancelText: "切换", + content: + linkEstate.cityName + + linkEstate.districtName + + "【" + + linkEstate.housingEstateName + + "】", + confirmText: "切换", + cancelText: "确定", onCancel: () => { - this.chooseCommunity(); - }, - onConfirm: () => { this.defaultLocalEstate(linkEstate); this.getGoodsList(); - countPartner() + countPartner(); + }, + onConfirm: () => { + this.chooseCommunity(); } }); } @@ -680,8 +734,8 @@ export default { }); } } else { + //this.getData(); this.getLocation(); - this.getData(); } var uuniqueId = this.$route.query.uniqueId;