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
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
<template>
    <div class="gift_info">
       <el-dialog :visible.sync="dialogVisible" :title="title" width="1100px" :close-on-click-modal="false" :modal-append-to-body="false" v-if="dialogVisible">
            <el-form size="mini" :model="form" :disabled="isDisabled" label-width="110px" ref="form" :rules="formRules">
              <el-form-item label="赠品编码:" prop="prodSpu.spuNum">
                <el-input v-model="form.prodSpu.spuNum" :disabled="title === '编辑赠品'" placeholder="请输入赠品编码" clearable></el-input>
              </el-form-item>
              <el-form-item label="赠品名称:" prop="prodSpu.spuName">
                <el-input v-model="form.prodSpu.spuName" placeholder="请输入赠品名称" clearable></el-input>
              </el-form-item>
              <el-form-item label="赠品类型:" prop="prodSpu.giftTypeEnum">
                <el-select v-model="form.prodSpu.giftTypeEnum" style="width:100%;" placeholder="请选择赠品类型">
                  <el-option label="物料" value="MATERIAL" key="2"></el-option>
                  <el-option label="商品" value="DEPUTY_PROD" key="3"></el-option>
                  <el-option label="品鉴" value="TASTING" key="4"></el-option>
                  <el-option label="非卖品" value="GIFT" key="1"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="赠品图片:" prop="prodImgs">
                <custom-upload-img
                  :limitNumber="3"
                  @handle-success="handleSuccess"
                  @handle-remove="handleRemove"
                  :isHideDelete='!isDisabled'
                  :fileList="form.prodImgs"
                  :imageSize="'754*754'"
                  :isAppendTobody="true"
                ></custom-upload-img>
              </el-form-item>
              <el-form-item label="赠品规格:" class="spuProps_propName" prop="spuProps.propName">
                <el-row>
                  <el-col class="item_prop" :span="2">规格名</el-col>
                  <el-col :span="6">
                    <el-input v-model="form.spuProps.propName" placeholder="请输入规格名" clearable></el-input>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item label="" v-for="(item, index) in form.spuProps.giftPropValueList" :key="index">
                <el-row>
                  <el-col class="item_prop" :span="2">规格值</el-col>
                  <el-col :span="6">
                    <el-form-item label="" :prop="'spuProps.giftPropValueList.'+index+'.value'" :rules="formRules.propValue">
                      <el-input v-model="item.value" placeholder="请输入规格值" clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col class="item_prop" :span="2">计量单位</el-col>
                  <el-col :span="6">
                    <el-form-item label="" :prop="'spuProps.giftPropValueList.'+index+'.skuReduceStorage'" :rules="formRules.skuReduceStorage">
                      <el-input-number v-model="item.skuReduceStorage" clearable></el-input-number>
                    </el-form-item>
                  </el-col>
                  <el-col class="button_style" :span="4">
                    <el-button type="primary" v-show="!isDisabled && index <= 1 && index === form.spuProps.giftPropValueList.length-1" @click="addItem(row,index)">新增</el-button>
                    <el-button type="danger" v-show="!isDisabled && form.spuProps.giftPropValueList.length !== 1"  @click="deleteItem(row,index)">删除</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item label="赠品描述:" v-if="!isDisabled" prop="spuDetails[0].spuContent">
                <Editor ref="editor" :content.sync="form.spuDetails[0].spuContent"></Editor>
              </el-form-item>
              <el-form-item v-else label="赠品描述:" prop="spuDetails[0].spuContent">
                <div class="content" v-html="form.spuDetails[0].spuContent"></div>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="mini" @click="submit" v-show="!isDisabled" type="primary"  :loading="loading">保存</el-button>
                <el-button type="success" v-show="!isDisabled"  size="mini" @click="preView">预览</el-button>
                <el-button size="mini" @click="dialogVisible = false">取消</el-button>
            </div>
        </el-dialog>
        <el-dialog :visible.sync="dialogPrevVisible" title="赠品详情" class="gift_pre_info" :close-on-click-modal="false" :modal-append-to-body="false" width="433px">
            <preview-info :form="previewform"></preview-info>
            <div slot="footer" class="dialog-footer">
                <el-button size="mini" @click="dialogPrevVisible=false">关闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import giftPrdApi from '@/api/giftPrd'
