feat: 增加获取位置的方法

This commit is contained in:
2026-05-08 14:58:13 +08:00
parent a3c82382c2
commit 25c8a3570d
6 changed files with 103 additions and 27 deletions

View File

@@ -29,10 +29,12 @@ import CardSwiper from "./components/CardSwiper/index.vue";
import QuickQuestions from "./components/QuickQuestions/index.vue";
import discoveryCover from "@/components/ImageSwiper/images/2025-07-12_180248.jpg";
import { homeTabsData, homeTabContentData, homeQuickQuestionData } from "../../request/api/MainPageDataApi";
import { useAppStore } from "@/store";
import { homeTabsData, getNearbyTags, homeTabContentData, homeQuickQuestionData } from "../../request/api/MainPageDataApi";
import { useAppStore, useLocationStore } from "@/store";
import { JumpType } from "../../model/ChatModel";
const appStore = useAppStore();
const locationStore = useLocationStore();
/// 从个渠道获取如二维码
const sceneId = appStore.sceneId || "";
@@ -58,15 +60,20 @@ const queryTabsList = async () => {
}));
/// 设置tab数据
discoveryTabs.value = tabList;
/// 查询是否有sceneId参数
const activeTabIndex = discoveryTabs.value.findIndex((tab) => tab.id === sceneId);
/// 如果有则优先展示对应tab数据没有则展示第一个tab数据
if (activeTabIndex > -1) {
activeIndex.value = activeTabIndex;
queryDiscoveryData(sceneId);
} else {
/// 根据sceneId查询对应tab数据
findTabByIdWithActiveTabIndex(sceneId);
}
}
/// 根据id查询tab并设置activeIndex
const findTabByIdWithActiveTabIndex = (tabsId) => {
/// 查询是否有id参数
const activeTabIndex = discoveryTabs.value.findIndex((tab) => tab.id === tabsId);
/// 如果有则优先展示对应tab数据没有则展示第一个tab数据
if (activeTabIndex > -1) {
activeIndex.value = activeTabIndex;
queryDiscoveryData(tabsId);
} else {
if (discoveryTabs.value.length > 0) {
activeIndex.value = 0;
queryDiscoveryData(discoveryTabs.value[0].id);
}
@@ -145,10 +152,37 @@ const handleClick = async (item) => {
}
}
/// 获取位置信息
const getLocation = () => {
/// 已经有sceneId了说明之前已经获取过位置信息了就不需要再获取一次了
if (sceneId) return;
uni.getLocation({
type: 'wgs84',
success: function (res) {
// 将位置信息存储到 Pinia 中
locationStore.setLocationData({
latitude: res.latitude,
longitude: res.longitude,
});
console.log('当前位置:' + JSON.stringify(res));
getNearbyTagsData();
}
});
}
/// 获取附近标签数据
const getNearbyTagsData = async () => {
const res = await getNearbyTags();
if (res.code === 0) {
const nearbyTagId = res.data;
findTabByIdWithActiveTabIndex(nearbyTagId);
}
}
/// 组件挂载后请求数据
onMounted(() => {
queryTabsList();
getLocation();
});
</script>

View File

@@ -21,7 +21,7 @@
<script setup>
import { onLoad } from "@dcloudio/uni-app";
import { ref, onUnmounted } from "vue";
import { ref, onUnmounted, onMounted } from "vue";
import { getUrlParams } from "@/utils/UrlParams";
import { useAppStore } from "@/store";
import { checkToken } from "@/hooks/useGoLogin";
@@ -31,7 +31,7 @@ import DrawerSection from "../DrawerSection/index.vue";
import Calender from "@/components/Calender/index.vue";
const appStore = useAppStore();
const locationStore = useLocationStore();
const calendarVisible = ref(false);
const selectedDate = ref("");
@@ -52,14 +52,6 @@ uni.$on("openCalendar", () => {
calendarVisible.value = true;
});
const getWeixinMiniProgramParams = (e) => {
console.log("Params:", e);
if (e.q && e.q != "undefined") {
const qrUrl = decodeURIComponent(e.q); // 获取到二维码原始链接内容
const params = getUrlParams(qrUrl);
appStore.setSceneId(params.sceneId || params.tagId);
}
};
// 打开窗口
const drawerRef = ref(null);
@@ -68,19 +60,32 @@ const showDrawer = async (e) => {
drawerRef.value.open();
};
uni.$on("SHOW_DRAWER", showDrawer);
// 关闭窗口
const closeDrawer = (e) => drawerRef.value.close();
///获取到二维码原始链接内容
const getWeixinMiniProgramParams = (e) => {
console.log("Params:", e);
if (e.q && e.q != "undefined") {
const qrUrl = decodeURIComponent(e.q);
const params = getUrlParams(qrUrl);
appStore.setSceneId(params.sceneId || params.tagId);
}
};
onLoad((e) => {
getWeixinMiniProgramParams(e);
});
onMounted(() => {
});
onUnmounted(() => {
// uni.$off('openCalendar')
});
</script>
<style lang="scss" scoped>