cloudroam
2025-03-28 cef2bb0eeeb91a22860cf5d23c7348af1ba921dc
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
<template>
  <el-bus-crud ref="crud" v-bind="tableConfig" />
</template>
 
<script>
import cloneDeep from 'lodash.clonedeep'
import { getSortConfig } from '@/utils/form-item-config'
export default {
  data() {
    return {
      originalList: [],
      expandIds: [],
      tableConfig: {
        url: 'flower/api/flower/category/tree',
        hasPagination: false,
        saveQuery: false,
        isTree: true,
        hasView: false,
        canNewChild: (row) => !row.parentId,
        dialogAttrs: {
          width: '70%',
        },
        /**
         * 优化树形数据量较大渲染卡顿的问题
         * 使用tree的懒加载后更新数据刷新树比较麻烦,暂时不考虑这个方案
         * 由于该分类列表最多两级
         * 目前在渲染树时先将每个父节点下的子元素children取第一个渲染树结构,然后在展开节点时通过id找到原始的children做替换
         * 考虑到更新数据时某些节点可能已经展开,已经展开的节点不能取第一个子元素,所以要记录展开的节点
         * 1.在expandChange事件中更新展开的节点列表
         * 2.当通过搜索条件搜索时,如果某些父节点不在搜索结果中,也要将它们从展开的节点中去除
         */
        afterRequest: (list) => {
          this.originalList = cloneDeep(list)
          this.expandIds = this.expandIds.filter((i) =>
            list.find((item) => item.id === i)
          )
          return list.map((i) => ({
            ...i,
            childrenCount: Array.isArray(i.children) ? i.children.length : 0,
            children: this.expandIds.includes(i.id)
              ? i.children
              : i.children.slice(0, 1),
          }))
        },
        tableEventHandlers: {
          expandChange: (row, expand) => {
            if (expand) {
              if (!this.expandIds.includes(row.id)) {
                this.expandIds.push(row.id)
              }
              row.children =
                this.originalList.find((i) => i.id === row.id)?.children || []
            } else {
              const index = this.expandIds.indexOf(row.id)
              if (index !== -1) {
                this.expandIds.splice(index, 1)
              }
            }
          },
        },
        beforeOpen(row, isNew) {
          if (isNew && row.name) {
            row.parentName = row.name
          }
          if (!isNew && !row.parentId && row.parentName) {
            row.parentName = ''
          }
        },
        extraParentKeys: ['parentName', 'levelLimit'],
        tableAttrs: {
          rowKey: 'id',
        },
        columns: [
          { label: '分类名称', prop: 'name' },
          {
            label: '分类图片',
            formatter: (row) => (
              <el-bus-image
                style="width:50px;height:50px"
                lazy={true}
                src={row.imageUrl}
              />
            ),
          },
          { label: '排序', prop: 'sortBy' },
          { label: '品种数量', formatter: (row) => row.childrenCount },
          {
            label: '是否显示',
            formatter: (row) => (
              <el-switch
                value={row.shown}
                onChange={this.onShownChange.bind(this, row)}
              ></el-switch>
            ),
          },
        ],
        searchForm: [
          {
            type: 'row',
            items: [{ label: '分类名称', id: 'name', type: 'input' }],
          },
        ],
        form: [
          {
            label: '上级分类:',
            id: 'parentName',
            type: 'input',
            readonly: true,
            hidden: (row) => !row.parentName,
          },
          {
            label: '分类名称:',
            id: 'name',
            type: 'input',
            rules: {
              required: true,
              message: '请输入分类名称',
              trigger: 'blur',
            },
          },
          {
            label: '分类图片:',
            id: 'imageUrl',
            type: 'bus-upload',
            el: {
              listType: 'picture-card',
              limit: 1,
              limitSize: 2,
              tipText: '建议上传164*164的图片,大小不超过2M',
              valueType: 'string',
            },
            rules: { required: true, message: '请上传分类图片' },
          },
          {
            label: '等级:',
            id: 'levelLimit',
            type: 'bus-select-dict',
            el: {
              code: 'FLOWER_LEVEL',
              multiple: true,
              style: 'width:100%',
            },
            rules: { required: true, message: '请选择等级' },
            inputFormat: (row) => {
              if ('levelLimit' in row) {
                return row.levelLimit ? row.levelLimit.split(',') : []
              }
            },
            outputFormat: (val) => {
              return Array.isArray(val) ? val.join(',') : null
            },
            hidden: (row) => row.parentName,
          },
          {
            label: '分类颜色:',
            id: 'color',
            type: 'input',
            hidden: (row) => !row.parentName,
          },
          {
            label: '规格:',
            id: 'unit',
            type: 'input',
            hidden: (row) => !row.parentName,
          },
          {
            type: 'row',
            items: [
              {
                label: 'A级重量:',
                id: 'weightA',
                type: 'input-number',
                el: {
                  min: 0,
                  precision: 1,
                  controls: false,
                },
                unit: 'kg',
                hidden: (row) =>
                  !row.parentName ||
                  !row.levelLimit ||
                  !row.levelLimit.includes('A'),
                rules: {
                  required: true,
                  message: '请输入A级重量',
                  trigger: 'blur',
                },
              },
              {
                label: 'B级重量:',
                id: 'weightB',
                type: 'input-number',
                el: {
                  min: 0,
                  precision: 1,
                  controls: false,
                },
                unit: 'kg',
                hidden: (row) =>
                  !row.parentName ||
                  !row.levelLimit ||
                  !row.levelLimit.includes('B'),
                rules: {
                  required: true,
                  message: '请输入B级重量',
                  trigger: 'blur',
                },
              },
              {
                label: 'C级重量:',
                id: 'weightC',
                type: 'input-number',
                el: {
                  min: 0,
                  precision: 1,
                  controls: false,
                },
                unit: 'kg',
                hidden: (row) =>
                  !row.parentName ||
                  !row.levelLimit ||
                  !row.levelLimit.includes('C'),
                rules: {
                  required: true,
                  message: '请输入C级重量',
                  trigger: 'blur',
                },
              },
              {
                label: 'D级重量:',
                id: 'weightD',
                type: 'input-number',
                el: {
                  min: 0,
                  precision: 1,
                  controls: false,
                },
                unit: 'kg',
                hidden: (row) =>
                  !row.parentName ||
                  !row.levelLimit ||
                  !row.levelLimit.includes('D'),
                rules: {
                  required: true,
                  message: '请输入D级重量',
                  trigger: 'blur',
                },
              },
              {
                label: 'E级重量:',
                id: 'weightE',
                type: 'input-number',
                el: {
                  min: 0,
                  precision: 1,
                  controls: false,
                },
                unit: 'kg',
                hidden: (row) =>
                  !row.parentName ||
                  !row.levelLimit ||
                  !row.levelLimit.includes('E'),
                rules: {
                  required: true,
                  message: '请输入E级重量',
                  trigger: 'blur',
                },
              },
              {
                label: 'O级重量:',
                id: 'weightO',
                type: 'input-number',
                el: {
                  min: 0,
                  precision: 1,
                  controls: false,
                },
                unit: 'kg',
                hidden: (row) =>
                  !row.parentName ||
                  !row.levelLimit ||
                  !row.levelLimit.includes('O'),
                rules: {
                  required: true,
                  message: '请输入O级重量',
                  trigger: 'blur',
                },
              },
            ],
          },
          {
            ...getSortConfig(),
          },
        ],
      },
    }
  },
  head() {
    return {
      title: '分类管理',
    }
  },
  methods: {
    onShownChange(row, e) {
      const url = e
        ? 'flower/api/flower/category/tree/shown'
        : 'flower/api/flower/category/tree/hidden'
      const text = e ? '显示' : '隐藏'
      this.$elBusUtil
        .confirm(`确定要${text}这个分类吗?`)
        .then(async () => {
          const { code } = await this.$elBusHttp.request(url, {
            params: {
              id: row.id,
            },
          })
          if (code === 0) {
            this.$message.success(`${text}成功`)
            this.$refs.crud.getList()
          }
        })
        .catch(() => {})
    },
  },
}
</script>
 
<style lang="scss" scoped>
.category-list {
}
</style>