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 QuickQuestions from "./components/QuickQuestions/index.vue";
import discoveryCover from "@/components/ImageSwiper/images/2025-07-12_180248.jpg"; import discoveryCover from "@/components/ImageSwiper/images/2025-07-12_180248.jpg";
import { homeTabsData, homeTabContentData, homeQuickQuestionData } from "../../request/api/MainPageDataApi"; import { homeTabsData, getNearbyTags, homeTabContentData, homeQuickQuestionData } from "../../request/api/MainPageDataApi";
import { useAppStore } from "@/store"; import { useAppStore, useLocationStore } from "@/store";
import { JumpType } from "../../model/ChatModel"; import { JumpType } from "../../model/ChatModel";
const appStore = useAppStore(); const appStore = useAppStore();
const locationStore = useLocationStore();
/// 从个渠道获取如二维码 /// 从个渠道获取如二维码
const sceneId = appStore.sceneId || ""; const sceneId = appStore.sceneId || "";
@@ -58,15 +60,20 @@ const queryTabsList = async () => {
})); }));
/// 设置tab数据 /// 设置tab数据
discoveryTabs.value = tabList; discoveryTabs.value = tabList;
/// 根据sceneId查询对应tab数据
/// 查询是否有sceneId参数 findTabByIdWithActiveTabIndex(sceneId);
const activeTabIndex = discoveryTabs.value.findIndex((tab) => tab.id === sceneId); }
}
/// 如果有则优先展示对应tab数据没有则展示第一个tab数据 /// 根据id查询tab并设置activeIndex
if (activeTabIndex > -1) { const findTabByIdWithActiveTabIndex = (tabsId) => {
activeIndex.value = activeTabIndex; /// 查询是否有id参数
queryDiscoveryData(sceneId); const activeTabIndex = discoveryTabs.value.findIndex((tab) => tab.id === tabsId);
} else { /// 如果有则优先展示对应tab数据没有则展示第一个tab数据
if (activeTabIndex > -1) {
activeIndex.value = activeTabIndex;
queryDiscoveryData(tabsId);
} else {
if (discoveryTabs.value.length > 0) {
activeIndex.value = 0; activeIndex.value = 0;
queryDiscoveryData(discoveryTabs.value[0].id); 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(() => { onMounted(() => {
queryTabsList(); queryTabsList();
getLocation();
}); });
</script> </script>

View File

@@ -21,7 +21,7 @@
<script setup> <script setup>
import { onLoad } from "@dcloudio/uni-app"; import { onLoad } from "@dcloudio/uni-app";
import { ref, onUnmounted } from "vue"; import { ref, onUnmounted, onMounted } from "vue";
import { getUrlParams } from "@/utils/UrlParams"; import { getUrlParams } from "@/utils/UrlParams";
import { useAppStore } from "@/store"; import { useAppStore } from "@/store";
import { checkToken } from "@/hooks/useGoLogin"; import { checkToken } from "@/hooks/useGoLogin";
@@ -31,7 +31,7 @@ import DrawerSection from "../DrawerSection/index.vue";
import Calender from "@/components/Calender/index.vue"; import Calender from "@/components/Calender/index.vue";
const appStore = useAppStore(); const appStore = useAppStore();
const locationStore = useLocationStore();
const calendarVisible = ref(false); const calendarVisible = ref(false);
const selectedDate = ref(""); const selectedDate = ref("");
@@ -52,14 +52,6 @@ uni.$on("openCalendar", () => {
calendarVisible.value = true; 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); const drawerRef = ref(null);
@@ -68,19 +60,32 @@ const showDrawer = async (e) => {
drawerRef.value.open(); drawerRef.value.open();
}; };
uni.$on("SHOW_DRAWER", showDrawer); uni.$on("SHOW_DRAWER", showDrawer);
// 关闭窗口 // 关闭窗口
const closeDrawer = (e) => drawerRef.value.close(); 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) => { onLoad((e) => {
getWeixinMiniProgramParams(e); getWeixinMiniProgramParams(e);
}); });
onMounted(() => {
});
onUnmounted(() => { onUnmounted(() => {
// uni.$off('openCalendar') // uni.$off('openCalendar')
}); });
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@@ -10,6 +10,11 @@ function homeTabContentData(args) {
return request.post("/hotelBiz/mainScene/queryContentListWithCache", args); return request.post("/hotelBiz/mainScene/queryContentListWithCache", args);
} }
/// 获取距离用户最近的标签
function getNearbyTags(args) {
return request.get("/hotelBiz/mainScene/nearestTag", args);
}
/// 获取本地天气 /// 获取本地天气
function getLocalWeather(args) { function getLocalWeather(args) {
return request.get("/hotelBiz/mainScene/getLocalWeather", args); return request.get("/hotelBiz/mainScene/getLocalWeather", args);
@@ -44,6 +49,7 @@ function discoveryCradComponent() {
export { export {
homeTabsData, homeTabsData,
homeTabContentData, homeTabContentData,
getNearbyTags,
getLocalWeather, getLocalWeather,
getTimeNoticeList, getTimeNoticeList,
homeQuickQuestionData, homeQuickQuestionData,

View File

@@ -1,14 +1,14 @@
import { getCurrentConfig } from "@/constant/base"; import { getCurrentConfig } from "@/constant/base";
import { useAppStore } from "@/store"; import { useAppStore, useLocationStore } from "@/store";
import { NOTICE_EVENT_LOGOUT } from "@/constant/constant"; import { NOTICE_EVENT_LOGOUT } from "@/constant/constant";
import { getAccessToken } from "@/constant/token"; import { getAccessToken, removeAccessToken } from "@/constant/token";
import { goLogin } from "../../hooks/useGoLogin"; import { goLogin } from "../../hooks/useGoLogin";
const clientId = getCurrentConfig().clientId; const clientId = getCurrentConfig().clientId;
const defaultConfig = { const defaultConfig = {
header: { header: {
Authorization: "", // 可在此动态设置 token
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: "", // 可在此动态设置 token
clientId: clientId, clientId: clientId,
}, },
}; };
@@ -19,6 +19,17 @@ function request(url, args = {}, method = "POST", customConfig = {}) {
if (!/^http/.test(url)) { if (!/^http/.test(url)) {
url = appStore.serverConfig?.baseUrl + url; url = appStore.serverConfig?.baseUrl + url;
} }
// 获取位置信息并添加到请求头
const locationStore = useLocationStore();
if (locationStore.latitude && locationStore.longitude) {
customConfig.header = {
...customConfig.header,
"X-Latitude": locationStore.latitude,
"X-Longitude": locationStore.longitude
};
}
// 动态获取 token // 动态获取 token
const token = getAccessToken(); const token = getAccessToken();

View File

@@ -1,5 +1,6 @@
import { useAppStore } from "./app"; import { useAppStore } from "./app";
import { useSelectedDateStore } from "./selectedDate"; import { useSelectedDateStore } from "./selectedDate";
import { usePictureStore } from "./picture"; import { usePictureStore } from "./picture";
import { useLocationStore } from "./location";
export { useAppStore, useSelectedDateStore, usePictureStore }; export { useAppStore, useSelectedDateStore, usePictureStore, useLocationStore };

View File

@@ -0,0 +1,19 @@
import { defineStore } from "pinia";
export const useLocationStore = defineStore("location", {
state() {
return {
latitude: 0, // 纬度
longitude: 0, // 经度
};
},
actions: {
setLocationData(data) {
this.latitude = data.latitude;
this.longitude = data.longitude;
},
},
unistorage: true,
});