1
YM
2024-07-18 c028e3f8ea083ed7ef78bf8aaf9f05866661f0dd
src/pages/academicSchools/index.vue
@@ -1,19 +1,683 @@
<template>
  <view>
    <headNav :idIndex="6" text="学术流派" />
  <view style="width: 100%; height: 100%; display: flex; flex-direction: column;">
    <headNav :idIndex="'6'" text="学术流派" />
    <!-- 高级搜索 -->
    <view style="margin-top: 0.1rem">
      <advancedSearch
        @onSearch="onSearch"
        @onSubmit="onSubmit"
        :from="From"
        placehold="请输入关键字"
        :keyword="searchKey"
        :advancedSearchBack="advancedSearchBack"
      />
    </view>
    <!-- 热门搜索 -->
    <view class="flex flex-center hotSearch">
      热门搜索:
      <ul class="flex" style="margin-right: 10rpx">
        <li
          @click="onSearch({ text: item }, index)"
          :class="{ cursor: true, active: hotAciveIndex === index }"
          v-for="(item, index) in hotKeyList"
          :key="item"
        >
          {{ item }}
        </li>
      </ul>
    </view>
    <!-- 关系表地图 -->
    <div
      id="relation"
      style="margin-top: 0.1rem; flex: 1; width: 100%; position: relative"
    >
      <div
        v-if="!loading && relationships.length == 0"
        style="
          position: absolute;
          top: 30%;
          left: 0;
          right: 0;
          text-align: center;
          font-size: 0.2rem;
          color: #666;
        "
      >
        暂无相关数据
      </div>
    </div>
  </view>
