CardFreeEdit.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <div class="card-free-edit">
  3. <card-free-design
  4. v-if="dataReady"
  5. ref="CardFreeDesign"
  6. :content="cardContent"
  7. @on-preview="toPreview"
  8. @on-save="toSave"
  9. @on-submit="toSubmit"
  10. @on-exit="toExit"
  11. ></card-free-design>
  12. <!-- card-view-frame -->
  13. <div v-if="cardPreviewUrl" class="design-preview-frame">
  14. <iframe :src="cardPreviewUrl" frameborder="0"></iframe>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. import { cardConfigInfos, cardDetail, saveCard } from "../api";
  20. import CardFreeDesign from "../modules/free/components/CardFreeDesign";
  21. export default {
  22. name: "CardFreeEdit",
  23. components: {
  24. CardFreeDesign,
  25. },
  26. data() {
  27. return {
  28. cardId: this.$route.params.cardId || this.$ls.get("cardId"),
  29. prepareTcPCard: this.$ls.get("prepareTcPCard", {
  30. examTaskId: "",
  31. courseCode: "",
  32. courseName: "",
  33. makeMethod: "SELF",
  34. cardRuleId: "",
  35. }),
  36. cardContent: {},
  37. cardPreviewUrl: "",
  38. isSubmit: false,
  39. canSave: false,
  40. dataReady: false,
  41. };
  42. },
  43. computed: {
  44. isEdit() {
  45. return !!this.cardId;
  46. },
  47. },
  48. mounted() {
  49. // if (!this.prepareTcPCard.examTaskId && !this.isEdit) {
  50. // this.$message.error("找不到命题任务,请退出题卡制作!");
  51. // return;
  52. // }
  53. this.initCard();
  54. this.registWindowSubmit();
  55. },
  56. beforeDestroy() {
  57. this.$ls.remove("cardId");
  58. this.$ls.remove("prepareTcPCard");
  59. delete window.submitCardTemp;
  60. },
  61. methods: {
  62. async initCard() {
  63. this.dataReady = false;
  64. if (this.isEdit) {
  65. await this.getCardTempDetail();
  66. } else {
  67. const cardConfig = await this.getCardConfig();
  68. this.cardContent = {
  69. pages: [],
  70. cardConfig,
  71. paperParams: {},
  72. };
  73. }
  74. this.dataReady = true;
  75. },
  76. getCardTitle(titleRule) {
  77. const fieldMap = {
  78. courseCode: this.prepareTcPCard.courseCode,
  79. courseName: this.prepareTcPCard.courseName,
  80. schoolName: this.prepareTcPCard.schoolName,
  81. };
  82. Object.entries(fieldMap).forEach(([key, val]) => {
  83. titleRule = titleRule.replace("${" + key + "}", val);
  84. });
  85. return titleRule;
  86. },
  87. async getCardTempDetail() {
  88. const detData = await cardDetail(this.cardId);
  89. // 可能存在题卡内容没有记录的情况
  90. if (detData.content) {
  91. this.cardContent = JSON.parse(detData.content);
  92. } else {
  93. let cardConfig = await this.getCardConfig();
  94. // 没有题卡内容时,直接创建新的内容
  95. if (detData.makeMethod === "CUST") {
  96. this.setCardConfig({ cardTitle: detData.title });
  97. }
  98. this.cardContent = {
  99. pages: [],
  100. cardConfig,
  101. paperParams: {},
  102. };
  103. }
  104. },
  105. async getCardConfig() {
  106. const data = await cardConfigInfos(this.prepareTcPCard.cardRuleId);
  107. if (!data) {
  108. this.$message.error("找不到题卡规则!");
  109. return;
  110. }
  111. let config = {
  112. ...data,
  113. ...{
  114. pageSize: "A3",
  115. columnNumber: 2,
  116. columnGap: 20,
  117. showForbidArea: true,
  118. cardDesc: "",
  119. makeMethod: this.prepareTcPCard.makeMethod,
  120. },
  121. };
  122. config.aOrB = true; // 默认开启A/B卷型
  123. config.requiredFields = JSON.parse(config.requiredFields);
  124. config.extendFields = JSON.parse(config.extendFields);
  125. config.cardTitle = this.getCardTitle(config.titleRule);
  126. return config;
  127. },
  128. // 操作
  129. getRequestConfig() {
  130. return this.prepareTcPCard.makeMethod === "CUST"
  131. ? {
  132. headers: {
  133. schoolId: this.prepareTcPCard.schoolId,
  134. },
  135. }
  136. : {};
  137. },
  138. getCardData(htmlContent = "", model = "") {
  139. let data = this.$refs.CardFreeDesign.getCardData(htmlContent, model);
  140. data = {
  141. ...data,
  142. type: "CUSTOM",
  143. ...this.prepareTcPCard,
  144. };
  145. if (this.cardId) data.id = this.cardId;
  146. return data;
  147. },
  148. async toPreview(datas) {
  149. await this.toSave(datas);
  150. const { href } = this.$router.resolve({
  151. name: "CardFreePreview",
  152. params: {
  153. cardId: this.cardId,
  154. viewType: "view",
  155. },
  156. });
  157. window.open(href);
  158. },
  159. async toSave(datas) {
  160. datas.status = "STAGE";
  161. const result = await saveCard(datas, this.getRequestConfig()).catch(
  162. () => {}
  163. );
  164. this.$refs.CardFreeDesign.unloading();
  165. if (!result) return;
  166. this.cardId = result;
  167. this.$ls.set("cardId", this.cardId);
  168. this.$message.success("保存成功!");
  169. },
  170. async toSubmit(cardData) {
  171. const res = await this.$confirm("确定要提交当前题卡吗?", "提示", {
  172. type: "warning",
  173. }).catch(() => {});
  174. if (res !== "confirm") return;
  175. window.cardData = cardData;
  176. const { href } = this.$router.resolve({
  177. name: "CardFreePreview",
  178. params: {
  179. cardId: 1,
  180. viewType: "frame",
  181. },
  182. });
  183. this.cardPreviewUrl = href;
  184. },
  185. registWindowSubmit() {
  186. window.submitCardTemp = async (htmlContent, model) => {
  187. const datas = this.getCardData(htmlContent, model);
  188. datas.status = "SUBMIT";
  189. const result = await saveCard(datas, this.getRequestConfig()).catch(
  190. () => {}
  191. );
  192. this.cardPreviewUrl = "";
  193. window.cardData = null;
  194. if (result) {
  195. this.cardId = result;
  196. this.$ls.set("cardId", this.cardId);
  197. this.canSave = false;
  198. this.$message.success("提交成功!");
  199. this.goback();
  200. } else {
  201. this.$message.error("提交失败,请重新尝试!");
  202. }
  203. };
  204. },
  205. toExit() {
  206. this.$confirm(
  207. "请确保当前题卡已经正常保存,确定要退出当前题卡编辑吗?",
  208. "提示",
  209. {
  210. type: "warning",
  211. }
  212. )
  213. .then(() => {
  214. this.goback();
  215. })
  216. .catch(() => {});
  217. },
  218. },
  219. };
  220. </script>