home.vue 4.3 KB
Newer Older
yubinCloud's avatar
yubinCloud 已提交
1
<template>
2 3 4 5 6
  <a-layout>
    <a-layout-sider width="200" style="background: #fff">
      <a-menu
          mode="inline"
          :style="{ height: '100%', borderRight: 0 }"
7
          @click="handleClick"
8
      >
9
        <a-menu-item key="welcome">
10 11
          <MailOutlined />
          <span>欢迎</span>
12
        </a-menu-item>
yubinCloud's avatar
yubinCloud 已提交
13
        <a-sub-menu v-for="item in level1" :key="item.id">
14
          <template v-slot:title>
yubinCloud's avatar
yubinCloud 已提交
15
            <span><user-outlined />{{ item.name }}</span>
16
          </template>
17
          <a-menu-item v-for="child in item.children" :key="child.id">
yubinCloud's avatar
yubinCloud 已提交
18
            <MailOutlined /><span>{{ child.name }}</span>
19
          </a-menu-item>
20 21 22 23 24 25
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >
26
      <div class="welcome" v-show="isShowWelcome">
27
        <h1>欢迎进入 Fairy Wiki</h1>
28
      </div>
29 30
      <a-list v-show="!isShowWelcome" item-layout="vertical" size="large"
              :grid="{ gutter: 20, column: 3 }" :data-source="ebooks">
31 32 33
        <template #renderItem="{ item }">
          <a-list-item key="item.name">
            <template #actions>
34 35 36 37 38 39 40 41 42 43 44
              <span>
                <component v-bind:is="'FileOutlined'" style="margin-right: 8px" />
                {{ item.docCount }}
              </span>
              <span>
                <component v-bind:is="'UserOutlined'" style="margin-right: 8px" />
                {{ item.viewCount }}
              </span>
              <span>
                <component v-bind:is="'LikeOutlined'" style="margin-right: 8px" />
                {{ item.voteCount }}
45 46 47 48 49 50 51 52 53 54 55 56 57
              </span>
            </template>
            <a-list-item-meta :description="item.description">
              <template #title>
                <router-link :to="'/doc?ebookId=' + item.id">
                  {{ item.name }}
                </router-link>
              </template>
              <template #avatar><a-avatar :src="item.cover"/></template>
            </a-list-item-meta>
          </a-list-item>
        </template>
      </a-list>
58 59
    </a-layout-content>
  </a-layout>
yubinCloud's avatar
yubinCloud 已提交
60 61 62
</template>

<script lang="ts">
63
import {defineComponent, onMounted, ref} from 'vue';
64
import axios from 'axios';
65
import { StarOutlined, LikeOutlined, MessageOutlined } from '@ant-design/icons-vue';
yubinCloud's avatar
yubinCloud 已提交
66 67 68
import {Tool} from "@/util/tool";
import {message} from "_ant-design-vue@2.0.0-rc.3@ant-design-vue";
import {Category} from "@/models";
69

yubinCloud's avatar
yubinCloud 已提交
70 71

export default defineComponent({
72
  name: 'Home',
73 74 75 76 77
  components: {
    StarOutlined,
    LikeOutlined,
    MessageOutlined,
  },
78 79
  setup() {
    console.log("setup");
80
    let ebooks = ref();
yubinCloud's avatar
yubinCloud 已提交
81 82
    let categories: Category[];
    let level1 = ref();
83
    let isShowWelcome = ref(true);
84
    let categoryId2: string|null = null;
85 86 87 88 89 90 91 92

    const pagination = {
      onChange: (page: number) => {
        console.log(page);
      },
      pageSize: 3,
    };

yubinCloud's avatar
yubinCloud 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    /**
     * 查询所有分类
     **/
    const handleQueryCategory = () => {
      axios.get("/category/all").then((response) => {
        const respData = response.data;

        if (respData.code == 0) {
          categories = respData.data;
          console.log("原始数组:", categories);

          level1.value = [];
          level1.value = Tool.array2Tree(categories, 0);
          console.log("树形结构:", level1);

        } else {
          message.error(respData.msg);
        }
      });
    };

114 115 116 117 118 119 120
    const handleQueryEbook = () => {
      let queryParams = {
        pageNum: 1,
        pageSize: 1000,
        categoryId2: categoryId2
      }
      axios.get("/ebook/query", {params: queryParams}).then((response) => {
yubinCloud's avatar
yubinCloud 已提交
121 122 123
        console.log(response);
        const respData = response.data;
        const pageData = respData.data;
124
        ebooks.value = pageData.list;
yubinCloud's avatar
yubinCloud 已提交
125
      })
126 127 128 129 130 131 132 133 134 135 136 137 138 139
    }

    const handleClick = (value: any) => {
      if (value.key === 'welcome') {
        isShowWelcome.value = true;
      } else {
        categoryId2 = value.key;
        isShowWelcome.value = false;
        handleQueryEbook();
      }
    };

    onMounted(() => {
      handleQueryCategory();
yubinCloud's avatar
yubinCloud 已提交
140 141 142
    });


143
    return {
144
      ebooks,
145
      pagination,
yubinCloud's avatar
yubinCloud 已提交
146
      level1,
147
      isShowWelcome,
yubinCloud's avatar
yubinCloud 已提交
148 149 150

      handleQueryCategory,
      handleClick,
151
    }
152
  }
yubinCloud's avatar
yubinCloud 已提交
153 154
});
</script>
155 156 157 158 159 160 161 162 163 164

<style scoped>
.ant-avatar {
  width: 50px;
  height: 50px;
  line-height: 50px;
  border-radius: 8%;
  margin: 5px 0;
}
</style>