1
MrShi
2025-01-06 d7dca690cedd12e271f0ee0b9050679d73796f5c
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
<template>
  <TableLayout :permissions="['business:areas:query']">
    <!-- 表格和分页 -->
    <template v-slot:table-wrap>
      <el-table
        ref="table"
        v-loading="isWorking.search"
        :data="tableData.list"
        lazy
        :load="load"
        :tree-props="{ children: 'childList', hasChildren: 'hasChildren' }"
        row-key="id"
        stripe
        border
        :header-row-class-name="'table-header'"
        class="doumee-element-table"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="name" label="地区名称" min-width="100px"></el-table-column>
        <el-table-column prop="code" label="行政区代码" align="center" min-width="140px"></el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center" min-width="140px"></el-table-column>
<!--
        <el-table-column prop="sortnum" label="排序码" align="center" min-width="140px"></el-table-column>
-->
        <el-table-column
          v-if="containPermissions(['business:areas:update', 'business:areas:create', 'business:areas:delete'])"
          label="操作"
          align="center"
          min-width="220"
          fixed="right"
        >
          <template slot-scope="{ row }">
            <el-button type="text" @click="edit(row)" v-permissions="['business:areas:update']">编辑</el-button>
            <el-button v-if="row.type!=2" type="text" @click="createChild(row)" v-permissions="['business:areas:create']">新建{{ row.type==0 ? '市' : '区县' }}</el-button>
            <el-button type="text" @click="deleteById(row)" v-permissions="['business:areas:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 新建/修改 -->
    <OperaAreasWindow ref="operaAreasWindow" @success="update"/>
  </TableLayout>
</template>
 
<script>
import BaseTable from '@/components/base/BaseTable'
import TableLayout from '@/layouts/TableLayout'
import Pagination from '@/components/common/Pagination'
import OperaAreasWindow from '@/components/business/OperaAreasWindow'
import { listByParentId } from '@/api/business/areas'
export default {
  name: 'Areas',
  extends: BaseTable,
  components: { TableLayout, Pagination, OperaAreasWindow },
  data () {
    return {
      // 搜索
      searchForm: {
        type: 0,
        parentId: ''
      },
      treeMaps: new Map(),
      parentId: null
    }
  },
  created () {
    this.config({
      module: '省市区信息表',
      api: '/business/areas',
      'field.id': 'id',
      'field.main': 'id'
    })
    this.search()
  },
  methods: {
 
    // 页码变更处理
    handlePageChange (pageIndex) {
 
      this.isWorking.search = true
      listByParentId(this.searchForm)
        .then(data => {
          this.tableData.list = this.dataAddBool(data)
        })
        .catch(e => {
          console.log(e)
          this.$tip.error('操作失败')
        })
        .finally(() => {
          this.isWorking.search = false
        })
    },
    dataAddBool(array) {
      if(array==null){
        return  []
      }
      array.forEach(item => {
        item.hasChildren = item.type != 2
        // item.childList = item.childList && this.dataAddBool(item.childList)
      })
      return array
    },
    load(tree, treeNode, resolve) {
      this.treeMaps.set(tree.id, { tree, treeNode, resolve })
      listByParentId({ parentId: tree.id, type: tree.type + 1 })
        .then(data => {
          resolve(this.dataAddBool(data || []))
        })
        .catch(e => {
          console.log(e)
          this.$tip.error('操作失败')
        })
        .finally(() => {
          this.isWorking.search = false
        })
    },
    refreshLoadTree(parentId) {
      if (this.treeMaps.get(parentId)) {
        const { tree, treeNode, resolve } = this.treeMaps.get(parentId)
        this.$set(this.$refs.table.store.states.lazyTreeNodeMap, parentId, [])
        if (tree) { // 重新执行父节点加载子级操作
          this.load(tree, treeNode, resolve)
          if (tree.parentId) { // 若存在爷爷结点,则执行爷爷节点加载子级操作,防止最后一个子节点被删除后父节点不显示删除按钮
            const a = this.treeMaps.get(tree.parentId)
            this.load(a.tree, a.treeNode, a.resolve)
          }
        }
      } else {
        this.handlePageChange()
      }
    },
    deleteById (row, childConfirm = true) {
      // let message = `确认删除${this.module}【${row[this.configData['field.main']]}】吗?`
      let message = `确认删除该记录吗?`
      if (childConfirm && row.childList != null && row.childList.length > 0) {
        // message = `确认删除${this.module}【${row[this.configData['field.main']]}】及其子${this.module}吗?`
        message = `确认删除该记录及其子数据吗?`
      }
      this.$dialog.deleteConfirm(message)
        .then(() => {
          this.isWorking.delete = true
          this.api.deleteById(row[this.configData['field.id']])
            .then(() => {
              this.$tip.apiSuccess('删除成功')
              this.refreshLoadTree(row.parentId)
            })
            .catch(e => {
              console.log(e)
              this.$tip.error('操作失败')
            })
            .finally(() => {
              this.isWorking.delete = false
            })
        })
        .catch(() => {})
    },
    edit(row) {
      // this.parentId = row.type==0 ? null : row.type==1 ? '编辑市' : '编辑区县'
      this.$refs.operaAreasWindow.open(row.type==0 ? '编辑省' : row.type==1 ? '编辑市' : '编辑区县', row)
    },
    createChild(row) {
      this.$refs.operaAreasWindow.open(row.type == 0 ? '新建市' : '新建县区', { parentId: row.id, name: '', type: row.type + 1 })
    },
    update(parentId) {
      this.refreshLoadTree(parentId)
    }
  }
}
</script>