schangxiang@126.com
2025-09-10 3d43ffa3152110b7823f9fa6320c08a6ae02358a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<template>
  <el-container class="designer-container">
    <el-aside width="250px" class="components-list">
      <sticky class-name="left-sticky">
        <el-collapse accordion>
          <el-collapse-item title="布局容器">
            <draggable :list="layoutComponents" :options="{group:{ name:'people', pull:'clone',put:false},sort:false, ghostClass: 'ghost'}" :move="handleMove" tag="ul" @end="handleMoveEnd" @start="handleMoveStart">
              <li v-for="(item, index) in layoutComponents" :key="index" class="form-edit-widget-label data-grid">
                <a>
                  <i :class="item.icon"></i>
                  <span>{{ item.label }}</span>
                </a>
              </li>
            </draggable>
          </el-collapse-item>
          <el-collapse-item v-for="(m, mIndex) in basicComponents" :key="mIndex" :title="m.title">
            <draggable :list="m.items" :options="{group:{ name:'people', pull:'clone',put:false},sort:false, ghostClass: 'ghost'}" :move="handleMove" tag="ul" @end="handleMoveEnd" @start="handleMoveStart">
              <li v-for="(item, index) in m.items" :key="index" class="form-edit-widget-label">
                <img :src="item.img" alt="">
                <a>
                  <span>{{ item.label }}</span>
                </a>
              </li>
            </draggable>
          </el-collapse-item>
          <el-collapse-item title="属性设置" class="widget-config-container">
            <component v-show="configTab=='widget'" :is="configType" :data="widgetFormSelect" :basic-components="basicComponents"></component>
          </el-collapse-item>
        </el-collapse>
      </sticky>
    </el-aside>
 
    <el-container class="center-container" direction="vertical">
      <sticky :z-index="2000" height="40px" class-name="content-tool">
        <div class="right-tools">
          <el-button type="text" size="medium" icon="el-icon-yrt-reset" @click="handleReset">重置</el-button>
          <el-button type="text" size="medium" icon="el-icon-tickets" @click="handleGenerateJson">查看</el-button>
          <el-button type="text" size="medium" icon="el-icon-yrt-baocun" @click="saveVue('save')">保存</el-button>
        </div>
      </sticky>
 
      <div class="content">
        <div :class="{'widget-empty': widgetFormData.fields.length == 0}" style="min-height:1200px">
          <widget-form ref="widgetForm" :data="widgetFormData" :select.sync="widgetFormSelect" :config-type.sync="configType" :detail-fields.sync="detailFields"></widget-form>
        </div>
      </div>
    </el-container>
 
    <cus-dialog ref="jsonPreview" :visible="jsonVisible" title="生成JSON并保存Vue文件" width="800px" form @on-close="jsonVisible = false">
      <div id="jsoneditor" style="height: 400px;width: 100%;">{{ jsonData }}</div>
      <template slot="action">
        <el-button data-clipboard-target=".ace_text-input" @dblclick.native="$message.success('复制成功');">双击复制</el-button>
        <el-button :loading="createLoading" type="primary" data-clipboard-target=".ace_text-input" @click.native="saveVue('jsoneditor')">保存并生成Vue文件</el-button>
      </template>
    </cus-dialog>
 
    <cus-dialog ref="codePreview" :visible="codeVisible" title="生成页面" form width="800px" @on-close="codeVisible = false">
      <div id="codeeditor" style="height: 500px; width: 100%;">{{ htmlTemplate }}</div>
      <template slot="action">
        <el-button id="closebtn" @click="codeVisible=false">关闭</el-button>
      </template>
    </cus-dialog>
  </el-container>
</template>
 
<script>
import Draggable from "vuedraggable";
import WidgetConfig from "./WidgetConfig";
import WidgetForm from "./WidgetForm";
import CusDialog from "./CusDialog";
// import JSONEditor from 'jsoneditor'
// import 'jsoneditor/dist/jsoneditor.min.css'
import Sticky from "@/components/Sticky";
 
import { basicComponents, layoutComponents } from "./componentsConfig.js";
import { loadJs } from "./util/index.js";
import { generateMixinCode, generateMainCode } from "./generateMixinCode.js";
 
