index.vue 3.8 KB

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