import wx from 'weixin-js-sdk'
setLocaltion: function () {
var _this = thisvar timer1 = window.setInterval(function () {
const addressItem = sessionStorage.getItem('addressItem')
if (addressItem) {
window.clearTimeout(timer1)
let cityName = JSON.parse(addressItem).detail.addressComponents.city
let cityId = _this.areaList.filter(item => item.name === cityName).map(item => item.id)
if (cityId) {
_this.defaultCity.name = cityName
_this.defaultCity.id = cityId[0]
Toast('定位到' + cityName)
} else {
Toast(cityName + '不在服务范围,已经调整到默认的城市服务')
}
} else {
Toast('未定位……')
}
}, 1000)
},
setLocaltion2: function (_this, addressItem) {
if (addressItem) {
let cityName = addressItem.detail.addressComponents.city
let cityId = _this.areaList.filter(item => item.name === cityName).map(item => item.id)
if (cityId) {
_this.defaultCity.name = cityName
_this.defaultCity.id = cityId[0]
_this.loadDiscountItems()
sessionStorage.setItem('defaultCity', JSON.stringify(_this.defaultCity))
Toast('定位到' + cityName)
} else {
_this.defaultCity.name = cityName
_this.defaultCity.id = cityName
_this.loadDiscountItems()
sessionStorage.setItem('defaultCity', JSON.stringify(_this.defaultCity))
Toast('您所在区域暂未开通该服务,我们会尽快开通,感谢您的关注与支持!')
}
console.info('清除定时器')
} else {
Toast('无定位数据')
}
},
loadAreaList: function () {
let _this = this
const setCityId = function (_this) {
let cityId = _this.areaList.filter(item => item.name === _this.defaultCity.name).map(item => item.id)
if (cityId) {
_this.defaultCity.id = cityId[0]
}
}
const defaultCityItem = sessionStorage.getItem('defaultCity')
if (defaultCityItem) {
this.defaultCity = JSON.parse(defaultCityItem)
}
const areaList = sessionStorage.getItem('areaList')
if (areaList) {
this.areaList = JSON.parse(areaList)
setCityId(_this)
} else {
mineInfoService.loadAreaList().then(res => {
if (res.t) {
this.areaList = res.t
// sessionStorage.setItem('areaList', JSON.stringify(res.t))
this.addressSlot[0].values = res.t.map(a => a.name)
setCityId(_this)
} else {
Toast('地区数据异常')
}
})
}
},
loadJsConfig: function () {
indexService.loadJsConfig(location.href.split('#')[0]).then(res => {
// console.group(res)
wx.config({
debug: false,
appId: res.t.appId,
timestamp: res.t.timestamp,
nonceStr: res.t.nonceStr,
signature: res.t.signature,
jsApiList: ['getLocation',
'checkJsApi']
})
})
},
doWxjsPai: function () {
let _this = this
wx.ready(function () {
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
wx.checkJsApi({
jsApiList: [
'getLocation'
],
success: function (res) {
// alert(JSON.stringify(res));
// alert(JSON.stringify(res.checkResult.getLocation));
if (res.checkResult.getLocation === false) {
alert('你的微信版本太低,不支持微信JS接口,请升级到最新的微信版本!')
return
} else {
const defaultCityItem = sessionStorage.getItem('defaultCity')
if (defaultCityItem) {
// 说明选择了城市
return
}
const addressItem = sessionStorage.getItem('addressItem')
if (!addressItem) {
wx.getLocation({
type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
var latitude = res.latitude // 纬度,浮点数,范围为90 ~ -90
var longitude = res.longitude // 经度,浮点数,范围为180 ~ -180。
// var speed = res.speed // 速度,以米/每秒计
// var accuracy = res.accuracy // 位置精度
var geocoder = new window.qq.maps.Geocoder({
complete: function (result) {
// 缓存地址数据
sessionStorage.setItem('addressItem', JSON.stringify(result))
_this.setLocaltion2(_this, result)
}
})
var latLng = new window.qq.maps.LatLng(latitude, longitude)
geocoder.getAddress(latLng)
},
cancel: function (res) {
alert('用户拒绝授权获取地理位置')
}
})
}
}
}
})
})
wx.error(function (res) {
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
console.error(res)
})
},