CardEdit.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <template>
  2. <div class="card-edit">
  3. <component
  4. v-if="dataReady"
  5. :is="editComp"
  6. ref="CardDesign"
  7. :content="cardContent"
  8. @on-preview="toPreview"
  9. @on-save="toSave"
  10. @on-submit="toSubmit"
  11. @on-exit="toExit"
  12. ></component>
  13. <!-- card-preview-dialog -->
  14. <card-preview-dialog
  15. ref="CardPreviewDialog"
  16. :cardData="cardData"
  17. :view-type="viewType"
  18. @confirm="previewConfirm"
  19. ></card-preview-dialog>
  20. </div>
  21. </template>
  22. <script>
  23. import { cardConfigInfos, cardDetail, saveCard } from "../api";
  24. import CardDesign from "../../../../card/components/CardDesign.vue";
  25. import CardFreeDesign from "../../../../card/modules/free/components/CardFreeDesign";
  26. import CardPreviewDialog from "../components/CardPreviewDialog.vue";
  27. import { examRuleDetail } from "../../base/api";
  28. import { getEnums } from "../../login/api";
  29. import timeMixin from "@/mixins/timeMixin";
  30. import { MD5 } from "@/plugins/md5";
  31. import { getBase64Barcode } from "@/plugins/utils";
  32. export default {
  33. name: "card-edit",
  34. components: {
  35. CardDesign,
  36. CardFreeDesign,
  37. CardPreviewDialog,
  38. },
  39. props: {
  40. isDialog: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. },
  45. mixins: [timeMixin],
  46. data() {
  47. return {
  48. cardId: this.$route.params.cardId || this.$ls.get("cardId"),
  49. prepareTcPCard: this.$ls.get("prepareTcPCard", {}),
  50. cardType: "CUSTOM",
  51. cardCreateMethod: "STANDARD",
  52. cardContent: {},
  53. cardName: "", // 题卡自定义名称
  54. cardPreviewUrl: "",
  55. canSave: false,
  56. dataReady: false,
  57. lastStageCardContent: "",
  58. sysCardSize: "",
  59. // card-preview-dialog
  60. viewType: "view",
  61. cardData: null,
  62. };
  63. },
  64. computed: {
  65. isEdit() {
  66. return !!this.cardId;
  67. },
  68. editComp() {
  69. return this.cardCreateMethod === "STANDARD"
  70. ? "card-design"
  71. : "card-free-design";
  72. },
  73. IS_GENERIC() {
  74. return this.cardType === "GENERIC";
  75. },
  76. },
  77. mounted() {
  78. this.cardCreateMethod = this.prepareTcPCard.createMethod || "STANDARD";
  79. this.cardId = this.cardId || this.prepareTcPCard.id;
  80. this.cardType = this.prepareTcPCard.type || "CUSTOM";
  81. this.sysCardSize = this.$ls.get("schoolInfo", { cardSize: "A3" }).cardSize;
  82. this.initCard();
  83. this.openAutoSave();
  84. },
  85. methods: {
  86. openAutoSave() {
  87. this.clearSetTs();
  88. this.addSetTime(async () => {
  89. await this.$refs.CardDesign.toSave();
  90. this.openAutoSave();
  91. }, 1 * 60 * 1000);
  92. },
  93. async initCard() {
  94. this.dataReady = false;
  95. if (this.isEdit) {
  96. await this.getCardTempDetail();
  97. } else {
  98. let cardConfig = await this.getCardConfig();
  99. if (this.IS_GENERIC) {
  100. cardConfig.cardTitle = this.prepareTcPCard.title;
  101. this.cardName = this.prepareTcPCard.title;
  102. } else {
  103. this.cardName = this.prepareTcPCard.cardName;
  104. }
  105. this.cardContent = {
  106. pages: [],
  107. cardConfig,
  108. };
  109. }
  110. // 设置默认的课程代码条码信息
  111. if (!this.cardContent.cardConfig.courseCodeBarcodeSrc) {
  112. this.cardContent.cardConfig.courseCodeBarcodeSrc =
  113. getBase64Barcode("123456789");
  114. this.cardContent.cardConfig.courseCodeBarcodeName = "123456789";
  115. }
  116. this.dataReady = true;
  117. },
  118. getCardTitle(titleRule) {
  119. const fieldMap = {
  120. courseCode: this.prepareTcPCard.courseCode || "",
  121. courseName: this.prepareTcPCard.courseName || "",
  122. schoolName: this.prepareTcPCard.schoolName || "",
  123. };
  124. Object.entries(fieldMap).forEach(([key, val]) => {
  125. titleRule = titleRule.replace("${" + key + "}", val);
  126. });
  127. return titleRule;
  128. },
  129. async getCardTempDetail() {
  130. const detData = await cardDetail(
  131. this.cardId,
  132. this.prepareTcPCard.isModel4
  133. );
  134. // 可能存在题卡内容没有记录的情况
  135. if (detData.content || detData.stageContent) {
  136. const content = detData.stageContent || detData.content;
  137. this.cardContent = JSON.parse(content);
  138. this.cardName = detData.title;
  139. this.lastStageCardContent = content;
  140. } else {
  141. let cardConfig = await this.getCardConfig();
  142. // 没有题卡内容时,直接创建新的内容
  143. if (detData.makeMethod === "CUST" || detData.type === "GENERIC") {
  144. cardConfig.cardTitle = detData.title;
  145. this.cardName = detData.title;
  146. } else {
  147. this.cardName = "";
  148. }
  149. this.cardContent = {
  150. pages: [],
  151. cardConfig,
  152. };
  153. }
  154. if (detData.courseCode) {
  155. this.cardContent.cardConfig.courseCodeBarcodeSrc = getBase64Barcode(
  156. detData.courseCode
  157. );
  158. this.cardContent.cardConfig.courseCodeBarcodeName = detData.courseCode;
  159. }
  160. },
  161. async getCardConfig() {
  162. if (this.cardCreateMethod === "STANDARD") {
  163. const data = await cardConfigInfos(this.prepareTcPCard.cardRuleId);
  164. if (!data) {
  165. this.$message.error("找不到题卡规则!");
  166. return Promise.reject();
  167. }
  168. let config = {
  169. ...data,
  170. ...{
  171. pageSize: this.sysCardSize,
  172. columnNumber: 2,
  173. columnGap: 20,
  174. showForbidArea: false,
  175. makeMethod: this.prepareTcPCard.makeMethod,
  176. courseCodeBarcodeSrc: "",
  177. courseCodeBarcodeName: "",
  178. },
  179. };
  180. config.fillNumber = data.examNumberDigit;
  181. config.aOrB = Boolean(this.prepareTcPCard.openAb);
  182. config.requiredFields = JSON.parse(config.requiredFields);
  183. config.extendFields = JSON.parse(config.extendFields);
  184. config.relationList = JSON.parse(config.relationList || "[]");
  185. config.cardTitle = this.getCardTitle(config.titleRule);
  186. config.firstLevelSubheading = this.getCardTitle(
  187. config.firstLevelSubheading
  188. );
  189. config.secondLevelSubheading = this.getCardTitle(
  190. config.secondLevelSubheading
  191. );
  192. return config;
  193. } else {
  194. let requiredFields = await getEnums("REQUIRED_FIELDS");
  195. requiredFields = requiredFields
  196. .filter((item) => item.enable)
  197. .map((item) => {
  198. return {
  199. code: item.code,
  200. name: item.name,
  201. enable: item.enable,
  202. };
  203. });
  204. const examRule = await examRuleDetail();
  205. const extendFields = examRule.extendFields || "[]";
  206. const config = {
  207. pageSize: this.sysCardSize,
  208. columnNumber: 2,
  209. columnGap: 20,
  210. showForbidArea: false,
  211. requiredFields,
  212. courseCodeBarcodeSrc: "",
  213. courseCodeBarcodeName: "",
  214. extendFields: JSON.parse(extendFields),
  215. };
  216. return config;
  217. }
  218. },
  219. // 操作
  220. getRequestConfig() {
  221. return this.prepareTcPCard.makeMethod === "CUST"
  222. ? {
  223. headers: {
  224. schoolId: this.prepareTcPCard.schoolId,
  225. },
  226. }
  227. : {};
  228. },
  229. getCardInfo(data) {
  230. let cardInfo = {
  231. ...data,
  232. ...this.prepareTcPCard,
  233. title: this.cardName,
  234. };
  235. // openAb以参数信息为主
  236. cardInfo.openAb = data.openAb || false;
  237. if (this.cardId) cardInfo.id = this.cardId;
  238. return cardInfo;
  239. },
  240. // card preview dialog
  241. dialogClosed() {
  242. this.cardDialogPreviewUrl = "";
  243. },
  244. toPreview(cardData) {
  245. this.cardData = {
  246. ...cardData,
  247. createMethod: this.cardCreateMethod,
  248. type: this.cardType,
  249. };
  250. this.viewType = "view";
  251. this.$refs.CardPreviewDialog.open();
  252. },
  253. // save
  254. async toSave(datas) {
  255. let cardInfo = this.getCardInfo(datas);
  256. cardInfo.status = "STAGE";
  257. if (MD5(cardInfo.content) === MD5(this.lastStageCardContent)) return;
  258. const result = await saveCard(cardInfo, this.getRequestConfig()).catch(
  259. () => {}
  260. );
  261. this.lastStageCardContent = cardInfo.content;
  262. this.$refs.CardDesign.updateUnsubmitStatus(true);
  263. if (!result) return;
  264. this.cardId = result.id;
  265. this.$ls.set("cardId", this.cardId);
  266. },
  267. async toSubmit(cardData) {
  268. const res = await this.$prompt("", "提示", {
  269. type: "warning",
  270. showInput: true,
  271. inputPlaceholder: "请输入题卡名称",
  272. inputValue: this.cardName,
  273. inputValidator: (val) => {
  274. if (!val) return "请输入题卡名称!";
  275. if (val.length > 50) return "题卡名称不得超过50个字符!";
  276. return true;
  277. },
  278. }).catch(() => {});
  279. if (!res || res.action !== "confirm") return;
  280. this.cardName = res.value;
  281. this.$refs.CardDesign.loading();
  282. this.cardData = {
  283. ...cardData,
  284. createMethod: this.cardCreateMethod,
  285. type: this.cardType,
  286. };
  287. this.viewType = "print";
  288. this.$refs.CardPreviewDialog.open();
  289. },
  290. async previewConfirm(content) {
  291. if (!content) {
  292. this.$refs.CardDesign.unloading();
  293. this.cardData = null;
  294. this.$message.error("错误:预览,提交失败,请重新尝试!");
  295. return;
  296. }
  297. const { htmlContent, model } = content;
  298. const datas = this.$refs.CardDesign.getCardData(htmlContent, model);
  299. const cardInfo = this.getCardInfo(datas);
  300. cardInfo.status = "SUBMIT";
  301. const result = await saveCard(cardInfo, this.getRequestConfig()).catch(
  302. () => {}
  303. );
  304. this.$refs.CardDesign.unloading();
  305. if (result) {
  306. this.cardId = result.id;
  307. this.$ls.set("cardId", this.cardId);
  308. this.$refs.CardDesign.updateUnsubmitStatus(false);
  309. this.$message.success("提交成功!");
  310. this.goback();
  311. } else {
  312. this.$message.error("提交失败,请重新尝试!");
  313. }
  314. },
  315. toExit() {
  316. this.$confirm(
  317. "请确保当前题卡已经正常保存,确定要退出当前题卡编辑吗?",
  318. "提示",
  319. {
  320. type: "warning",
  321. }
  322. )
  323. .then(() => {
  324. this.goback();
  325. })
  326. .catch(() => {});
  327. },
  328. goback() {
  329. if (this.isDialog) {
  330. this.$emit("exit", { id: this.cardId, title: this.cardName });
  331. } else {
  332. this.$router.go(-1);
  333. }
  334. },
  335. },
  336. beforeDestroy() {
  337. this.$ls.remove("cardId");
  338. this.$ls.remove("prepareTcPCard");
  339. this.clearSetTs();
  340. },
  341. };
  342. </script>