CardEdit.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <div class="card-edit">
  3. <card-design
  4. v-if="dataReady"
  5. ref="CardDesign"
  6. :content="cardContent"
  7. :paper-json="paperJson"
  8. @on-preview="toPreview"
  9. @on-save="toSave"
  10. @on-submit="toSubmit"
  11. @on-exit="toExit"
  12. ></card-design>
  13. <!-- card-view-frame -->
  14. <div v-if="cardPreviewUrl" class="design-preview-frame">
  15. <iframe :src="cardPreviewUrl" frameborder="0"></iframe>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. import { cardConfigInfos, cardDetail, saveCard, paperDetailApi } from "../api";
  21. import CardDesign from "../components/CardDesign";
  22. import { getPaperJsonSimpleStructInfo } from "../autoBuild/paperStruct";
  23. export default {
  24. name: "CardEdit",
  25. components: {
  26. CardDesign,
  27. },
  28. data() {
  29. return {
  30. cardId: this.$route.params.cardId,
  31. paperId: this.$route.params.paperId,
  32. paperJson: null,
  33. cardContent: {},
  34. cardPreviewUrl: "",
  35. canSave: false,
  36. dataReady: false,
  37. };
  38. },
  39. computed: {
  40. isEdit() {
  41. return !!this.cardId;
  42. },
  43. },
  44. mounted() {
  45. this.initCard();
  46. this.registWindowSubmit();
  47. },
  48. beforeDestroy() {
  49. delete window.submitCardTemp;
  50. },
  51. methods: {
  52. async initCard() {
  53. this.dataReady = false;
  54. const paperRes = await paperDetailApi(this.paperId);
  55. this.paperJson = paperRes.data;
  56. if (this.isEdit) {
  57. await this.getCardTempDetail();
  58. } else {
  59. await this.initCardContent();
  60. }
  61. this.dataReady = true;
  62. },
  63. async getCardTempDetail() {
  64. const detData = await cardDetail(this.cardId);
  65. // 可能存在题卡内容没有记录的情况
  66. if (detData.content) {
  67. this.cardContent = JSON.parse(detData.content);
  68. const curPaperSimpleStruct = getPaperJsonSimpleStructInfo(
  69. this.paperJson
  70. );
  71. if (curPaperSimpleStruct !== this.cardContent.paperSimpleStruct) {
  72. this.$message.info("试卷结构有变化,将重新生成题卡");
  73. this.cardContent = {
  74. pages: [],
  75. cardConfig: detData.cardConfig,
  76. paperSimpleStruct: getPaperJsonSimpleStructInfo(this.paperJson),
  77. };
  78. }
  79. } else {
  80. this.$message.info("无题卡内容,将重新生成题卡");
  81. await this.initCardContent();
  82. }
  83. },
  84. async initCardContent() {
  85. const cardConfig = await this.getCardConfig();
  86. this.cardContent = {
  87. pages: [],
  88. cardConfig,
  89. paperSimpleStruct: getPaperJsonSimpleStructInfo(this.paperJson),
  90. };
  91. },
  92. async getCardConfig() {
  93. const data = await cardConfigInfos();
  94. if (!data) {
  95. this.$message.error("找不到题卡版头!");
  96. return;
  97. }
  98. const config = {
  99. ...data,
  100. ...{
  101. pageSize: "A3",
  102. columnNumber: 2,
  103. columnGap: 20,
  104. showForbidArea: true,
  105. showScorePan: false,
  106. },
  107. };
  108. return config;
  109. },
  110. // 操作
  111. getCardData(htmlContent = "", model = "") {
  112. let data = this.$refs.CardDesign.getCardData(htmlContent, model);
  113. data = {
  114. ...data,
  115. paperId: this.paperId,
  116. };
  117. if (this.cardId) data.id = this.cardId;
  118. return data;
  119. },
  120. async toPreview(datas) {
  121. await this.toSave(datas);
  122. const { href } = this.$router.resolve({
  123. name: "CardPreview",
  124. params: {
  125. cardId: this.cardId,
  126. viewType: "view",
  127. },
  128. });
  129. window.open(href);
  130. },
  131. // save
  132. async toSave(datas) {
  133. datas.status = "STAGE";
  134. const result = await saveCard(datas).catch(() => {});
  135. this.$refs.CardDesign.unloading();
  136. if (!result) return;
  137. this.cardId = result;
  138. // this.$ls.set("cardId", this.cardId);
  139. this.$message.success("保存成功!");
  140. },
  141. async toSubmit(cardData) {
  142. const res = await this.$confirm("确定要提交当前题卡吗?", "提示", {
  143. type: "warning",
  144. }).catch(() => {});
  145. if (res !== "confirm") return;
  146. window.cardData = cardData;
  147. const { href } = this.$router.resolve({
  148. name: "CardPreview",
  149. params: {
  150. cardId: 1,
  151. viewType: "frame",
  152. },
  153. });
  154. this.cardPreviewUrl = href;
  155. },
  156. registWindowSubmit() {
  157. window.submitCardTemp = async (htmlContent, model, cardImages) => {
  158. if (!cardImages || !cardImages.length) {
  159. this.$message.error("题卡图片生成失败!");
  160. return;
  161. }
  162. const datas = this.getCardData(htmlContent, model);
  163. datas.status = "SUBMIT";
  164. datas.cardImages = cardImages;
  165. const result = await saveCard(datas, this.getRequestConfig()).catch(
  166. () => {}
  167. );
  168. this.cardPreviewUrl = "";
  169. window.cardData = null;
  170. if (result) {
  171. this.cardId = result;
  172. // this.$ls.set("cardId", this.cardId);
  173. this.canSave = false;
  174. this.$message.success("提交成功!");
  175. this.goback();
  176. } else {
  177. this.$message.error("提交失败,请重新尝试!");
  178. }
  179. };
  180. },
  181. toExit() {
  182. this.$confirm(
  183. "请确保当前题卡已经正常保存,确定要退出当前题卡编辑吗?",
  184. "提示",
  185. {
  186. type: "warning",
  187. }
  188. )
  189. .then(() => {
  190. this.goback();
  191. })
  192. .catch(() => {});
  193. },
  194. },
  195. };
  196. </script>