knowledgeGraph.vue 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. <template>
  2. <div class="knowledge_graph" ref="knowledgeGraph" :style="{ maxHeight: containerHeight + 'px' }">
  3. <div class="graph_top">
  4. <!-- 顶部信息模块 -->
  5. <div class="knowledge_header">
  6. <div class="header_top">
  7. <span class="header_title">我的知识图谱</span>
  8. <!-- 年级选择标签,只有一个年级时不显示 -->
  9. <el-checkbox-group v-if="gradeList && gradeList.length > 1" v-model="currentGradeList" @change="handleGradeChange">
  10. <el-checkbox v-for="grade in gradeList" :key="grade.value" :label="grade.value">
  11. {{ grade.label }}
  12. </el-checkbox>
  13. </el-checkbox-group>
  14. </div>
  15. <div class="header_stats">
  16. <span class="stats_text">
  17. 你的知识图谱共包含 <span class="stats_num">{{ totalCount }}</span> 个知识点。当前
  18. 红色薄弱知识点<span class="stats_weak"> {{ weakCount }}</span> 个,
  19. 黄色待提升知识点<span class="stats_good"> {{ goodCount }}</span> 个,
  20. <span class="stats_excellent">坚持练习,把全部知识点都进阶为绿色已掌握状态吧!</span>
  21. </span>
  22. </div>
  23. </div>
  24. <!-- 环形扩散图组件 -->
  25. <circular-diffusion :subjectName="actualSubjectName" :subjectScoreRate="subjectScoreRate"
  26. :tableData="displayTableData"
  27. :classLevel="classLevel" :classGroupName="classGroupName"
  28. :currentKnowledgeId="currentLinkedKnowledgeId || currentKnowledgeId || defaultKnowledgeId"
  29. :addedKnowledgeIds="addedKnowledgeIds"
  30. @legend-change="handleLegendChange" @activeTabChange="handleActiveTabChange"
  31. @add-practice="handleAddPractice"
  32. @knowledge-item-click="handleKnowledgeItemClick" />
  33. <!-- 区域展示组件 -->
  34. <region
  35. :partitionData="partitionData"
  36. :addedKnowledgeIds="addedKnowledgeIds"
  37. :selectedKnowledgeId="currentLinkedKnowledgeId || currentKnowledgeId || defaultKnowledgeId"
  38. @add-practice="handleAddPractice"
  39. @item-click="handleRegionItemClick" />
  40. </div>
  41. </div>
  42. </template>
  43. <script>
  44. import CircularDiffusion from './knowledgeGraph/circularDiffusion.vue';
  45. import Region from './knowledgeGraph/region.vue';
  46. import { mapState } from 'vuex';
  47. export default {
  48. name: 'KnowledgeGraph',
  49. components: {
  50. CircularDiffusion,
  51. Region
  52. },
  53. props: {
  54. subjectScoreRate: {
  55. type: Number,
  56. default: 85
  57. },
  58. tableData: {
  59. type: Array,
  60. default: () => []
  61. },
  62. fatalVulnerability: {
  63. type: Array,
  64. default: () => []
  65. },
  66. highVulnerability: {
  67. type: Array,
  68. default: () => []
  69. },
  70. allKnowledgeList: {
  71. type: Array,
  72. default: () => []
  73. },
  74. classLevel: {
  75. type: [String, Number],
  76. default: ''
  77. },
  78. classGroupName: {
  79. type: String,
  80. default: ''
  81. },
  82. currentKnowledgeId: {
  83. type: [String, Number],
  84. default: ''
  85. },
  86. showRegion: {
  87. type: Boolean,
  88. default: true
  89. }
  90. },
  91. data() {
  92. return {
  93. // 容器最大高度
  94. containerHeight: 600,
  95. // 当前选中的年级列表(多选)
  96. currentGradeList: [],
  97. // 年级列表(从localStorage的student_gradesList读取)
  98. gradeList: [],
  99. // 中心圆点学科名称(通过 subjectCode 从 student_subjectList 匹配)
  100. subjectNameState: '数学知识点',
  101. // 默认选中知识点ID:接口返回的 knowList 中"第一棵节点树的最后一个子节点(叶子)"
  102. defaultKnowledgeId: '',
  103. // 接口返回的真实统计数据
  104. graphStats: {
  105. allKnowNum: 0, // 知识点总数
  106. weakKnowNum: 0, // 薄弱数量
  107. goodKnowNum: 0 // 待提升数量
  108. },
  109. // 接口返回的树形知识数据(第一层直接传给环形扩散图)
  110. knowList: [],
  111. // knowList 扁平化后的一维数组(用于统计、列表展示)
  112. flatKnowList: [],
  113. // 知识点分区:由 knowledge_partition 接口返回
  114. // conquerZone: 薄弱补强区 / improveZone: 能力提升区 / consolidateZone: 扎实巩固区
  115. partitionData: {
  116. conquerZone: [],
  117. improveZone: [],
  118. consolidateZone: []
  119. },
  120. // 当前会话中"已加入练习"的知识点 ID 容器(响应式:父级 $set 后自动驱动子组件按钮置灰)
  121. // 形如 { '1001': true }
  122. addedKnowledgeIds: {},
  123. // 环形扩散图 ↔ 区域卡片 的联动选中知识点 ID
  124. // - 点环形扩散图圆点 → 写这里 → region 读 prop :selectedKnowledgeId 高亮并滚动居中
  125. // - 点 region 某行条目 → 写这里 → circularDiffusion 读 prop :currentKnowledgeId 放大圆点 + 更新右侧折线表格
  126. currentLinkedKnowledgeId: '',
  127. // 假数据:表格树形结构数据(用于无接口数据时兜底)
  128. mockTableData: []
  129. };
  130. },
  131. computed: {
  132. ...mapState('target', ['subjectCode']),
  133. // 中心圆点学科名称:优先从 localStorage 的 student_subjectList 匹配 subjectCode
  134. actualSubjectName() {
  135. return this.subjectNameState || '知识点';
  136. },
  137. // 统计知识点总数:优先使用接口返回的 allKnowNum
  138. totalCount() {
  139. if (this.graphStats.allKnowNum !== null && this.graphStats.allKnowNum !== undefined) {
  140. return this.graphStats.allKnowNum;
  141. }
  142. return this.displayAllKnowledgeList.length;
  143. },
  144. // 薄弱知识点数量(knowledgeType === 1):优先使用接口返回的 weakKnowNum
  145. weakCount() {
  146. if (this.graphStats.weakKnowNum !== null && this.graphStats.weakKnowNum !== undefined) {
  147. return this.graphStats.weakKnowNum;
  148. }
  149. return this.displayAllKnowledgeList.filter(item => item.knowledgeType === 1).length;
  150. },
  151. // 良好/待提升知识点数量(knowledgeType === 2):优先使用接口返回的 goodKnowNum
  152. goodCount() {
  153. if (this.graphStats.goodKnowNum !== null && this.graphStats.goodKnowNum !== undefined) {
  154. return this.graphStats.goodKnowNum;
  155. }
  156. return this.displayAllKnowledgeList.filter(item => item.knowledgeType === 2).length;
  157. },
  158. // 优秀知识点数量(knowledgeType === 3)
  159. excellentCount() {
  160. return this.displayAllKnowledgeList.filter(item => item.knowledgeType === 3).length;
  161. },
  162. // 实际显示的表格数据:接口knowList > props.tableData > 假数据
  163. displayTableData() {
  164. if (this.knowList && this.knowList.length > 0) {
  165. return this.knowList;
  166. }
  167. if (this.tableData && this.tableData.length > 0) {
  168. return this.tableData;
  169. }
  170. return this.mockTableData;
  171. },
  172. // 实际显示的全部知识点数据:flatKnowList > props.allKnowledgeList > 假数据
  173. displayAllKnowledgeList() {
  174. if (this.flatKnowList && this.flatKnowList.length > 0) {
  175. return this.flatKnowList;
  176. }
  177. if (this.allKnowledgeList && this.allKnowledgeList.length > 0) {
  178. return this.allKnowledgeList;
  179. }
  180. // 假数据:全部知识点(扁平化结构,用于列表显示和区域展示)
  181. return [
  182. { knowledgeId: '1', knowledgeName: '函数与方程', knowledgeType: 1, gradeScoreRate: 45, classScoreRate: 45, examNum: 8, questionNum: 120, scoreRateDiff: -5 },
  183. { knowledgeId: '1-1', knowledgeName: '一元二次方程', knowledgeType: 1, gradeScoreRate: 35, classScoreRate: 35, examNum: 5, questionNum: 60, scoreRateDiff: -10 },
  184. { knowledgeId: '1-2', knowledgeName: '函数图像', knowledgeType: 1, gradeScoreRate: 55, classScoreRate: 55, examNum: 3, questionNum: 40, scoreRateDiff: -2 },
  185. { knowledgeId: '2', knowledgeName: '几何图形', knowledgeType: 2, gradeScoreRate: 72, classScoreRate: 72, examNum: 4, questionNum: 80, scoreRateDiff: 3 },
  186. { knowledgeId: '2-1', knowledgeName: '三角形', knowledgeType: 2, gradeScoreRate: 68, classScoreRate: 68, examNum: 2, questionNum: 30, scoreRateDiff: 2 },
  187. { knowledgeId: '2-2', knowledgeName: '圆的性质', knowledgeType: 2, gradeScoreRate: 78, classScoreRate: 78, examNum: 2, questionNum: 50, scoreRateDiff: 5 },
  188. { knowledgeId: '3', knowledgeName: '概率统计', knowledgeType: 3, gradeScoreRate: 90, classScoreRate: 90, examNum: 1, questionNum: 25, scoreRateDiff: 8 },
  189. { knowledgeId: '3-1', knowledgeName: '数据分析', knowledgeType: 3, gradeScoreRate: 92, classScoreRate: 92, examNum: 1, questionNum: 15, scoreRateDiff: 10 },
  190. { knowledgeId: '3-2', knowledgeName: '概率计算', knowledgeType: 3, gradeScoreRate: 88, classScoreRate: 88, examNum: 1, questionNum: 10, scoreRateDiff: 6 },
  191. { knowledgeId: '4', knowledgeName: '数列', knowledgeType: 2, gradeScoreRate: 65, classScoreRate: 65, examNum: 6, questionNum: 90, scoreRateDiff: 1 },
  192. { knowledgeId: '5', knowledgeName: '立体几何', knowledgeType: 1, gradeScoreRate: 50, classScoreRate: 50, examNum: 7, questionNum: 100, scoreRateDiff: -3 },
  193. { knowledgeId: '6', knowledgeName: '向量', knowledgeType: 2, gradeScoreRate: 82, classScoreRate: 82, examNum: 3, questionNum: 45, scoreRateDiff: 4 },
  194. { knowledgeId: '7', knowledgeName: '导数应用', knowledgeType: 3, gradeScoreRate: 95, classScoreRate: 95, examNum: 2, questionNum: 20, scoreRateDiff: 12 },
  195. { knowledgeId: '8', knowledgeName: '不等式', knowledgeType: 1, gradeScoreRate: 58, classScoreRate: 58, examNum: 5, questionNum: 75, scoreRateDiff: -1 }
  196. ];
  197. }
  198. },
  199. watch: {
  200. // 监听学科变化,重新调用知识图谱接口
  201. subjectCode(newVal, oldVal) {
  202. if (newVal && newVal !== oldVal) {
  203. this.fetchKnowledgeGraph();
  204. this.fetchKnowledgePartition();
  205. }
  206. }
  207. },
  208. mounted() {
  209. // 从localStorage读取年级列表
  210. this.loadGradeListFromStorage();
  211. // 初始化容器高度
  212. this.calculateContainerHeight();
  213. // 监听窗口大小变化
  214. window.addEventListener('resize', this.calculateContainerHeight);
  215. // 首次加载知识图谱数据 + 分区数据
  216. this.fetchKnowledgeGraph();
  217. this.fetchKnowledgePartition();
  218. },
  219. beforeDestroy() {
  220. // 移除窗口大小变化监听
  221. window.removeEventListener('resize', this.calculateContainerHeight);
  222. },
  223. methods: {
  224. /**
  225. * 从 localStorage 读取年级列表 student_gradesList
  226. * 并规范化为 [{ label, value }] 结构
  227. */
  228. loadGradeListFromStorage() {
  229. try {
  230. const raw = localStorage.getItem('student_gradesList');
  231. if (!raw) return;
  232. const list = JSON.parse(raw);
  233. if (!Array.isArray(list)) return;
  234. // 规范化字段:支持 label/value、label/code、name/value 等常见组合
  235. this.gradeList = list.map(item => ({
  236. label: item.label || item.name || String(item.value ?? item.code ?? item),
  237. value: item.value ?? item.code ?? item.label ?? item.name ?? String(item)
  238. }));
  239. // 默认全部选中
  240. this.currentGradeList = this.gradeList.map(g => g.value);
  241. } catch (e) {
  242. console.warn('[KnowledgeGraph] 解析 localStorage.student_gradesList 失败:', e);
  243. }
  244. },
  245. /**
  246. * 【接口调用】获取知识图谱数据
  247. * 参数:
  248. * - levelCode: 学段CODE(localStorage.student_level_code)
  249. * - gradeCodes: 年级codes数组(checkbox选中的value,即使只有1个也要传)
  250. * - subjectCode: 学科code(vuex -> target.subjectCode)
  251. * 调用成功后打印返回数据
  252. */
  253. fetchKnowledgeGraph() {
  254. // 读取学段CODE
  255. const levelCode = localStorage.getItem('student_level_code') || '';
  256. // 年级codes:当前选中的value数组;如未初始化则取gradeList全部value
  257. let gradeCodes = this.currentGradeList && this.currentGradeList.length
  258. ? this.currentGradeList
  259. : (this.gradeList || []).map(g => g.value);
  260. // 学科code
  261. const subjectCode = this.subjectCode || '';
  262. const params = {
  263. levelCode,
  264. gradeCodes,
  265. subjectCode
  266. };
  267. this.$api.target.knowledgeGraph(params).then(res => {
  268. if (res.code == 200 && res.data) {
  269. const d = res.data;
  270. // 1. 统计数据
  271. this.graphStats = {
  272. allKnowNum: d.allKnowNum !== undefined ? Number(d.allKnowNum) : 0,
  273. weakKnowNum: d.weakKnowNum !== undefined ? Number(d.weakKnowNum) : 0,
  274. goodKnowNum: d.goodKnowNum !== undefined ? Number(d.goodKnowNum) : 0
  275. };
  276. // 2. 中心圆点学科名称:通过 subjectCode 从 localStorage.student_subjectList 匹配
  277. this.resolveSubjectNameByCode();
  278. // 3. knowList 树形数据 + 拍平(并把 personalScoreRate 映射为 classScoreRate / gradeScoreRate 以便图表组件识别)
  279. const list = Array.isArray(d.knowList) ? d.knowList : [];
  280. this.knowList = this.mapKnowListItem(list);
  281. this.flatKnowList = this.flattenKnowList(this.knowList);
  282. // 4. 默认选中:第一棵节点树的最后一个子节点(叶子节点)
  283. const defaultNode = this.findFirstTreeLastLeafNode(this.knowList);
  284. this.defaultKnowledgeId = defaultNode ? (defaultNode.knowledgeId ?? '') : '';
  285. } else {
  286. // 接口失败 / code !== 200 / 无data:清空数据,扩散图和统计数字归零
  287. this.resetKnowledgeGraphEmpty();
  288. }
  289. }).catch(err => {
  290. console.error('[KnowledgeGraph] knowledgeGraph 接口异常:', err);
  291. this.resetKnowledgeGraphEmpty();
  292. });
  293. },
  294. /**
  295. * 参数和 knowledgeGraph 相同:levelCode / gradeCodes / subjectCode
  296. * 返回:conquerZone(薄弱) / improveZone(提升) / consolidateZone(巩固)
  297. */
  298. fetchKnowledgePartition() {
  299. const levelCode = localStorage.getItem('student_level_code') || '';
  300. let gradeCodes = this.currentGradeList && this.currentGradeList.length
  301. ? this.currentGradeList
  302. : (this.gradeList || []).map(g => g.value);
  303. const subjectCode = this.subjectCode || '';
  304. const params = { levelCode, gradeCodes, subjectCode };
  305. this.$api.target.knowledgePartition(params).then(res => {
  306. if (res.code == 200 && res.data) {
  307. const d = res.data || {};
  308. this.partitionData = {
  309. conquerZone: this.mapPartitionItem(Array.isArray(d.conquerZone) ? d.conquerZone : []),
  310. improveZone: this.mapPartitionItem(Array.isArray(d.improveZone) ? d.improveZone : []),
  311. consolidateZone:this.mapPartitionItem(Array.isArray(d.consolidateZone)? d.consolidateZone : [])
  312. };
  313. } else {
  314. this.resetPartitionEmpty();
  315. }
  316. }).catch(err => {
  317. console.error('[KnowledgeGraph] knowledgePartition 接口异常:', err);
  318. this.resetPartitionEmpty();
  319. });
  320. },
  321. /**
  322. * 分区接口返回的每个子数组字段规范化
  323. * - knowledgeId / knowledgeName / personalScoreRate / examNum / knowledgeType
  324. * 顺便兜底转换成 Number,避免字符串导致渲染异常
  325. */
  326. mapPartitionItem(list) {
  327. if (!Array.isArray(list)) return [];
  328. return list.map(item => {
  329. const node = { ...(item || {}) };
  330. node.knowledgeId = node.knowledgeId ?? node.id;
  331. node.knowledgeName = node.knowledgeName ?? node.name ?? node.label ?? '';
  332. node.knowledgeType = Number(node.knowledgeType ?? node.type ?? 3);
  333. const pRate = node.personalScoreRate ?? node.scoreRate ?? node.personalRate;
  334. if (pRate !== null && pRate !== undefined && pRate !== '') {
  335. const n = Number(pRate);
  336. node.personalScoreRate = isNaN(n) ? 0 : n;
  337. if (node.classScoreRate === null || node.classScoreRate === undefined) {
  338. node.classScoreRate = node.personalScoreRate;
  339. }
  340. if (node.gradeScoreRate === null || node.gradeScoreRate === undefined) {
  341. node.gradeScoreRate = node.personalScoreRate;
  342. }
  343. }
  344. // 考频:examNum
  345. if (node.examNum !== null && node.examNum !== undefined && node.examNum !== '') {
  346. const n = Number(node.examNum);
  347. node.examNum = isNaN(n) ? 0 : n;
  348. }
  349. return node;
  350. });
  351. },
  352. /**
  353. * 清空分区数据(接口失败或无数据时调用)
  354. */
  355. resetPartitionEmpty() {
  356. this.partitionData = {
  357. conquerZone: [],
  358. improveZone: [],
  359. consolidateZone: []
  360. };
  361. },
  362. /**
  363. * 清空知识图谱所有数据(接口失败或无数据时调用)
  364. * - knowList / flatKnowList 清空
  365. * - graphStats 三项归零
  366. * - defaultKnowledgeId 清空
  367. * - subjectNameState 不重置(保持本地匹配兜底名,避免名称闪烁)
  368. */
  369. resetKnowledgeGraphEmpty() {
  370. this.knowList = [];
  371. this.flatKnowList = [];
  372. this.graphStats = {
  373. allKnowNum: 0,
  374. weakKnowNum: 0,
  375. goodKnowNum: 0
  376. };
  377. this.defaultKnowledgeId = '';
  378. },
  379. /**
  380. * 从树形 knowList 中取"第一棵节点树的最后一个子节点(叶子)"
  381. * 规则:取 list[0] 作为根节点,然后一路沿 children[children.length-1] 下钻
  382. * 直到某节点没有 children 或 children 为空,返回该节点;
  383. * list 为空返回 null
  384. */
  385. findFirstTreeLastLeafNode(list) {
  386. if (!Array.isArray(list) || list.length === 0) return null;
  387. let node = list[0];
  388. while (node && Array.isArray(node.children) && node.children.length > 0) {
  389. node = node.children[node.children.length - 1];
  390. }
  391. return node || null;
  392. },
  393. /**
  394. * 通过 subjectCode 从 localStorage.student_subjectList 匹配学科名称
  395. * 并写入 this.subjectNameState
  396. */
  397. resolveSubjectNameByCode() {
  398. try {
  399. const subjectCode = String(this.subjectCode ?? '');
  400. if (!subjectCode) return;
  401. const raw = localStorage.getItem('student_subjectList');
  402. if (!raw) return;
  403. const list = JSON.parse(raw);
  404. if (!Array.isArray(list) || list.length === 0) return;
  405. const match = list.find(item => {
  406. const val = String(item.value ?? item.code ?? item.subjectCode ?? item.id ?? '');
  407. return val === subjectCode;
  408. });
  409. if (match) {
  410. const name = match.label ?? match.name ?? match.subjectName ?? String(match);
  411. if (name) {
  412. this.subjectNameState = name;
  413. return;
  414. }
  415. }
  416. // 兜底:取第一个
  417. const first = list[0];
  418. if (first) {
  419. const fallbackName = first.label ?? first.name ?? first.subjectName ?? String(first);
  420. if (fallbackName) this.subjectNameState = fallbackName;
  421. }
  422. } catch (e) {
  423. console.warn('[KnowledgeGraph] 解析 localStorage.student_subjectList 失败:', e);
  424. }
  425. },
  426. /**
  427. * 递归遍历 knowList,确保每个节点具备环形图需要的字段:
  428. * - knowledgeId / knowledgeName / knowledgeType
  429. * - personalScoreRate → 同时映射为 classScoreRate、gradeScoreRate(组件读取的是这两个)
  430. * - isJoin 原样保留(子组件需要据此显示"已加入练习")
  431. * - children 递归处理
  432. */
  433. mapKnowListItem(list) {
  434. if (!Array.isArray(list)) return [];
  435. return list.map(item => {
  436. const node = { ...(item || {}) };
  437. // 兼容字段
  438. node.knowledgeId = node.knowledgeId ?? node.id;
  439. node.knowledgeName = node.knowledgeName ?? node.name ?? node.label;
  440. node.knowledgeType = Number(node.knowledgeType ?? node.type ?? 3);
  441. const pRate = node.personalScoreRate ?? node.scoreRate ?? node.personalRate;
  442. if (pRate !== null && pRate !== undefined) {
  443. // 环形扩散图 generateTreeData 中会按 classLevel / mode 读取 classScoreRate 或 gradeScoreRate
  444. // 为了兼容两种路径,把 personalScoreRate 同时复制到两者
  445. if (node.classScoreRate === null || node.classScoreRate === undefined) {
  446. node.classScoreRate = Number(pRate);
  447. }
  448. if (node.gradeScoreRate === null || node.gradeScoreRate === undefined) {
  449. node.gradeScoreRate = Number(pRate);
  450. }
  451. node.personalScoreRate = Number(pRate);
  452. }
  453. if (node.children && node.children.length > 0) {
  454. node.children = this.mapKnowListItem(node.children);
  455. }
  456. return node;
  457. });
  458. },
  459. /**
  460. * 将 knowList(树形)拍平为一维数组(保留 isJoin 等全部字段)
  461. */
  462. flattenKnowList(list, out = []) {
  463. if (!Array.isArray(list)) return out;
  464. list.forEach(item => {
  465. out.push(item);
  466. if (item.children && item.children.length > 0) {
  467. this.flattenKnowList(item.children, out);
  468. }
  469. });
  470. return out;
  471. },
  472. // 年级切换(多选)
  473. handleGradeChange(gradeList) {
  474. this.currentGradeList = gradeList;
  475. this.$emit('grade-change', gradeList);
  476. // 年级变化后重新请求知识图谱 + 分区数据
  477. this.fetchKnowledgeGraph();
  478. this.fetchKnowledgePartition();
  479. },
  480. // 计算容器高度
  481. calculateContainerHeight() {
  482. if (this.$refs.knowledgeGraph) {
  483. // 获取容器距离视口顶部的距离
  484. const rect = this.$refs.knowledgeGraph.getBoundingClientRect();
  485. // 计算容器最大高度:窗口高度 - 容器距离顶部的距离 - 底部留白
  486. const maxHeight = window.innerHeight - rect.top - 20;
  487. // 设置最小高度为400px
  488. this.containerHeight = Math.max(maxHeight, 400);
  489. }
  490. },
  491. // 处理图例变化
  492. handleLegendChange(data) {
  493. this.$emit('legend-change', data);
  494. },
  495. // 处理tab切换
  496. handleActiveTabChange(tab) {
  497. this.$emit('activeTabChange', tab);
  498. },
  499. // 处理知识点点击(来自环形扩散图组件:圆点被用户点击或默认选中)
  500. handleKnowledgeItemClick(data) {
  501. // 兼容两种 payload 结构:
  502. // 1) 圆点点击 / setDefaultSelection 新结构:{ knowledgeId, knowledgeName, ... } (顶层)
  503. // 2) 旧的 item 包装结构:{ item: {knowledgeId,...}, index: N }
  504. let knowledgeId = data?.knowledgeId;
  505. if ((knowledgeId === undefined || knowledgeId === null || knowledgeId === '')
  506. && data?.item) {
  507. knowledgeId = data.item.knowledgeId;
  508. }
  509. if (knowledgeId !== undefined && knowledgeId !== null && knowledgeId !== '') {
  510. this.currentLinkedKnowledgeId = String(knowledgeId);
  511. }
  512. this.$emit('knowledge-item-click', data);
  513. },
  514. // 处理区域卡片中某行条目被点击(来自 region 组件)→ 反向联动到环形扩散图对应圆点高亮 + 右侧数据
  515. handleRegionItemClick(item) {
  516. const id = item?.knowledgeId;
  517. if (id === undefined || id === null || id === '') return;
  518. this.currentLinkedKnowledgeId = String(id);
  519. },
  520. // 处理加入练习
  521. // 参数来源:
  522. // - 环形扩散图按钮:emit 传出 { knowledgeId, knowledgeName, personalScoreRate?, knowledgeType? }
  523. // - region 卡片按钮:emit 传出整条 item(含 knowledgeId/Name/personalScoreRate/knowledgeType/examNum...)
  524. handleAddPractice(item) {
  525. if (!item) {
  526. this.$message.warning('未获取到知识点信息');
  527. return;
  528. }
  529. const knowledgeId = item.knowledgeId ?? item.id;
  530. const knowledgeName = item.knowledgeName ?? item.name ?? item.label ?? '';
  531. if (!knowledgeId || !knowledgeName) {
  532. this.$message.warning('知识点信息不完整,请稍后重试');
  533. return;
  534. }
  535. // 已经加过的直接返回,避免重复请求
  536. if (this.addedKnowledgeIds[String(knowledgeId)]) {
  537. this.$message.info('该知识点已加入练习');
  538. return;
  539. }
  540. // 得分率:优先 personalScoreRate → gradeScoreRate → classScoreRate
  541. let personalScoreRate = item.personalScoreRate;
  542. if (personalScoreRate === null || personalScoreRate === undefined || personalScoreRate === '') {
  543. personalScoreRate = item.gradeScoreRate ?? item.classScoreRate;
  544. }
  545. if (personalScoreRate === null || personalScoreRate === undefined || personalScoreRate === '') {
  546. personalScoreRate = 0;
  547. } else {
  548. personalScoreRate = Number(personalScoreRate);
  549. if (isNaN(personalScoreRate)) personalScoreRate = 0;
  550. }
  551. // 知识点类型:1薄弱 2一般 3优秀
  552. let knowledgeType = Number(item.knowledgeType ?? item.type ?? 0);
  553. if (isNaN(knowledgeType)) knowledgeType = 0;
  554. const subjectCode = this.subjectCode || '';
  555. const levelCode = localStorage.getItem('student_level_code') || '';
  556. // 年级CODE:取 gradeList 数组中最后一个年级的 value
  557. // - 多个年级(如[{高一,10},{高二,11}])→ 取最后一个(高二.value = 11)
  558. // - 只有一个年级 → 直接取该年级 value
  559. // - 空/无效 → 传空串兜底
  560. let gradeCode = '';
  561. if (Array.isArray(this.gradeList) && this.gradeList.length > 0) {
  562. const lastGrade = this.gradeList[this.gradeList.length - 1];
  563. if (lastGrade && lastGrade.value !== undefined && lastGrade.value !== null && lastGrade.value !== '') {
  564. gradeCode = String(lastGrade.value);
  565. }
  566. }
  567. const params = {
  568. knowledgeId,
  569. knowledgeName,
  570. personalScoreRate,
  571. knowledgeType,
  572. subjectCode,
  573. levelCode,
  574. gradeCode
  575. };
  576. this.$api.target.addExercise(params).then(res => {
  577. if (res && (res.code == 200 || res.code == 0)) {
  578. // 成功:提示 + 记录 ID(用 $set 保证响应式,region/circularDiffusion 会自动重新渲染并置灰按钮)
  579. this.$message.success('已加入练习成功');
  580. this.$set(this.addedKnowledgeIds, String(knowledgeId), true);
  581. } else {
  582. const msg = (res && res.message) || '加入练习失败';
  583. this.$message.error(msg);
  584. }
  585. }).catch(err => {
  586. console.error('[KnowledgeGraph] addExercise 接口异常:', err);
  587. this.$message.error('加入练习失败,请稍后重试');
  588. });
  589. }
  590. }
  591. };
  592. </script>
  593. <style lang="scss" scoped>
  594. .knowledge_graph {
  595. overflow-y: auto;
  596. overflow-x: hidden;
  597. .graph_top {
  598. background: #FFFFFF;
  599. border-radius: 10px;
  600. padding: 20px;
  601. }
  602. /* 顶部信息模块 */
  603. .knowledge_header {
  604. margin-bottom: 10px;
  605. .header_top {
  606. display: flex;
  607. align-items: center;
  608. gap: 20px;
  609. margin-bottom: 10px;
  610. .header_title {
  611. font-size: 16px;
  612. font-weight: 500;
  613. color: #333;
  614. }
  615. .el-checkbox-group{
  616. .el-checkbox{
  617. margin-right:20px;
  618. }
  619. }
  620. }
  621. .header_stats {
  622. font-size: 14px;
  623. .stats_text {
  624. color: #999999;
  625. }
  626. .stats_num {
  627. color: #2E64FA;
  628. }
  629. .stats_weak {
  630. color: #F56C6C;
  631. }
  632. .stats_good {
  633. color: #FAC858;
  634. }
  635. }
  636. }
  637. /* 自定义滚动条样式 */
  638. &::-webkit-scrollbar {
  639. width: 6px;
  640. }
  641. &::-webkit-scrollbar-track {
  642. background: #f1f1f1;
  643. border-radius: 3px;
  644. }
  645. &::-webkit-scrollbar-thumb {
  646. background: #c1c1c1;
  647. border-radius: 3px;
  648. &:hover {
  649. background: #a8a8a8;
  650. }
  651. }
  652. }
  653. </style>