create.vue 7.9 KB

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