| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <template>
- <div class="guide-pages">
- <div class="guide-bos">
- <div class="guide-info">
- <div class="title">{{ dataInfo.title }}</div>
- <div class="time">{{ dataInfo.releaseTime }}</div>
- <div v-if="dataInfo.content" class="guide-html" v-html="dataInfo.content"></div>
- </div>
- <div class="guide-box">
- <div class="flex-row-between related-title">
- <div class="fw-[600]">相关专题</div>
- <div class="flex-row-start related-huan" @click="getList">
- <el-icon><Sort /></el-icon>
- <div style="margin-left: 4px">换一换</div>
- </div>
- </div>
- <div class="data-bos">
- <div v-for="(item, index) in dataList" :key="index" class="data-list" @click="onPath('/plan_info/guide?id=' + item.id)">
- <el-image class="data-img" :src="item.coverImage" fit="cover" />
- <div class="procure1">{{ item.title }}</div>
- <div class="procure2">{{ item.releaseTime }}</div>
- </div>
- </div>
- <div class="flex-row-center w100% no-data" v-if="dataList.length === 0">
- <el-empty description="暂无数据" />
- </div>
- </div>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { onMounted, ref, watch } from 'vue'; // 引入 watch
- import { useRoute, onBeforeRouteUpdate } from 'vue-router';
- import { getPurchaseGuideDetail, getPurchaseGuideList } from '@/api/plan/index';
- import { onPath } from '@/utils/siteConfig';
- const route = useRoute();
- const id = ref<any>(null);
- const dataInfo = ref<any>({});
- const dataList = ref<any>([]);
- // 封装获取详情的逻辑
- const getInfo = (currentId: any) => {
- if (!currentId) return;
- // 可选:添加 loading 状态
- // isLoading.value = true;
- getPurchaseGuideDetail(currentId)
- .then((res) => {
- if (res.code == 200) {
- dataInfo.value = res.data;
- }
- })
- .finally(() => {
- // isLoading.value = false;
- });
- };
- // 封装获取列表的逻辑
- const getList = () => {
- getPurchaseGuideList({
- pageSize: 10,
- pageNum: 1
- }).then((res) => {
- if (res.code == 200) {
- if (res.rows && res.rows.length > 0) {
- // 保持原有的随机逻辑
- if (res.rows.length <= 2) {
- dataList.value = res.rows;
- } else {
- dataList.value = getRandomElements(res.rows, 3);
- }
- }
- }
- });
- };
- // 核心初始化函数
- const initData = () => {
- // 1. 获取最新 ID
- const currentId = route.query.id;
- id.value = currentId;
- // 2. 请求详情 (传入当前 ID,避免依赖 ref 的异步更新问题)
- getInfo(currentId);
- // 3. 请求列表 (通常列表不需要随 ID 变化,但如果需求是每次换一换,则保留;如果只需加载一次,可移出)
- // 注意:原代码中点击“换一换”也会调用 getList,所以这里可以只加载一次,或者每次都刷新看需求
- // 如果希望每次进入页面都刷新右侧推荐列表,保留此行;否则建议只在 onMounted 中调用一次
- getList();
- };
- onMounted(() => {
- initData();
- });
- // 监听路由查询参数变化
- watch(
- () => route.query.id,
- (newId, oldId) => {
- if (newId !== oldId) {
- initData();
- }
- }
- );
- // 兼容 onBeforeRouteUpdate (作为双重保险)
- onBeforeRouteUpdate((to, from, next) => {
- // 如果 watch 已经处理了,这里可以只做 next()
- // 但为了确保万无一失,可以再次调用,或者仅在此处处理一些 watch 无法覆盖的边缘情况
- if (to.query.id !== from.query.id) {
- // 注意:如果在 watch 中已经调用了 initData,这里可能会重复请求。
- // 建议二选一:推荐使用 watch 监听 query 变化,更符合 Vue 3 组合式 API 习惯。
- // 如果保留此钩子,请确保不要与 watch 冲突,或者在此处直接调用逻辑并 next()
- }
- next();
- });
- function getRandomElements(arr: any, count: any) {
- if (count > arr.length) {
- throw new Error('要取的数量不能超过数组长度');
- }
- const copy = [...arr];
- const result = [];
- for (let i = 0; i < count; i++) {
- const randomIndex = Math.floor(Math.random() * copy.length);
- result.push(copy.splice(randomIndex, 1)[0]);
- }
- return result;
- }
- </script>
- <style lang="scss" scoped>
- .guide-pages {
- width: 100%;
- .guide-bos {
- width: 100%;
- max-width: 1500px;
- min-width: 1200px;
- margin: 0 auto;
- padding-bottom: 30px;
- display: flex;
- gap: 20px;
- position: relative;
- .guide-info {
- min-height: calc(100vh - 280px);
- background: #ffffff;
- border-radius: 5px;
- padding: 20px;
- flex: 1;
- .title {
- font-size: 20px;
- color: #666666;
- }
- .time {
- font-size: 14px;
- color: #999999;
- margin: 10px 0 18px 0;
- }
- .guide-html {
- width: 100%;
- :deep(img) {
- width: 100%;
- display: block;
- margin: 0;
- padding: 0;
- }
- :deep(p) {
- margin: 0;
- padding: 0;
- }
- :deep(*) {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
- }
- }
- .guide-box {
- width: 400px;
- position: sticky;
- top: 0;
- .related-title {
- font-size: 15px;
- background-color: #ffffff;
- border-radius: 5px;
- padding: 12px 20px;
- .related-huan {
- font-size: 14px;
- cursor: pointer;
- &:hover {
- color: #e7000b !important;
- }
- }
- }
- .data-bos {
- width: 100%;
- display: flex;
- flex-direction: column;
- margin-top: 12px;
- gap: 12px 0px;
- .data-list {
- width: 100%;
- // height: 268px;
- background-color: #ffffff;
- cursor: pointer;
- border-radius: 5px;
- overflow: hidden;
- transition:
- transform 0.2s ease,
- box-shadow 0.2s ease;
- display: flex;
- flex-direction: column;
- padding-bottom: 15px;
- &:hover {
- transform: translateY(-2px);
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
- .procure1 {
- color: #e7000b !important;
- }
- }
- .data-img {
- width: 100%;
- aspect-ratio: 393 / 220;
- // height: 200px;
- }
- .procure1 {
- padding: 12px 20px 4px 20px;
- font-weight: 600;
- font-size: 14px;
- color: #101828;
- }
- .procure2 {
- padding-left: 20px;
- font-size: 12px;
- color: #364153;
- }
- }
- }
- .no-data {
- background-color: #ffffff;
- border-radius: 5px;
- margin-top: 10px;
- }
- }
- }
- }
- </style>
|