CardDesign.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <template>
  2. <div class="card-design">
  3. <div class="design-top">
  4. <div class="design-top-logo">
  5. <h1><i class="icon icon-back" @click="toExit"></i>答题卡制作</h1>
  6. </div>
  7. <!-- <div class="design-top-info">
  8. <div class="info-help"><i class="icon icon-help"></i>帮助</div>
  9. </div> -->
  10. </div>
  11. <div class="design-main">
  12. <!-- menus -->
  13. <div class="design-head">
  14. <div class="design-steps">
  15. <div class="step-item" v-for="(step, index) in steps" :key="index">
  16. <i>{{ index + 1 }}</i>
  17. <span>{{ step }}</span>
  18. </div>
  19. </div>
  20. <div class="design-control">
  21. <div class="control-right">
  22. <el-button
  23. class="btn-white"
  24. :loading="isSubmit"
  25. :disabled="!pages.length"
  26. @click="toPreview"
  27. >预览</el-button
  28. >
  29. <el-button
  30. type="primary"
  31. :loading="isSubmit"
  32. :disabled="canSave || !pages.length"
  33. @click="toSave"
  34. >暂存</el-button
  35. >
  36. <el-button type="primary" :loading="isSubmit" @click="toSubmit"
  37. >提交</el-button
  38. >
  39. </div>
  40. <div class="control-left">
  41. <el-button
  42. v-for="(page, pageNo) in pages"
  43. :key="pageNo"
  44. :class="{ 'btn-white': curPageNo === pageNo }"
  45. @click="swithPage(pageNo)"
  46. >第{{ pageNo + 1 }}页</el-button
  47. >
  48. </div>
  49. </div>
  50. </div>
  51. <!-- actions -->
  52. <div class="design-action">
  53. <div class="action-part">
  54. <div class="action-part-title"><h2>基本设置</h2></div>
  55. <div class="action-part-body">
  56. <page-prop-edit @init-page="initPageData"></page-prop-edit>
  57. </div>
  58. </div>
  59. <div class="action-part">
  60. <div class="action-part-title"><h2>试题配置</h2></div>
  61. <div class="action-part-body">
  62. <div class="type-list">
  63. <div
  64. class="type-item"
  65. v-for="(item, index) in TOPIC_LIST"
  66. :key="index"
  67. >
  68. <el-button @click="addNewTopic(item)"
  69. ><i class="el-icon-plus"></i>{{ item.name }}</el-button
  70. >
  71. </div>
  72. </div>
  73. </div>
  74. </div>
  75. <div class="action-part">
  76. <div class="action-part-title"><h2>插入元素</h2></div>
  77. <div class="action-part-body">
  78. <div class="type-list">
  79. <div
  80. class="type-item"
  81. v-for="(item, index) in ELEMENT_LIST"
  82. :key="index"
  83. draggable="true"
  84. @dragstart="dragstart(item)"
  85. >
  86. <el-button
  87. ><i class="el-icon-plus"></i>{{ item.name }}</el-button
  88. >
  89. </div>
  90. </div>
  91. <!-- Develop btns -->
  92. <!-- <card-config-prop-edit></card-config-prop-edit> -->
  93. </div>
  94. <!-- <br /><br /> -->
  95. <!-- <el-button @click="initCard">新建页面</el-button> -->
  96. </div>
  97. <!-- <div class="action-part">
  98. <div class="action-part-title"><h2>阅卷参数</h2></div>
  99. <div class="action-part-body">
  100. <el-button type="primary" @click="modifyParams"
  101. >上传阅卷参数<span class="color-danger"
  102. >({{ paperParams["pageSumScore"] || 0 }}分)</span
  103. ></el-button
  104. >
  105. </div>
  106. </div> -->
  107. </div>
  108. <!-- edit body -->
  109. <div class="design-body">
  110. <div
  111. :class="['page-box', `page-box-${curPageNo % 2}`]"
  112. v-if="curPage.locators"
  113. >
  114. <div
  115. :class="[
  116. 'page-locators',
  117. `page-locators-${curPage.locators.length}`
  118. ]"
  119. >
  120. <ul
  121. class="page-locator-group"
  122. v-for="(locator, iind) in curPage.locators"
  123. :key="iind"
  124. >
  125. <li
  126. v-for="(elem, eindex) in locator"
  127. :key="eindex"
  128. :id="elem.id"
  129. ></li>
  130. </ul>
  131. </div>
  132. <!-- inner edit area -->
  133. <div class="page-main-inner">
  134. <div
  135. :class="['page-main', `page-main-${curPage.columns.length}`]"
  136. :style="{ margin: `0 -${curPage.columnGap / 2}px` }"
  137. >
  138. <div
  139. class="page-column"
  140. v-for="(column, columnNo) in curPage.columns"
  141. :key="columnNo"
  142. :style="{ padding: `0 ${curPage.columnGap / 2}px` }"
  143. >
  144. <div
  145. class="page-column-main"
  146. :id="[`column-${curPageNo}-${columnNo}`]"
  147. >
  148. <div class="page-column-body" v-if="column.elements.length">
  149. <topic-element-edit
  150. class="page-column-element"
  151. :data-h="element.h"
  152. v-for="element in column.elements"
  153. :key="element.id"
  154. :data="element"
  155. ></topic-element-edit>
  156. </div>
  157. <div class="page-column-body" v-else>
  158. <div
  159. class="page-column-forbid-area"
  160. v-if="cardConfig.showForbidArea"
  161. >
  162. <p>该区域严禁作答</p>
  163. </div>
  164. </div>
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. <!-- outer edit area -->
  170. <div class="page-main-outer">
  171. <page-number
  172. type="rect"
  173. :total="pages.length"
  174. :current="curPageNo + 1"
  175. ></page-number>
  176. <page-number
  177. type="text"
  178. :total="pages.length"
  179. :current="curPageNo + 1"
  180. ></page-number>
  181. </div>
  182. </div>
  183. </div>
  184. </div>
  185. <!-- all topics -->
  186. <div class="topic-list">
  187. <div class="page-box">
  188. <div class="page-main-inner">
  189. <div
  190. :class="['page-main', `page-main-${cardConfig.columnNumber}`]"
  191. :style="{ margin: `0 -${cardConfig.columnGap / 2}px` }"
  192. >
  193. <div
  194. class="page-column"
  195. :style="{ padding: `0 ${cardConfig.columnGap / 2}px` }"
  196. >
  197. <div class="page-column-main" id="topic-column">
  198. <div class="page-column-body">
  199. <!-- card-head-sample -->
  200. <card-head-sample
  201. :data="cardHeadSampleData"
  202. id="simple-card-head"
  203. v-if="topics.length && cardHeadSampleData"
  204. ></card-head-sample>
  205. <!-- topic element -->
  206. <topic-element-preview
  207. class="page-column-element"
  208. v-for="element in topics"
  209. :key="element.id"
  210. :data="element"
  211. ></topic-element-preview>
  212. </div>
  213. </div>
  214. </div>
  215. </div>
  216. </div>
  217. </div>
  218. </div>
  219. <!-- element-prop-edit -->
  220. <element-prop-edit ref="ElementPropEdit"></element-prop-edit>
  221. <!-- right-click-menu -->
  222. <right-click-menu @inset-topic="insetNewTopic"></right-click-menu>
  223. <!-- card-view-frame -->
  224. <div class="design-preview-frame" v-if="cardPreviewUrl">
  225. <iframe :src="cardPreviewUrl" frameborder="0"></iframe>
  226. </div>
  227. <!-- paper-params -->
  228. <paper-params
  229. :pages="pages"
  230. :paper-params="paperParams"
  231. @confirm="paperParamsModified"
  232. ref="PaperParams"
  233. ></paper-params>
  234. <!-- topic select dialog -->
  235. <topic-select-dialog
  236. ref="TopicSelectDialog"
  237. :topics="topicList"
  238. @confirm="addNewTopic"
  239. ></topic-select-dialog>
  240. </div>
  241. </template>
  242. <script>
  243. import { mapState, mapMutations, mapActions } from "vuex";
  244. import { cardConfigInfos, cardDetail, saveCard } from "../api";
  245. import {
  246. getElementModel,
  247. getCardHeadModel,
  248. ELEMENT_LIST,
  249. TOPIC_LIST
  250. } from "../elementModel";
  251. import { CARD_VERSION } from "../enumerate";
  252. // import CardConfigPropEdit from "../components/CardConfigPropEdit";
  253. import TopicElementEdit from "../components/TopicElementEdit";
  254. import TopicElementPreview from "../components/TopicElementPreview";
  255. import PagePropEdit from "../components/PagePropEdit";
  256. import ElementPropEdit from "../components/ElementPropEdit";
  257. import RightClickMenu from "../components/RightClickMenu";
  258. import PageNumber from "../components/PageNumber";
  259. import PaperParams from "../components/PaperParams";
  260. import CardHeadSample from "../elements/card-head/CardHead";
  261. import TopicSelectDialog from "../components/TopicSelectDialog";
  262. export default {
  263. name: "card-design",
  264. components: {
  265. // CardConfigPropEdit,
  266. TopicElementEdit,
  267. TopicElementPreview,
  268. PagePropEdit,
  269. ElementPropEdit,
  270. RightClickMenu,
  271. CardHeadSample,
  272. PageNumber,
  273. PaperParams,
  274. TopicSelectDialog
  275. },
  276. data() {
  277. return {
  278. cardId: this.$route.params.cardId || this.$ls.get("cardId"),
  279. prepareTcPCard: this.$ls.get("prepareTcPCard", {
  280. examTaskId: "",
  281. courseCode: "",
  282. courseName: "",
  283. makeMethod: "SELF",
  284. cardRuleId: "",
  285. paperType: ""
  286. }),
  287. ELEMENT_LIST,
  288. TOPIC_LIST,
  289. topicList: [],
  290. steps: ["添加标题", "基本设置", "试题配置", "设置阅卷参数", "预览生成"],
  291. columnWidth: 0,
  292. cardPreviewUrl: "",
  293. isSubmit: false,
  294. canSave: false
  295. };
  296. },
  297. computed: {
  298. ...mapState("card", [
  299. "cardConfig",
  300. "topics",
  301. "pages",
  302. "paperParams",
  303. "curElement",
  304. "curPage",
  305. "curPageNo"
  306. ]),
  307. isEdit() {
  308. return !!this.cardId;
  309. },
  310. cardHeadSampleData() {
  311. if (!this.cardConfig["pageSize"]) return;
  312. const data = getCardHeadModel(this.cardConfig);
  313. data.isSimple = true;
  314. return data;
  315. }
  316. },
  317. mounted() {
  318. if (!this.prepareTcPCard.examTaskId && !this.isEdit) {
  319. this.$message.error("找不到命题任务,请退出题卡制作!");
  320. return;
  321. }
  322. this.initCard();
  323. this.registWindowSubmit();
  324. },
  325. methods: {
  326. ...mapMutations("card", [
  327. "addPage",
  328. "setCurPage",
  329. "setCurElement",
  330. "setCardConfig",
  331. "setOpenElementEditDialog",
  332. "setCurDragElement",
  333. "setPages",
  334. "setPaperParams",
  335. "setInsetTarget",
  336. "initState"
  337. ]),
  338. ...mapActions("card", [
  339. "resetTopicSeries",
  340. "removePage",
  341. "addElement",
  342. "modifyCardHead",
  343. "modifyElement",
  344. "rebuildPages",
  345. "initTopicsFromPages"
  346. ]),
  347. async initCard() {
  348. if (this.isEdit) {
  349. await this.getCardTempDetail();
  350. } else {
  351. await this.getCardConfig();
  352. this.initPageData();
  353. }
  354. this.addWatch();
  355. },
  356. getCardTitle(titleRule) {
  357. const fieldMap = {
  358. courseCode: this.prepareTcPCard.courseCode,
  359. courseName: this.prepareTcPCard.courseName,
  360. schoolName: this.prepareTcPCard.schoolName
  361. };
  362. Object.entries(fieldMap).forEach(([key, val]) => {
  363. titleRule = titleRule.replace("${" + key + "}", val);
  364. });
  365. return titleRule;
  366. },
  367. async getCardTempDetail() {
  368. const detData = await cardDetail(this.cardId);
  369. // this.canSave = !detData.operateStatus;
  370. // 可能存在题卡内容没有记录的情况
  371. if (detData.content) {
  372. const cont = JSON.parse(detData.content);
  373. this.setPages(cont.pages);
  374. this.setCardConfig(cont.cardConfig);
  375. this.setPaperParams(cont.paperParams);
  376. this.initTopicsFromPages();
  377. this.resetTopicSeries();
  378. this.setCurPage(0);
  379. } else {
  380. await this.getCardConfig();
  381. // 没有题卡内容时,直接创建新的内容
  382. let mod = { aOrB: detData.paperType.split(",").length > 1 };
  383. if (detData.makeMethod === "CUST") mod.cardTitle = detData.title;
  384. this.setCardConfig(mod);
  385. this.initPageData();
  386. }
  387. },
  388. initPageData() {
  389. this.modifyCardHead({
  390. ...getCardHeadModel(this.cardConfig)
  391. });
  392. this.$nextTick(() => {
  393. this.rebuildPages();
  394. this.setCurPage(0);
  395. });
  396. },
  397. async getCardConfig() {
  398. const data = await cardConfigInfos(this.prepareTcPCard.cardRuleId);
  399. if (!data) {
  400. this.$message.error("找不到题卡规则!");
  401. return;
  402. }
  403. let config = {
  404. ...data,
  405. ...{
  406. pageSize: "A3",
  407. columnNumber: 2,
  408. columnGap: 20,
  409. showForbidArea: true,
  410. cardDesc: "",
  411. makeMethod: this.prepareTcPCard.makeMethod
  412. }
  413. };
  414. config.aOrB = this.prepareTcPCard["paperType"]
  415. ? this.prepareTcPCard.paperType.split(",").length > 1
  416. : 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. 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. cancelButtonClass: "el-button--danger is-plain",
  577. confirmButtonClass: "el-button--primary",
  578. type: "warning"
  579. })
  580. .then(() => {
  581. window.cardData = {
  582. cardConfig: this.cardConfig,
  583. pages: this.pages,
  584. paperParams: this.paperParams
  585. };
  586. this.isSubmit = true;
  587. const { href } = this.$router.resolve({
  588. name: "CardPreview",
  589. params: {
  590. cardId: 1,
  591. viewType: "frame"
  592. }
  593. });
  594. this.cardPreviewUrl = href;
  595. })
  596. .catch(() => {});
  597. },
  598. registWindowSubmit() {
  599. window.submitCardTemp = async (htmlContent, model) => {
  600. const datas = this.getCardData(htmlContent, model);
  601. datas.status = "SUBMIT";
  602. const result = await saveCard(
  603. datas,
  604. this.getRequestConfig()
  605. ).catch(() => {});
  606. this.cardPreviewUrl = "";
  607. this.isSubmit = false;
  608. window.cardData = null;
  609. if (result) {
  610. this.cardId = result;
  611. this.$ls.set("cardId", this.cardId);
  612. this.canSave = false;
  613. this.$message.success("提交成功!");
  614. this.goback();
  615. } else {
  616. this.$message.error("提交失败,请重新尝试!");
  617. }
  618. };
  619. },
  620. toExit() {
  621. this.$confirm(
  622. "请确保当前题卡已经正常保存,确定要退出当前题卡编辑吗?",
  623. "提示",
  624. {
  625. cancelButtonClass: "el-button--danger is-plain",
  626. confirmButtonClass: "el-button--primary",
  627. type: "warning"
  628. }
  629. )
  630. .then(() => {
  631. this.goback();
  632. })
  633. .catch(() => {});
  634. }
  635. },
  636. beforeDestroy() {
  637. this.$ls.remove("cardId");
  638. this.$ls.remove("prepareTcPCard");
  639. this.initState();
  640. delete window.submitCardTemp;
  641. }
  642. };
  643. </script>