You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

366 lines
9.4 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. <template>
  2. <div class="container">
  3. <div class="main">
  4. <div class="content">
  5. <div class="title">
  6. {{ questionList[0].name }}
  7. </div>
  8. <template v-for="(question, questionIndex) in questionList" :key="questionIndex">
  9. <!-- 单选 -->
  10. <template v-if="question.type == 1">
  11. <div class="question">
  12. {{ questionIndex + 1 }}. {{ question.description }}单选
  13. </div>
  14. <div v-for="(answer, index) in question.content" :key="index" class="answer">
  15. <div class="selected">
  16. <input :id="`select-${questionIndex}-${index}`" :name="`radio-group-${questionIndex}`" type="radio"
  17. v-model="selectedAnswers[questionIndex]"
  18. :value="answer.text"
  19. >
  20. <label :for="`select-${questionIndex}-${index}`">{{ answer.text }}</label>
  21. </div>
  22. </div>
  23. </template>
  24. <!-- 多选 -->
  25. <template v-if="question.type == 2">
  26. <div class="question">
  27. {{ questionIndex + 1 }}. {{ question.description }}多选
  28. </div>
  29. <div v-for="(answer, index) in question.content" :key="index" class="answer">
  30. <div class="selected">
  31. <input :id="`checkbox-${questionIndex}-${index}`" name="checkbox" type="checkbox"
  32. v-model="selectedAnswers[questionIndex]"
  33. :value="answer.text"
  34. >
  35. <label :for="`checkbox-${questionIndex}-${index}`">{{ answer.text }}</label>
  36. </div>
  37. </div>
  38. </template>
  39. <!-- 简答题 -->
  40. <template v-if="question.type == 3">
  41. <div class="Short-answers">
  42. <div class="question">
  43. {{ questionIndex + 1 }}. {{ question.description }}
  44. </div>
  45. <el-input
  46. v-model="editorTitles[questionIndex]"
  47. style="width: 100%; padding: 3.5%"
  48. size="large"
  49. placeholder="请输入标题(5-100字符)"
  50. maxlength="100"
  51. minlength="5"
  52. show-word-limit
  53. />
  54. <div class="editor">
  55. <div style="border: 1px solid #ccc">
  56. <Toolbar
  57. style="border-bottom: 1px solid #ccc"
  58. :editor="editorRefs[questionIndex]"
  59. :defaultConfig="toolbarConfig"
  60. :mode="mode"
  61. />
  62. <Editor
  63. style="height: 500px; overflow-y: hidden;"
  64. v-model="editorContents[questionIndex]"
  65. :defaultConfig="editorConfig"
  66. :mode="mode"
  67. @onCreated="editor => handleCreated(editor, questionIndex)"
  68. />
  69. </div>
  70. </div>
  71. </div>
  72. </template>
  73. </template>
  74. <!-- 提交按钮 -->
  75. <div class="submit">
  76. <img src="https://d31zlh4on95l9h.cloudfront.net/images/5iujb101000d2foxm6thylaa50qz6lgl.png" @click="submit">
  77. </div>
  78. <!-- 提交次数 -->
  79. <div class="submit-times">
  80. <span>您已提交{{sub}}每个作业可以提交3次</span>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </template>
  86. <script setup>
  87. import { onMounted, ref, shallowRef } from 'vue';
  88. import homeworkApi from "@/api/HomeworkApi.js";
  89. import { ElMessage } from "element-plus";
  90. import '@wangeditor/editor/dist/css/style.css' // 引入 css
  91. import { onBeforeUnmount } from 'vue'
  92. import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  93. import {useRoute, useRouter} from "vue-router";
  94. const router = useRouter();
  95. const route = useRoute();
  96. let groupId = route.params.id;
  97. let sub = route.params.sub;
  98. // 编辑器实例,必须用 shallowRef
  99. const editorRefs = ref([]);
  100. // 内容 HTML
  101. const editorContents = ref([]);
  102. const editorTitles = ref([]);
  103. // 模拟 ajax 异步获取内容
  104. onMounted(() => {
  105. setTimeout(() => {
  106. editorContents.value = editorContents.value.map(() => '');
  107. }, 1500)
  108. })
  109. const toolbarConfig = {
  110. excludeKeys: [
  111. 'justify',
  112. 'table',
  113. 'codeBlock',
  114. 'divider',
  115. 'undo',
  116. 'redo',
  117. 'insertImage', // 去掉插入图片
  118. 'insertLink', // 去掉插入链接
  119. 'insertTable', // 去掉插入表格
  120. 'video',
  121. 'group-more-style',
  122. 'group-image',
  123. 'group-video',
  124. 'fullScreen',
  125. 'todo'
  126. ],
  127. insertKeys: {
  128. index: 0,
  129. keys: ['bold', 'italic']
  130. }
  131. }
  132. const editorConfig = {
  133. placeholder: '请输入内容...',
  134. toolbarKeys: toolbarConfig.toolbarKeys
  135. }
  136. // 组件销毁时,也及时销毁编辑器
  137. onBeforeUnmount(() => {
  138. editorRefs.value.forEach(editor => {
  139. if (editor == null) return;
  140. editor.destroy();
  141. });
  142. })
  143. const handleCreated = (editor, questionIndex) => {
  144. editorRefs.value[questionIndex] = editor; // 记录 editor 实例
  145. editor.config.toolbarConfig = toolbarConfig; // 确保配置正确应用
  146. }
  147. const questionList = ref([]);
  148. function getQuestionList() {
  149. console.log("ljghasjkhdjksahjkhsajkhjkasd",groupId);
  150. homeworkApi.getHomeworkQuestion(groupId).then(resp => {
  151. if (resp.code == 200) {
  152. questionList.value = resp.data;
  153. for (let i = 0; i < questionList.value.length; i++) {
  154. questionList.value[i].content = JSON.parse(questionList.value[i].content);
  155. // 初始化 editorTitles 和 editorContents
  156. editorTitles.value.push('');
  157. editorContents.value.push('');
  158. // 初始化 selectedAnswers 为数组
  159. selectedAnswers.value.push([]);
  160. }
  161. } else {
  162. ElMessage.error("未知错误,请联系管理员");
  163. }
  164. });
  165. }
  166. getQuestionList();
  167. // 提交作业
  168. const homework = ref([]);
  169. const selectedAnswers = ref([]);
  170. questionList.value.forEach(() => {
  171. selectedAnswers.value.push([]);
  172. });
  173. // 上一个页面传递的参数 groupId
  174. function submit() {
  175. homework.value = [];
  176. for (let i = 0; i < questionList.value.length; i++) {
  177. if (questionList.value[i].type == 1 || questionList.value[i].type == 2) {
  178. homework.value.push({
  179. "id": questionList.value[i].id,
  180. "answer": selectedAnswers.value[i],
  181. "type": questionList.value[i].type
  182. });
  183. } else if (questionList.value[i].type == 3) {
  184. const editor = editorRefs.value[i];
  185. const plainText = editor ? editor.getText() : '';
  186. homework.value.push({
  187. "id": questionList.value[i].id,
  188. "answer": [editorTitles.value[i], plainText],
  189. "type": questionList.value[i].type
  190. });
  191. }
  192. }
  193. let groupId = route.params.id;
  194. let sub = route.params.sub;
  195. if (sub <=2){
  196. homeworkApi.submitHomework(homework.value, groupId).then(resp => {
  197. if (resp.code == 200) {
  198. ElMessage.success("提交成功");
  199. homework.value = [];
  200. router.push("/show");
  201. } else {
  202. ElMessage.error("未知错误,请联系管理员");
  203. }
  204. });
  205. }else {
  206. ElMessage.error("提交失败您,已提交3次,每个作业可以提交3次");
  207. }
  208. }
  209. </script>
  210. <style scoped>
  211. .container {
  212. text-align: center;
  213. margin-left: auto;
  214. margin-right: auto;
  215. width: 34.375rem;
  216. height: 114.399375rem;
  217. }
  218. .main {
  219. background-image: url("https://lfjf.rzfwq.com/jtzy/Product/pcjingwang/images/20230823/qiangdianbanHomeWorkBg.png");
  220. background-size: contain;
  221. background-position: center 0;
  222. background-repeat: no-repeat;
  223. margin: 0;
  224. width: 100%;
  225. height: 1830px;
  226. text-align: center;
  227. position: relative;
  228. }
  229. .content {
  230. position: absolute;
  231. width: 93%;
  232. height: 80%;
  233. overflow-y: scroll;
  234. /* 居中显示 */
  235. left: 50%;
  236. transform: translate(-50%, 0);
  237. top: 12.5%;
  238. text-align: left;
  239. }
  240. /* 滚动条样式 */
  241. .content::-webkit-scrollbar {
  242. width: 3px;
  243. }
  244. .content::-webkit-scrollbar-track {
  245. background: #f1f1f1; /* 滚动条轨道的背景色 */
  246. }
  247. .content::-webkit-scrollbar-thumb {
  248. background: #d9d9d9; /* 滚动条滑块的颜色 */
  249. border-radius: 6px; /* 滑块的圆角 */
  250. }
  251. .title {
  252. font-size: 1.65rem;
  253. font-weight: 600;
  254. /* 居中显示 */
  255. text-align: center;
  256. /*字体颜色*/
  257. color: #3f27b1;
  258. }
  259. .question {
  260. padding-top: 10%;
  261. padding-left: 4%;
  262. font-size: 1.35rem;
  263. font-weight: 500;
  264. }
  265. .answer {
  266. padding-top: 2.5%;
  267. padding-left: 7%;
  268. font-size: 1.35rem;
  269. }
  270. .selected {
  271. display: block;
  272. padding: 1% 0 1.2% 0;
  273. }
  274. .selected input[name="checkbox"] {
  275. margin-right: 1%;
  276. width: 1.2rem;
  277. height: 1rem;
  278. /* 透明度 */
  279. opacity: 0.8;
  280. cursor: pointer; /* 鼠标悬停时变成手的形状 */
  281. }
  282. .selected input[type="radio"]{
  283. margin-right: 1%;
  284. width: 1.2rem;
  285. height: 1rem;
  286. /* 透明度 */
  287. opacity: 0.8;
  288. cursor: pointer; /* 鼠标悬停时变成手的形状 */
  289. }
  290. /* 单选按钮选中时的样式 */
  291. .selected input[type="radio"]:checked {
  292. accent-color: blue; /* 设置选中时的颜色为蓝色 */
  293. }
  294. /* 多选按钮选中时的样式 */
  295. .selected input[type="checkbox"]:checked {
  296. accent-color: blue; /* 设置选中时的颜色为蓝色 */
  297. }
  298. .selected label {
  299. cursor: pointer;
  300. }
  301. .editor {
  302. padding-top: 2.5%;
  303. padding-left: 3.5%;
  304. padding-right: 3.5%;
  305. height: 30rem;
  306. }
  307. .Short-answers {
  308. height: 48rem;
  309. }
  310. .submit {
  311. display: block;
  312. text-align: center;
  313. }
  314. .submit img {
  315. padding-top: 2.5%;
  316. padding-left: 3.5%;
  317. padding-right: 3.5%;
  318. cursor: pointer;
  319. width: 67%;
  320. }
  321. .submit-times {
  322. font-size: 0.8rem;
  323. color: #858585;
  324. text-align: center;
  325. }
  326. </style>