CardEdit.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. };
  66. }
  67. this.dataReady = true;
  68. },
  69. getCardTitle(titleRule) {
  70. const fieldMap = {
  71. courseCode: this.prepareTcPCard.courseCode,
  72. courseName: this.prepareTcPCard.courseName,
  73. schoolName: this.prepareTcPCard.schoolName,
  74. };
  75. Object.entries(fieldMap).forEach(([key, val]) => {
  76. titleRule = titleRule.replace("${" + key + "}", val);
  77. });
  78. return titleRule;
  79. },
  80. async getCardTempDetail() {
  81. const detData = await cardDetail(this.cardId);
  82. // 可能存在题卡内容没有记录的情况
  83. if (detData.content) {
  84. this.cardContent = JSON.parse(detData.content);
  85. } else {
  86. let cardConfig = await this.getCardConfig();
  87. // 没有题卡内容时,直接创建新的内容
  88. if (detData.makeMethod === "CUST") {
  89. cardConfig.cardTitle = detData.title;
  90. }
  91. this.cardContent = {
  92. pages: [],
  93. cardConfig,
  94. };
  95. }
  96. },
  97. async getCardConfig() {
  98. const data = await cardConfigInfos(this.prepareTcPCard.cardRuleId);
  99. if (!data) {
  100. this.$message.error("找不到题卡规则!");
  101. return;
  102. }
  103. let config = {
  104. ...data,
  105. ...{
  106. pageSize: "A3",
  107. columnNumber: 2,
  108. columnGap: 20,
  109. showForbidArea: false,
  110. cardDesc: "",
  111. makeMethod: this.prepareTcPCard.makeMethod,
  112. },
  113. };
  114. config.aOrB = true; // 默认开启A/B卷型
  115. config.requiredFields = JSON.parse(config.requiredFields);
  116. config.extendFields = JSON.parse(config.extendFields);
  117. config.cardTitle = this.getCardTitle(config.titleRule);
  118. return config;
  119. },
  120. // 操作
  121. getRequestConfig() {
  122. return this.prepareTcPCard.makeMethod === "CUST"
  123. ? {
  124. headers: {
  125. schoolId: this.prepareTcPCard.schoolId,
  126. },
  127. }
  128. : {};
  129. },
  130. getCardData(htmlContent = "", model = "") {
  131. let data = this.$refs.CardDesign.getCardData(htmlContent, model);
  132. data = {
  133. ...data,
  134. type: "CUSTOM",
  135. ...this.prepareTcPCard,
  136. };
  137. if (this.cardId) data.id = this.cardId;
  138. return data;
  139. },
  140. async toPreview(datas) {
  141. await this.toSave(datas);
  142. const { href } = this.$router.resolve({
  143. name: "CardPreview",
  144. params: {
  145. cardId: this.cardId,
  146. viewType: "view",
  147. },
  148. });
  149. window.open(href);
  150. },
  151. // save
  152. async toSave(datas) {
  153. datas.status = "STAGE";
  154. const result = await saveCard(datas, this.getRequestConfig()).catch(
  155. () => {}
  156. );
  157. this.$refs.CardDesign.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: "CardPreview",
  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>