index.vue 1.9 KB
<template>
  <div class="md:p-10 p-4 pt-0" style="min-height: calc(100vh - 320px)">
    <HomeRecommend
      :recommendList="list"
      :navTitle="findLabelByAlias(name as string, sortList)"
      navIcon="tag"
    />
    <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";
const sortList = useState<classifyType[]>("sortTree");
const route = useRoute();
const router = useRouter();
const { name } = route.params;
const list = ref<appType[]>([]);
const total = ref<number>(0);
const params = ref<any>({
  pageNum: 1,
  pageSize: 5,
  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 "";
  }

  // 1. 首先在当前层级查找
  for (const item of data) {
    if (item.alias === alias) {
      return item.label || ""; // 返回 label 或空字符串
    }
  }

  // 2. 如果当前层级没找到,递归查找子节点
  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 "";
}

function onPageChange(pageNum: number) {
  router.push({
    path: route.path + "/page/" + pageNum,
  });
}

const res = await getAppList(params.value);
list.value = res.rows;
total.value = res.total;
</script>