CardFreeEdit.vue 6.0 KB

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