Mr.Shi
2023-09-19 02eb974bcf9f85a4a6594540fab97007abef4291
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
<template>
    <GlobalWindow
      :title="title"
      :visible.sync="visible"
      :confirm-working="isWorking"
      @confirm="confirm"
    >
      <el-alert title="如果从平台选择商品进行直播,建议类别选择从平台库选择,系统已做数据关联;" type="warning" effect="dark" :closable="false"></el-alert>
      <h3 style="margin: 20px 0 10px 0;">基本信息</h3>
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item label="类别来源" prop="type">
          <el-radio-group v-model="form.type" @change="changeRadio">
            <el-radio :label="1">从平台库选择</el-radio>
            <el-radio :label="0">企业自建</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="选择类别" prop="platCateId" v-if="form.type == 1">
          <el-select v-model="form.platCateId" placeholder="请选择" @change="changeCategory">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类别名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入类别名称,不超过6个字" maxlength="6" v-trim/>
        </el-form-item>
        <el-form-item label="图标" prop="imgurl">
          <el-upload
            :action="action"
            :class="{ hide: hideUpload }"
            :file-list="form.fileList"
            :data="{ folder: 'category_img' }"
            list-type="picture-card"
            :limit="1"
            :on-success="fileSuccess"
            :on-exceed="exceed"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
            <div slot="tip" class="el-upload__tip">只能上传图片格式,png格式,建议尺寸120*120px</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="排序码(升序)" prop="sortnum">
          <el-input v-model="form.sortnum" placeholder="请输入排序码" v-trim/>
        </el-form-item>
 
        <div style="margin: 20px 0 10px 0; height: 30px; display: flex; justify-content: space-between;">
          <h3 style="display: flex; align-items: flex-end;">参数属性配置<h6 style="margin-left: 10px;">配置当前类别的产品参数名</h6></h3>
          <el-button style="display: inline-block;" type="primary" v-if="form.type == 0" @click="addItem">+ 新增</el-button>
        </div>
        <el-table
          :data="form.tableData"
          border
          style="width: 100%">
          <el-table-column label="参数名">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入内容"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="设置为选项">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isselect"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="是否显示">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isshow"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" v-if="form.type == 0">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- <el-form-item label="筛选属性1名称" prop="attrFirst">
          <el-input v-model="form.attrFirst" maxlength="6" placeholder="请输入筛选属性名称,不超过6个字" v-trim/>
        </el-form-item>
        <el-form-item label="筛选属性1的值" prop="attrFirst">
          <el-input v-model="form.attrFirst1" maxlength="10" @keypress.enter.native="confirmVal(1)" placeholder="配置筛选属性值,按回车完成配置,单个值不超过10个字" v-trim/>
          <div style="display: flex; align-items: center; flex-wrap: wrap;">
            <el-tag style="margin-right: 10px; margin-top: 10px;" @close="close(index, 1)" closable v-for="(item, index) in form.attrFirstList" :key="index">{{ item.name }}</el-tag>
          </div>
        </el-form-item>
        <el-form-item label="筛选属性2名称" prop="attrSecond">
          <el-input v-model="form.attrSecond" maxlength="6" placeholder="请输入筛选属性名称,不超过6个字" v-trim/>
        </el-form-item>
        <el-form-item label="筛选属性2的值" prop="attrSecond">
          <el-input v-model="form.attrSecond1" maxlength="10" @keypress.enter.native="confirmVal(2)" placeholder="配置筛选属性值,按回车完成配置,单个值不超过10个字" v-trim/>
          <div style="display: flex; align-items: center; flex-wrap: wrap;">
            <el-tag style="margin-right: 10px; margin-top: 10px;" @close="close(index, 2)" closable v-for="(item, index) in form.attrSecondList" :key="index">{{ item.name }}</el-tag>
          </div>
        </el-form-item>
 
        <el-form-item label="参数属性名" prop="parameter">
          <el-input v-model="form.parameter" maxlength="10" @keypress.enter.native="confirmVal(3)" placeholder="配置参数属性名,按回车完成配置,单个值不超过10个字" v-trim/>
          <div style="display: flex; align-items: center; flex-wrap: wrap;">
            <el-tag style="margin-right: 10px; margin-top: 10px;" @close="close(index, 3)" closable v-for="(item, index) in form.paramList" :key="index">{{ item.name }}</el-tag>
          </div>
        </el-form-item> -->
 
        <h3 style="margin: 20px 0 10px 0; display: flex; align-items: flex-end;">预算区间配置<h6 style="margin-left: 10px;">配置当前类别的预算区间筛选值,上限和下限不能同时为空</h6></h3>
        <el-form-item label="预算区间" prop="attrSecond">
          <div style="display: flex; align-items: center; margin-bottom: 10px;" v-for="(item, index) in form.budgetList" :key="index">
            <el-input v-model="item.minamount" type="number" @input="changeInput(1, index)" placeholder="下限" v-trim/>
            <span style="margin: 0 20px;"> ~ </span>
            <el-input v-model="item.maxamount" type="number" @input="changeInput(2, index)" placeholder="上限" v-trim/>
            <el-button type="primary" style="margin-left: 20px;" @click="add" v-if="index === 0">增加区间</el-button>
            <el-button type="danger" style="margin-left: 20px;" @click="dele(index)" v-else>删除区间</el-button>
          </div>
        </el-form-item>
      </el-form>
    </GlobalWindow>
  </template>
  
  <script>
  import BaseOpera from '@/components/base/BaseOpera'
  import GlobalWindow from '@/components/common/GlobalWindow'
  import { baseCategoryList, companyCreate, companyUpdateById } from '@/api/business/category.js'
  export default {
    name: 'OperaCategoryWindow',
    extends: BaseOpera,
    components: { GlobalWindow },
    data () {
      return {
        action: process.env.VUE_APP_API_PREFIX + '/public/upload',
 
        hideUpload: false,
        categoryList: [],
        
        // 表单数据
        form: {
          id: null,
          type: 1,
          platCateId: '',
          name: '',
          // attrFirst: '',
          // attrFirst1: '',
          // attrFirstList: [],
          // attrSecond: '',
          // attrSecond1: '',
          // attrSecondList: [],
          sortnum: '',
          // parameter: '',
          paramList: [],
          imgurl: '',
          tableData: [],
          budgetList: [
            {
              maxamount: '',
              minamount: ''
            }
          ],
          fileList: [],
          // attributeValueOne: []
        },
        // 验证规则
        rules: {
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        },
        dialogVisible: false
      }
    },
    created () {
      this.config({
        api: '/business/category',
        'field.id': 'id'
      })
    },
    methods: {
      addItem() {
        this.form.tableData.push({ name: '', isselect: 0, isshow: 1 })
      },
      handleDelete(index) {
        this.form.tableData.splice(index, 1)
      },
      changeRadio(e) {
        this.form.name = ''
        this.form.platCateId = ''
        this.form.sortnum = ''
        this.form.tableData = []
        this.form.imgurl = ''
        this.form.fileList = []
        this.form.budgetList = [{
          maxamount: '',
          minamount: ''
        }]
      },
      confirm() {
        this.$refs.form.validate((valid) => {
          if (!valid) return
          // 调用新建接口
          this.isWorking = true
          if (!this.form.id) {
            companyCreate({
              paramList: this.form.tableData,
              type: this.form.type,
              sortnum: this.form.sortnum,
              platCateId: this.form.platCateId,
              name: this.form.name,
              imgurl: this.form.imgurl,
              budgetList: this.form.budgetList
            })
              .then(() => {
                this.visible = false
                this.$tip.apiSuccess('新建成功')
                this.$emit('success')
              })
              .catch(e => {
                this.$tip.apiFailed(e)
              })
              .finally(() => {
                this.isWorking = false
              })
          } else {
            companyUpdateById({
              id: this.form.id,
              paramList: this.form.tableData,
              type: this.form.type,
              sortnum: this.form.sortnum,
              platCateId: this.form.platCateId,
              name: this.form.name,
              imgurl: this.form.imgurl,
              budgetList: this.form.budgetList
            })
              .then(() => {
                this.visible = false
                this.$tip.apiSuccess('编辑成功')
                this.$emit('success')
              })
              .catch(e => {
                this.$tip.apiFailed(e)
              })
              .finally(() => {
                this.isWorking = false
              })
          }
        })
      },
      // 切换分类
      changeCategory(id) {
        this.categoryList.forEach(item => {
          if (item.id === id) {
            this.form.name = item.name
            this.form.sortnum = item.sortnum
            let arr = []
            item.baseCateParamList.forEach((row, index) => {
              arr.push({ name: row.name, baseCateParamId: row.id, isselect: index <= 1 ? 1 : 0, isshow: 1 })
            })
            this.form.tableData = arr
            this.form.imgurl = item.imgurl
            this.form.fileList = [{ url: item.imgfullurl }]
          }
        })
      },
      changeInput(type, index) {
        if (type === 1) {
          if (!this.form.budgetList[index].minamount) return
          if (!/^[0-9]*[1-9][0-9]*$/.test(this.form.budgetList[index].minamount)) {
            this.$message.warning({
              type: 'warning',
              message: '预算只能输入正整数'
            })
            this.form.budgetList[index].minamount = ''
          }
        } else {
          if (!this.form.budgetList[index].maxamount) return
          if (!/^[0-9]*[1-9][0-9]*$/.test(this.form.budgetList[index].maxamount)) {
            this.$message.warning({
              type: 'warning',
              message: '预算只能输入正整数'
            })
            this.form.budgetList[index].maxamount = ''
          }
        }
      },
      exceed() {
        this.$message.warning({
          message: '只能上传一个图标'
        })
      },
      // confirm() {
      //   this.$refs.form.validate((valid) => {
      //     if (valid) {
      //       this.isWorking = true
      //       if (!this.form.id) {
      //         create(this.form)
      //         .then(() => {
      //           this.visible = false
      //           this.$tip.apiSuccess('新建成功')
      //           this.$emit('success')
      //         })
      //         .catch(e => {
      //           this.$tip.apiFailed(e)
      //         })
      //         .finally(() => {
      //           this.isWorking = false
      //         })
      //       } else {
      //         updateById(this.form)
      //           .then(() => {
      //             this.visible = false
      //             this.$tip.apiSuccess('编辑成功')
      //             this.$emit('success')
      //           })
      //           .catch(e => {
      //             this.$tip.apiFailed(e)
      //           })
      //           .finally(() => {
      //             this.isWorking = false
      //           })
      //       }
      //     } else {
      //       return false;
      //     }
      //   });
      // },
      close(index, type) {
        if (type === 1) {
          this.form.attrFirstList.splice(index, 1)
        } else if (type === 2) {
          this.form.attrSecondList.splice(index, 1)
        } else if (type === 3) {
          this.form.paramList.splice(index, 1)
        }
      },
      confirmVal(type) {
        if (type === 1) {
          if (!this.form.attrFirst1) return
          this.form.attrFirstList.push({ name: this.form.attrFirst1 })
          this.form.attrFirst1 = ''
        } else if (type === 2) {
          if (!this.form.attrSecond1) return
          this.form.attrSecondList.push({ name: this.form.attrSecond1 })
          this.form.attrSecond1 = ''
        } else if (type === 3) {
          if (!this.form.parameter) return
          this.form.paramList.push({ name: this.form.parameter })
          this.form.parameter = ''
        }
      },
      fileSuccess(response) {
        this.form.fileList.push({ imgaddr: response.data.imgaddr, url: response.data.url })
        this.form.imgurl = response.data.imgaddr
      },
      handleRemove() {
        this.form.fileList = []
        this.form.imgurl = ''
      },
      add() {
        this.form.budgetList.push({ minamount: '', maxamount: '' })
      },
      dele(i) {
        if (this.form.budgetList.length === 1) return
        this.form.budgetList.splice(i, 1)
      }
    },
    watch: {
      visible: {
        handler(news, old) {
          if (!news) {
            this.form = {
              id: null,
              name: '',
              type: 1,
              platCateId: '',
              tableData: [],
              // attrFirst: '',
              // attrFirst1: '',
              // attrFirstList: [],
              // attrSecond: '',
              // attrSecond1: '',
              // attrSecondList: [],
              sortnum: '',
              // parameter: '',
              // paramList: [],
              imgurl: '',
              budgetList: [
                {
                  maxamount: '',
                  minamount: ''
                }
              ],
              fileList: [],
              // attributeValueOne: []
            }
          } else {
            // 获取分类列表
            baseCategoryList({})
              .then(res => {
                this.categoryList = res
              })
          }
        }
      }
    }
  }
  </script>