officialAccount/unpackage/dist/dev/mp-weixin/pages/map/index.js

296 lines
9.6 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
const common_assets = require("../../common/assets.js");
const request_index = require("../../request/index.js");
const api_main = require("../../api/main.js");
if (!Array) {
const _component_uview = common_vendor.resolveComponent("uview");
_component_uview();
}
const defaultLat = 39.9042;
const defaultLng = 116.4074;
const DEBOUNCE_DELAY = 500;
const _sfc_main = {
__name: "index",
setup(__props) {
common_vendor.ref(null);
const keyword = common_vendor.ref("");
const pois = common_vendor.ref([]);
const openinput = common_vendor.ref(false);
let map = null;
let marker = null;
function initMap(lat, lng) {
const center = new qq.maps.LatLng(lat, lng);
map = new qq.maps.Map(document.getElementById("map"), {
center,
zoom: 15,
zoomControl: false,
// 隐藏放大缩小按钮
panControl: false,
// 隐藏平移控件
mapTypeControl: false
// 隐藏右上角地图切换
});
marker = new qq.maps.Marker({
position: center,
map
});
}
function loadWxJSSDK() {
return new Promise((resolve) => {
if (window.wx && typeof common_vendor.wx$1.config === "function") {
return resolve();
}
const script = document.createElement("script");
script.src = "https://res.wx.qq.com/open/js/jweixin-1.6.0.js";
script.onload = () => {
const checkWx = () => {
if (window.wx && typeof common_vendor.wx$1.config === "function") {
resolve();
} else {
setTimeout(checkWx, 50);
}
};
checkWx();
};
script.onerror = () => {
common_vendor.index.__f__("error", "at pages/map/index.vue:129", "加载微信 JSSDK 脚本失败");
resolve();
};
document.head.appendChild(script);
});
}
async function initLocation() {
await loadWxJSSDK();
try {
const res = await fetch(`${request_index.base_url}/weixin/getJsApiInfo`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
url: location.href.split("#")[0]
})
});
const data = await res.json();
if (window.wx && typeof common_vendor.wx$1.config === "function") {
common_vendor.wx$1.config({
debug: false,
appId: "wx8fc3e4305d2fbf0b",
timestamp: data.timestamp,
nonceStr: data.nonceStr,
signature: data.signature,
jsApiList: ["getLocation", "openLocation"]
});
common_vendor.wx$1.ready(() => {
common_vendor.wx$1.getLocation({
type: "wgs84",
success(ret) {
initMap(ret.latitude, ret.longitude);
fujinGet(ret.latitude, ret.longitude);
},
fail(err) {
common_vendor.index.__f__("warn", "at pages/map/index.vue:172", "微信定位失败,使用默认位置", err);
common_vendor.index.showToast({
title: "定位失败,使用默认位置",
icon: "none"
});
initMap(defaultLat, defaultLng);
fujinGet(defaultLat, defaultLng);
}
});
});
common_vendor.wx$1.error((err) => {
common_vendor.index.__f__("error", "at pages/map/index.vue:185", "wx.config 验证失败:", err);
initMap(defaultLat, defaultLng);
});
} else {
common_vendor.index.__f__("warn", "at pages/map/index.vue:189", "wx.config 不可用,使用默认位置");
initMap(defaultLat, defaultLng);
}
} catch (err) {
common_vendor.index.__f__("error", "at pages/map/index.vue:193", "获取 JSSDK 签名失败:", err);
initMap(defaultLat, defaultLng);
}
}
const dingwei = () => {
common_vendor.wx$1.getLocation({
type: "wgs84",
success(ret) {
initMap(ret.latitude, ret.longitude);
fujinGet(ret.latitude, ret.longitude);
},
fail(err) {
common_vendor.index.__f__("warn", "at pages/map/index.vue:206", "微信定位失败,使用默认位置", err);
common_vendor.index.showToast({
title: err.errMsg,
icon: "none"
});
initMap(defaultLat, defaultLng);
fujinGet(defaultLat, defaultLng);
}
});
};
const jumpBack = () => {
common_vendor.index.navigateBack();
};
const jumpBackValue = () => {
const payload = {
name: pois.value[listTarget.value].name,
lat: pois.value[listTarget.value].lat,
lng: pois.value[listTarget.value].lng
};
common_vendor.index.setStorageSync("dingwei", payload);
common_vendor.index.navigateBack();
};
async function onSearch() {
const kw = keyword.value.trim();
if (!kw) {
common_vendor.index.showToast({
title: "请输入搜索内容",
icon: "none"
});
return;
}
pois.value = [];
const center = map.getCenter();
const lat = center.getLat();
const lng = center.getLng();
let inIt = {
apiUrl: `https://apis.map.qq.com/ws/place/v1/search`,
other: `keyword=${encodeURIComponent(kw)}%26boundary=nearby(${lat},${lng},1000)%26key=LOLBZ-Z2PKW-JJ6RO-3Y7Z7-BSKWT-DLFNC`
};
api_main.proxy(inIt).then((data) => {
if (data.status === 0 && data.data.length) {
data.data.map((item) => {
let element = {
name: item.title,
juli: `${item._distance}km`,
where: item.address,
lat: item.location.lat,
lng: item.location.lng
};
pois.value.push(element);
});
} else {
common_vendor.index.showToast({
title: "未搜索到结果",
icon: "none"
});
}
});
}
const fujinGet = (lat, lng) => {
let inIt = {
apiUrl: `https://apis.map.qq.com/ws/geocoder/v1`,
other: `location=${lat},${lng}%26key=LOLBZ-Z2PKW-JJ6RO-3Y7Z7-BSKWT-DLFNC%26get_poi=1%26radius=1000%26poi_options=page_size=10;radius=1000;policy=distance;address_format=short`
};
api_main.proxy(inIt).then((data) => {
if (data.status === 0) {
pois.value = [];
data.result.pois.map((item) => {
let element = {
name: item.title,
juli: `${item._distance}km`,
where: item.address,
lat: item.location.lat,
lng: item.location.lng
};
pois.value.push(element);
});
} else {
common_vendor.index.showToast({
title: "未搜索到结果",
icon: "none"
});
}
});
};
const listTarget = common_vendor.ref(0);
function selectPoi(poi, ids) {
listTarget.value = ids;
const pos = new qq.maps.LatLng(poi.lat, poi.lng);
map.setCenter(pos);
marker.setPosition(pos);
}
const onMapDragCancel = () => {
const center = map.getCenter();
const lat = center.getLat();
const lng = center.getLng();
listTarget.value = 0;
fujinGet(lat, lng);
};
const query = common_vendor.ref("");
let timerId = null;
function onInput(e) {
var _a;
query.value = ((_a = e.detail) == null ? void 0 : _a.value) ?? e.target.value;
if (timerId) {
clearTimeout(timerId);
}
if (/[A-Za-z]/.test(query.value)) {
return;
}
timerId = setTimeout(() => {
const val = query.value.trim();
if (val) {
onSearch();
}
}, DEBOUNCE_DELAY);
}
const close = () => {
openinput.value = false;
listTarget.value = -1;
if (!pois.value.length) {
onMapDragCancel();
}
};
common_vendor.onMounted(async () => {
await initLocation();
});
common_vendor.onBeforeUnmount(() => {
if (timerId) {
clearTimeout(timerId);
}
});
return (_ctx, _cache) => {
return {
a: common_assets._imports_0$7,
b: common_vendor.o(jumpBack),
c: common_vendor.o(jumpBackValue),
d: listTarget.value != -1,
e: listTarget.value == -1,
f: common_assets._imports_1$7,
g: common_vendor.o(onMapDragCancel),
h: common_assets._imports_2$4,
i: common_vendor.o(dingwei),
j: common_assets._imports_3$2,
k: common_vendor.o(($event) => {
openinput.value = true;
pois.value = [];
listTarget.value = -1;
}),
l: !openinput.value,
m: common_vendor.o([($event) => keyword.value = $event.detail.value, onInput]),
n: keyword.value,
o: common_vendor.o(close),
p: openinput.value,
q: common_vendor.f(pois.value, (poi, idx, i0) => {
return common_vendor.e({
a: common_vendor.t(poi.name),
b: common_vendor.t(poi.juli),
c: common_vendor.t(poi.where),
d: idx == listTarget.value
}, idx == listTarget.value ? {} : {}, {
e: idx,
f: common_vendor.o(($event) => selectPoi(poi, idx), idx)
});
})
};
};
}
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-a3704cdf"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/map/index.js.map