CardEdit.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <template>
  2. <div class="card-edit">
  3. <card-design
  4. v-if="dataReady"
  5. ref="CardDesign"
  6. :content="cardContent"
  7. @on-preview="toPreview"
  8. @on-save="toSave"
  9. @on-submit="toSubmit"
  10. @on-exit="toExit"
  11. ></card-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 CardDesign from "../components/CardDesign";
  21. export default {
  22. name: "card-edit",
  23. components: {
  24. CardDesign,
  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. canSave: false,
  39. dataReady: false,
  40. };
  41. },
  42. computed: {
  43. isEdit() {
  44. return !!this.cardId;
  45. },
  46. },
  47. mounted() {
  48. if (!this.prepareTcPCard.examTaskId && !this.isEdit) {
  49. this.$message.error("找不到命题任务,请退出题卡制作!");
  50. return;
  51. }
  52. this.initCard();
  53. this.registWindowSubmit();
  54. },
  55. methods: {
  56. async initCard() {
  57. this.dataReady = false;
  58. if (this.isEdit) {
  59. await this.getCardTempDetail();
  60. } else {
  61. const cardConfig = await this.getCardConfig();
  62. this.cardContent = {
  63. pages: [],
  64. cardConfig,
  65. paperParams: {},
  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. cardConfig.cardTitle = detData.title;
  91. }
  92. this.cardContent = {
  93. pages: [],
  94. cardConfig,
  95. paperParams: {},
  96. };
  97. }
  98. },
  99. async getCardConfig() {
  100. const data = await cardConfigInfos(this.prepareTcPCard.cardRuleId);
  101. if (!data) {
  102. this.$message.error("找不到题卡规则!");
  103. return;
  104. }
  105. let config = {
  106. ...data,
  107. ...{
  108. pageSize: "A3",
  109. columnNumber: 2,
  110. columnGap: 20,
  111. showForbidArea: false,
  112. cardDesc: "",
  113. makeMethod: this.prepareTcPCard.makeMethod,
  114. },
  115. };
  116. config.aOrB = true; // 默认开启A/B卷型
  117. config.requiredFields = JSON.parse(config.requiredFields);
  118. config.extendFields = JSON.parse(config.extendFields);
  119. config.cardTitle = this.getCardTitle(config.titleRule);
  120. return config;
  121. },
  122. // 操作
  123. getRequestConfig() {
  124. return this.prepareTcPCard.makeMethod === "CUST"
  125. ? {
  126. headers: {
  127. schoolId: this.prepareTcPCard.schoolId,
  128. },
  129. }
  130. : {};
  131. },
  132. getCardData(htmlContent = "", model = "") {
  133. let data = this.$refs.CardDesign.getCardData(htmlContent, model);
  134. data = {
  135. ...data,
  136. type: "CUSTOM",
  137. ...this.prepareTcPCard,
  138. };
  139. if (this.cardId) data.id = this.cardId;
  140. return data;
  141. },
  142. async toPreview(datas) {
  143. await this.toSave(datas);
  144. const { href } = this.$router.resolve({
  145. name: "CardPreview",
  146. params: {
  147. cardId: this.cardId,
  148. viewType: "view",
  149. },
  150. });
  151. window.open(href);
  152. },
  153. // save
  154. async toSave(datas) {
  155. datas.status = "STAGE";
  156. const result = await saveCard(datas, this.getRequestConfig()).catch(
  157. () => {}
  158. );
  159. this.$refs.CardDesign.unloading();
  160. if (!result) return;
  161. this.cardId = result;
  162. this.$ls.set("cardId", this.cardId);
  163. this.$message.success("保存成功!");
  164. },
  165. async toSubmit(cardData) {
  166. const res = await this.$confirm("确定要提交当前题卡吗?", "提示", {
  167. type: "warning",
  168. }).catch(() => {});
  169. if (res !== "confirm") return;
  170. window.cardData = cardData;
  171. const { href } = this.$router.resolve({
  172. name: "CardPreview",
  173. params: {
  174. cardId: 1,
  175. viewType: "frame",
  176. },
  177. });
  178. this.cardPreviewUrl = href;
  179. },
  180. registWindowSubmit() {
  181. window.submitCardTemp = async (htmlContent, model) => {
  182. const datas = this.getCardData(htmlContent, model);
  183. datas.status = "SUBMIT";
  184. const result = await saveCard(datas, this.getRequestConfig()).catch(
  185. () => {}
  186. );
  187. this.cardPreviewUrl = "";
  188. window.cardData = null;
  189. if (result) {
  190. this.cardId = result;
  191. this.$ls.set("cardId", this.cardId);
  192. this.canSave = false;
  193. this.$message.success("提交成功!");
  194. this.goback();
  195. } else {
  196. this.$message.error("提交失败,请重新尝试!");
  197. }
  198. };
  199. },
  200. toExit() {
  201. this.$confirm(
  202. "请确保当前题卡已经正常保存,确定要退出当前题卡编辑吗?",
  203. "提示",
  204. {
  205. type: "warning",
  206. }
  207. )
  208. .then(() => {
  209. this.goback();
  210. })
  211. .catch(() => {});
  212. },
  213. },
  214. beforeDestroy() {
  215. this.$ls.remove("cardId");
  216. this.$ls.remove("prepareTcPCard");
  217. delete window.submitCardTemp;
  218. },
  219. };
  220. </script>