yangys
2025-05-27 81060ec4cc3ead9080d4bdb3875920e257583de4
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<template>
  <basic-container>
    <avue-crud
      :option="option"
      v-model:search="search"
      v-model:page="page"
      v-model="form"
      :table-loading="loading"
      :data="data"
      :permission="permissionList"
      :before-open="beforeOpen"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <el-button type="danger" icon="el-icon-delete" plain @click="handleDelete"
          >删 除
        </el-button>
      </template>
      <template #menu="scope">
        <el-button type="primary" text icon="el-icon-setting" @click.stop="handleDesign(scope.row)"
          >设 计
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
      title="可视化表单设计"
      v-model="designBox"
      :fullscreen="true"
      :before-close="handleDesignClose"
      append-to-body
    >
      <nf-form-design
        ref="formDesign"
        style="height: 90vh"
        :options="options"
        :toolbar="['clear', 'preview', 'import', 'generate']"
        :includeFields="[
          'group', // 分组
          'dynamic', // 子表单
          'title', // 标题
          'table', // 表格
          'input', // 输入框
          'password', // 密码
          'textarea', // 文本域
          'number', // 数字
          'ueditor', // 富文本
          'map', // 地图选择器
          'radio', // 单选
          'checkbox', // 多选
          'select', // 下拉选择
          'tree', // 树形选择
          'cascader', // 级联选择
          'table-select', // 表格选择
          'upload', // 上传
          'year', // 年
          'month', // 月
          'week', // 周
          'date', // 日期
          'time', // 时间
          'datetime', // 日期时间
          'daterange', // 日期范围
          'datetimerange', // 日期时间范围
          'timerange', // 时间范围
          'sign', // 签名
          'switch', // 开关
          'rate', // 评价
          'color', // 颜色
          'icon', // 图标
          'slider', // 滑块
        ]"
        :is-crud="isCrud"
      >
        <template #toolbar>
          <el-button
            style="padding: 0"
            text
            type="primary"
            size="default"
            icon="el-icon-download"
            @click="handleSubmit"
          >
            保存
          </el-button>
        </template>
      </nf-form-design>
    </el-dialog>
  </basic-container>
</template>
 
<script>
import { getList, getDetail, add, update, remove, getTablePrototype } from '@/api/tool/codesetting';
import option from '@/option/tool/formsetting';
import { mapGetters } from 'vuex';
import { validatenull } from '@/utils/validate';
import { getTableInfoByName, getTableList } from '@/api/tool/model';
import func from '@/utils/func';
 
export default {
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: true,
      loadingOption: {
        lock: true,
        text: '物理表读取中',
        background: 'rgba(0, 0, 0, 0.7)',
      },
      designId: '',
      designBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: option,
      data: [],
      modelTable: '',
      datasourceId: '',
      isCrud: true,
      options: {},
      // 默认不需要显示的字段名
      hideFields: [
        'id',
        'tenant_id',
        'create_user',
        'create_dept',
        'create_time',
        'update_user',
        'update_time',
        'status',
        'is_deleted',
      ],
    };
  },
  computed: {
    ...mapGetters(['userInfo', 'permission']),
    permissionList() {
      return {
        addBtn: true,
        viewBtn: true,
        delBtn: true,
        editBtn: true,
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
  },
  watch: {
    'form.datasourceId'() {
      if (!validatenull(this.form.datasourceId)) {
        this.datasourceId = this.form.datasourceId;
        const fullLoading = this.$loading(this.loadingOption);
        getTableList(this.form.datasourceId)
          .then(res => {
            const column = this.findObject(this.option.column, 'modelTable');
            column.dicData = res.data.data;
            fullLoading.close();
          })
          .catch(() => {
            fullLoading.close();
          });
      }
    },
    'form.modelTable'() {
      if (!validatenull(this.form.modelTable)) {
        this.modelTable = this.form.modelTable;
        const fullLoading = this.$loading(this.loadingOption);
        getTableInfoByName(this.form.modelTable, this.form.datasourceId)
          .then(res => {
            const result = res.data;
            if (result.success) {
              // 赋默认值
              const { comment } = result.data;
              this.form.name = comment;
              this.form.code = this.form.modelTable;
              fullLoading.close();
            }
          })
          .catch(() => {
            fullLoading.close();
          });
      }
    },
  },
  methods: {
    rowSave(row, done, loading) {
      const data = {
        name: row.name,
        code: row.code,
        category: 2,
        settings: row.settings,
      };
      add(data).then(
        res => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          let design = {
            id: res.data.data.id,
            name: data.name,
            code: data.code,
          };
          this.handleDesign(design);
          done();
        },
        error => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      const data = {
        id: row.id,
        name: row.name,
        code: row.code,
        settings: row.settings,
      };
      update(data).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done();
        },
        error => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          this.$refs.crud.toggleSelection();
        });
    },
    handleDesign(design) {
      this.designBox = true;
      this.designId = design.id;
      getDetail(design.id).then(res => {
        const settings = res.data.data.settings;
        if (!validatenull(settings)) {
          this.options = JSON.parse(settings);
        } else if (!validatenull(this.modelTable) && !validatenull(this.datasourceId)) {
          getTablePrototype(this.modelTable, this.datasourceId).then(res => {
            const result = res.data;
            if (result.success) {
              const column = result.data
                .filter(field => {
                  return !validatenull(field.name) && !this.hideFields.includes(field.name);
                })
                .map(field => {
                  return {
                    type: 'input',
                    label: field.comment,
                    display: true,
                    prop: func.camelCaseString(field.name),
                  };
                });
              this.options = {
                column: column,
              };
              this.loading = false;
            }
          });
        }
      });
    },
    handleDesignClose() {
      this.modelTable = '';
      this.datasourceId = '';
      this.options = {};
      this.designBox = false;
    },
    // 可视化表单提交
    handleSubmit() {
      // json, string, app
      this.$refs.formDesign.getData('json').then(data => {
        // 表单/表格option
        console.log(data);
        const row = {
          id: this.designId,
          settings: JSON.stringify(data),
        };
        update(row).then(
          () => {
            this.onLoad(this.page);
            this.$message({
              type: 'success',
              message: '操作成功!',
            });
            this.designBox = false;
          },
          error => {
            console.log(error);
          }
        );
        this.$refs.formDesign.getChangeList().then(changeList => {
          // 字段prop, type, label修改记录
          console.log(changeList);
        });
      });
    },
    beforeOpen(done, type) {
      if (['add', 'edit'].includes(type)) {
      }
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = {
            ...this.form,
          };
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
 
      let values = {
        ...params,
        ...this.query,
        category: 2,
      };
 
      getList(page.currentPage, page.pageSize, values).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
  },
};
</script>
 
<style></style>