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
<template>
  <el-dialog :visible.sync="showDialog" v-if="showDialog" :title="title" :close-on-click-modal="false" :modal-append-to-body="false">
    <el-tree
      ref="tree"
      :data="treeData"
      show-checkbox
      node-key="publicityId"
      v-loading="isLoading"
      default-expand-all
      @check="handleNodeSelected"
      check-on-click-node
      :props="defaultProps">
    </el-tree>
    <div slot="footer" class="dialog-footer buttonPosition">
        <el-button size="mini" type="primary" @click="submit">确定</el-button>
        <el-button size="mini" @click="showDialog=false">取消</el-button>
    </div>
  </el-dialog>
</template>
 
<script>
import catPublicityApi from '@/api/catPublicity'
export default {
  props: {
    title: {
      type: String,
      default: null
    },
    show: {
      type: Boolean,
      default: false
    },
    checkedTreeData: {
      type: Array,
      default: function () {
        return []
      }
    },
    firstCategoryData: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data () {
    return {
      showDialog: false,
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'publicityName'
      },
      isLoading: false,
      checkedData: [],
      firstCategory: []
    }
  },
  watch: {
    /**
         * 监控外部显示变量变化
         * 传递到dialog组件
         */
    show: function (newShow, oldShow) {
      this.showDialog = this.show
      if (this.show) {
        this.queryList()
      }
    },
    /**
         * 监控内部显示属性变化
         * 传递到外部调用变量
         */
    showDialog: function (newDialogShow, oldDialogShow) {
      this.$emit('update:show', newDialogShow)
    }
  },
  methods: {
    /**
     * 树节点选择
     */
    handleNodeSelected (data, checkedData) {
      if (!data.pid && this.firstCategory.length > 3 && !this.firstCategory.find(item => {
        return item.publicityId === data.publicityId
      })) {
        this.$message({
          message: '一级分类最多只能选4个',
          type: 'info'
        })
        this.$refs.tree.setCheckedKeys(this.checkedData.map(item => {
          return item.publicityId
        }))
        return
      }
      this.firstCategory = []
      this.checkedData = []
      const checkedDatas = []
      const arr = checkedData.checkedNodes.concat(checkedData.halfCheckedNodes)
      arr.forEach(item => {
        if (!item.pid) {
          this.firstCategory.push({
            publicityId: item.publicityId,
            publicityName: item.publicityName
          })
        }
      })
      checkedData.checkedNodes.forEach(item => {
        checkedDatas.push({
          publicityId: item.publicityId,
          publicityName: item.publicityName,
          pid: item.pid
        })
      })
      checkedDatas.forEach((rowItem) => {
        const data = this.firstCategory.find((item) => {
          return rowItem.pid === item.publicityId
        })
        rowItem.content = data ? `${data.publicityName}--${rowItem.publicityName}` : rowItem.publicityName
        if (!checkedDatas.find((item) => {
          return rowItem.publicityId === item.pid
        })
        ) {
          this.checkedData.push(rowItem)
        }
      })
    },
    /**
     * 提交
     */
    submit () {
      // if (this.checkedData.length && this.firstCategory) {
      this.$emit('handle-tree-select', this.checkedData, this.firstCategory)
      // }
      this.showDialog = false
    },
    /**
     * 查询列表
     */
    async queryList (pageInfo = { pageNum: 1, pageSize: 100000 }) {
      try {
        this.isLoading = true
        const res = await catPublicityApi.getList({ ...pageInfo, param: {} })
        if (res.code === '0') {
          this.isLoading = false
          this.treeData = res.data.list
          this.checkedData = JSON.parse(JSON.stringify(this.checkedTreeData))
          this.firstCategory = JSON.parse(JSON.stringify(this.firstCategoryData))
          const arr = []
          const checkedArr = []
          res.data.list.forEach(v => {
            this.firstCategory.forEach(item => {
              if (item.publicityId === v.publicityId) {
                arr.push(item)
              }
            })
            this.checkedData.forEach(item => {
              if (item.publicityId === v.publicityId) {
                checkedArr.push(item)
              }
              if (v.children && v.children.length) {
                v.children.forEach(i => {
                  if (item.publicityId === i.publicityId) {
                    checkedArr.push(item)
                  }
                })
              }
            })
          })
          this.firstCategory = JSON.parse(JSON.stringify(arr))
          this.checkedData = JSON.parse(JSON.stringify(checkedArr))
          this.$refs.tree.setCheckedKeys(this.checkedTreeData.map(item => {
            return item.publicityId
          }))
        } else {
          this.isLoading = false
        }
      } catch (error) {
      }
    }
  }
}
</script>
 
<style>
 
</style>