CardEdit.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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: true,
  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(
  157. datas,
  158. this.getRequestConfig()
  159. ).catch(() => {});
  160. this.$refs.CardDesign.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: "CardPreview",
  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>