index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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
  7. v-if="isEditExercise"
  8. v-model="exercise.name"
  9. @keyup.enter.native="UpdateExerciseName"
  10. @blur="UpdateExerciseName"
  11. />
  12. <span v-else class="nowrap-ellipsis">{{ exercise.name }}</span>
  13. <SvgIcon icon-class="edit" class-name="edit" @click="editExercise" />
  14. </div>
  15. <div class="exercise-list">
  16. <ul>
  17. <draggable v-model="index_list" animation="300" @end="moveQuestion" @start="handleStart">
  18. <transition-group>
  19. <li v-for="(item, i) in index_list" :key="i" :class="['exercise-item', { active: i === curIndex }]">
  20. <SvgIcon icon-class="child" :size="20" />
  21. <span class="item-name nowrap-ellipsis" @click="selectExerciseItem(i)">
  22. {{ i + 1 }}. {{ exerciseNames[item.type] }}
  23. </span>
  24. <SvgIcon icon-class="copy" class="pointer" :size="10" @click="copy(i)" />
  25. </li>
  26. </transition-group>
  27. </draggable>
  28. </ul>
  29. </div>
  30. <div class="list-operate">
  31. <el-button type="primary" @click="oneClickImport">一键导入</el-button>
  32. <el-button type="primary" @click="showSelectQuestionType">新建</el-button>
  33. </div>
  34. </div>
  35. <CreateMain
  36. ref="createMain"
  37. v-loading="loading"
  38. :cur-index="curIndex"
  39. :index-list="index_list"
  40. :total-score="totalScore"
  41. :loading="loading"
  42. @selectExerciseItem="selectExerciseItem"
  43. @setPreview="setPreview"
  44. @deleteQuestion="deleteQuestion"
  45. />
  46. <div class="preview" :style="{ height: preview ? 'calc(50vh - 32px)' : 'auto' }">
  47. <div class="preview-header">
  48. <span class="quick-preview">快捷预览:</span>
  49. <div class="preview-right">
  50. <template v-if="preview">
  51. <span class="preview-button plain" @click="refreshPreviewData">
  52. <SvgIcon icon-class="loop" size="14" /><span>刷新</span>
  53. </span>
  54. <span class="preview-button" @click="fullPreview">
  55. <SvgIcon icon-class="eye" /> <span>完整预览</span>
  56. </span>
  57. <span class="preview-button plain" @click="setPreview"><SvgIcon icon-class="close" />关闭预览</span>
  58. </template>
  59. <template v-else>
  60. <span class="preview-button" @click="setPreview"><SvgIcon icon-class="eye" />预览</span>
  61. </template>
  62. </div>
  63. </div>
  64. <div class="preview-content">
  65. <component :is="curPreviewPage" v-if="preview" :data="previewData" :child-preview-data="childPreviewData" />
  66. </div>
  67. </div>
  68. <SelectQuestionType :visible.sync="visible" @addQuestionToExercise="addQuestionToExercise" />
  69. <ImportPage :visible.sync="importVisible" @oneKeyImport="oneKeyImport" />
  70. </div>
  71. </template>
  72. <script>
  73. import { exerciseNames, questionDataList, analysisOneKeyImportData } from '../data/questionType';
  74. import {
  75. AddQuestionToExercise,
  76. DeleteQuestion,
  77. GetExerciseQuestionIndexList,
  78. GetQuestionInfo,
  79. GetExerciseInfo,
  80. UpdateExercise,
  81. MoveQuestion,
  82. } from '@/api/exercise';
  83. import CreateMain from './components/create.vue';
  84. import PreviewQuestionTypeMixin from '../data/PreviewQuestionTypeMixin';
  85. import ImportPage from './components/common/ImportPage.vue';
  86. import SelectQuestionType from './components/common/SelectQuestionType.vue';
  87. import draggable from 'vuedraggable';
  88. export default {
  89. name: 'CreateExercise',
  90. components: {
  91. CreateMain,
  92. SelectQuestionType,
  93. draggable,
  94. ImportPage,
  95. },
  96. mixins: [PreviewQuestionTypeMixin],
  97. provide() {
  98. return {
  99. updateCurQuestionType: this.updateCurQuestionType,
  100. exercise_id: this.exercise_id,
  101. refreshPreviewData: this.refreshPreviewData,
  102. updateLoading: (loading) => {
  103. this.loading = loading;
  104. },
  105. };
  106. },
  107. data() {
  108. const { id, back_url } = this.$route.query;
  109. return {
  110. loading: false, // 题目加载中
  111. exercise_id: id, // 练习id
  112. exercise: { name: '' }, // 练习信息
  113. isEditExercise: false, // 是否编辑练习
  114. moveQuestionData: {}, // 移动题目数据
  115. isMoveQuestion: false, // 是否移动题目
  116. curIndex: 0, // 当前练习索引
  117. index_list: [], // 练习列表
  118. exerciseNames, // 练习名称
  119. preview: false, // 预览显示
  120. previewData: {}, // 预览数据
  121. back_url: back_url || '/personal_question', // 返回地址
  122. visible: false, // 选择题目类型弹窗
  123. importVisible: false, // 一键导入弹窗
  124. totalScore: 0, // 总分
  125. };
  126. },
  127. computed: {
  128. curPreviewPage() {
  129. if (this.index_list.length === 0) return '';
  130. return this.previewComponents[this.index_list[this.curIndex].type];
  131. },
  132. },
  133. watch: {
  134. curIndex() {
  135. if (this.index_list.length === 0 || this.isMoveQuestion) return;
  136. this.getQuestionInfo();
  137. },
  138. },
  139. created() {
  140. this.getExerciseQuestionIndexList(true);
  141. this.getExerciseInfo();
  142. },
  143. methods: {
  144. // 返回练习管理
  145. back() {
  146. this.$router.push(this.back_url);
  147. },
  148. /**
  149. * 添加题目到练习
  150. * @param {string} type 题目类型
  151. * @param {string} additional_type 附加类型
  152. * @param {string} content 题目内容
  153. */
  154. addQuestionToExercise(type, additional_type, content = '') {
  155. this.$refs.createMain.saveQuestion();
  156. AddQuestionToExercise({
  157. exercise_id: this.exercise_id,
  158. type,
  159. additional_type,
  160. content,
  161. })
  162. .then(() => {
  163. this.getExerciseQuestionIndexList(false, true);
  164. })
  165. .catch(() => {
  166. this.$message.error('添加失败');
  167. });
  168. },
  169. showSelectQuestionType() {
  170. this.visible = true;
  171. },
  172. oneClickImport() {
  173. this.importVisible = true;
  174. },
  175. async oneKeyImport(text) {
  176. let dataList = await analysisOneKeyImportData(text.split(/\n\s*\n+/));
  177. this.importVisible = false;
  178. const loading = this.$loading({
  179. text: '正在导入题目,请稍等...',
  180. });
  181. // 一键导入题目,按 dataList 顺序添加题目,等待第一个题目添加成功后,再添加第二个题目
  182. dataList
  183. .reduce((promise, { type, additional_type, content }) => {
  184. return promise.then(() => {
  185. return AddQuestionToExercise({
  186. exercise_id: this.exercise_id,
  187. type,
  188. additional_type,
  189. content,
  190. });
  191. });
  192. }, Promise.resolve())
  193. .then(() => {
  194. this.getExerciseQuestionIndexList(false, true);
  195. loading.close();
  196. });
  197. },
  198. // 创建默认题目
  199. createDefaultQuestion() {
  200. this.addQuestionToExercise('select', 'single', questionDataList['select']);
  201. },
  202. /**
  203. * 获取练习题目索引列表
  204. * @param {boolean} init 是否初始化
  205. * @param {boolean} isAdd 是否添加题目
  206. */
  207. getExerciseQuestionIndexList(init = false, isAdd = false) {
  208. GetExerciseQuestionIndexList({ exercise_id: this.exercise_id })
  209. .then(({ index_list, score }) => {
  210. this.index_list = index_list;
  211. this.totalScore = score;
  212. if (isAdd) {
  213. this.curIndex = this.index_list.length - 1;
  214. }
  215. if (!init) return;
  216. this.getQuestionInfo();
  217. })
  218. .catch((err) => {
  219. console.log(err);
  220. });
  221. },
  222. getExerciseInfo() {
  223. GetExerciseInfo({ exercise_id: this.exercise_id }).then(({ exercise }) => {
  224. this.exercise = exercise;
  225. });
  226. },
  227. editExercise() {
  228. if (this.isEditExercise) {
  229. return this.UpdateExerciseName();
  230. }
  231. this.isEditExercise = !this.isEditExercise;
  232. },
  233. UpdateExerciseName() {
  234. UpdateExercise(this.exercise)
  235. .then(() => {
  236. this.isEditExercise = false;
  237. })
  238. .catch(() => {
  239. this.$message.error('修改失败');
  240. });
  241. },
  242. /**
  243. * 获取题目信息
  244. */
  245. getQuestionInfo() {
  246. if (this.index_list.length === 0) return;
  247. this.$refs.createMain?.clearSaveDate();
  248. this.loading = true;
  249. let curId = this.index_list[this.curIndex].id;
  250. GetQuestionInfo({ question_id: this.index_list[this.curIndex].id })
  251. .then(({ question, file_list }) => {
  252. if (curId !== this.index_list[this.curIndex].id) return;
  253. this.$refs.createMain.resetSaveDate();
  254. if (!question.content) return;
  255. // 将题目文件id列表添加到题目内容中
  256. let file_id_list = file_list.map(({ file_id }) => file_id);
  257. let content = JSON.parse(question.content);
  258. content.file_id_list = file_id_list;
  259. this.$nextTick(() => {
  260. this.$refs.createMain.$refs.exercise?.[0].setQuestion(content);
  261. this.refreshPreviewData();
  262. });
  263. })
  264. .catch(() => {});
  265. },
  266. /**
  267. * 复制练习
  268. * @param {Number} index 练习索引
  269. */
  270. copy(index) {
  271. this.$confirm('是否复制当前题目', '提示', {
  272. confirmButtonText: '确定',
  273. cancelButtonText: '取消',
  274. type: 'warning',
  275. })
  276. .then(() => {
  277. GetQuestionInfo({ question_id: this.index_list[index].id }).then(
  278. ({ question: { type, additional_type, content } }) => {
  279. this.addQuestionToExercise(type, additional_type, content);
  280. },
  281. );
  282. })
  283. .catch(() => {});
  284. },
  285. /**
  286. * 选择练习
  287. * @param {number} index 练习索引
  288. */
  289. selectExerciseItem(index) {
  290. if (index < 0 || index > this.index_list.length - 1) return;
  291. this.$refs.createMain?.clearSaveDate();
  292. this.$refs.createMain.saveQuestion();
  293. this.curIndex = index;
  294. },
  295. // 刷新预览数据
  296. refreshPreviewData() {
  297. this.previewData = this.$refs.createMain.$refs.exercise?.[0].data || {};
  298. this.childPreviewData = this.$refs.createMain.$refs.exercise?.[0].childPreviewData || [];
  299. },
  300. // 完整预览
  301. fullPreview() {
  302. window.open(
  303. this.$router.resolve({
  304. path: '/answer',
  305. query: {
  306. id: this.exercise_id,
  307. type: 'show',
  308. question_index: this.curIndex,
  309. back_url: 'not-return',
  310. },
  311. }).href,
  312. '_blank',
  313. );
  314. },
  315. // 预览
  316. setPreview() {
  317. this.preview = !this.preview;
  318. this.refreshPreviewData();
  319. },
  320. // 删除练习
  321. deleteQuestion() {
  322. DeleteQuestion({ question_id: this.index_list[this.curIndex].id })
  323. .then(() => {
  324. let index = this.curIndex - 1;
  325. this.curIndex = Math.max(0, index);
  326. // 当删除的题目是第一题时,需要重新获取题目信息,因为 curIndex 没有变化
  327. this.getExerciseQuestionIndexList(index < 0);
  328. this.$message.success('删除成功');
  329. })
  330. .catch(() => {
  331. this.$message.error('删除失败');
  332. });
  333. },
  334. /**
  335. * 修改当前题目类型
  336. * @param {array} arr
  337. */
  338. updateCurQuestionType(arr) {
  339. let type = arr[arr.length - 1];
  340. this.index_list[this.curIndex].type = type;
  341. },
  342. /**
  343. * 移动题目
  344. * @param {object} param 移动参数
  345. * @param {number} param.newIndex 移动后的索引
  346. * @param {number} param.oldIndex 移动前的索引
  347. */
  348. moveQuestion({ newIndex, oldIndex }) {
  349. if (newIndex === oldIndex) {
  350. this.isMoveQuestion = false;
  351. this.moveQuestionData = {};
  352. return;
  353. }
  354. let isMoveCur = newIndex === this.curIndex || oldIndex === this.curIndex; // 是否移动当前题目
  355. let isEffectCurIndex = newIndex < this.curIndex !== oldIndex < this.curIndex; // 是否影响当前题目索引
  356. if (isMoveCur) {
  357. this.curIndex = this.curIndex === newIndex ? oldIndex : newIndex;
  358. } else if (isEffectCurIndex) {
  359. this.curIndex = newIndex < this.curIndex ? this.curIndex + 1 : this.curIndex - 1;
  360. }
  361. this.isMoveQuestion = false;
  362. if (isMoveCur || isEffectCurIndex) {
  363. this.$nextTick(() => {
  364. this.$refs.createMain.$refs.exercise?.[0].setQuestion(this.moveQuestionData);
  365. this.moveQuestionData = {};
  366. this.refreshPreviewData();
  367. this.$refs.createMain.resetSaveDate();
  368. });
  369. }
  370. let order = newIndex > oldIndex;
  371. MoveQuestion({
  372. question_id: this.index_list[newIndex].id,
  373. dest_question_id: this.index_list[order ? newIndex - 1 : newIndex + 1].id,
  374. dest_position: order ? 1 : 0,
  375. })
  376. .then(() => {
  377. this.$message.success('移动成功');
  378. let curI = this.curIndex;
  379. // 移动题目后,单独更新当前题目的题号
  380. GetQuestionInfo({ question_id: this.index_list[this.curIndex].id })
  381. .then(({ question }) => {
  382. if (!question) return;
  383. if (curI !== this.curIndex) return;
  384. this.$refs.createMain.$refs.exercise?.[0].setQuestionNumber(question.question_number);
  385. })
  386. .catch(() => {});
  387. })
  388. .catch(() => {
  389. this.$message.error('移动失败');
  390. });
  391. },
  392. /**
  393. * 开始移动题目
  394. * @param {object} param 移动参数
  395. * @param {number} param.newIndex 移动后的索引
  396. * @param {number} param.oldIndex 移动前的索引
  397. */
  398. handleStart({ newIndex, oldIndex }) {
  399. this.$refs.createMain?.clearSaveDate();
  400. let isMoveCur = newIndex === this.curIndex || oldIndex === this.curIndex; // 是否移动当前题目
  401. let isEffectCurIndex = newIndex < this.curIndex !== oldIndex < this.curIndex; // 是否影响当前题目索引
  402. if (isMoveCur || isEffectCurIndex) {
  403. this.isMoveQuestion = true;
  404. this.moveQuestionData = this.$refs.createMain.$refs.exercise?.[0].data;
  405. }
  406. },
  407. },
  408. };
  409. </script>
  410. <style lang="scss" scoped>
  411. .exercise {
  412. display: grid;
  413. grid-template: 1fr auto / 224px minmax(800px, 1fr);
  414. height: 100%;
  415. .list {
  416. display: flex;
  417. flex-direction: column;
  418. grid-area: 1 / 1 / -1;
  419. row-gap: 16px;
  420. padding: 16px 8px;
  421. background-color: #fff;
  422. border-right: 1px solid $border-color;
  423. &-title {
  424. display: flex;
  425. column-gap: 8px;
  426. align-items: center;
  427. font-weight: bold;
  428. :first-child {
  429. flex: 1;
  430. }
  431. .edit {
  432. cursor: pointer;
  433. }
  434. }
  435. .exercise-list {
  436. max-height: calc(100% - 130px);
  437. overflow-y: auto;
  438. .exercise-item {
  439. display: flex;
  440. column-gap: 8px;
  441. align-items: center;
  442. padding: 4px 8px;
  443. color: #333;
  444. cursor: pointer;
  445. border-radius: 2px;
  446. &.active {
  447. color: $main-color;
  448. background-color: #f4f8ff;
  449. }
  450. .item-name {
  451. flex: 1;
  452. }
  453. }
  454. }
  455. &-operate {
  456. display: flex;
  457. > .el-button {
  458. flex: 1;
  459. }
  460. }
  461. }
  462. .preview {
  463. position: relative;
  464. padding: 16px 24px;
  465. overflow: auto;
  466. background-color: #eff1f5;
  467. border-top: 1px solid $border-color;
  468. .preview-header {
  469. display: flex;
  470. justify-content: space-between;
  471. width: 100%;
  472. .quick-preview {
  473. font-weight: bold;
  474. }
  475. .preview-right {
  476. display: flex;
  477. column-gap: 18px;
  478. align-items: center;
  479. .preview-button {
  480. display: flex;
  481. column-gap: 8px;
  482. align-items: center;
  483. color: #175dff;
  484. cursor: pointer;
  485. &.plain {
  486. color: #2f3742;
  487. }
  488. }
  489. }
  490. }
  491. &-content {
  492. width: 100%;
  493. height: calc(100% - 24px);
  494. overflow: auto;
  495. }
  496. }
  497. }
  498. </style>
  499. <style lang="scss">
  500. .el-cascader-menu__wrap {
  501. height: 235px;
  502. }
  503. </style>