import { objectCopy } from '@/utils/objectCopyHelper'
import Editor from '@/components/editor/editor.vue'
import previewInfo from '@/views/giftMgt/components/previewInfo.vue'
import { inputNumberValid } from '@/utils/validator'
 
export default {
  components: { Editor, previewInfo },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '添加赠品'
    },
    row: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      isDisabled: false,
      dialogVisible: false,
      form: {
        prodSpu: {
          spuNum: null,
          spuName: null,
          isGift: '1',
          giftTypeEnum: null
        },
        prodImgs: [], // 图片数组集合
        spuProps: {
          propName: null,
          giftPropValueList: [{
            value: null,
            skuReduceStorage: null
          }]
        },
        spuDetails: [{
          spuContent: '',
          spuShowType: '2'
        }]
      },
      formRules: {
        'prodSpu.spuNum': [
          { required: true, message: '请输入赠品编码', trigger: 'change' },
          { max: 32, message: '赠品编码最多只能输入 32 个字' },
          { pattern: /^[0-9a-zA-Z]+(-[0-9a-zA-Z]+)*$/, message: '仅支持数字、字母、字符【-】;开头和结尾不允许输入【-】;示例:14aa-0900-001,DY000', trigger: 'blur' }
        ],
        'prodSpu.spuName': [{ required: true, message: '请输入赠品名称', trigger: 'change' },
          { max: 128, message: '赠品名称最多只能输入 128 个字' }
        ],
        prodImgs: [{ type: 'array', required: true, message: '请上传赠品图片' }],
        'spuDetails[0].spuContent': [{ required: true, message: '请输入赠品描述', trigger: 'change' },
          { max: 4194304, message: '赠品描述最多只能输入 4194304 个字' }
        ],
        'spuProps.propName': [{ required: true, message: '请输入规格名称', trigger: 'change' }],
        'prodSpu.giftTypeEnum': [{ required: true, message: '请选择赠品类型', trigger: 'change' }],
        skuReduceStorage: [
          { required: true, message: '请输入赠品数量' },
          { validator: inputNumberValid }
        ],
        propValue: [{ required: true, message: '请输入规格值', trigger: 'change' },
          { max: 128, message: '规格值最多只能输入 128 字', trigger: 'change' }]
      },
      loading: false,
      dialogPrevVisible: false, // 预览弹窗
      previewform: {}
    }
  },
  watch: {
    row: {
      handler (newValue, oldValue) {
        if (newValue.spuBase.spuId) {
          const formData = objectCopy(this.row)
          if (this.title === '赠品详情') {
            this.isDisabled = true
          }
          this.form.prodImgs = []
          this.handGetData(formData)
        }
      },
      deep: true
    },
    /**
     * 监控外部显示变量变化
     * 传递到dialog组件
     */
    show: function (newShow, oldShow) {
      this.dialogVisible = newShow
    },
    /**
     * 监控内部显示属性变化
     * 传递到外部调用变量
     */
    dialogVisible: function (newDialogShow, oldDialogShow) {
      this.$emit('update:show', newDialogShow)
      if (!newDialogShow) {
        this.form = {
          prodSpu: {
            spuNum: null,
            spuName: null,
            isGift: '1'
          },
          prodImgs: [], // 图片数组集合
          spuProps: {
            propName: null,
            giftPropValueList: [{
              value: null,
              skuReduceStorage: null
            }]
          },
          spuDetails: [{
            spuContent: '',
            spuShowType: '2'
          }]
        }
        this.isDisabled = false
      }
    }
  },
  methods: {
    // 处理获取的数据
    handGetData (data) {
      const { spuName, spuNum, spuId, giftType } = data.spuBase
      if (data.images.length) {
        this.form.prodImgs = data.images.map(item => { return { url: item.imgUrl, id: item.imgId } })
      }
      this.form.spuDetails = data.spuDetails
      this.form.prodSpu = { spuName, spuNum }
      this.form.prodSpu.isGift = '1'
      this.$set(this.form.prodSpu, 'giftTypeEnum', giftType)
      this.form.spuId = spuId
      data.spuProp.skuProps.forEach(item => {
        this.form.spuProps = {
          propName: item.propName,
          giftPropValueList: JSON.parse(item.propVal)
        }
      })
    },
    // 预览
    preView () {
      this.previewform = JSON.parse(JSON.stringify(this.form))
      this.previewform.articleContent = this.$refs.editor.getData()
      this.dialogPrevVisible = true
    },
    /**
     * 获取上传成功的图片
     */
    handleSuccess (data) {
      this.form.prodImgs.push(data)
      this.$refs.form.clearValidate('prodImgs')
    },
    /**
     * 移除图片
     */
    handleRemove (file) {
      this.form.prodImgs = this.form.prodImgs.filter(v => {
        return v.id !== file.id
      })
    },
    // 新增规格
    addItem (row, index) {
      this.form.spuProps.giftPropValueList.push({
        value: null,
        skuReduceStorage: null
      })
    },
    // 删除规格
    deleteItem (row, index) {
      this.form.spuProps.giftPropValueList.splice(index, 1)
    },
    /**
       * 提交
       */
    submit () {
      this.form.spuDetails[0].spuContent = this.$refs.editor.getData()
      this.$refs.form.validate().then(res => {
        this.loading = true
        const data = JSON.parse(JSON.stringify(this.form))
        data.prodImgs = this.form.prodImgs.map((item, index) => { return { imgId: item.id, imgNum: index + 1, imgUrl: item.url } })
        if (this.form.spuId) {
          this.editInfo(data)
        } else {
          this.addInfo(data) // 新增文章
        }
      })
    },
    /**
     * 增加文章
     */
    async addInfo (data) {
      try {
        const res = await giftPrdApi.addInfo(data)
        if (res.code === '0') {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.loading = false
          this.$parent.queryData()
          this.dialogVisible = false
        } else {
          this.loading = false
        }
      } catch (error) {
        this.loading = false
      }
    },
    /**
 *     编辑文章
 */
    async editInfo (data) {
      try {
        const res = await giftPrdApi.updateInfo(data)
        if (res.code === '0') {
          this.$message({
            message: '编辑成功',
            type: 'success'
          })
          this.loading = false
          this.$parent.queryData()
          this.dialogVisible = false
        } else {
          this.loading = false
        }
      } catch (error) {
        this.loading = false
      }
    }
  }
}
</script>
<style lang="scss">
.gift_info{
  .spuProps_propName{
   .el-form-item__error{
     margin-left: 62px;
   }
  }
  .item_prop{
    width: auto;
    padding: 0 10px;
  }
  .button_style {
    margin-left: 10px;
  }
  .content {
    width: 90%;
    max-height: 440px;
    border: 1px solid #cccccc;
    padding: 25px;
    overflow-y: auto;
    overflow-x: hidden;
    border-radius: 6px;
    background-color: #F5F7FA;
    border-color: #E4E7ED;
    img, video{width: 100%;height: auto}
    div,p{
      width: 100% !important;
    }
  }
}
.gift_pre_info{
  .el-dialog__body{
    padding:15px 20px;
    height: 600px;
    position: relative;
    overflow: hidden;
   .gift_prd_pre{
      position: absolute;
      top: 0;
      right: -17px;
      bottom: 0;
      overflow-y: scroll;
      overflow-x: hidden;
      padding: 0 10px;
   }
  }
}
</style>