create.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <template>
  2. <main class="create">
  3. <div class="create-operate">
  4. <div class="left-operate">
  5. <a
  6. :class="['pre', { disabled: curIndex === 0 }]"
  7. :disabled="curIndex === 0"
  8. @click="selectExerciseItem(curIndex - 1)"
  9. >
  10. <i class="el-icon-back"></i><span>上一题</span>
  11. </a>
  12. <a
  13. :class="['next', { disabled: curIndex === indexList.length - 1 || indexList.length === 0 }]"
  14. @click="selectExerciseItem(curIndex + 1)"
  15. >
  16. <span>下一题</span><i class="el-icon-right"></i>
  17. </a>
  18. </div>
  19. <div class="save-tip">
  20. <span>{{ curSaveDate }} </span><span>已自动保存</span>
  21. <span class="now-save" @click="saveQuestion">立即保存</span>
  22. </div>
  23. <div class="right-operate">
  24. <span class="line"></span>
  25. <a class="preview" @click="setPreview"><SvgIcon icon-class="eye" /><span>预览</span></a>
  26. <a class="delete" @click="deleteQuestion"><SvgIcon icon-class="delete" /><span>删除</span></a>
  27. </div>
  28. </div>
  29. <div class="create-content">
  30. <QuestionHeader v-if="indexList.length > 0" :type="exerciseTypeList[indexList[curIndex].type]" />
  31. <template v-for="{ id } in indexList">
  32. <component
  33. :is="curExercisePage"
  34. v-if="id === indexList[curIndex].id"
  35. :key="id"
  36. ref="exercise"
  37. :question-id="id"
  38. />
  39. </template>
  40. </div>
  41. </main>
  42. </template>
  43. <script>
  44. import { SaveQuestion } from '@/api/exercise';
  45. import { timeFormatConversion } from '@/utils/transform';
  46. import { exerciseTypeList } from '@/views/exercise_questions/data/questionType';
  47. import QuestionHeader from './common/QuestionHeader.vue';
  48. import SelectQuestion from './exercises/SelectQuestion.vue';
  49. import JudgeQuestion from './exercises/JudgeQuestion.vue';
  50. import MatchingQuestion from './exercises/MatchingQuestion.vue';
  51. import FillQuestion from './exercises/FillQuestion.vue';
  52. import ReadAloudQuestion from './exercises/ReadAloudQuestion.vue';
  53. import ChineseQuestion from './exercises/ChineseQuestion.vue';
  54. import WriteQuestion from './exercises/WriteQuestion.vue';
  55. import DialogueQuestion from './exercises/DialogueQuestion.vue';
  56. import TalkPictureQuestion from './exercises/TalkPictureQuestion.vue';
  57. import ChooseToneQuestion from './exercises/ChooseToneQuestion.vue';
  58. import RepeatQuestion from './exercises/RepeatQuestion.vue';
  59. import ReadQuestion from './exercises/ReadQuestion.vue';
  60. import SortQuestion from './exercises/SortQuestion.vue';
  61. import ListenSelectQuestion from './exercises/ListenSelectQuestion.vue';
  62. import ListenJudgeQuestion from './exercises/ListenJudgeQuestion.vue';
  63. import ListenFillQuestion from './exercises/ListenFillQuestion.vue';
  64. import WordCardQuestion from './exercises/WordCardQuestion.vue';
  65. import AnswerQuestion from './exercises/AnswerQuestion.vue';
  66. import WritePictureQuestion from './exercises/WritePictureQuestion.vue';
  67. import ReplaceAnswerQuestion from './exercises/ReplaceAnswerQuestion.vue';
  68. import EssayQuestion from './exercises/EssayQuestion.vue';
  69. import TableFillQuestion from './exercises/TableFillQuestion.vue';
  70. import WordDictationQuestion from './exercises/WordDictationQuestion.vue';
  71. import ActivityQuestion from './exercises/ActivityQuestion.vue';
  72. export default {
  73. name: 'CreateMain',
  74. components: {
  75. QuestionHeader,
  76. },
  77. provide() {
  78. return {
  79. curSaveDate: () => this.curSaveDate,
  80. recognition: this.recognition,
  81. };
  82. },
  83. props: {
  84. curIndex: {
  85. type: Number,
  86. required: true,
  87. },
  88. indexList: {
  89. type: Array,
  90. required: true,
  91. },
  92. loading: {
  93. type: Boolean,
  94. required: true,
  95. },
  96. },
  97. data() {
  98. return {
  99. exerciseTypeList,
  100. timer: null,
  101. curSaveDate: '',
  102. exerciseComponents: {
  103. select: SelectQuestion,
  104. judge: JudgeQuestion,
  105. matching: MatchingQuestion,
  106. fill: FillQuestion,
  107. read_aloud: ReadAloudQuestion,
  108. chinese: ChineseQuestion,
  109. write: WriteQuestion,
  110. dialogue: DialogueQuestion,
  111. talk_picture: TalkPictureQuestion,
  112. choose_tone: ChooseToneQuestion,
  113. repeat: RepeatQuestion,
  114. read: ReadQuestion,
  115. sort: SortQuestion,
  116. listen_select: ListenSelectQuestion,
  117. listen_judge: ListenJudgeQuestion,
  118. listen_fill: ListenFillQuestion,
  119. word_card: WordCardQuestion,
  120. answer_question: AnswerQuestion,
  121. write_picture: WritePictureQuestion,
  122. replace_answer: ReplaceAnswerQuestion,
  123. essay_question: EssayQuestion,
  124. table_fill: TableFillQuestion,
  125. word_dictation: WordDictationQuestion,
  126. activity: ActivityQuestion,
  127. },
  128. };
  129. },
  130. computed: {
  131. curExercisePage() {
  132. if (this.indexList.length === 0) return '';
  133. return this.exerciseComponents[this.indexList[this.curIndex].type];
  134. },
  135. },
  136. mounted() {
  137. this.resetSaveDate();
  138. },
  139. beforeDestroy() {
  140. this.saveQuestion();
  141. clearInterval(this.timer);
  142. },
  143. methods: {
  144. // 重置保存时间
  145. resetSaveDate() {
  146. if (this.timer) {
  147. clearInterval(this.timer);
  148. }
  149. this.timer = setInterval(() => {
  150. this.saveQuestion();
  151. }, 30000);
  152. },
  153. // 清除保存时间
  154. clearSaveDate() {
  155. clearInterval(this.timer);
  156. },
  157. /**
  158. * 选择练习
  159. * @param {number} i 练习索引
  160. */
  161. selectExerciseItem(i) {
  162. if (this.indexList.length <= 0) return;
  163. this.$emit('selectExerciseItem', i);
  164. },
  165. /**
  166. * 预览
  167. */
  168. setPreview() {
  169. this.$emit('setPreview');
  170. },
  171. /**
  172. * 删除
  173. */
  174. deleteQuestion() {
  175. this.$confirm('是否删除当前题目', '提示', {
  176. confirmButtonText: '确定',
  177. cancelButtonText: '取消',
  178. type: 'warning',
  179. })
  180. .then(() => {
  181. this.$emit('deleteQuestion');
  182. })
  183. .catch(() => {});
  184. },
  185. /**
  186. * 数据格式转换
  187. */
  188. dataConversion() {
  189. const data = this.$refs.exercise[0].data;
  190. const { select_type } = data.property;
  191. return {
  192. question_id: this.indexList[this.curIndex].id,
  193. type: data.type,
  194. additional_type: select_type || '',
  195. content: JSON.stringify(data),
  196. };
  197. },
  198. /**
  199. * 保存题目
  200. */
  201. async saveQuestion() {
  202. if (this.indexList.length <= 0) return;
  203. if (this.loading) return;
  204. try {
  205. let childrenExercise = this.$refs.exercise[0].$refs.exercise;
  206. if (childrenExercise) {
  207. childrenExercise.forEach((item) => {
  208. item?.saveChildQuestion();
  209. });
  210. }
  211. await SaveQuestion(this.dataConversion());
  212. const curDate = timeFormatConversion(new Date());
  213. if (this.curSaveDate !== curDate) {
  214. this.curSaveDate = curDate;
  215. }
  216. } catch (err) {
  217. console.log(err);
  218. }
  219. },
  220. /**
  221. * 智能识别
  222. * @param {String} text
  223. */
  224. recognition(text) {
  225. this.$refs.exercise[0]?.recognition(text);
  226. },
  227. },
  228. };
  229. </script>
  230. <style lang="scss" scoped>
  231. .create {
  232. padding: 16px 24px;
  233. overflow: hidden;
  234. background-color: $main-background-color;
  235. &-operate {
  236. display: flex;
  237. align-items: center;
  238. justify-content: space-between;
  239. .left-operate {
  240. .pre,
  241. .next {
  242. display: inline-flex;
  243. column-gap: 8px;
  244. align-items: center;
  245. height: 32px;
  246. padding: 5px 8px;
  247. margin-left: 8px;
  248. font-size: 14px;
  249. color: #2f3742;
  250. cursor: pointer;
  251. &:hover {
  252. color: $main-color;
  253. background-color: #f4f8ff;
  254. }
  255. &.disabled {
  256. color: #999;
  257. cursor: not-allowed;
  258. &:hover {
  259. color: #999;
  260. background-color: $main-background-color;
  261. }
  262. }
  263. }
  264. }
  265. .save-tip {
  266. font-size: 12px;
  267. color: #858585;
  268. .now-save {
  269. margin-left: 8px;
  270. color: $main-color;
  271. cursor: pointer;
  272. }
  273. }
  274. .right-operate {
  275. display: flex;
  276. column-gap: 8px;
  277. align-items: center;
  278. %setting,
  279. .setting {
  280. display: flex;
  281. column-gap: 8px;
  282. align-items: center;
  283. padding: 4px 8px;
  284. color: #2f3742;
  285. cursor: pointer;
  286. border-radius: 2px;
  287. &:hover {
  288. background-color: $border-color;
  289. }
  290. }
  291. .preview {
  292. @extend %setting;
  293. color: #175dff;
  294. }
  295. .delete {
  296. @extend %setting;
  297. color: $danger-color;
  298. &:hover {
  299. background-color: $main-background-color;
  300. }
  301. }
  302. > span {
  303. color: #999;
  304. cursor: pointer;
  305. }
  306. }
  307. }
  308. &-content {
  309. width: 100%;
  310. height: calc(100% - 32px);
  311. margin-top: 16px;
  312. overflow: auto;
  313. }
  314. }
  315. </style>