</template>
<script>
// import * as echarts from "echarts";
// import axios from "axios";
import {
  getPersonData,
  getHotSearch,
  getPDataStatistics
} from "@/api/index.js";
import * as echarts from "echarts";
export default {
  data() {
    return {};
    return {
      loading: true,
      hotAciveIndex: "",
      currentId: 21682,
      // echarts数据
      nodes: [],
      // echarts两者的关系
      relationships: [],
      // 高级搜索
      From: {
        from: [
          {
            type: "input",
            label: "姓名",
            name: "name",
            value: ""
          },
          {
            type: "input",
            label: "别名",
            name: "alias",
            value: ""
          },
          {
            type: "input",
            label: "籍贯",
            name: "location",
            value: ""
          },
          {
            type: "select",
            label: "职业",
            name: "sdId",
            value: "",
            options: [
              {
                label: "未指明",
                value: "UNKNOWN"
              }
            ]
          },
          {
            type: "input",
            label: "官职",
            name: "office",
            value: ""
          },
          {
            type: "select",
            label: "性别",
            value: "",
            name: "genderTypeEO",
            options: [
              {
                label: "未指明",
                value: "UNKNOWN"
              },
              {
                label: "男",
                value: "MALE"
              },
              {
                label: "女",
                value: "WOMAN"
              }
            ]
          },
          {
            type: "input",
            label: "时期",
            name: "period",
            value: ""
          },
          {
            type: "input",
            label: "机构",
            name: "inst",
            value: ""
          },
          {
            type: "number",
            label: "页码",
            name: "page",
            value: ""
          },
          {
            type: "number",
            label: "页大小",
            name: "pageSize",
            value: ""
          }
        ]
      },
      // 下面的朝代1
      dynastyColor: 1,
      // 朝代
      dynasty: [
        {
          name: "夏朝",
          coord: "2070BC",
          id: 1
        },
        {
          name: "商朝",
          id: 2,
          coord: "1600BC"
        },
        {
          name: "西周",
          id: 3,
          coord: "1600BC"
        },
        {
          name: "春秋战国",
          id: 4,
          coord: "770BC"
        },
        {
          name: "秦朝",
          id: 5,
          coord: "221BC"
        },
        {
          name: "汉朝",
          id: 6,
          coord: "202BC"
        },
        {
          name: "三国两晋南北朝",
          id: 7,
          coord: "184"
        },
        {
          name: "隋朝",
          id: 8,
          coord: "581"
        },
        {
          name: "唐朝",
          id: 9,
          coord: "618"
        },
        {
          name: "五代十国",
          id: 10,
          coord: "907"
        },
        {
          name: "辽夏金",
          id: 11,
          coord: "907"
        },
        {
          name: "宋朝",
          id: 12,
          coord: "960"
        },
        {
          name: "元朝",
          id: 13,
          coord: "1271"
        },
        {
          name: "明朝",
          id: 14,
          coord: "1368"
        },
        {
          name: "清朝",
          id: 15,
          coord: "1636"
        }
      ],
      // 热门搜索
      hotKeyList: [],
      searchKey: "",
      // 分类
      categories: [],
      // 人物信息
      infoOfPersons: [],
      // 当前人物信息
      ownInfo: null
    };
  },
  onLoad(options) {},
  methods: {}
  mounted() {
    this.getData();
    // this.echartsArr();
    this.hotSearch();
    // 监听窗口大小变化
    window.addEventListener("resize", this.relation);
    this.innt();
  },
  onLoad(options) {
    this.searchKey = options.keyword;
  },
  methods: {
    getPersonInfo(arr, str, newArr, order, type) {
      for (let i = 0; i < arr.length; i++) {
        const ele = arr[i];
        const personInfo = this.infoOfPersons.find(
          (item) => item.id == ele.personId
        );
        const obj = {
          personId: ele.personId,
          id1: this.ownInfo.id,
          id2: personInfo.id,
          relationTypeId: ele.relationTypeId,
          relationTypeName: ele.relationTypeName,
          name1: this.ownInfo?.NAME[0]?.content1,
          name2: personInfo?.NAME[0]?.content1,
          category: type
        };
        newArr.push(obj);
        if (ele[str] && ele[str]?.length) {
          this.getPersonInfo(ele[str], "kinList", newArr, 2, type);
        }
      }
    },
    //数组去重
    deduplicateArray(arr, idKey) {
      // 使用一个辅助对象来跟踪已经见过的ID
      const seen = {};
      // 使用filter方法过滤数组
      const deduplicatedArray = arr.filter((item) => {
        // 获取当前对象的ID
        const id = item[idKey];
        // 如果ID在辅助对象中不存在,则将其添加到对象并返回true
        if (!seen[id]) {
          seen[id] = true;
          return true;
        }
        // 如果ID已经存在,返回false
        return false;
      });
      return deduplicatedArray;
    },
    getData() {
      this.loading = true;
      getPersonData(this.currentId).then((res) => {
        this.nodes = [];
        this.relationships = [];
        let kin = [];
        let teacher = [];
        if (res && res.success && res.object) {
          console.log(res.object);
          this.infoOfPersons = res.object?.infoOfPersons;
          if (res.object?.personChain) {
            this.ownInfo = this.infoOfPersons.find(
              (item) => item.id == res.object?.personChain.personId
            );
            const obj = res.object?.personChain;
            console.log(obj.kinList, "kinList");
            if (obj.kinList?.length) {
              let newArr = [];
              this.categories.push({ name: "亲属关系" });
              this.getPersonInfo(obj.kinList, "kinList", newArr, 1, 0);
              console.log(newArr, "list");
              newArr.forEach((item) => {
                let exists1 = this.nodes.some(
                  (node) => node.id === item.id1 && node.name === item.name1
                );
                let exists2 = this.nodes.some(
                  (node) => node.id === item.id1 && node.name === item.name2
                );
                // 证明不存在
                if (item.name1 && exists1 !== true) {
                  this.nodes.push({
                    name: item.name1,
                    id: item.id1,
                    ...item
                  });
                }
                if (item.name2 && exists2 !== true) {
                  this.nodes.push({
                    name: item.name2,
                    id: item.id2,
                    ...item
                  });
                }
                let obj = {
                  source: item.name1,
                  target: item.name2,
                  relation: item.relationTypeName || "" // 如果存在 relationName 字段则使用,否则为空字符串
                };
                this.relationships.push(obj);
              });
            }
            if (obj.teacherStudentList?.length) {
              this.categories.push({ name: "师承关系" });
            }
            if (obj.literatureList?.length) {
              this.categories.push({ name: "文献作品" });
            }
            if (obj.socialList?.length) {
              this.categories.push({ name: "社会关系" });
            }
            if (obj.placeList?.length) {
              this.categories.push({ name: "活动地" });
            }
            if (obj.schoolList?.length) {
              this.categories.push({ name: "学术流派" });
            }
          }
        }
        this.loading = false;
        this.relation();
      });
    },
    // 关系图谱
    relation() {
      var chartDom = document.getElementById("relation");
      var myChart = echarts.init(chartDom);
      // 假设您有节点名称和它们之间的关系数据
      var nodes = this.deduplicateArray(this.nodes, "id");
      var relationships = this.relationships;
      let FontSize = 12; // 字体大小
      let BorderWidth = 2; // 边框大小
      let SymbolSize = 80; // 尺寸距离
      let Distance = 10;
      // 不同尺寸下修改echarts的字体
      if (window.innerWidth > 2560 && window.innerWidth <= 3840) {
        FontSize = 20;
        BorderWidth = 5;
        SymbolSize = 90;
        Distance = 33;
      } else if (window.innerWidth > 1920 && window.innerWidth <= 2560) {
        FontSize = 18;
        BorderWidth = 4;
        SymbolSize = 80;
        Distance = 22;
      } else if (window.innerWidth >= 1366 && window.innerWidth <= 1920) {
        FontSize = 14;
        BorderWidth = 4;
        SymbolSize = 70;
        Distance = 22;
      }
      // 根据关系数据生成连接线
      var links = [];
      relationships.forEach((relationship, i) => {
        var sourceIndex = nodes.findIndex(
          (node) => node.name === relationship.source
        );
        var targetIndex = nodes.findIndex(
          (node) => node.name === relationship.target
        );
        if (relationship.relation) {
          if (sourceIndex !== -1 && targetIndex !== -1) {
            links.push({
              source: sourceIndex,
              target: targetIndex,
              label: {
                show: true,
                formatter: function (params) {
                  // 使用函数动态生成标签内容
                  // return relationships.find(rel => rel.source === nodes[params.data
                  //       .source] && rel.target === nodes[params.data.target])
                  //    .relation;
                  return relationship.relation;
                }, // 设置关系标签内容为"Child-Parent"
                color: "#2C2C2C",
                fontSize: FontSize - 2,
                backgroundColor: "rgba(255, 255, 255, 1)",
                padding: [3, 8],
                borderRadius: 30,
                position: "middle", // 设置标签文本在线的中间位置上居中显示
                // z: -1, // 设置标签的z轴高度,使其比连接线更高
                distance: -10 // 将标签放置在连接线上
              }
            });
          }
        }
      });
      // 根据nodes动态生成节点数据
      var nodeData = nodes.map(function (node, index) {
        return {
          name: node.name,
          id: node.id,
          // itemStyle: {
          //   color: "#eae0eb", // 可以根据索引设置不同的颜色
          //   borderWidth: BorderWidth,
          //   borderColor: "#996f9f"
          // }
        };
      });
      var option;
      option = {
        title: {
          // text: '书中最常提到的100位人物',
          // subtext: '',
          left: "center",
          top: FontSize + 10,
          // textStyle: {
          //    fontSize: FontSize // 设置标题文字大小为 18px
          // },
          subtextStyle: {
            fontSize: FontSize, // 设置副标题文字大小为 14px
            color: "#2C2C2C"
          }
        },
        backgroundColor: {
          type: "image",
          image: "/static/image/characterRelationBg.png",
          // repeat: 'repeat-x', // 是否平铺,可以是 'repeat-x', 'repeat-y', 'no-repeat'
          size: "100% 100%", // 背景图片的尺寸,可以是百分比或者像素
          position: "center center" // 背景图片的位置,可以是 top, bottom, middle 或者百分比
        },
        tooltip: {},
        legend: [
          {
            // selectedMode: 'single',
            data: this.categories.map(function (a) {
              return a.name;
            })
          }
        ],
        animationDurationUpdate: 1500,
        animationEasingUpdate: "quinticInOut",
        series: [
          // {
          //   type: "graph",
          //   layout: "force",
          //   roam: true,
          //   symbolSize: SymbolSize, // 调整节点大小
          //   label: {
          //     show: true,
          //     color: "black", // 设置节点文字颜色为黑色
          //     fontSize: FontSize, // 设置文字大小
          //   },
          //   edgeSymbol: ["circle", "arrow"],
          //   edgeSymbolSize: [4, 10],
          //   data: nodeData,
          //   links,
          //   categories: this.categories,
          //   lineStyle: {
          //     opacity: 0.9,
          //     width: 2,
          //     curveness: 0.3,
          //   },
          //   emphasis: {
          //     focus: "adjacency",
          //     lineStyle: {
          //       width: 10,
          //     },
          //   },
          //   force: {
          //     // layoutAnimation: false,
          //     // friction: 1,
          //     repulsion: 800,
          //     edgeLength: 400,
          //   },
          // },
          {
            name: "Les Miserables",
            type: "graph",
            legendHoverLink: false,
            layout: 'force',
            data: nodeData,
            links: links,
            categories: this.categories,
            roam: true,
            label: {
              position: "right",
              formatter: "{b}"
            },
            lineStyle: {
              color: "source",
              curveness: 0.3
            },
            emphasis: {
              focus: "adjacency",
              lineStyle: {
                width: 10
              }
            }
          }
        ]
      };
      // 将生成的连接线添加到echarts图表的option中
      // option.series[0].links = links;
      // 设置点击事件监听
      myChart.off("click");
      myChart.on("click", (params) => {
        if (params.dataType === "node") {
          this.spaceTimeArr(params.data.id);
        }
      });
      console.log(option, "option");
      myChart.setOption(option);
    },
    // 基础搜索
    onSearch(val, index) {
      if (index !== undefined) {
        this.searchKey = "";
        this.hotAciveIndex = index;
      } else {
        this.hotAciveIndex = "";
      }
      // this.searchKey = val.text;
      this.getData(val.text);
    },
    // 高级搜索
    onSubmit(val) {
      getAdvanceSearch({
        ...val,
        page: 1,
        pageSize: 100
      }).then((res) => {
        console.log(res, "res");
      });
    },
    advancedSearchBack() {
      uni.navigateTo({
        url: "/pages/knowledgeBase/knowledgeBase"
      });
    },
    // 点击下面的朝代按钮
    dynastyBottomClick(item) {
      this.dynastyColor = item.id;
      this.onSearch({
        text: item.name
      });
    },
    // 关系图 点击里面的元素
    spaceTimeArr(id) {
      // 关系图的数据
      uni.navigateTo({
        url: "/pages/character/detail?id=" + id
      });
    },
    // 获取朝代echarts的数据
    echartsArr() {
      getDataStatistics().then((res) => {
        res.object.dynastyStatistic.details.map((item, index) => {
          // this.dynasty.id = item.dynastyId
          // this.dynasty.name = item.dynastyName
          // this.dynasty.coord = item.count
          this.dynasty[index].id = item.dynastyId;
          this.dynasty[index].name = item.dynastyName;
          this.dynasty[index].coord = item.count;
        });
      });
    },
    // 热门搜索
    hotSearch() {
      getHotSearch().then((res) => {
        const indexList = Object.keys(res.object);
        indexList.sort((a, b) => parseInt(b) - parseInt(a));
        this.hotKeyList = indexList.map((item) => {
          return res.object[item];
        });
      });
    },
    // 高级搜索里面职业的下拉菜单
    innt() {
      getPDataStatistics().then((res) => {
        const professionIndex = this.From.from.findIndex(
          (field) => field.label === "职业"
        );
        // 如果找到了职业字段
        if (professionIndex !== -1) {
          // 将 profession.list 转换为 options 格式
          const options = res.object.occupationStatistic.details.map(
            (item) => ({
              label: item.tagName,
              value: item.tagId.toString() // 将 id 转换为字符串,确保与 value 类型一致
            })
          );
          // 更新职业字段的 options 属性
          this.$set(this.From.from[professionIndex], "options", options);
        }
      });
    }
  }
};
</script>
<style></style>
<style>
#relatio {
  width: 100%;
  height: 100%;
}
@media screen and (min-width: 2560px) and (max-width: 3840px) {
}
@media screen and (min-width: 1366px) and (max-width: 1920px) {
}
::v-deep .uni-input-placeholder {
  line-height: 1;
  font-size: 0.12rem;
}
::v-deep .ffff {
  border-radius: 0.5rem;
}
::v-deep .widget_button {
  border-radius: 0.5rem;
  margin: 0.02rem;
}
.hotSearch {
  font-size: 0.12rem;
  color: #2c2c2c;
  li {
    color: #244a7b;
    margin: 0 0.1rem;
    color: #244a7b;
    &.active {
      color: #027edc;
      font-weight: bold;
    }
  }
}
</style>