[pageNum].vue 3.0 KB
<template>
  <div class="p-10">
    <!-- 推荐工具区 -->
    <HomeRecommend
      :recommendList="list"
      :navTitle="findLabelByAlias(name as string, sortList)"
      navIcon="tag"
      :navTitleWidth="120.5"
    />
    <el-pagination
      background
      layout="prev, pager, next"
      :hide-on-single-page="true"
      v-model:page-size="params.pageSize"
      v-model:current-page="params.pageNum"
      :total="total"
      @current-change="onPageChange"
    />
  </div>
</template>

<script lang="ts" setup>
import type { appType } from "~/api/types/app";
import { getAppList } from "~/api/app";
import type { classifyType } from "~/api/types/classify";
import type { webSiteType } from "~/api/types/webSite";
const sortList = useState<classifyType[]>("sortTree");
const webSite = useState<webSiteType>("webSite");
const route = useRoute();
const router = useRouter();
const config = useRuntimeConfig();
const { pageNum, name } = route.params;
const list = ref<appType[]>([]);
const total = ref<number>(0);
const params = ref<any>({
  pageNum: Number(pageNum),
  pageSize: 30,
  typeAlias: name as string,
});

function findLabelByAlias<
  T extends { alias?: string; label?: string; children?: T[] }
>(alias: string, data: T[], childrenKey: string = "children"): string {
  if (!data || data.length === 0) {
    return "";
  }

  for (const item of data) {
    if (item.alias === alias) {
      return item.label || "";
    }
  }

  for (const item of data) {
    const children = (item as any)[childrenKey] as T[];
    if (children && children.length > 0) {
      const foundLabel = findLabelByAlias(alias, children, childrenKey);
      if (foundLabel !== "") {
        return foundLabel;
      }
    }
  }

  return "";
}

const categoryLabel = findLabelByAlias(name as string, sortList.value);

function onPageChange(pageNum: number) {
  if (pageNum === 1) {
    router.push({
      path: "/category/" + name,
    });
  } else if (pageNum > 1) {
    router.push({
      path: `/category/${name}/page/${pageNum}`,
    });
  }
}

const res = await getAppList(params.value);
list.value = res.rows;
total.value = res.total;

useHead({
  title: `${categoryLabel} - 第${pageNum}页 - ${webSite.value.webname}`,
  meta: [
    {
      name: "description",
      content: `${categoryLabel}分类下的AI工具推荐第${pageNum}页,精选优质${categoryLabel}相关AI工具。`,
    },
    {
      name: "keywords",
      content: `${categoryLabel},AI工具,${categoryLabel}AI,人工智能,第${pageNum}页`,
    },
    { name: "robots", content: "index, follow" },
    {
      property: "og:title",
      content: `${categoryLabel} - 第${pageNum}页 - ${webSite.value.webname}`,
    },
    {
      property: "og:description",
      content: `${categoryLabel}分类下的AI工具推荐第${pageNum}页。`,
    },
    { property: "og:type", content: "website" },
    { property: "og:url", content: config.public.baseUrl + route.fullPath },
    { property: "og:site_name", content: webSite.value.webname },
  ],
  link: [{ rel: "canonical", href: config.public.baseUrl + route.fullPath }],
});
</script>