我需要制作一个微信公众号获取用户定位功能,首先进行微信校验时在进行微信配置时没有反应,没有进入成功回调,已查参数齐全
api.getWxTicket({code: getCode()}).then(response => {
wx.config({ // eslint-disable-line no-undef
appId: response.resultData.appId,
timestamp: response.resultData.timestamp,
noncestr: response.resultData.noncestr,
signature: response.resultData.signature,
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
jsApiList: ['checkJsApi', 'openLocation', 'getLocation'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
})
wx.error(function (res) { // eslint-disable-line no-undef
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
alert('验证出错')
})
wx.ready(function () { // eslint-disable-line no-undef
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
wx.getlocation({ // eslint-disable-line no-undef
success: function (res) {
this.latitude = res.latitude // 纬度,浮点数,范围为90 ~ -90
this.longitude = res.longitude // 经度,浮点数,范围为180 ~ -180。
alert(this.latitude)
alert(this.longitude)
},
cancel: function (res) {
alert('未能获取地理位置')
}
})
})
if (this.latitude && this.longitude) {
api.location({latitude: this.latitude, longitude: this.longitude}).then(response => {
this.type = response.resultData
}).catch(error => {
if (error) console.error(error)
})
}
})
}
}
}
</script>```