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