index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <div>
  3. <div class="json-box-9136076486841527">
  4. <codemirror style="height:100%;" ref="myEditor" :value="editorJson"></codemirror>
  5. </div>
  6. <div>
  7. </div>
  8. <div class="copy-btn-box-9136076486841527">
  9. 表单名:
  10. <a-input placeholder="请输入表名" style="width:20%;" v-model="formData.text" />&nbsp;
  11. 数据库表名:
  12. <!-- <a-input placeholder="请输入表名" style="width:20%;" v-model="formData.businessTable" />&nbsp; -->
  13. <a-select v-model="formData.businessTable" style="width: 20%" placeholder="请选择数据库表名" allowClear show-search >
  14. <a-select-option v-for="(item) in tableList" :key="item.tableName" :value="item.tableName">{{item.tableName}}{{item.tableComment}}</a-select-option>
  15. </a-select>
  16. 表单描述
  17. <a-tooltip>
  18. <template slot="title">
  19. 用来明确表单的使用含义
  20. </template>
  21. <a-icon type="question-circle-o" />
  22. </a-tooltip>:
  23. <a-input placeholder="请输入表单描述" style="width:20%;" v-model="formData.stepMemo" />&nbsp;
  24. </div>
  25. <div class="copy-btn-box-9136076486841527" style="text-align:right">
  26. <a-divider style="margin: 5px 0;"/>
  27. <a-button
  28. @click="handleCopyJson"
  29. type="primary"
  30. class="copy-btn"
  31. data-clipboard-action="copy"
  32. :data-clipboard-text="editorJson"
  33. >复制数据</a-button>
  34. <a-button @click="handleExportJson" type="primary">导出代码</a-button>&nbsp;
  35. <a-button @click="save" type="primary">保存</a-button>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. // 剪切板组件
  41. import Clipboard from "clipboard";
  42. import { codemirror } from "vue-codemirror-lite";
  43. import { getIdcsList, formAdd, getTableList } from "../api/api";
  44. export default {
  45. name: "PreviewCode",
  46. props: {
  47. fileFormat: {
  48. type: String,
  49. default: "json"
  50. },
  51. editorJson: {
  52. type: String,
  53. default: ""
  54. },
  55. formData: {
  56. default: function() {
  57. return {};
  58. }
  59. }
  60. },
  61. data() {
  62. return {
  63. visible: false,
  64. businessTable: "",
  65. text: "",
  66. tableList:[]//数据库下拉列表数据
  67. };
  68. },
  69. created(){
  70. //获取数据库表名下拉数据
  71. getTableList().then(res=>{
  72. if(res.data.success){
  73. this.tableList=res.data.result;
  74. }
  75. });
  76. },
  77. components: {
  78. codemirror
  79. },
  80. methods: {
  81. exportData(data, fileName = `demo.${this.fileFormat}`) {
  82. let content = "data:text/csv;charset=utf-8,";
  83. content += data;
  84. var encodedUri = encodeURI(content);
  85. var actions = document.createElement("a");
  86. actions.setAttribute("href", encodedUri);
  87. actions.setAttribute("download", fileName);
  88. actions.click();
  89. },
  90. handleExportJson() {
  91. // 导出JSON
  92. getIdcsList();
  93. console.log(this.editorJson);
  94. this.exportData(this.editorJson);
  95. },
  96. save() {
  97. const json = JSON.stringify(this.editorJson).toString();
  98. if (json.indexOf("div") != -1) {
  99. this.$message.error("保存数据不是json格式");
  100. return;
  101. }
  102. if (!this.formData.businessTable) {
  103. this.$message.error("请输入数据库表名");
  104. return;
  105. }
  106. if (!this.formData.text) {
  107. this.$message.error("请输入表单名");
  108. return;
  109. }
  110. if(!this.formData.stepMemo){
  111. this.$message.error("请输入表单描述");
  112. return;
  113. }
  114. console.log(this.editorJson);
  115. //定义保存数据格式
  116. const formData = {
  117. text: this.formData.text,
  118. routeName: "自定义$" + this.formData.businessTable,
  119. businessTable: this.formData.businessTable,
  120. jsonContent: JSON.parse(this.editorJson),
  121. stepMemo:this.formData.stepMemo
  122. };
  123. console.log(formData);
  124. //保存接口
  125. formAdd(formData).then(res => {
  126. if (res.data.success) {
  127. this.$message.success("保存成功");
  128. } else {
  129. this.$message.errore("保存失败");
  130. }
  131. });
  132. },
  133. handleCopyJson() {
  134. // 复制数据
  135. const clipboard = new Clipboard(".copy-btn");
  136. clipboard.on("success", () => {
  137. this.$message.success("复制成功");
  138. });
  139. clipboard.on("error", () => {
  140. this.$message.error("复制失败");
  141. });
  142. setTimeout(() => {
  143. // 销毁实例
  144. clipboard.destroy();
  145. }, 122);
  146. }
  147. }
  148. };
  149. </script>