export default {
  name: "sys-dev-tools-layout-designer",
  components: {
    Draggable,
    WidgetConfig,
    WidgetForm,
    CusDialog,
    Sticky
  },
  data() {
    return {
      basicComponents,
      layoutComponents,
      // 明细字段
      detailFields: [],
 
      // 当前配置参数设置组件
      configType: "WidgetConfig",
 
      // 模块tree prop参数配置
      moduleProps: {
        label: "CnName",
        // children: "zones",
        isLeaf: function(data, node) {
          return data.hasChild !== 1;
        }
      },
      // 设计器数据结构
      widgetFormData: {
        fields: [],
        config: {
          labelWidth: "100px", // 标签文字宽度
          labelPosition: "right", // 标签位置
          top: "3vh", // 对话框底部距离
          width: "1200px", // 对话框宽度
          visible: false, // 是否显示添加实验室对话框
          disabled: false, // 是否禁用
          title: null, // 对话框标题
          action: "add", // 接收值为:add、edit
          formInline: false, // 行内表单
          saveButtonText: "保存"
        }
      },
 
      // 设计器类型:管理页面、编辑页面
      editRegionTab: "WidgetConfig",
      configTab: "widget",
      widgetFormSelect: null, // 选中项
      previewVisible: false,
      jsonVisible: false,
      codeVisible: false,
      widgetModels: {},
      blank: "",
      htmlTemplate: "",
      jsonData: "", // 创建混入文件内容
      createLoading: false // 创建页面loading
    };
  },
  mounted() {
    loadJs("lib/ace/src/ace.js");
    this.init();
  },
  methods: {
    handleMoveEnd(evt) {
      // console.log("end", evt);
    },
    handleMoveStart({ oldIndex }) {
      // console.log("start", oldIndex);
    },
    handleMove() {
      return true;
    },
    // 预览设计
    handlePreview() {
      this.previewVisible = true;
    },
    // 重置,重新开始设计
    handleReset() {
      this.$confirm("确定要重置设计,将重新开始设计, 是否继续?", "重置提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.reset();
          this.$message({
            type: "success",
            message: "重置成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消重置"
          });
        });
    },
    reset() {
      this.widgetFormData = Object.assign(
        {},
        {
          fields: [],
          config: {
            labelWidth: "100px", // 标签文字宽度
            labelPosition: "right", // 标签位置
            top: "3vh", // 对话框底部距离
            width: "1200px", // 对话框宽度
            visible: false, // 是否显示添加实验室对话框
            disabled: false, // 是否禁用
            title: null, // 对话框标题
            action: "add", // 接收值为:add、edit
            formInline: false, // 行内表单
            saveButtonText: "保存"
          }
        }
      );
      // 对话框标题名称
      this.configType = "WidgetConfig"; // 参数设置组件类型
      this.widgetFormSelect = null; // 编辑页面选中项
    },
    // 生成JSON
    handleGenerateJson() {
      this.jsonVisible = true;
      this.jsonData = generateMixinCode(this.widgetFormData);
 
      this.$nextTick(() => {
        // eslint-disable-next-line
        const editor = ace.edit("jsoneditor");
        editor.session.setMode("ace/mode/jsx");
      });
    },
    // 加载数据
    init() {
      const url = "/api/sys/designer/initLayout";
      const params = {};
      this.common.ajax(
        url,
        params,
        res => {
          this.common.showMsg(res);
          if (res.result) {
            if (res.data) {
              this.widgetFormData = res.data;
            }
          }
        },
        true
      );
    },
    // 保存
    saveVue(type) {
      var jsonData = null;
      if (type === "jsoneditor") {
        // eslint-disable-next-line
        const editor = ace.edit("jsoneditor");
        jsonData = editor.getSelectedText();
        type = "json";
      }
 
      // UI布局参数
      if (!jsonData) {
        jsonData = JSON.stringify(this.widgetFormData, null, 2);
      } else {
        this.widgetFormData = JSON.parse(jsonData);
      }
      if (!jsonData) {
        this.$message.error("没有可保存的数据!");
        return;
      }
      const mainCode = generateMainCode();
 
      this.createLoading = true;
      var url = "/api/sys/designer/saveLayout";
      var params = {
        mainCode: mainCode,
        jsonData: jsonData,
        type: type
      };
 
      this.common.ajax(
        url,
        params,
        res => {
          this.common.showMsg(res);
          if (res.result) {
            this.jsonVisible = false;
          }
          this.createLoading = false;
        },
        true
      );
    }
  }
};
</script>
 
<style lang="scss">
@import "./styles/cover.scss";
@import "./styles/index.scss";
.content {
  /deep/ .el-tabs__header {
    margin: 0 0 2px;
  }
}
</style>