CardFreeEdit.vue 6.0 KB

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