fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
<template>
    <div>
       <el-dialog :visible.sync="dialogVisible" inline :title="title" v-if="dialogVisible" :close-on-click-modal="false" :modal-append-to-body="false">
            <el-form size="mini" :model="form" label-width="140px" ref="form" :rules="formRules" class="propMeta">
                 <el-form-item label="属性名称:" prop="propName">
                     <el-input v-model="form.propName" placeholder="请输入属性名称" clearable></el-input>
                 </el-form-item>
                <el-form-item label="属性值类型:" prop="propValueType">
                    <el-select placeholder="请选择属性值类型" :disabled="form.propId ? true : false" v-model="form.propValueType"  clearable @change="changeType">
                         <el-option v-for="item in propValueTypeArray" :key="item.value" :value="item.value" :label="item.name"></el-option>
                    </el-select>
                 </el-form-item>
                <el-form-item label="属性值校验类型:" prop="propValueCheckType" v-show="typeof form.propValueType === 'number' && !isNaN(form.propValueType)">
                     <el-select placeholder="请选择属性值校验类型"  :disabled="form.propId ? true : false" v-model="form.propValueCheckType" @change="getCheckExplain" clearable>
                         <el-option v-for="item in checkTypeArray" v-show="item.checkType !== 99" :key="item.checkType" :value="item.checkType" :label="item.checkTypeName"></el-option>
                    </el-select>
                 </el-form-item>
                 <el-form-item v-show="form.propValueCheckExplain">
                     <el-tag class="propValueCheckExplain" type="warning">{{form.propValueCheckExplain}}</el-tag>
                 </el-form-item>
                 <el-form-item label="默认属性值:" v-if="!form.propId" prop="defaultPropValue">
                     <el-input v-model="form.defaultPropValue" placeholder="请输入默认属性值" clearable></el-input>
                 </el-form-item>
                  <el-form-item label="属性值:" v-if="form.propId">
                      {{getPropValue()}}
                 </el-form-item>
                <el-form-item label="属性值单位:" prop="propUnit">
                     <el-input v-model="form.propUnit" placeholder="请输入属性值单位" clearable></el-input>
                 </el-form-item>
                 <el-form-item label="属性描述:" prop="propDesc">
                     <el-input v-model="form.propDesc" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"  placeholder="请输入属性描述" clearable></el-input>
                 </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="mini" @click="submit" type="primary" :loading="loading">保存</el-button>
                <el-button size="mini" @click="dialogVisible = false">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import productPropMetaApi from '@/api/productmanagement/productPropMeta'
import { objectCopy } from '@/utils/objectCopyHelper'
import { getStrLength } from '@/utils/getStrLength'
 
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '添加属性'
    },
    row: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      dialogVisible: false,
      checkTypeArray: [], // 校验类型数组
      loading: false,
      form: {
        propName: null,
        propValueType: null,
        propValueCheckType: null,
        propUnit: null,
        propDesc: null,
        propValueCheckExplain: null
      },
      propValueTypeArray: [
        {
          name: '字符类型',
          value: 0
        },
        {
          name: '数字类型',
          value: 1
        },
        {
          name: '时间类型',
          value: 2
        },
        {
          name: '日期类型',
          value: 3
        },
        {
          name: '日期时间型',
          value: 4
        }
      ],
      formRules: {
        propName: [
          { required: true, message: '请输入属性名称', trigger: 'change' }, {
            validator: (rule, value, callback) => {
              callback(getStrLength(rule, value, callback, 128, '属性名称'))
            }
          }],
        propValueType: [{ required: true, message: '请选择属性值类型', trigger: 'change' }],
        propValueCheckType: [{ required: true, message: '请选择属性值校验类型', trigger: 'change' }],
        propUnit: {
          validator: (rule, value, callback) => {
            callback(getStrLength(rule, value, callback, 8, '属性值单位'))
          }
        },
        propDesc: {
          validator: (rule, value, callback) => {
            callback(getStrLength(rule, value, callback, 1024, '属性描述'))
          }
        },
        defaultPropValue: [
          { required: true, message: '请输入默认属性值', trigger: 'change' },
          { max: 128, message: '默认属性值最多只能输入 128 字', trigger: 'change' }
        ]
      }
    }
  },
  watch: {
    row: {
      handler (newValue, oldValue) {
        if (newValue.propId) {
          this.form = objectCopy(this.row)
          if (typeof this.form.propValueType === 'number' && !isNaN(this.form.propValueType)) {
            this.changeType(this.form.propValueType)
            this.form.propValueCheckType = this.row.propValueCheckType
            this.form.propValueCheckExplain = this.row.propValueCheckExplain
          }
        }
      },
      deep: true
    },
    /**
     * 监控外部显示变量变化
     * 传递到dialog组件
     */
    show: function (newShow, oldShow) {
      this.dialogVisible = newShow
    },
    /**
     * 监控内部显示属性变化
     * 传递到外部调用变量
     */
    dialogVisible: function (newDialogShow, oldDialogShow) {
      if (!newDialogShow) {
        for (const i in this.form) {
          this.form[i] = null
        }
        this.$emit('update:show', newDialogShow)
      }
    }
  },
  methods: {
    /**
     * 获取属性值
     */
    getPropValue () {
      let str = null
      str = this.form.ecProductPropMetaValueList.map(item => { return item.propValue }).join(',')
      return str
    },
    /**
     * 获取校验说明
     */
    getCheckExplain (val) {
      if (typeof val === 'number' && !isNaN(val)) {
        this.checkTypeArray.forEach(item => {
          if (item.checkType === val) {
            this.form.propValueCheckExplain = item.explain
          }
        })
      } else {
        this.form.propValueCheckExplain = null
      }
    },
    /**
       *选择属性值类型时if
       */
    changeType (val) {
      this.form.propValueCheckType = null
      this.form.propValueCheckExplain = null
      if (typeof val === 'number' && !isNaN(val)) {
        productPropMetaApi.getValueCheckTypes({ valueType: val }).then(res => {
          this.checkTypeArray = res.data
        })
      }
    },
    /**
       * 提交
       */
    submit () {
      this.$refs.form.validate().then(res => {
        this.loading = true
        let resDate = null
        if (this.form.propId) {
          resDate = productPropMetaApi.updateInfo(this.form)
        } else {
          resDate = productPropMetaApi.addInfo(this.form)
        }
        resDate.then(res => {
          if (res && res.data) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.loading = false
            this.$parent.queryData()
            this.dialogVisible = false
          } else {
            this.loading = false
          }
        }).catch(() => {
          this.loading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
.propMeta {
  .el-select{
    width: 100%;
  }
  .propValueCheckExplain {
    word-break: break-all;
    word-wrap: break-word;
    white-space: normal;
    height: auto;
  }
}
 
</style>