knowledgeGraph.vue 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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', 'subjectName']),
  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. // 例:[{高一,10},{高二,11}] → currentGradeList = ['11'](仅选中高二)
  241. // 同“加入练习接口 gradeCode = gradeList 末尾 value”规则保持一致
  242. this.currentGradeList = this.gradeList.length > 0
  243. ? [this.gradeList[this.gradeList.length - 1].value]
  244. : [];
  245. } catch (e) {
  246. console.warn('[KnowledgeGraph] 解析 localStorage.student_gradesList 失败:', e);
  247. }
  248. },
  249. /**
  250. * 【接口调用】获取知识图谱数据
  251. * 参数:
  252. * - levelCode: 学段CODE(localStorage.student_level_code)
  253. * - gradeCodes: 年级codes数组(checkbox选中的value,即使只有1个也要传)
  254. * - subjectCode: 学科code(vuex -> target.subjectCode)
  255. * 调用成功后打印返回数据
  256. */
  257. fetchKnowledgeGraph() {
  258. // 读取学段CODE
  259. const levelCode = localStorage.getItem('student_level_code') || '';
  260. // 年级codes:当前选中的value数组;如未初始化则取gradeList全部value
  261. let gradeCodes = this.currentGradeList && this.currentGradeList.length
  262. ? this.currentGradeList
  263. : (this.gradeList || []).map(g => g.value);
  264. // 学科code
  265. const subjectCode = this.subjectCode || '';
  266. const params = {
  267. levelCode,
  268. gradeCodes,
  269. subjectCode
  270. };
  271. this.$api.target.knowledgeGraph(params).then(res => {
  272. if (res.code == 200 && res.data) {
  273. const d = res.data;
  274. // 1. 统计数据
  275. this.graphStats = {
  276. allKnowNum: d.allKnowNum !== undefined ? Number(d.allKnowNum) : 0,
  277. weakKnowNum: d.weakKnowNum !== undefined ? Number(d.weakKnowNum) : 0,
  278. goodKnowNum: d.goodKnowNum !== undefined ? Number(d.goodKnowNum) : 0
  279. };
  280. // 2. 中心圆点学科名称:通过 subjectCode 从 localStorage.student_subjectList 匹配
  281. this.resolveSubjectNameByCode();
  282. // 3. knowList 树形数据 + 拍平(并把 personalScoreRate 映射为 classScoreRate / gradeScoreRate 以便图表组件识别)
  283. const list = Array.isArray(d.knowList) ? d.knowList : [];
  284. this.knowList = this.mapKnowListItem(list);
  285. this.flatKnowList = this.flattenKnowList(this.knowList);
  286. // 4. 默认选中:第一棵节点树的最后一个子节点(叶子节点)
  287. const defaultNode = this.findFirstTreeLastLeafNode(this.knowList);
  288. this.defaultKnowledgeId = defaultNode ? (defaultNode.knowledgeId ?? '') : '';
  289. } else {
  290. // 接口失败 / code !== 200 / 无data:清空数据,扩散图和统计数字归零
  291. this.resetKnowledgeGraphEmpty();
  292. }
  293. }).catch(err => {
  294. console.error('[KnowledgeGraph] knowledgeGraph 接口异常:', err);
  295. this.resetKnowledgeGraphEmpty();
  296. });
  297. },
  298. /**
  299. * 参数和 knowledgeGraph 相同:levelCode / gradeCodes / subjectCode
  300. * 返回:conquerZone(薄弱) / improveZone(提升) / consolidateZone(巩固)
  301. */
  302. fetchKnowledgePartition() {
  303. const levelCode = localStorage.getItem('student_level_code') || '';
  304. let gradeCodes = this.currentGradeList && this.currentGradeList.length
  305. ? this.currentGradeList
  306. : (this.gradeList || []).map(g => g.value);
  307. const subjectCode = this.subjectCode || '';
  308. const params = { levelCode, gradeCodes, subjectCode };
  309. this.$api.target.knowledgePartition(params).then(res => {
  310. if (res.code == 200 && res.data) {
  311. const d = res.data || {};
  312. this.partitionData = {
  313. conquerZone: this.mapPartitionItem(Array.isArray(d.conquerZone) ? d.conquerZone : []),
  314. improveZone: this.mapPartitionItem(Array.isArray(d.improveZone) ? d.improveZone : []),
  315. consolidateZone:this.mapPartitionItem(Array.isArray(d.consolidateZone)? d.consolidateZone : [])
  316. };
  317. } else {
  318. this.resetPartitionEmpty();
  319. }
  320. }).catch(err => {
  321. console.error('[KnowledgeGraph] knowledgePartition 接口异常:', err);
  322. this.resetPartitionEmpty();
  323. });
  324. },
  325. /**
  326. * 分区接口返回的每个子数组字段规范化
  327. * - knowledgeId / knowledgeName / personalScoreRate / examNum / knowledgeType
  328. * 顺便兜底转换成 Number,避免字符串导致渲染异常
  329. */
  330. mapPartitionItem(list) {
  331. if (!Array.isArray(list)) return [];
  332. return list.map(item => {
  333. const node = { ...(item || {}) };
  334. node.knowledgeId = node.knowledgeId ?? node.id;
  335. node.knowledgeName = node.knowledgeName ?? node.name ?? node.label ?? '';
  336. node.knowledgeType = Number(node.knowledgeType ?? node.type ?? 3);
  337. const pRate = node.personalScoreRate ?? node.scoreRate ?? node.personalRate;
  338. if (pRate !== null && pRate !== undefined && pRate !== '') {
  339. const n = Number(pRate);
  340. node.personalScoreRate = isNaN(n) ? 0 : n;
  341. if (node.classScoreRate === null || node.classScoreRate === undefined) {
  342. node.classScoreRate = node.personalScoreRate;
  343. }
  344. if (node.gradeScoreRate === null || node.gradeScoreRate === undefined) {
  345. node.gradeScoreRate = node.personalScoreRate;
  346. }
  347. }
  348. // 考频:examNum
  349. if (node.examNum !== null && node.examNum !== undefined && node.examNum !== '') {
  350. const n = Number(node.examNum);
  351. node.examNum = isNaN(n) ? 0 : n;
  352. }
  353. return node;
  354. });
  355. },
  356. /**
  357. * 清空分区数据(接口失败或无数据时调用)
  358. */
  359. resetPartitionEmpty() {
  360. this.partitionData = {
  361. conquerZone: [],
  362. improveZone: [],
  363. consolidateZone: []
  364. };
  365. },
  366. /**
  367. * 清空知识图谱所有数据(接口失败或无数据时调用)
  368. * - knowList / flatKnowList 清空
  369. * - graphStats 三项归零
  370. * - defaultKnowledgeId 清空
  371. * - subjectNameState 不重置(保持本地匹配兜底名,避免名称闪烁)
  372. */
  373. resetKnowledgeGraphEmpty() {
  374. this.knowList = [];
  375. this.flatKnowList = [];
  376. this.graphStats = {
  377. allKnowNum: 0,
  378. weakKnowNum: 0,
  379. goodKnowNum: 0
  380. };
  381. this.defaultKnowledgeId = '';
  382. },
  383. /**
  384. * 从树形 knowList 中取"第一棵节点树的最后一个子节点(叶子)"
  385. * 规则:取 list[0] 作为根节点,然后一路沿 children[children.length-1] 下钻
  386. * 直到某节点没有 children 或 children 为空,返回该节点;
  387. * list 为空返回 null
  388. */
  389. findFirstTreeLastLeafNode(list) {
  390. if (!Array.isArray(list) || list.length === 0) return null;
  391. let node = list[0];
  392. while (node && Array.isArray(node.children) && node.children.length > 0) {
  393. node = node.children[node.children.length - 1];
  394. }
  395. return node || null;
  396. },
  397. /**
  398. * 通过 subjectCode 从 localStorage.student_subjectList 匹配学科名称
  399. * 并写入 this.subjectNameState
  400. */
  401. resolveSubjectNameByCode() {
  402. try {
  403. const subjectCode = String(this.subjectCode ?? '');
  404. if (!subjectCode) return;
  405. const raw = localStorage.getItem('student_subjectList');
  406. if (!raw) return;
  407. const list = JSON.parse(raw);
  408. if (!Array.isArray(list) || list.length === 0) return;
  409. const match = list.find(item => {
  410. const val = String(item.value ?? item.code ?? item.subjectCode ?? item.id ?? '');
  411. return val === subjectCode;
  412. });
  413. if (match) {
  414. const name = match.label ?? match.name ?? match.subjectName ?? String(match);
  415. if (name) {
  416. this.subjectNameState = name;
  417. return;
  418. }
  419. }
  420. // 兜底:取第一个
  421. const first = list[0];
  422. if (first) {
  423. const fallbackName = first.label ?? first.name ?? first.subjectName ?? String(first);
  424. if (fallbackName) this.subjectNameState = fallbackName;
  425. }
  426. } catch (e) {
  427. console.warn('[KnowledgeGraph] 解析 localStorage.student_subjectList 失败:', e);
  428. }
  429. },
  430. /**
  431. * 递归遍历 knowList,确保每个节点具备环形图需要的字段:
  432. * - knowledgeId / knowledgeName / knowledgeType
  433. * - personalScoreRate → 同时映射为 classScoreRate、gradeScoreRate(组件读取的是这两个)
  434. * - isJoin 原样保留(子组件需要据此显示"已加入练习")
  435. * - children 递归处理
  436. */
  437. mapKnowListItem(list) {
  438. if (!Array.isArray(list)) return [];
  439. return list.map(item => {
  440. const node = { ...(item || {}) };
  441. // 兼容字段
  442. node.knowledgeId = node.knowledgeId ?? node.id;
  443. node.knowledgeName = node.knowledgeName ?? node.name ?? node.label;
  444. node.knowledgeType = Number(node.knowledgeType ?? node.type ?? 3);
  445. const pRate = node.personalScoreRate ?? node.scoreRate ?? node.personalRate;
  446. if (pRate !== null && pRate !== undefined) {
  447. // 环形扩散图 generateTreeData 中会按 classLevel / mode 读取 classScoreRate 或 gradeScoreRate
  448. // 为了兼容两种路径,把 personalScoreRate 同时复制到两者
  449. if (node.classScoreRate === null || node.classScoreRate === undefined) {
  450. node.classScoreRate = Number(pRate);
  451. }
  452. if (node.gradeScoreRate === null || node.gradeScoreRate === undefined) {
  453. node.gradeScoreRate = Number(pRate);
  454. }
  455. node.personalScoreRate = Number(pRate);
  456. }
  457. if (node.children && node.children.length > 0) {
  458. node.children = this.mapKnowListItem(node.children);
  459. }
  460. return node;
  461. });
  462. },
  463. /**
  464. * 将 knowList(树形)拍平为一维数组(保留 isJoin 等全部字段)
  465. */
  466. flattenKnowList(list, out = []) {
  467. if (!Array.isArray(list)) return out;
  468. list.forEach(item => {
  469. out.push(item);
  470. if (item.children && item.children.length > 0) {
  471. this.flattenKnowList(item.children, out);
  472. }
  473. });
  474. return out;
  475. },
  476. // 年级切换(多选)
  477. handleGradeChange(gradeList) {
  478. this.currentGradeList = gradeList;
  479. this.$emit('grade-change', gradeList);
  480. // 年级变化后重新请求知识图谱 + 分区数据
  481. this.fetchKnowledgeGraph();
  482. this.fetchKnowledgePartition();
  483. },
  484. // 计算容器高度
  485. calculateContainerHeight() {
  486. if (this.$refs.knowledgeGraph) {
  487. // 获取容器距离视口顶部的距离
  488. const rect = this.$refs.knowledgeGraph.getBoundingClientRect();
  489. // 计算容器最大高度:窗口高度 - 容器距离顶部的距离 - 底部留白
  490. const maxHeight = window.innerHeight - rect.top - 20;
  491. // 设置最小高度为400px
  492. this.containerHeight = Math.max(maxHeight, 400);
  493. }
  494. },
  495. // 处理图例变化
  496. handleLegendChange(data) {
  497. this.$emit('legend-change', data);
  498. },
  499. // 处理tab切换
  500. handleActiveTabChange(tab) {
  501. this.$emit('activeTabChange', tab);
  502. },
  503. // 处理知识点点击(来自环形扩散图组件:圆点被用户点击或默认选中)
  504. handleKnowledgeItemClick(data) {
  505. // 兼容两种 payload 结构:
  506. // 1) 圆点点击 / setDefaultSelection 新结构:{ knowledgeId, knowledgeName, ... } (顶层)
  507. // 2) 旧的 item 包装结构:{ item: {knowledgeId,...}, index: N }
  508. let knowledgeId = data?.knowledgeId;
  509. if ((knowledgeId === undefined || knowledgeId === null || knowledgeId === '')
  510. && data?.item) {
  511. knowledgeId = data.item.knowledgeId;
  512. }
  513. if (knowledgeId !== undefined && knowledgeId !== null && knowledgeId !== '') {
  514. this.currentLinkedKnowledgeId = String(knowledgeId);
  515. }
  516. this.$emit('knowledge-item-click', data);
  517. },
  518. // 处理区域卡片中某行条目被点击(来自 region 组件)→ 反向联动到环形扩散图对应圆点高亮 + 右侧数据
  519. handleRegionItemClick(item) {
  520. const id = item?.knowledgeId;
  521. if (id === undefined || id === null || id === '') return;
  522. this.currentLinkedKnowledgeId = String(id);
  523. },
  524. // 处理加入练习
  525. // 参数来源:
  526. // - 环形扩散图按钮:emit 传出 { knowledgeId, knowledgeName, personalScoreRate?, knowledgeType? }
  527. // - region 卡片按钮:emit 传出整条 item(含 knowledgeId/Name/personalScoreRate/knowledgeType/examNum...)
  528. handleAddPractice(item) {
  529. if (!item) {
  530. this.$message.warning('未获取到知识点信息');
  531. return;
  532. }
  533. const knowledgeId = item.knowledgeId ?? item.id;
  534. const knowledgeName = item.knowledgeName ?? item.name ?? item.label ?? '';
  535. if (!knowledgeId || !knowledgeName) {
  536. this.$message.warning('知识点信息不完整,请稍后重试');
  537. return;
  538. }
  539. // 已经加过的直接返回,避免重复请求
  540. if (this.addedKnowledgeIds[String(knowledgeId)]) {
  541. this.$message.info('该知识点已加入练习');
  542. return;
  543. }
  544. // 得分率:优先 personalScoreRate → gradeScoreRate → classScoreRate
  545. let personalScoreRate = item.personalScoreRate;
  546. if (personalScoreRate === null || personalScoreRate === undefined || personalScoreRate === '') {
  547. personalScoreRate = item.gradeScoreRate ?? item.classScoreRate;
  548. }
  549. if (personalScoreRate === null || personalScoreRate === undefined || personalScoreRate === '') {
  550. personalScoreRate = 0;
  551. } else {
  552. personalScoreRate = Number(personalScoreRate);
  553. if (isNaN(personalScoreRate)) personalScoreRate = 0;
  554. }
  555. // 知识点类型:1薄弱 2一般 3优秀
  556. let knowledgeType = Number(item.knowledgeType ?? item.type ?? 0);
  557. if (isNaN(knowledgeType)) knowledgeType = 0;
  558. const subjectCode = this.subjectCode || '';
  559. const levelCode = localStorage.getItem('student_level_code') || '';
  560. // 年级CODE:取 gradeList 数组中最后一个年级的 value
  561. // - 多个年级(如[{高一,10},{高二,11}])→ 取最后一个(高二.value = 11)
  562. // - 只有一个年级 → 直接取该年级 value
  563. // - 空/无效 → 传空串兜底
  564. let gradeCode = '';
  565. let gradeName = '';
  566. let schoolYearId = '';
  567. if (Array.isArray(this.gradeList) && this.gradeList.length > 0) {
  568. const lastGrade = this.gradeList[this.gradeList.length - 1];
  569. if (lastGrade && lastGrade.value !== undefined && lastGrade.value !== null && lastGrade.value !== '') {
  570. gradeCode = String(lastGrade.value);
  571. }
  572. // 年级NAME:与 gradeCode 一一对应,取同一元素的 label
  573. if (lastGrade && lastGrade.label !== undefined && lastGrade.label !== null) {
  574. gradeName = String(lastGrade.label);
  575. }
  576. const raw = JSON.parse(localStorage.getItem('student_gradesList'));
  577. schoolYearId = raw?.[0]?.schoolYearId || '';
  578. }
  579. const subjectName = this.subjectName || '';
  580. const params = {
  581. knowledgeId,
  582. knowledgeName,
  583. personalScoreRate,
  584. knowledgeType,
  585. subjectCode,
  586. subjectName,
  587. levelCode,
  588. gradeCode,
  589. gradeName,
  590. schoolYearId
  591. };
  592. this.$api.target.addExercise(params).then(res => {
  593. if (res && (res.code == 200 || res.code == 0)) {
  594. // 成功:提示 + 记录 ID(用 $set 保证响应式,region/circularDiffusion 会自动重新渲染并置灰按钮)
  595. this.$message.success('已加入练习成功');
  596. this.$set(this.addedKnowledgeIds, String(knowledgeId), true);
  597. } else {
  598. const msg = (res && res.message) || '加入练习失败';
  599. this.$message.error(msg);
  600. }
  601. }).catch(err => {
  602. console.error('[KnowledgeGraph] addExercise 接口异常:', err);
  603. this.$message.error('加入练习失败,请稍后重试');
  604. });
  605. }
  606. }
  607. };
  608. </script>
  609. <style lang="scss" scoped>
  610. .knowledge_graph {
  611. overflow-y: auto;
  612. overflow-x: hidden;
  613. .graph_top {
  614. background: #FFFFFF;
  615. border-radius: 10px;
  616. padding: 20px;
  617. }
  618. /* 顶部信息模块 */
  619. .knowledge_header {
  620. margin-bottom: 10px;
  621. .header_top {
  622. display: flex;
  623. align-items: center;
  624. gap: 20px;
  625. margin-bottom: 10px;
  626. .header_title {
  627. font-size: 16px;
  628. font-weight: 500;
  629. color: #333;
  630. }
  631. .el-checkbox-group{
  632. .el-checkbox{
  633. margin-right:20px;
  634. }
  635. }
  636. }
  637. .header_stats {
  638. font-size: 14px;
  639. .stats_text {
  640. color: #999999;
  641. }
  642. .stats_num {
  643. color: #2E64FA;
  644. }
  645. .stats_weak {
  646. color: #F56C6C;
  647. }
  648. .stats_good {
  649. color: #FAC858;
  650. }
  651. }
  652. }
  653. /* 自定义滚动条样式 */
  654. &::-webkit-scrollbar {
  655. width: 6px;
  656. }
  657. &::-webkit-scrollbar-track {
  658. background: #f1f1f1;
  659. border-radius: 3px;
  660. }
  661. &::-webkit-scrollbar-thumb {
  662. background: #c1c1c1;
  663. border-radius: 3px;
  664. &:hover {
  665. background: #a8a8a8;
  666. }
  667. }
  668. }
  669. </style>