jiangping
2023-09-05 d3a2b49a770ac5247fba0b7a8e15c720beb0cad2
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
<template>
    <GlobalWindow
      :title="title"
      :visible.sync="visible"
      :confirm-working="isWorking"
      @confirm="confirm"
    >
      <el-form :model="form" ref="form" :rules="rules">
        <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: 'projects' }"
            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-input v-model="form.imgurl" placeholder="请输入图标" v-trim/> -->
        </el-form-item>
        <el-form-item label="排序码(升序)" prop="sortnum">
          <el-input v-model="form.sortnum" placeholder="请输入排序码" v-trim/>
        </el-form-item>
 
        <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>
 
        <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 { create, updateById } from '@/api/business/category.js'
  export default {
    name: 'OperaCategoryWindow',
    extends: BaseOpera,
    components: { GlobalWindow },
    data () {
      return {
        action: process.env.VUE_APP_API_PREFIX + '/public/uploadLocal',
 
        hideUpload: false,
        
        // 表单数据
        form: {
          id: null,
          name: '',
          attrFirst: '',
          attrFirst1: '',
          attrFirstList: [],
          attrSecond: '',
          attrSecond1: '',
          attrSecondList: [],
          sortnum: '',
          parameter: '',
          paramList: [],
          imgurl: '',
          budgetList: [
            {
              maxamount: '',
              minamount: ''
            }
          ],
          fileList: [],
          attributeValueOne: []
        },
        // 验证规则
        rules: {
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        },
        dialogVisible: false
      }
    },
    created () {
      this.config({
        api: '/business/category',
        'field.id': 'id'
      })
    },
    methods: {
      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: '',
              attrFirst: '',
              attrFirst1: '',
              attrFirstList: [],
              attrSecond: '',
              attrSecond1: '',
              attrSecondList: [],
              sortnum: '',
              parameter: '',
              paramList: [],
              imgurl: '',
              budgetList: [
                {
                  maxamount: '',
                  minamount: ''
                }
              ],
              fileList: [],
              attributeValueOne: []
            }
          }
        }
      }
    }
  }
  </script>