index.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { defineComponent, onMounted, reactive, watch } from "vue";
  2. import styles from "./index.module.less";
  3. // import { verifyMembershipServices } from "../vip-verify";
  4. import { api_lessonTrainingSubmitTraining, api_lessonTrainingTrainingStudentDetail } from "../../api";
  5. import state, { handleSetSpeed, hanldeDirectSelection, setSection } from "/src/state";
  6. export default defineComponent({
  7. name: "HomeWork",
  8. props: {
  9. workeData: {
  10. type: Object,
  11. default: () => ({}),
  12. }
  13. },
  14. emits: ["change"],
  15. setup(props, {expose}) {
  16. const training = reactive({
  17. trainingTimes: "",
  18. trainingSpeed: 0,
  19. times: 0,
  20. workRecord: "",
  21. isAddOk: 0,
  22. starTime: 0,
  23. start: "" as any,
  24. end: "" as any,
  25. });
  26. /** 隐藏评测功能 */
  27. const handleHide = () => {
  28. const ids = ["studnetT-0", "studnetT-2", "studnetT-4"];
  29. for (let i = 0; i < ids.length; i++) {
  30. const speedBtn = document.getElementById(ids[i]);
  31. if (speedBtn) {
  32. speedBtn.style.pointerEvents = "none";
  33. speedBtn.style.opacity = ".5";
  34. }
  35. }
  36. };
  37. /** 获取作业详情 */
  38. const getWorkData = async () => {
  39. const workeData = props.workeData;
  40. if (workeData.id) {
  41. let trainingContent: any = {};
  42. try {
  43. trainingContent = JSON.parse(workeData.trainingContent);
  44. } catch (error) {
  45. console.log("🚀 ~ error:", error);
  46. }
  47. training.times = trainingContent.trainingTimes || 0;
  48. training.trainingTimes = (workeData.trainingTimes / 60).toFixed(1) || "0";
  49. training.trainingSpeed = trainingContent.practiceSpeed;
  50. training.start = Number(trainingContent.practiceChapterBegin);
  51. training.end = Number(trainingContent.practiceChapterEnd);
  52. state.userChooseEndIndex = training.end
  53. state.isWorkDone = Number(training.trainingTimes) >= Number(training.times);
  54. if (training.isAddOk === 0) {
  55. // 设置小节
  56. setSection(training.start, training.end, training.trainingSpeed);
  57. }
  58. }
  59. };
  60. const getWorkDetail = async () => {
  61. const res = await api_lessonTrainingTrainingStudentDetail(props.workeData.id);
  62. if (res?.code === 200) {
  63. training.trainingTimes = (res.data.trainingTimes / 60).toFixed(1) || "0";
  64. state.isWorkDone = Number(training.trainingTimes) >= Number(training.times);
  65. }
  66. };
  67. /** 添加作业记录 */
  68. const addHomeworkRecored = async () => {
  69. let total = Math.ceil((Date.now() - training.starTime) / 1000);
  70. try {
  71. const res = await api_lessonTrainingSubmitTraining({
  72. id: props.workeData.id,
  73. trainingTimes: total,
  74. });
  75. if (res?.code == 200) {
  76. getWorkDetail();
  77. }
  78. } catch (error) {}
  79. };
  80. watch(
  81. () => state.playState,
  82. () => {
  83. if (state.playState === "play") {
  84. training.starTime = Date.now();
  85. } else {
  86. addHomeworkRecored();
  87. }
  88. }
  89. );
  90. const handleAdd = () => {
  91. if (state.playState === "play") {
  92. console.log("退出");
  93. addHomeworkRecored();
  94. }
  95. }
  96. onMounted(() => {
  97. handleHide();
  98. getWorkData();
  99. // verifyMembershipServices();
  100. });
  101. expose({
  102. handleAdd,
  103. getWorkData
  104. })
  105. return () => (
  106. <div class={styles.homework}>
  107. {training.trainingTimes} / {training.times} 分钟
  108. </div>
  109. );
  110. },
  111. });