You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
2.9 KiB

import Vue from "vue";
import wx from "weixin-js-sdk";
import axios from "axios";
import wxShareModel from "../models/wxShare-model.js";
//引入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;
}
}
});
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:'+data.result);
}
});
},
cancel: function () {
alert("拒绝定位");
}
});
// } else {
// }
// alert("lat:" + localStorage.getItem("latitude") + ";lng:" + localStorage.getItem("longitude"));
wx.error(function (res) {
alert("wx-js初始化: " + res);
});
});
});
}