index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <div class="exercise">
  3. <div class="list">
  4. <el-button icon="el-icon-back" @click="back">返回练习管理</el-button>
  5. <div class="list-title">
  6. <el-input v-if="isEditExercise" v-model="exercise.name" @keyup.enter.native="UpdateExerciseName" />
  7. <span v-else class="nowrap-ellipsis">{{ exercise.name }}</span>
  8. <SvgIcon icon-class="edit" class-name="edit" @click="editExercise" />
  9. </div>
  10. <div class="exercise-list">
  11. <ul>
  12. <li v-for="(item, i) in index_list" :key="i" :class="['exercise-item', { active: i === curIndex }]">
  13. <SvgIcon icon-class="child" :size="20" />
  14. <span class="item-name nowrap-ellipsis" @click="selectExerciseItem(i)">
  15. {{ i + 1 }}. {{ exerciseNames[item.type] }}
  16. </span>
  17. <SvgIcon icon-class="copy" class="pointer" :size="10" @click="copy(i)" />
  18. </li>
  19. </ul>
  20. </div>
  21. <div class="list-operate">
  22. <!-- <el-button type="primary">批量导入</el-button> -->
  23. <el-button type="primary" @click="addQuestionToExercise('select', 'single')">新建</el-button>
  24. </div>
  25. </div>
  26. <CreateMain
  27. ref="createMain"
  28. :cur-index="curIndex"
  29. :index-list="index_list"
  30. @selectExerciseItem="selectExerciseItem"
  31. @setUp="setUp"
  32. @setPreview="setPreview"
  33. @deleteQuestion="deleteQuestion"
  34. @createNewQuestion="createNewQuestion"
  35. />
  36. <div class="preview" :style="{ minHeight: preview ? '300px' : 'auto' }">
  37. <div class="preview-header">
  38. <span class="quick-preview">快捷预览:</span>
  39. <div class="preview-right">
  40. <template v-if="preview">
  41. <span class="preview-button plain" @click="refreshPreviewData">
  42. <SvgIcon icon-class="loop" size="14" /><span>刷新</span>
  43. </span>
  44. <span class="preview-button" @click="fullPreview">
  45. <SvgIcon icon-class="eye" /> <span>完整预览</span>
  46. </span>
  47. <span class="preview-button plain" @click="setPreview"><SvgIcon icon-class="close" />关闭预览</span>
  48. </template>
  49. <template v-else>
  50. <span class="preview-button" @click="setPreview"><SvgIcon icon-class="eye" />预览</span>
  51. </template>
  52. </div>
  53. </div>
  54. <div class="preview-content">
  55. <component :is="curPreviewPage" v-if="preview" :data="previewData" :child-preview-data="childPreviewData" />
  56. </div>
  57. </div>
  58. </div>
  59. </template>
  60. <script>
  61. import { exerciseNames } from '../data/common';
  62. import {
  63. AddQuestionToExercise,
  64. DeleteQuestion,
  65. GetExerciseQuestionIndexList,
  66. GetQuestionInfo,
  67. GetExerciseInfo,
  68. UpdateExercise,
  69. } from '@/api/exercise';
  70. import CreateMain from './components/create.vue';
  71. import SelectPreview from '@/views/exercise_questions/preview/SelectPreview.vue';
  72. import JudgePreview from '@/views/exercise_questions/preview/JudgePreview.vue';
  73. import MatchingPreview from '@/views/exercise_questions/preview/MatchingPreview.vue';
  74. import ChinesePreview from '@/views/exercise_questions/preview/ChinesePreview.vue';
  75. import WritePreview from '../preview/WritePreview.vue';
  76. import FillPreview from '../preview/FillPreview.vue';
  77. import ReadAloudPreview from '../preview/ReadAloudPreview.vue';
  78. import DialoguePreview from '../preview/DialoguePreview.vue';
  79. import TalkPictruePreview from '../preview/TalkPictruePreview.vue';
  80. import ChooseTonePreview from '../preview/ChooseTonePreview.vue';
  81. import RepeatPreview from '../preview/RepeatPreview.vue';
  82. import ReadPreview from '../preview/ReadPreview.vue';
  83. import SortPreview from '../preview/SortPreview.vue';
  84. import ListenSelectPreview from '../preview/ListenSelectPreview.vue';
  85. import ListenFillPreview from '../preview/ListenFillPreview.vue';
  86. import ListenJudgePreview from '../preview/ListenJudgePreview.vue';
  87. import WordCardPreview from '../preview/WordCardPreview.vue';
  88. import AnswerQuestionPreview from '../preview/AnswerQuestionPreview.vue';
  89. import WritePictruePreview from '../preview/WritePictruePreview.vue';
  90. export default {
  91. name: 'CreateExercise',
  92. components: {
  93. CreateMain,
  94. SelectPreview,
  95. JudgePreview,
  96. MatchingPreview,
  97. ChinesePreview,
  98. WritePreview,
  99. FillPreview,
  100. ReadAloudPreview,
  101. DialoguePreview,
  102. TalkPictruePreview,
  103. ChooseTonePreview,
  104. RepeatPreview,
  105. ReadPreview,
  106. SortPreview,
  107. ListenSelectPreview,
  108. ListenFillPreview,
  109. ListenJudgePreview,
  110. WordCardPreview,
  111. AnswerQuestionPreview,
  112. WritePictruePreview,
  113. },
  114. provide() {
  115. return {
  116. isSetUp: () => this.isSetUp,
  117. updateCurQuestionType: this.updateCurQuestionType,
  118. exercise_id: this.exercise_id,
  119. refreshPreviewData: this.refreshPreviewData,
  120. };
  121. },
  122. data() {
  123. const { id } = this.$route.query;
  124. return {
  125. exercise_id: id, // 练习id
  126. exercise: { name: '' }, // 练习信息
  127. isEditExercise: false, // 是否编辑练习
  128. curIndex: 0, // 当前练习索引
  129. index_list: [], // 练习列表
  130. exerciseNames, // 练习名称
  131. isSetUp: false, // 设置
  132. preview: false, // 预览显示
  133. previewData: {}, // 预览数据
  134. previewComponents: {
  135. select: SelectPreview,
  136. judge: JudgePreview,
  137. matching: MatchingPreview,
  138. chinese: ChinesePreview,
  139. write: WritePreview,
  140. fill: FillPreview,
  141. read_aloud: ReadAloudPreview,
  142. dialogue: DialoguePreview,
  143. talk_picture: TalkPictruePreview,
  144. choose_tone: ChooseTonePreview,
  145. repeat: RepeatPreview,
  146. read: ReadPreview,
  147. sort: SortPreview,
  148. listen_select: ListenSelectPreview,
  149. listen_fill: ListenFillPreview,
  150. listen_judge: ListenJudgePreview,
  151. word_card: WordCardPreview,
  152. answer_question: AnswerQuestionPreview,
  153. write_picture: WritePictruePreview,
  154. },
  155. };
  156. },
  157. computed: {
  158. curPreviewPage() {
  159. if (this.index_list.length === 0) return '';
  160. return this.previewComponents[this.index_list[this.curIndex].type];
  161. },
  162. },
  163. watch: {
  164. curIndex() {
  165. if (this.index_list.length === 0) return;
  166. this.getQuestionInfo();
  167. },
  168. },
  169. created() {
  170. this.getExerciseQuestionIndexList(true);
  171. this.getExerciseInfo();
  172. },
  173. methods: {
  174. // 返回练习管理
  175. back() {
  176. this.$router.push('/personal_question');
  177. },
  178. /**
  179. * 添加题目到练习
  180. * @param {string} type 题目类型
  181. * @param {string} additional_type 附加类型
  182. * @param {string} content 题目内容
  183. */
  184. addQuestionToExercise(type, additional_type, content = '') {
  185. AddQuestionToExercise({
  186. exercise_id: this.exercise_id,
  187. type,
  188. additional_type,
  189. content,
  190. })
  191. .then(() => {
  192. this.getExerciseQuestionIndexList();
  193. })
  194. .catch(() => {
  195. this.$message.error('添加失败');
  196. });
  197. },
  198. // 创建新题
  199. createNewQuestion() {
  200. if (this.index_list.length === 0) {
  201. this.addQuestionToExercise('select', 'single');
  202. return;
  203. }
  204. let { type, additional_type } = this.index_list[this.curIndex];
  205. this.addQuestionToExercise(type, additional_type);
  206. },
  207. /**
  208. * 获取练习题目索引列表
  209. */
  210. getExerciseQuestionIndexList(init) {
  211. GetExerciseQuestionIndexList({ exercise_id: this.exercise_id })
  212. .then(({ index_list }) => {
  213. this.index_list = index_list;
  214. if (!init) return;
  215. this.getQuestionInfo();
  216. })
  217. .catch((err) => {
  218. console.log(err);
  219. });
  220. },
  221. getExerciseInfo() {
  222. GetExerciseInfo({ exercise_id: this.exercise_id }).then(({ exercise }) => {
  223. this.exercise = exercise;
  224. });
  225. },
  226. editExercise() {
  227. if (this.isEditExercise) {
  228. return this.UpdateExerciseName();
  229. }
  230. this.isEditExercise = !this.isEditExercise;
  231. },
  232. UpdateExerciseName() {
  233. UpdateExercise(this.exercise)
  234. .then(() => {
  235. this.isEditExercise = false;
  236. })
  237. .catch(() => {
  238. this.$message.error('修改失败');
  239. });
  240. },
  241. /**
  242. * 获取题目信息
  243. */
  244. getQuestionInfo() {
  245. if (this.index_list.length === 0) return;
  246. GetQuestionInfo({ question_id: this.index_list[this.curIndex].id })
  247. .then(({ question }) => {
  248. if (!question.content) return;
  249. this.$nextTick(() => {
  250. this.$refs.createMain.$refs.exercise?.[0].setQuestion(question);
  251. this.refreshPreviewData();
  252. });
  253. })
  254. .catch(() => {});
  255. },
  256. /**
  257. * 复制练习
  258. * @param {Number} index 练习索引
  259. */
  260. copy(index) {
  261. this.$confirm('是否复制当前题目', '提示', {
  262. confirmButtonText: '确定',
  263. cancelButtonText: '取消',
  264. type: 'warning',
  265. })
  266. .then(() => {
  267. GetQuestionInfo({ question_id: this.index_list[index].id }).then(
  268. ({ question: { type, additional_type, content } }) => {
  269. this.addQuestionToExercise(type, additional_type, content);
  270. },
  271. );
  272. })
  273. .catch(() => {});
  274. },
  275. /**
  276. * 选择练习
  277. * @param {Number} index 练习索引
  278. * @param {object} data 练习数据
  279. */
  280. selectExerciseItem(index, data) {
  281. if (index < 0 || index > this.index_list.length - 1) return;
  282. this.curIndex = index;
  283. this.$refs.createMain.resetSaveDate();
  284. this.$refs.createMain.saveQuestion(data);
  285. },
  286. // 设置
  287. setUp() {
  288. this.isSetUp = !this.isSetUp;
  289. },
  290. // 刷新预览数据
  291. refreshPreviewData() {
  292. this.previewData = this.$refs.createMain.$refs.exercise?.[0].data || {};
  293. this.childPreviewData = this.$refs.createMain.$refs.exercise?.[0].childPreviewData || [];
  294. },
  295. // 完整预览
  296. fullPreview() {
  297. window.open(
  298. this.$router.resolve({
  299. path: '/answer',
  300. query: {
  301. id: this.exercise_id,
  302. },
  303. }).href,
  304. '_blank',
  305. );
  306. },
  307. // 预览
  308. setPreview() {
  309. this.preview = !this.preview;
  310. this.refreshPreviewData();
  311. },
  312. // 删除练习
  313. deleteQuestion() {
  314. DeleteQuestion({ question_id: this.index_list[this.curIndex].id })
  315. .then(() => {
  316. this.curIndex = Math.max(0, this.curIndex - 1);
  317. this.getExerciseQuestionIndexList();
  318. this.$message.success('删除成功');
  319. })
  320. .catch(() => {
  321. this.$message.error('删除失败');
  322. });
  323. },
  324. /**
  325. * 修改当前题目类型
  326. * @param {Array} arr
  327. */
  328. updateCurQuestionType(arr) {
  329. let type = arr[arr.length - 1];
  330. this.index_list[this.curIndex].type = type;
  331. },
  332. },
  333. };
  334. </script>
  335. <style lang="scss" scoped>
  336. .exercise {
  337. display: grid;
  338. grid-template: 1fr auto / 224px 1fr;
  339. height: 100%;
  340. .list {
  341. display: flex;
  342. flex-direction: column;
  343. grid-area: 1 / 1 / -1;
  344. row-gap: 16px;
  345. padding: 16px 8px;
  346. background-color: #fff;
  347. border-right: 1px solid $border-color;
  348. &-title {
  349. display: flex;
  350. column-gap: 8px;
  351. align-items: center;
  352. font-weight: bold;
  353. :first-child {
  354. flex: 1;
  355. }
  356. .edit {
  357. cursor: pointer;
  358. }
  359. }
  360. .exercise-list {
  361. max-height: calc(100% - 130px);
  362. overflow-y: auto;
  363. .exercise-item {
  364. display: flex;
  365. column-gap: 8px;
  366. align-items: center;
  367. padding: 4px 8px;
  368. color: #333;
  369. cursor: pointer;
  370. border-radius: 2px;
  371. &.active {
  372. color: $main-color;
  373. background-color: #f4f8ff;
  374. }
  375. .item-name {
  376. flex: 1;
  377. }
  378. }
  379. }
  380. &-operate {
  381. display: flex;
  382. > .el-button {
  383. flex: 1;
  384. }
  385. }
  386. }
  387. .preview {
  388. position: relative;
  389. max-height: 450px;
  390. padding: 16px 24px;
  391. overflow: auto;
  392. background-color: #eff1f5;
  393. border-top: 1px solid $border-color;
  394. .preview-header {
  395. display: flex;
  396. justify-content: space-between;
  397. width: 100%;
  398. .quick-preview {
  399. font-weight: bold;
  400. }
  401. .preview-right {
  402. display: flex;
  403. column-gap: 18px;
  404. align-items: center;
  405. .preview-button {
  406. display: flex;
  407. column-gap: 8px;
  408. align-items: center;
  409. color: #175dff;
  410. cursor: pointer;
  411. &.plain {
  412. color: #2f3742;
  413. }
  414. }
  415. }
  416. }
  417. &-content {
  418. width: 100%;
  419. height: calc(100% - 24px);
  420. overflow: auto;
  421. }
  422. }
  423. }
  424. </style>
  425. <style>
  426. .el-cascader-menu__wrap {
  427. height: 235px;
  428. }
  429. </style>