schangxiang@126.com
2025-09-19 9be9c3784b2881a3fa25e93ae2033dc2803c0ed0
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
<template>
  <el-form-item v-if="!hideFeilds.find(item=>item==field.options.prop)" :prop="field.options.prop" :readonly="field.options.readonly" :required="field.options.required" :rules="changeRules(field)" :label-width="field.options['label-width']!=null?field.options['label-width']+'px':null" :class="{'no-margin':field.options.noMargin}" class="input-container">
    <template slot="label">
      <slot :name="'label-' + field.options.prop">
        {{ (field.options.noLabel?null:field.label) }}
      </slot>
    </template>
    <!--单行分组-->
    <template v-if="field.type=='inline-group'">
      <template v-for="subField in field.fields">
        <el-input v-if="subField.type=='input'" :ref="'input-'+subField.options.prop" :key="subField.key" v-model="formData[subField.options.prop]" v-bind="subField.options" :style="{width:subField.options.width}" class="inline-input" @blur="(event)=>{onBlur($refs['input-' + subField.options.prop][0], formData[subField.options.prop], event, subField)}" @focus="(event)=>{onFocus($refs['input-' + subField.options.prop][0], formData[subField.options.prop], event, subField)}"></el-input>
 
        <el-cascader v-else-if="subField.type=='cascader'" :key="subField.key" v-model="formData[subField.options.prop]" :options="subField.options" :style="{width:subField.options.width}" class="inline-input" change-on-select @focus="(event)=>{onFocus($refs['cascader-' + subField.options.prop][0], formData[subField.options.prop], event, subField);}" @change="(val)=>{change($refs['cascader-' + subField.options.prop], val, event, subField);}"></el-cascader>
 
        <!--下拉框选择器-->
        <template v-else-if="subField.type=='select'">
          <el-select v-if="subField.options.keyProp" :ref="'select-'+subField.options.prop" :key="subField.key" v-model="formData[subField.options.keyProp]" :style="{width:subField.options.width}" :field="subField" :disabled="subField.options.disabled" :multiple="subField.options.multiple" :filterable="!!subField.options.filterable" class="inline-input" @change="(val)=>{change($refs['select-' + subField.options.prop][0], val, subField);}">
            <el-option v-for="item in getOptions(subField)" :key="item.id" v-bind="item" :label="item.label" :value="item.value" :option="item">
            </el-option>
          </el-select>
          <el-select v-else :ref="'select-'+subField.options.prop" :key="subField.key" v-model="formData[subField.options.prop]" :style="{width:subField.options.width}" :field="subField" :disabled="subField.options.disabled" :multiple="subField.options.multiple" :filterable="!!subField.options.filterable" class="inline-input" @change="(val)=>{change($refs['select-' + subField.options.prop][0], val, subField);}">
            <el-option v-for="item in getOptions(subField)" :key="item.id" v-bind="item" :label="item.label" :value="item.value" :option="item">
            </el-option>
          </el-select>
        </template>
 
        <template v-else-if="subField.type === 'time'">
          <el-time-picker :key="subField.key" v-model="formData[subField.options.prop]" :is-range="subField.options.isRange" :placeholder="subField.options.placeholder" :start-placeholder="subField.options.startPlaceholder" :end-placeholder="subField.options.endPlaceholder" :readonly="subField.options.readonly" :disabled="subField.options.disabled" :editable="subField.options.editable" :clearable="subField.options.clearable" :arrow-control="subField.options.arrowControl" :style="{width: subField.options.width}">
          </el-time-picker>
        </template>
 
        <template v-else-if="subField.type === 'date'">
          <el-date-picker :key="subField.key" v-model="formData[subField.options.prop]" :type="subField.options.type" :is-range="subField.options.isRange" :placeholder="subField.options.placeholder" :start-placeholder="subField.options.startPlaceholder" :end-placeholder="subField.options.endPlaceholder" :readonly="subField.options.readonly" :disabled="subField.options.disabled" :editable="subField.options.editable" :clearable="subField.options.clearable" :format="subField.options.format" :value-format="subField.options.format" :style="{width: subField.options.width}">
          </el-date-picker>
        </template>
 
        <!-- 单选组 -->
        <el-radio-group v-else-if="subField.type=='radio'" :key="subField.key" v-model="formData[subField.options.prop]" :style="{display: subField.options.inline ? 'inline-block' : 'block'}" class="margin-right-10">
          <!-- 静态数据 -->
          <template v-if="subField.options.remote === false">
            <el-radio v-for="(item, optionIndex) in subField.options.options" :key="subField.key + '_ops_' + optionIndex" :label="item.value">{{ item.label }}</el-radio>
          </template>
          <!-- 远程动态函数 -->
          <template v-else-if="subField.options.remote === true">
          </template>
          <!-- 下拉框绑定 -->
          <template v-else-if="subField.options.remote === 'bindDropdown'">
            <el-radio v-for="(item, optionIndex) in getOptions(subField)" :key="subField.key + '_ops_' + optionIndex" :label="item.value">{{ item.label }}</el-radio>
          </template>
        </el-radio-group>
 
        <!-- 复选组 -->
        <el-checkbox-group v-else-if="subField.type=='checkbox'" :ref="'checkbox-' + subField.options.prop" :key="subField.key" v-model="formData[subField.options.prop]" @change="(val)=>{change($refs['checkbox-' + subField.options.prop], val, subField);}">
          <!-- 静态数据 -->
          <template v-if="subField.options.remote === false">
            <el-checkbox v-for="(item, optionIndex) in subField.options.options" :key="subField.key + '_ops_' + optionIndex" :label="item.value">{{ item.label }}</el-checkbox>
          </template>
          <!-- 远程动态函数 -->
          <template v-else-if="subField.options.remote === true">
          </template>
          <!-- 下拉框绑定 -->
          <template v-else-if="subField.options.remote === 'bindDropdown'">
            <el-checkbox v-for="(item, optionIndex) in getOptions(subField)" :key="subField.key + '_ops_' + optionIndex" :label="item.value">{{ item.label }}</el-checkbox>
          </template>
        </el-checkbox-group>
 
        <!--空白-->
        <template v-else-if="subField.type=='blank'">
          <slot :name="'blank-' + subField.options.prop" :formData="formData"></slot>
        </template>
      </template>
    </template>
    <!--独立行-->
    <template v-else>
      <template v-if="'input,textarea'.indexOf(field.type)>=0">
        <el-input v-if="['int','int32', 'int64'].indexOf(field.options.dataType)>=0" :ref="'input-'+field.options.prop" v-model.number="formData[field.options.prop]" v-bind="field.options" :type="field.type" :style="{width: field.options.width||'auto'}" clearable @blur="(event)=>{onBlur($refs['input-' + field.options.prop], formData[field.options.prop], event, field)}" @focus="(event)=>{onFocus($refs['input-' + field.options.prop], formData[field.options.prop], event, field)}" @change="(val)=>{change($refs['input-' + field.options.prop], val, field);}"></el-input>
        <el-input v-else-if="['decimal'].indexOf(field.options.dataType)>=0" :ref="'input-'+field.options.prop" v-model="formData[field.options.prop]" v-bind="field.options" :style="{width: field.options.width||'auto'}" type="number" @blur="(event)=>{onBlur($refs['input-' + field.options.prop], formData[field.options.prop], event, field)}" @focus="(event)=>{onFocus($refs['input-' + field.options.prop], formData[field.options.prop], event, field)}" @change="(val)=>{change($refs['input-' + field.options.prop], val, field);}" @keyup.native="(event)=>{onKeyup($refs['input-' + field.options.prop], formData[field.options.prop], event, field)}"></el-input>
        <el-input v-else :ref="'input-'+field.options.prop" v-model="formData[field.options.prop]" v-bind="field.options" :type="field.type" :style="{width: field.options.width||'auto'}" clearable @blur="(event)=>{onBlur($refs['input-' + field.options.prop], formData[field.options.prop], event, field)}" @focus="(event)=>{onFocus($refs['input-' + field.options.prop], formData[field.options.prop], event, field)}" @change="(val)=>{change($refs['input-' + field.options.prop], val, field);}"></el-input>
      </template>
      <!--下拉框选择器-->
      <template v-else-if="field.type=='select'">
        <!--有关联主键字段-->
        <el-select v-if="field.options.keyProp" :ref="'select-'+field.options.prop" :key="field.key" v-model="formData[field.options.keyProp]" :style="{width:field.options.width}" :field="field" :disabled="field.options.disabled" :multiple="field.options.multiple" :filterable="!!field.options.filterable" :remote="field.options.remote===true" :remote-method="(query)=>{remoteMethod($refs['select-' + field.options.prop], query, field)}" reserve-keyword class="inline-input" @click.native="selectClick(field)" @change="(val)=>{change($refs['select-' + field.options.prop], val, field);}">
          <el-option v-for="item in getOptions(field)" :key="item.id" v-bind="item" :label="item.label" :value="item.value" :option="item">
          </el-option>
        </el-select>
        <!--无关联主键字段-->
        <el-select v-else :ref="'select-'+field.options.prop" :key="field.key" v-model="formData[field.options.prop]" :style="{width:field.options.width}" :field="field" :disabled="field.options.disabled" :multiple="field.options.multiple" :filterable="!!field.options.filterable" :remote="field.options.remote===true" :remote-method="(query)=>{remoteMethod(query, field)}" reserve-keyword class="inline-input" @change="(val)=>{change($refs['select-' + field.options.prop], val, field);}">
          <el-option v-for="item in getOptions(field)" :key="item.id" v-bind="item" :label="item.label" :value="item.value" :option="item">
          </el-option>
        </el-select>
      </template>
 
      <!-- 级联 -->
      <el-cascader v-else-if="field.type=='cascader'" :ref="'cascader-' + field.options.prop" v-model="formData[field.options.prop]" :options="field.options.options" :placeholder="field.options.placeholder" :style="{width: field.options.width}" :rules="field.rules" :field="field" filterable change-on-select @focus="(event)=>{onFocus($refs['cascader-' + field.options.prop][0], formData[field.options.prop], event, field);}" @change="(val)=>{change($refs['cascader-' + field.options.prop], val, field);}"></el-cascader>
 
      <!-- 单选组 -->
      <el-radio-group v-else-if="field.type=='radio'" :ref="'radio-' + field.options.prop" :key="field.key" v-model="formData[field.options.prop]" @change="(val)=>{change($refs['radio-' + field.options.prop], val, field);}">
        <!-- 静态数据 -->
        <template v-if="field.options.remote === false">
          <el-radio v-for="(item, optionIndex) in field.options.options" :key="field.key + '_ops_' + optionIndex" :value="item.value" :label="item.label" :style="{display: (field.options.inline ? 'inline-block' : 'block'),'margin-bottom':(field.options.inline?0:'15px')}">{{ item.label }}</el-radio>
        </template>
        <!-- 远程动态函数 -->
        <template v-else-if="field.options.remote === true">
        </template>
        <!-- 下拉框绑定 -->
        <template v-else-if="field.options.remote === 'bindDropdown'">
          <el-radio v-for="(item, optionIndex) in getOptions(field)" :key="field.key + '_ops_' + optionIndex" :value="item.value" :label="item.label" :style="{display: field.options.inline ? 'inline-block' : 'block'}">{{ item.label }}</el-radio>
        </template>
      </el-radio-group>
 
      <!-- 复选组 -->
      <el-checkbox-group v-else-if="field.type=='checkbox'" :ref="'checkbox-' + field.options.prop" :key="field.key" v-model="formData[field.options.prop]" @change="(val)=>{change($refs['checkbox-' + field.options.prop], val, field);}">
        <!-- 静态数据 -->
        <template v-if="field.options.remote === false">
          <el-checkbox v-for="(item, optionIndex) in field.options.options" :key="field.key + '_ops_' + optionIndex" :label="item.value">{{ item.label }}</el-checkbox>
        </template>
        <!-- 远程动态函数 -->
        <template v-else-if="field.options.remote === true">
        </template>
        <!-- 下拉框绑定 -->
        <template v-else-if="field.options.remote === 'bindDropdown'">
          <el-checkbox v-for="(item, optionIndex) in getOptions(field)" :key="field.key + '_ops_' + optionIndex" :label="item.value">{{ item.label }}</el-checkbox>
        </template>
      </el-checkbox-group>
      <!-- 开关 -->
      <el-switch v-else-if="field.type=='switch'" :ref="'radio-' + field.options.prop" :key="field.key" v-model.number="formData[field.options.prop]" v-bind="field.options">
      </el-switch>
 
      <!-- 树选择框 -->
      <template v-else-if="field.type === 'tree'">
        <tree-select v-model="formData[field.options.prop]" :options="field.options" :label="field.label" :tree-load="(node, resolve)=>{treeLoad(node, resolve, field)}" :disabled="currentDisabled" :node-key-value="getNodeKeyValue(field)" @onFocus="(ref, val, event)=>{onFocus(ref, val, event, field);}" @on-change="(ref, val)=>{change(ref, val, field);}" @on-tree-node-click="(data, node, el, labels, values)=>{onTreeNodeClick(data, node, el, labels, values, field)}">
        </tree-select>
      </template>
 
      <!-- 输入选择框 -->
      <template v-else-if="field.type === 'input-select'">
        <input-select v-model="formData[field.options.prop]" :options="getInputSelectOptions(field).options" :label="field.label" :disabled="currentDisabled || getInputSelectOptions(field).disabled" :input-width="getInputSelectOptions(field).width" :placeholder="getInputSelectOptions(field).placeholder" @on-focus="(ref, val, event)=>{onFocus(ref, val, event, field);}" @on-item-click="(ref, val, itemData)=>{itemClick(ref, val, itemData, field);}" @on-change="(ref, val)=>{change(ref, val, field);}">
        </input-select>
      </template>
 
      <!-- 表格选择框 -->
      <template v-else-if="field.type === 'table-select'">
        <table-select v-model="formData[field.options.prop]" :form-data="formData" :field="field" :columns="field.options.columns" :label="field.label" :disabled="currentDisabled || field.options.disabled" :input-width="field.options.width" :placeholder="field.options.placeholder" @on-focus="(ref, val, event)=>{onFocus(ref, val, event, field);}" @on-item-click="(ref, val, itemData)=>{itemClick(ref, val, itemData, field);}" @on-change="(ref, val)=>{change(ref, val, field);}" @on-key-up="(ref, val, event, tableData)=>{onKeyup(ref, val, event, field, tableData);}" @on-row-change="(ref, selectedRow) => {onRowChange(ref, selectedRow, field);}">
        </table-select>
      </template>
 
      <template v-else-if="field.type === 'time'">
        <el-time-select v-if="field.options.fixedTimeSelect" v-model="formData[field.options.prop]" :placeholder="field.options.placeholder" :readonly="field.options.readonly" :disabled="field.options.disabled" :editable="field.options.editable" :clearable="field.options.clearable" :style="{width: field.options.width}" :picker-options="{start:field.options.start, end:field.options.end, step:field.options.step}">
        </el-time-select>
        <el-time-picker v-else v-model="formData[field.options.prop]" :is-range="field.options.isRange" :placeholder="field.options.placeholder" :start-placeholder="field.options.startPlaceholder" :end-placeholder="field.options.endPlaceholder" :readonly="field.options.readonly" :disabled="field.options.disabled" :editable="field.options.editable" :clearable="field.options.clearable" :arrow-control="field.options.arrowControl" :style="{width: field.options.width}">
        </el-time-picker>
      </template>
 
      <template v-else-if="['date', 'datetime'].indexOf(field.type)>=0">
        <el-date-picker :ref="'date-' + field.options.prop" v-model="formData[field.options.prop]" :type="field.options.type" :is-range="field.options.isRange" :placeholder="field.options.placeholder" :start-placeholder="field.options.startPlaceholder" :end-placeholder="field.options.endPlaceholder" :readonly="field.options.readonly" :disabled="field.options.disabled" :editable="field.options.editable" :clearable="field.options.clearable" :format="field.options.format" :value-format="field.options.format" :style="{width: field.options.width}" @change="(val)=>{change($refs['date-' + field.options.prop], val, field);}">
        </el-date-picker>
      </template>
 
      <!--上传图片-->
      <template v-else-if="field.type=='upload-image'">
        <el-upload ref="upload" :http-request="uploadHttp" :before-upload="(file)=>beforeAvatarUpload(file, field)" :multiple="field.options.multiple" :on-preview="(file)=>handlePicPreview(file, field)" :on-remove="handlePicRemove" :disabled="field.options.disabled" :on-change="(file, fileList)=>{handlePicChange(file, fileList)}" :file-list="picList" :list-type="field.options.listType" :class="{'hide-button': field.options.readonly}" action="">
          <i v-if="field.options.listType=='picture-card'" class="el-icon-plus"></i>
          <el-button v-else size="small" type="primary">点击上传</el-button>
        </el-upload>
 
        <!--预览图片-->
        <el-dialog :visible.sync="dialogPicVisible" :append-to-body="true">
          <img :src="dialogImageUrl" width="100%" alt="">
        </el-dialog>
      </template>
 
      <!--空白-->
      <template v-if="field.type=='blank'">
        <slot :name="'blank-' + field.options.prop"></slot>
      </template>
 
      <!--富文本编辑器-->
      <template v-else-if="field.type === 'tinymce-editor'">
        <tinymce ref="tinymce" v-model="formData[field.options.prop]" :height="field.options.size.height" />
      </template>
 
      <!--静态文本-->
      <template v-if="field.type=='static'">
        <div :style="{'padding-top':'8px','font-size':field.options.styles.fontSize+'px', 'line-height':field.options.styles.lineHeight+'px'}">
          {{ formData[field.options.prop] }}
        </div>
      </template>
    </template>
  </el-form-item>
