From fad125d84328988cba9fe8fe00d164d10ecd2b79 Mon Sep 17 00:00:00 2001 From: huangchenhao <123673748@qq.com> Date: Thu, 20 Nov 2025 11:16:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BD=BF=E7=94=A8=E9=AB=98?= =?UTF-8?q?=E5=BE=B7=E5=9C=B0=E5=9B=BEApi=E8=8E=B7=E5=8F=96=E5=AE=9A?= =?UTF-8?q?=E4=BD=8D=E5=9D=90=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/mobile/index.html | 34 ++++++--- .../src/views/Material/MaterialManagement.vue | 75 +++++++++---------- packages/shared/utils/aMap.js | 24 ++++++ 3 files changed, 81 insertions(+), 52 deletions(-) create mode 100644 packages/shared/utils/aMap.js diff --git a/packages/mobile/index.html b/packages/mobile/index.html index c408d1e..d0c1457 100644 --- a/packages/mobile/index.html +++ b/packages/mobile/index.html @@ -1,14 +1,24 @@ - - - - - - H5移动端 - - -
- - - + + + + + + + H5移动端 + + + + + +
+ + + + \ No newline at end of file diff --git a/packages/mobile/src/views/Material/MaterialManagement.vue b/packages/mobile/src/views/Material/MaterialManagement.vue index 4f4ab58..97dab2c 100644 --- a/packages/mobile/src/views/Material/MaterialManagement.vue +++ b/packages/mobile/src/views/Material/MaterialManagement.vue @@ -211,6 +211,7 @@ import { ref, onMounted, reactive, toRaw, watch } from "vue"; import { useRouter, useRoute } from "vue-router"; import { showToast, showLoadingToast } from "vant"; import { request } from "../../../../shared/utils/request"; +import { loadAMap } from "../../../../shared/utils/amap"; const router = useRouter(); const route = useRoute(); @@ -218,7 +219,7 @@ const searchValue = ref(""); // 搜索框输入值 const showPopup = ref(false); // 控制弹出层显示隐藏 const yhzDetail = ref({}); // 养护站详情数据 const materialList = ref([]); // 物资列表数据 -const INIT_FORM = { +const getInitForm = () => ({ material: { jd: "", // 物资经度 wd: "", // 物资纬度 @@ -238,8 +239,8 @@ const INIT_FORM = { remark: "", // 备注 }, photos: [], -}; -const form = reactive({ ...INIT_FORM }); // 表单 +}); +const form = reactive(getInitForm()); // 根据养护站rid获取物资列表 const getMaterialList = async (wzmc) => { @@ -281,15 +282,6 @@ onMounted(() => { // 购置日期相关 const showTimePicker = ref(false); -const currentDate = ref([ - new Date().getFullYear(), - new Date().getMonth() + 1, - new Date().getDate(), -]); -const onDateConfirm = ({ selectedValues }) => { - form.rkrq = selectedValues.join("-"); - showTimePicker.value = false; -}; // 选择单位相关 const dwField = ref(null); @@ -338,7 +330,6 @@ const handleSubmit = async () => { message: "新增成功", }); onPopupClose(); - Object.assign(form, { ...INIT_FORM }); getMaterialList(searchValue.value); } else { throw new Error(res.message); @@ -423,37 +414,38 @@ const afterRead = async (file) => { }; // 获取经纬度 -const handleGetLocation = () => { - if (!navigator.geolocation) { - showToast("您的浏览器不支持地理位置获取"); - return; +const handleGetLocation = async () => { + // 确保AMap完成加载 + if (!window.AMap) { + await loadAMap(); } - - showLoadingToast({ - message: "定位中...", + const loadingToast = showLoadingToast({ + message: "正在获取位置", forbidClick: true, + duration: 0, // 设置为0表示不会自动关闭 + zIndex: 9999, }); - navigator.geolocation.getCurrentPosition( - (position) => { - form.material.jd = position.coords.longitude.toFixed(6); - form.material.wd = position.coords.latitude.toFixed(6); - }, - (error) => { - const errorMessage = - { - 1: "位置服务被拒绝", - 2: "暂时无法获取位置", - 3: "定位超时", - }[error.code] || "定位失败"; - showToast(errorMessage); - }, - { - enableHighAccuracy: true, // 高精度模式 - timeout: 5000, // 超时时间 - maximumAge: 0, // 不缓存位置 - } - ); + AMap.plugin("AMap.Geolocation", () => { + const geolocation = new AMap.Geolocation({ + enableHighAccuracy: true, + timeout: 5000, + showMarker: false, + zoomToAccuracy: true, + }); + + geolocation.getCurrentPosition((status, result) => { + if (status === "complete") { + form.material.jd = result.position.lng.toFixed(6); + form.material.wd = result.position.lat.toFixed(6); + loadingToast.close(); + } else { + loadingToast.close(); + showToast("定位失败 请检查网络情况后重试"); + console.log("result", result); + } + }); + }); }; watch( @@ -505,6 +497,9 @@ const handleAdd = async () => { }; const onPopupClose = () => { + Object.assign(form, getInitForm()); + fileList.value = []; + [showDwPicker, showFzrPicker].forEach((v) => (v.value = false)); showPopup.value = false; }; diff --git a/packages/shared/utils/aMap.js b/packages/shared/utils/aMap.js new file mode 100644 index 0000000..62f0251 --- /dev/null +++ b/packages/shared/utils/aMap.js @@ -0,0 +1,24 @@ +export function loadAMap() { + return new Promise((resolve, reject) => { + if (window.AMap) return resolve(); + + // 确保AMapLoader存在 + if (!window.AMapLoader) { + return reject(new Error("AMapLoader未正确加载")); + } + + window.AMapLoader.load({ + key: "848ab05db2a57a7782c153119f50dcef", + version: "2.0", + }) + .then((AMap) => { + window.AMap = AMap; + console.log("AMap初始化完成", AMap); + resolve(); + }) + .catch((err) => { + console.error("AMap加载失败:", err); + reject(new Error("地图加载失败,请检查网络连接")); + }); + }); +}; \ No newline at end of file