CardDesign.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. <template>
  2. <div class="card-design">
  3. <div class="design-header">
  4. <div class="design-steps">
  5. <div class="step-item" v-for="(step, index) in steps" :key="index">
  6. <i>{{ index + 1 }}</i>
  7. <span>{{ step }}</span>
  8. </div>
  9. </div>
  10. </div>
  11. <!-- actions -->
  12. <div class="design-action">
  13. <div class="design-logo">
  14. <h1>
  15. <i class="el-icon-d-arrow-left" @click="toExit" title="退出"></i>
  16. 答题卡制作
  17. </h1>
  18. </div>
  19. <div class="action-part">
  20. <div class="action-part-title"><h2>基本设置</h2></div>
  21. <div class="action-part-body">
  22. <page-prop-edit @init-page="initPageData"></page-prop-edit>
  23. </div>
  24. </div>
  25. <div class="action-part">
  26. <div class="action-part-title"><h2>试题配置</h2></div>
  27. <div class="action-part-body">
  28. <div class="type-list">
  29. <div
  30. class="type-item"
  31. v-for="(item, index) in TOPIC_LIST"
  32. :key="index"
  33. >
  34. <el-button @click="addNewTopic(item)"
  35. ><i class="el-icon-plus"></i>{{ item.name }}</el-button
  36. >
  37. </div>
  38. </div>
  39. <p class="tips-info">提示:点击创建试题</p>
  40. </div>
  41. </div>
  42. <div class="action-part">
  43. <div class="action-part-title"><h2>插入元素</h2></div>
  44. <div class="action-part-body">
  45. <div class="type-list">
  46. <div
  47. class="type-item"
  48. v-for="(item, index) in ELEMENT_LIST"
  49. :key="index"
  50. draggable="true"
  51. @dragstart="dragstart(item)"
  52. >
  53. <el-button><i class="el-icon-plus"></i>{{ item.name }}</el-button>
  54. </div>
  55. <p class="tips-info">提示:拖动插入元素</p>
  56. </div>
  57. <!-- Develop btns -->
  58. <!-- <card-config-prop-edit></card-config-prop-edit> -->
  59. </div>
  60. <!-- <br /><br /> -->
  61. <!-- <el-button @click="initCard">新建页面</el-button> -->
  62. </div>
  63. <!-- <div class="action-part">
  64. <div class="action-part-title"><h2>阅卷参数</h2></div>
  65. <div class="action-part-body">
  66. <el-button type="primary" @click="modifyParams"
  67. >上传阅卷参数<span class="color-danger"
  68. >({{ paperParams["pageSumScore"] || 0 }}分)</span
  69. ></el-button
  70. >
  71. </div>
  72. </div> -->
  73. </div>
  74. <div class="design-main">
  75. <!-- menus -->
  76. <div class="design-control">
  77. <div class="control-left tab-btns">
  78. <el-button
  79. v-for="(page, pageNo) in pages"
  80. :key="pageNo"
  81. :type="curPageNo === pageNo ? 'primary' : 'default'"
  82. @click="swithPage(pageNo)"
  83. >第{{ pageNo + 1 }}页</el-button
  84. >
  85. </div>
  86. <div class="control-right">
  87. <el-button
  88. type="success"
  89. :loading="isSubmit"
  90. :disabled="!pages.length"
  91. @click="toPreview"
  92. >预览</el-button
  93. >
  94. <el-button
  95. type="primary"
  96. :loading="isSubmit"
  97. :disabled="canSave || !pages.length"
  98. @click="toSave"
  99. >暂存</el-button
  100. >
  101. <el-button type="primary" :loading="isSubmit" @click="toSubmit"
  102. >提交</el-button
  103. >
  104. </div>
  105. </div>
  106. <!-- edit body -->
  107. <div class="design-body">
  108. <div
  109. :class="[
  110. 'page-box',
  111. `page-box-${cardConfig.pageSize}`,
  112. `page-box-${curPageNo % 2}`
  113. ]"
  114. v-if="curPage.locators"
  115. >
  116. <div
  117. :class="[
  118. 'page-locators',
  119. `page-locators-${curPage.locators.length}`
  120. ]"
  121. >
  122. <ul
  123. class="page-locator-group"
  124. v-for="(locator, iind) in curPage.locators"
  125. :key="iind"
  126. >
  127. <li
  128. v-for="(elem, eindex) in locator"
  129. :key="eindex"
  130. :id="elem.id"
  131. ></li>
  132. </ul>
  133. </div>
  134. <!-- inner edit area -->
  135. <div class="page-main-inner">
  136. <div
  137. :class="['page-main', `page-main-${curPage.columns.length}`]"
  138. :style="{ margin: `0 -${curPage.columnGap / 2}px` }"
  139. >
  140. <div
  141. class="page-column"
  142. v-for="(column, columnNo) in curPage.columns"
  143. :key="columnNo"
  144. :style="{ padding: `0 ${curPage.columnGap / 2}px` }"
  145. >
  146. <div
  147. class="page-column-main"
  148. :id="[`column-${curPageNo}-${columnNo}`]"
  149. >
  150. <div class="page-column-body" v-if="column.elements.length">
  151. <topic-element-edit
  152. class="page-column-element"
  153. :data-h="element.h"
  154. v-for="element in column.elements"
  155. :key="element.id"
  156. :data="element"
  157. ></topic-element-edit>
  158. </div>
  159. <div class="page-column-body" v-else>
  160. <div
  161. class="page-column-forbid-area"
  162. v-if="cardConfig.showForbidArea"
  163. >
  164. <p>该区域严禁作答</p>
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. </div>
  170. </div>
  171. <!-- outer edit area -->
  172. <div class="page-main-outer">
  173. <page-number
  174. type="rect"
  175. :total="pages.length"
  176. :current="curPageNo + 1"
  177. ></page-number>
  178. <page-number
  179. type="text"
  180. :total="pages.length"
  181. :current="curPageNo + 1"
  182. ></page-number>
  183. </div>
  184. </div>
  185. </div>
  186. </div>
  187. <!-- all topics -->
  188. <div class="topic-list">
  189. <div :class="['page-box', `page-box-${cardConfig.pageSize}`]">
  190. <div class="page-main-inner">
  191. <div
  192. :class="['page-main', `page-main-${cardConfig.columnNumber}`]"
  193. :style="{ margin: `0 -${cardConfig.columnGap / 2}px` }"
  194. >
  195. <div
  196. class="page-column"
  197. :style="{ padding: `0 ${cardConfig.columnGap / 2}px` }"
  198. >
  199. <div class="page-column-main" id="topic-column">
  200. <div class="page-column-body">
  201. <!-- card-head-sample -->
  202. <card-head-sample
  203. :data="cardHeadSampleData"
  204. id="simple-card-head"
  205. v-if="topics.length && cardHeadSampleData"
  206. ></card-head-sample>
  207. <!-- topic element -->
  208. <topic-element-preview
  209. class="page-column-element"
  210. v-for="element in topics"
  211. :key="element.id"
  212. :data="element"
  213. ></topic-element-preview>
  214. </div>
  215. </div>
  216. </div>
  217. </div>
  218. </div>
  219. </div>
  220. </div>
  221. <!-- element-prop-edit -->
  222. <element-prop-edit ref="ElementPropEdit"></element-prop-edit>
  223. <!-- right-click-menu -->
  224. <right-click-menu @inset-topic="insetNewTopic"></right-click-menu>
  225. <!-- card-view-frame -->
  226. <div class="design-preview-frame" v-if="cardPreviewUrl">
  227. <iframe :src="cardPreviewUrl" frameborder="0"></iframe>
  228. </div>
  229. <!-- paper-params -->
  230. <paper-params
  231. :pages="pages"
  232. :paper-params="paperParams"
  233. @confirm="paperParamsModified"
  234. ref="PaperParams"
  235. ></paper-params>
  236. <!-- topic select dialog -->
  237. <topic-select-dialog
  238. ref="TopicSelectDialog"
  239. :topics="topicList"
  240. @confirm="addNewTopic"
  241. ></topic-select-dialog>
  242. </div>
  243. </template>
  244. <script>
  245. import { mapState, mapMutations, mapActions } from "vuex";
  246. import { cardConfigInfos, cardDetail, saveCard } from "../api";
  247. import {
  248. getElementModel,
  249. getCardHeadModel,
  250. ELEMENT_LIST,
  251. TOPIC_LIST
  252. } from "../elementModel";
  253. import { CARD_VERSION } from "../enumerate";
  254. // import CardConfigPropEdit from "../components/CardConfigPropEdit";
  255. import TopicElementEdit from "../components/TopicElementEdit";
  256. import TopicElementPreview from "../components/TopicElementPreview";
  257. import PagePropEdit from "../components/PagePropEdit";
  258. import ElementPropEdit from "../components/ElementPropEdit";
  259. import RightClickMenu from "../components/RightClickMenu";
  260. import PageNumber from "../components/PageNumber";
  261. import PaperParams from "../components/PaperParams";
  262. import CardHeadSample from "../elements/card-head/CardHead";
  263. import TopicSelectDialog from "../components/TopicSelectDialog";
  264. export default {
  265. name: "card-design",
  266. components: {
  267. // CardConfigPropEdit,
  268. TopicElementEdit,
  269. TopicElementPreview,
  270. PagePropEdit,
  271. ElementPropEdit,
  272. RightClickMenu,
  273. CardHeadSample,
  274. PageNumber,
  275. PaperParams,
  276. TopicSelectDialog
  277. },
  278. data() {
  279. return {
  280. cardId: this.$route.params.cardId || this.$ls.get("cardId"),
  281. prepareTcPCard: this.$ls.get("prepareTcPCard", {
  282. examTaskId: "",
  283. courseCode: "",
  284. courseName: "",
  285. makeMethod: "SELF",
  286. cardRuleId: ""
  287. }),
  288. ELEMENT_LIST,
  289. TOPIC_LIST,
  290. topicList: [],
  291. steps: ["添加标题", "基本设置", "试题配置", "预览生成"],
  292. columnWidth: 0,
  293. cardPreviewUrl: "",
  294. isSubmit: false,
  295. canSave: false
  296. };
  297. },
  298. computed: {
  299. ...mapState("card", [
  300. "cardConfig",
  301. "topics",
  302. "pages",
  303. "paperParams",
  304. "curElement",
  305. "curPage",
  306. "curPageNo"
  307. ]),
  308. isEdit() {
  309. return !!this.cardId;
  310. },
  311. cardHeadSampleData() {
  312. if (!this.cardConfig["pageSize"]) return;
  313. const data = getCardHeadModel(this.cardConfig);
  314. data.isSimple = true;
  315. return data;
  316. }
  317. },
  318. mounted() {
  319. if (!this.prepareTcPCard.examTaskId && !this.isEdit) {
  320. this.$message.error("找不到命题任务,请退出题卡制作!");
  321. return;
  322. }
  323. this.initCard();
  324. this.registWindowSubmit();
  325. },
  326. methods: {
  327. ...mapMutations("card", [
  328. "addPage",
  329. "setCurPage",
  330. "setCurElement",
  331. "setCardConfig",
  332. "setOpenElementEditDialog",
  333. "setCurDragElement",
  334. "setPages",
  335. "setPaperParams",
  336. "setInsetTarget",
  337. "initState"
  338. ]),
  339. ...mapActions("card", [
  340. "resetTopicSeries",
  341. "removePage",
  342. "addElement",
  343. "modifyCardHead",
  344. "modifyElement",
  345. "rebuildPages",
  346. "initTopicsFromPages"
  347. ]),
  348. async initCard() {
  349. if (this.isEdit) {
  350. await this.getCardTempDetail();
  351. } else {
  352. await this.getCardConfig();
  353. this.initPageData();
  354. }
  355. this.addWatch();
  356. },
  357. getCardTitle(titleRule) {
  358. const fieldMap = {
  359. courseCode: this.prepareTcPCard.courseCode,
  360. courseName: this.prepareTcPCard.courseName,
  361. schoolName: this.prepareTcPCard.schoolName
  362. };
  363. Object.entries(fieldMap).forEach(([key, val]) => {
  364. titleRule = titleRule.replace("${" + key + "}", val);
  365. });
  366. return titleRule;
  367. },
  368. async getCardTempDetail() {
  369. const detData = await cardDetail(this.cardId);
  370. // this.canSave = !detData.operateStatus;
  371. // 可能存在题卡内容没有记录的情况
  372. if (detData.content) {
  373. const cont = JSON.parse(detData.content);
  374. this.setPages(cont.pages);
  375. this.setCardConfig(cont.cardConfig);
  376. this.setPaperParams(cont.paperParams);
  377. this.initTopicsFromPages();
  378. this.resetTopicSeries();
  379. this.setCurPage(0);
  380. } else {
  381. await this.getCardConfig();
  382. // 没有题卡内容时,直接创建新的内容
  383. if (detData.makeMethod === "CUST") {
  384. this.setCardConfig({ cardTitle: detData.title });
  385. }
  386. this.initPageData();
  387. }
  388. },
  389. initPageData() {
  390. this.modifyCardHead({
  391. ...getCardHeadModel(this.cardConfig)
  392. });
  393. this.$nextTick(() => {
  394. this.rebuildPages();
  395. this.setCurPage(0);
  396. });
  397. },
  398. async getCardConfig() {
  399. const data = await cardConfigInfos(this.prepareTcPCard.cardRuleId);
  400. if (!data) {
  401. this.$message.error("找不到题卡规则!");
  402. return;
  403. }
  404. let config = {
  405. ...data,
  406. ...{
  407. pageSize: "A3",
  408. columnNumber: 2,
  409. columnGap: 20,
  410. showForbidArea: true,
  411. cardDesc: "",
  412. makeMethod: this.prepareTcPCard.makeMethod
  413. }
  414. };
  415. config.aOrB = true; // 默认开启A/B卷型
  416. config.requiredFields = JSON.parse(config.requiredFields);
  417. config.extendFields = JSON.parse(config.extendFields);
  418. config.cardTitle = this.getCardTitle(config.titleRule);
  419. this.setCardConfig(config);
  420. },
  421. addNewTopic(item) {
  422. let element = getElementModel(item.type);
  423. element.w = document.getElementById("topic-column").offsetWidth;
  424. this.setCurElement(element);
  425. this.$refs.ElementPropEdit.open();
  426. // to elementPropEdit/ElementPropEdit open topic edit dialog
  427. },
  428. insetNewTopic({ id, type }) {
  429. console.log(id, type);
  430. this.setInsetTarget({ id, type });
  431. if (type === "FILL_QUESTION") {
  432. this.topicList = this.TOPIC_LIST;
  433. } else {
  434. this.topicList = this.TOPIC_LIST.filter(
  435. item => item.type !== "FILL_QUESTION"
  436. );
  437. }
  438. this.$refs.TopicSelectDialog.open();
  439. },
  440. // 元件编辑
  441. dragstart(element) {
  442. this.setCurDragElement(getElementModel(element.type));
  443. },
  444. addWatch() {
  445. this.$watch("cardConfig", val => {
  446. const element = getCardHeadModel(val);
  447. this.modifyCardHead(element);
  448. this.$nextTick(() => {
  449. this.rebuildPages();
  450. });
  451. });
  452. },
  453. // 操作
  454. async toPreview() {
  455. if (this.isSubmit) return;
  456. this.isSubmit = true;
  457. const result = await this.save().catch(() => {});
  458. this.isSubmit = false;
  459. if (!result) return;
  460. const { href } = this.$router.resolve({
  461. name: "CardPreview",
  462. params: {
  463. cardId: this.cardId,
  464. viewType: "view"
  465. }
  466. });
  467. window.open(href);
  468. },
  469. swithPage(pindex) {
  470. if (this.curPageNo === pindex) return;
  471. this.setCurPage(pindex);
  472. this.setCurElement({});
  473. },
  474. // paper-params
  475. modifyParams() {
  476. this.$refs.PaperParams.open();
  477. },
  478. paperParamsModified(paperParams) {
  479. this.setPaperParams(paperParams);
  480. },
  481. // save
  482. getCardData(htmlContent = "", model = "") {
  483. let data = {
  484. title: this.cardConfig.cardTitle,
  485. content: model,
  486. htmlContent,
  487. type: "CUSTOM",
  488. ...this.prepareTcPCard
  489. };
  490. if (this.cardId) data.id = this.cardId;
  491. return data;
  492. },
  493. getRequestConfig() {
  494. return this.prepareTcPCard.makeMethod === "CUST"
  495. ? {
  496. headers: {
  497. schoolId: this.prepareTcPCard.schoolId
  498. }
  499. }
  500. : {};
  501. },
  502. checkElementCovered() {
  503. let elements = [];
  504. this.pages.forEach(page => {
  505. page.columns.forEach(column => {
  506. column.elements.forEach(element => {
  507. if (element.isCovered) {
  508. elements.push(element.id);
  509. }
  510. });
  511. });
  512. });
  513. return elements.length;
  514. },
  515. checkCardValid() {
  516. if (!this.cardConfig.cardTitle) {
  517. this.$message.error("题卡标题不能为空!");
  518. this.setCurPageNo(0);
  519. setTimeout(() => {
  520. document.getElementById("cardTitleInput").focus();
  521. });
  522. return false;
  523. }
  524. // if (!this.cardConfig.cardDesc) {
  525. // this.$message.error("题卡描述信息不能为空!");
  526. // this.setCurPage(0);
  527. // setTimeout(() => {
  528. // document.getElementById("cardDescInput").focus();
  529. // });
  530. // return false;
  531. // }
  532. if (this.checkElementCovered()) {
  533. this.$message.error("题卡中存在被遮挡的元件,请注意调整!");
  534. return false;
  535. }
  536. return true;
  537. },
  538. getModel() {
  539. // 防止页面未渲染完成,各试题高度未及时更新,保存数据有误的问题
  540. return new Promise((resolve, reject) => {
  541. setTimeout(() => {
  542. const data = JSON.stringify(
  543. {
  544. version: CARD_VERSION,
  545. cardConfig: this.cardConfig,
  546. paperParams: this.paperParams,
  547. pages: this.pages
  548. },
  549. (k, v) => (k.startsWith("_") ? undefined : v)
  550. );
  551. resolve(data);
  552. }, 100);
  553. });
  554. },
  555. async save() {
  556. if (!this.checkCardValid()) return;
  557. const model = await this.getModel();
  558. let datas = this.getCardData("", model);
  559. datas.status = "STAGE";
  560. const result = await saveCard(datas, this.getRequestConfig());
  561. this.cardId = result;
  562. this.$ls.set("cardId", this.cardId);
  563. return true;
  564. },
  565. async toSave() {
  566. if (this.isSubmit) return;
  567. this.isSubmit = true;
  568. const result = await this.save().catch(() => {});
  569. this.isSubmit = false;
  570. if (result) this.$message.success("保存成功!");
  571. },
  572. toSubmit() {
  573. if (this.isSubmit) return;
  574. if (!this.checkCardValid()) return;
  575. this.$confirm("确定要提交当前题卡吗?", "提示", {
  576. type: "warning"
  577. })
  578. .then(() => {
  579. window.cardData = {
  580. cardConfig: this.cardConfig,
  581. pages: this.pages,
  582. paperParams: this.paperParams
  583. };
  584. this.isSubmit = true;
  585. const { href } = this.$router.resolve({
  586. name: "CardPreview",
  587. params: {
  588. cardId: 1,
  589. viewType: "frame"
  590. }
  591. });
  592. this.cardPreviewUrl = href;
  593. })
  594. .catch(() => {});
  595. },
  596. registWindowSubmit() {
  597. window.submitCardTemp = async (htmlContent, model) => {
  598. const datas = this.getCardData(htmlContent, model);
  599. datas.status = "SUBMIT";
  600. const result = await saveCard(
  601. datas,
  602. this.getRequestConfig()
  603. ).catch(() => {});
  604. this.cardPreviewUrl = "";
  605. this.isSubmit = false;
  606. window.cardData = null;
  607. if (result) {
  608. this.cardId = result;
  609. this.$ls.set("cardId", this.cardId);
  610. this.canSave = false;
  611. this.$message.success("提交成功!");
  612. this.goback();
  613. } else {
  614. this.$message.error("提交失败,请重新尝试!");
  615. }
  616. };
  617. },
  618. toExit() {
  619. this.$confirm(
  620. "请确保当前题卡已经正常保存,确定要退出当前题卡编辑吗?",
  621. "提示",
  622. {
  623. type: "warning"
  624. }
  625. )
  626. .then(() => {
  627. this.goback();
  628. })
  629. .catch(() => {});
  630. }
  631. },
  632. beforeDestroy() {
  633. this.$ls.remove("cardId");
  634. this.$ls.remove("prepareTcPCard");
  635. this.initState();
  636. delete window.submitCardTemp;
  637. }
  638. };
  639. </script>