</template>
 
<script>
import Tinymce from "@/components/Tinymce";
import TreeSelect from "@/components/base/TreeSelect";
import InputSelect from "@/components/base/InputSelect";
import TableSelect from "@/components/base/TableSelect";
 
export default {
  name: "yrt-editor-input",
  components: {
    Tinymce,
    TreeSelect,
    InputSelect,
    TableSelect
  },
  props: {
    // 字段数据
    field: {
      type: Object,
      required: true
    },
    // 校验规则
    formData: {
      type: Object,
      required: true
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      required: true
    },
    // 下拉框值集合
    dropdownData: {
      type: Object,
      required: true
    },
    // 校验规则
    rules: {
      type: Object,
      default: () => {
        return null;
      }
    },
    // 隐藏字段
    hideFeilds: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  data() {
    return {
      dialogImageUrl: null,
      dialogPicVisible: false,
      BASE_API: process.env.BASE_API
    };
  },
 
  computed: {
    // 是否禁用
    currentDisabled: {
      get: function() {
        return this.disabled;
      },
      set: function(newValue) {
        this.$emit("update:disabled", newValue); // 双向绑定prop.action,通知父级组件变量值同步更新
      }
    },
    // 获得下拉框options
    getOptions: {
      get: function() {
        var the = this;
        return function(field) {
          if (field.options.remote === "bindDropdown") {
            field.options.options = the.dropdownData["dropdown" + field.options.dropdown_Id];
          } else if (field.options.remote === true) {
            this.appendOptions(field);
          }
          return field.options.options;
        };
      }
    },
    // 获得输入选择框options
    getInputSelectOptions: {
      get: function() {
        var the = this;
        return function(field) {
          if (field.options.remote === "bindDropdown") {
            field.options.options = the.dropdownData["dropdown" + field.options.dropdown_Id];
          }
          return field.options;
        };
      }
    },
    // 图片列表
    picList: {
      get: function() {
        var values = this.formData[this.field.options.prop];
        if (!values) return [];
        var valueList = this.common.showImages(values);
 
        return valueList.map(item => {
          const f = item.src.split("/");
          const name = decodeURI(f[f.length - 1]);
          return {
            name: name,
            url: item.src
          };
        });
      },
      set: function(val) {
        this.formData[this.field.options.prop] = JSON.stringify(
          val.map(item => {
            return {
              src: item.url
            };
          })
        );
      }
    }
  },
  watch: {
    formData: {
      handler: function() {
        if (this.$refs.tinymce) {
          if (this.formData) {
            const val = this.formData[this.field.options.prop];
            this.$refs.tinymce.setContent(val || "");
          }
        }
      },
      deep: true
    }
  },
  created() {
    this.loadCascaderData();
  },
  mounted() {},
  destroyed() {},
  methods: {
    // 初始化
    init() {
      if (this.$refs.tinymce) {
        this.$refs.tinymce.initTinymce();
      }
    },
    // 字段change事件
    change(ref, val, field) {
      if (["decimal"].indexOf(field.options.dataType) >= 0) {
        this.formData[field.options.prop] = Number(val);
      }
      if (field.type === "select") {
        const itemOptions = this.getOptions(field);
        let prop = field.options.prop;
        const keyProp = field.options.keyProp;
        // 设置表单数据
        if (Array.isArray(val)) {
          const names = [];
          for (let index = 0; index < val.length; index++) {
            const item = val[index];
            if (prop.charAt(prop.length - 1) === "s") {
              prop = prop.substr(0, prop.length - 1);
            }
            const itemOption = itemOptions.find(option => option[keyProp] === item);
            const v = itemOption[prop] || itemOption.value;
            names.push(v);
          }
          this.formData[field.options.prop] = names;
        } else {
          const itemOption = itemOptions.find(option => option[prop] === val || option.value === val);
          Object.keys(itemOption).forEach((key, index) => {
            if (["value", "label"].indexOf(key) < 0) {
              this.$set(this.formData, key, itemOption[key]);
            }
          });
        }
      } else if (field.type === "checkbox") {
        // this.$set(this.formData, key, itemOption[key]);
      }
      this.$emit("on-change", ref, val, field, this.formData);
    },
    // input-select item click event
    itemClick(ref, val, itemData, field) {
      this.$emit("on-item-click", ref, val, itemData, field, this.formData);
    },
    // 树形下拉框获得点击项
    onTreeNodeClick(data, node, el, labels, values, field) {
      var the = this;
      // 仅选择叶节点
      if (!node.isLeaf && field.options.onlySelectLeaf) return;
 
      var key = field.options.prop;
      // 将下拉框中的值赋给表单
      if (Object.keys(data).findIndex(item => item === key) >= 0) {
        Object.keys(data).forEach((key, index) => {
          if (["value", "label", "hasChild", "hasFactChild", "state", "attributes"].indexOf(key) < 0) {
            the.$set(the.formData, key, data[key]);
          }
        });
      } else {
        the.$set(the.formData, key, data[key]);
      }
      this.$emit("on-tree-node-click", data, node, el, labels, values, field);
    },
    // 加载级联数据
    loadCascaderData() {
      this.$nextTick(() => {
        Object.keys(this.$refs).forEach(refName => {
          if (refName.indexOf("cascader") >= 0) {
            var ref = this.$refs[refName];
            var field = ref.$attrs["field"];
            this.$emit("on-focus", ref, this.formData[field.options.prop], null, field);
          }
        });
      });
    },
    // 树形输入框load
    treeLoad(node, resolve, subField) {
      var the = this;
      this.$emit("tree-load", node, resolve, subField);
      // 默认加载数据
      if (!subField.isLoaded) {
        var keyName = subField.ajaxParams.keyName;
        if (!keyName) {
          the.$message.error("未设置tree下拉框ajax加载参数!");
          return;
        }
        the.$nextTick(() => {
          var where = "";
          var userInfo = this.common.getUserInfo();
          if (node.level === 0) {
            where = {
              userProduct_Id: userInfo.userProduct_Id,
              parentId: 0
            };
          } else {
            where = {
              userProduct_Id: userInfo.userProduct_Id,
              parentId: node.data[subField.ajaxParams.keyName]
            };
          }
 
          var url = "/api/common/loadTreeNode";
          var params = subField.ajaxParams;
          params.where = where;
          the.common.ajax(
            url,
            params,
            res => {
              if (res.result) {
                resolve(res.data);
              } else {
                the.$message.error(res.msg);
              }
            },
            true
          );
        });
      }
    },
    // tree当前Key Value
    getNodeKeyValue(field) {
      if (field.options.keyProp) {
        return this.formData[field.options.keyProp];
      } else {
        return this.formData[field.options.prop];
      }
    },
    // 图片删除
    handlePicRemove(file, fileList) {
      var urlList = fileList.map(item => {
        return {
          src: item.url
        };
      });
      this.formData[this.field.options.prop] = JSON.stringify(urlList);
    },
    // 预览图片
    handlePicPreview(file, field) {
      if (field.options.listType === "text") {
        window.open(file.url);
      } else {
        this.dialogImageUrl = file.url;
        this.dialogPicVisible = true;
      }
    },
    // 上传图片change事件
    handlePicChange(file, fileList) {
      var status = "success";
      fileList.forEach(item => {
        if (item.status === "ready") {
          status = item.status;
        }
      });
      if (status === "ready") return;
 
      var urlList = fileList.map(item => {
        var url = item.response ? item.response.Data.Url : item.url;
        url = url.replace(this.BASE_API, "");
        return url;
      });
      this.formData[this.field.options.prop] = urlList.join(",");
    },
    // 失去焦点事件
    onBlur(ref, val, event, field) {
      this.$emit("on-blur", ref, val, event, field);
    },
    // 获得焦点
    onFocus(ref, val, event, field) {
      this.$emit("on-focus", ref, val, event, field);
    },
    // 按键抬起
    onKeyup(ref, val, event, field, tableData) {
      // if (["decimal"].indexOf(field.options.dataType) >= 0) {
      //   this.formData[field.options.prop] = Number(val);
      // }
      this.$emit("on-key-up", ref, val, event, field, tableData);
    },
    // 获得焦点
    onRowChange(ref, selectedRow, field) {
      this.$emit("on-row-change", ref, selectedRow, field);
    },
    // 转换验证规则
    changeRules(field) {
      if (this.rules && this.rules[field.options.prop]) {
        return this.rules[field.options.prop];
      }
      return field.rules;
    },
    // 阿里云图片上传
    uploadHttp(up) {
      debugger;
      const file = this.$refs.upload.uploadFiles[0];
      // 定义唯一的文件名
      const name = up.file.name;
      const formData = new FormData();
      const headerConfig = {
        headers: {
          "Content-Type": "multipart/form-data"
        }
      };
      if (!file) {
        // 若未选择文件
        alert("请选择文件");
        return;
      }
      formData.append("file", file.raw);
      formData.append("name", this.name);
      const url = this.common.domain + "/api/common/uploadSingleFile";
      this.$http.post(url, formData, headerConfig).then(res => {
        res = res.data;
        this.common.showMsg(res);
        if (res.result) {
          const list = this.picList;
          const url = `/api/common/download?url=${res.data.url}`;
          list.push({
            name: name,
            url: url
          });
          this.picList = list;
        }
      });
    },
    /**
     * 图片限制
     */
    beforeAvatarUpload(file, field) {
      if (!field.options.multiple) {
        if (this.picList.length >= 1) {
          this.$message.error("已存在文件,请先删除然后重新上传");
          return false;
        }
      }
      if (field.options.listType === "text") {
        const names = file.name.split(".");
        const extName = names[names.length - 1];
        const isLt5MB = file.size / 1024 / 1024 < 5;
        const type = ["xls", "xlsx", "doc", "docx", "pdf"];
        if (type.indexOf(extName) < 0) {
          this.$message.error("只能上传xls/xlsx/doc/docx/pdf格式文档!");
        }
        if (!isLt5MB) {
          this.$message.error("大小不能超过 5MB!");
        }
        return type.indexOf(extName) >= 0 && isLt5MB;
      } else {
        const names = file.name.split(".");
        const extName = names[names.length - 1];
        const isLt5MB = file.size / 1024 / 1024 < 5;
        const type = ["jpeg", "jpg", "png"];
        if (type.indexOf(extName) < 0) {
          this.$message.error("上传图片只能是 JPEG/JPG/PNG 格式!");
        }
        if (!isLt5MB) {
          this.$message.error("单张图片大小不能超过 5MB!");
        }
        return type.indexOf(extName) >= 0 && isLt5MB;
      }
    },
    // 下拉框远程访问
    remoteMethod(ref, query, field) {
      const url = field.options.remoteFunc;
      const params = {
        query: query
      };
      this.common.ajax(url, params, res => {
        if (res.result) {
          field.options.options = res.data;
          this.appendOptions(field);
          this.$nextTick(() => {
            ref.setSelected(); // 刷新条目
          });
        }
      });
    },
    // 下拉框单击事件
    selectClick(field) {
      if (field.options.options && !field.options.options.length) {
        this.remoteMethod("", field);
      }
    },
    // 下拉框追加已选择项
    appendOptions(field) {
      // 下拉框绑定
      if (field.options.options && field.options.keyProp) {
        const keys = this.formData[field.options.keyProp];
        const labels = this.formData[field.options.prop];
        if (Array.isArray(keys) && Array.isArray(labels)) {
          for (let index = 0; index < keys.length; index++) {
            if (field.options.options.find(item => item[field.options.keyProp] === keys[index])) {
              continue;
            }
            const item = {};
            item[field.options.keyProp] = keys[index];
            item[field.options.prop] = labels[index];
            item.value = keys[index];
            item.label = labels[index];
            field.options.options.push(item);
          }
        }
      }
    }
  }
};
</script>
 
<style rel="stylesheet/scss" lang="scss" scoped>
.input-container {
  .inline-input {
    & + .inline-input {
      margin-left: 10px;
    }
  }
 
  div.el-input:not(.no-bg) {
    ::v-deep input.el-input__inner[readonly] {
      background-color: #f5f7fa;
      cursor: not-allowed;
    }
  }
  ::v-deep .el-select,
  ::v-deep .el-cascader {
    width: 100%;
    input.el-input__inner[readonly] {
      background-color: white;
      cursor: pointer;
    }
  }
  ::v-deep .el-upload--picture-card {
    margin-bottom: 10px;
  }
  ::v-deep .el-upload-list__item {
    -webkit-transition: all 0s cubic-bezier(0.55, 0, 0.1, 1);
    transition: all 0s cubic-bezier(0.55, 0, 0.1, 1);
    width: auto;
    height: auto;
    max-width: 348px;
    min-width: 60px;
    max-height: 148px;
  }
  .hide-button {
    ::v-deep .el-upload--text {
      display: none;
    }
    ::v-deep .el-upload-list__item-status-label {
      display: none;
    }
    ::v-deep .el-upload-list__item:hover .el-icon-close {
      display: none;
    }
  }
}
</style>