马宇豪
2024-11-12 7a7265353879a63d47553324c19ab4bd349d03f4
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
<template>
  <div class="inner">
    <a-row type="flex" justify="space-between" style="margin-bottom: 20px">
      <a-col :span="6" v-if="unittype && unittype !== null">
        <a-button type="primary" @click="editData('add',{})">新增</a-button>
        <a-button type="primary" @click="visible = true" style="margin: 0 12px">导入通讯录表</a-button>
        <a-button type="primary" @click="openGroup()">分组管理</a-button>
      </a-col>
      <a-col :span="18">
        <a-row type="flex" :gutter="14">
          <a-col :span="6">
            <a-input v-model="search.searchParams.company" placeholder="单位名称" style="width: 100%"/>
          </a-col>
          <a-col :span="6">
            <a-select v-model="search.searchParams.addressBookGroupId" placeholder="选择分组" style="width: 100%" @change="handleChange">
              <a-select-option v-for="item in groupData" :value="item.id" :key="item.id">{{item.name}}</a-select-option>
            </a-select>
          </a-col>
          <a-col :span="6">
            <a-button type="primary" @click="searchData()">查询</a-button>
            <a-button style="margin-left: 12px" @click="resetSearch">重置</a-button>
          </a-col>
        </a-row>
      </a-col>
    </a-row>
    <div class="table-cont">
      <a-table :columns="columns" :data-source="tableData" :pagination="pagination" :rowKey="record=>record.id" bordered>
        <template #group="groupId">
          {{ getGroupName(groupId) }}
        </template>
        <template #action="action,row">
          <a-button type="link" @click="editData('edit',row)">编辑</a-button>
          <a-button type="link" class="delBtn" @click="delData(row)">删除</a-button>
        </template>
      </a-table>
    </div>
    <address-user-mod ref="addressRef" :groupList="groupData" @refresh="getAddressBook"></address-user-mod>
    <group-list-mod ref="groupRef" @refresh="getGroupList"></group-list-mod>
    <a-modal v-model="visible" title="导入通讯录" ok-text="导入通讯录" :confirmLoading="uploadLoading" cancel-text="取消" @ok="uploadFile" centered :afterClose="clearMod">
      <a-form-model ref="ruleForm" :label-col="labelCol" :wrapper-col="wrapperCol" :colon="false">
        <a-form-model-item label="通讯录表格模板" extra="导入通讯录须依据此模板">
          <a-button type="primary" @click="downloadFile">下载模板</a-button>
        </a-form-model-item>
        <a-form-model-item label="通讯录表格文件">
          <a-upload :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload" accept=".xlsx,.xls">
            <a-button> <a-icon type="upload"/> 点击上传 </a-button>
          </a-upload>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>
<script>
import {delGroupUser, getAddressBook, getGroupList, importFile} from '@/api/user'
import {getUserInfo} from "@/util/storage"
import addressUserMod from "@/views/Admin/components/addressUserMod"
import groupListMod from "@/views/Admin/components/groupListMod";
import exampleFile from '@/assets/example.xlsx'
export default {
  name: 'addressBook',
  components: { addressUserMod,groupListMod },
  data () {
    return {
      groupData: [],
      unittype: null,
      districtId: null,
      search:{
        pageIndex: 1,
        pageSize: 20,
        searchParams:{
          company: '',
          addressBookGroupId: null
        }
      },
      columns:[
        {
          title: '分组',
          dataIndex: 'addressBookGroupId',
          key: 'addressBookGroupId',
          scopedSlots: { customRender: 'group' }
        },
        {
          title: '单位',
          dataIndex: 'company',
          key: 'company'
        },
        {
          title: '姓名',
          dataIndex: 'name',
          key: 'name'
        },
        {
          title: '手机号码',
          dataIndex: 'phone',
          key: 'phone'
        },
        {
          title: '操作',
          key: 'action',
          width: '15%',
          scopedSlots: { customRender: 'action' }
        },
      ],
      tableData: [],
      pagination: {
        current: 1,
        defaultCurrent: 1,
        defaultPageSize: 20,
        total: 0,
        onChange: ( page, pageSize ) => this.onPageChange(page,pageSize),
        showTotal: total => `共 ${total} 条`
      },
      visible: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 14 },
      exampleFile,
      fileList: [],
      delList: [],
      uploadLoading: false
    }
  },
  created() {
    const t = this
    t.unittype = getUserInfo().unittype
    t.districtId = getUserInfo().districtId
    t.getAddressBook()
    t.getGroupList()
  },
  methods:{
    async getAddressBook(){
      const t = this
      const res = await getAddressBook(t.search)
      if(res.data.code == 100){
        t.tableData = res.data.data
        t.pagination.total = res.data.total
      }else{
        t.$message.warning(res.data.msg);
      }
    },
 
    async getGroupList(){
      let res = await getGroupList()
      if(res.data.code == 100){
        this.groupData = res.data.data
      } else {
        this.$message.warning(res.data.msg);
      }
    },
 
    handleChange(value) {
      console.log(`selected ${value}`);
    },
 
    searchData(){
      this.search.pageIndex = 1
      this.getAddressBook()
    },
 
    resetSearch(){
      const t = this
      t.search = {
        pageIndex: 1,
        pageSize: 20,
        searchParams:{
          company: '',
          addressBookGroupId: null
        }
      }
      t.getAddressBook()
    },
 
    editData(type,data){
      const t = this
      t.$refs.addressRef.openDialog(type,data)
    },
    openGroup(){
      this.$refs.groupRef.openDialog()
    },
 
    async delData(row){
      const t = this
      this.$confirm({
        title: '提示',
        content: h => <div>是否删除该条用户信息?</div>,
        cancelText: '取消',
        okText: '确认',
        centered: true,
        onOk() {
          delGroupUser({id: row.id}).then(res=>{
            if(res.data.code == 100){
              t.$message.success('删除用户信息成功');
              t.getAddressBook()
            }else{
              t.$message.warning(res.data.msg);
            }
          })
        },
        onCancel() {
          console.log('Cancel');
        },
      });
    },
 
    onPageChange(page, pageSize) {
      const t= this
      t.pagination.current = page
      t.search.pageIndex = page
      t.getAddressBook()
    },
    getGroupName(id){
      return this.groupData.find(i => i.id === id)?.name;
    },
 
    downloadFile(){
      const link = document.createElement('a')
      link.href = exampleFile
      link.target = '_blank'
      link.download = '通讯录导入模板.xlsx'
      link.click()
    },
 
    handleRemove(file) {
      const index = this.fileList.indexOf(file)
      const newFileList = this.fileList.slice()
      newFileList.splice(index, 1)
      this.fileList = newFileList;
    },
    beforeUpload(file) {
      this.fileList = [...this.fileList, file]
      this.fileList = this.fileList.slice(-1)
      return false;
    },
 
    async uploadFile(){
      if(this.fileList.length == 0){
        this.$message.warning('请先上传通讯录表格');
        return
      }else{
        this.uploadLoading = true
        const { fileList } = this;
        const formData = new FormData();
        fileList.forEach((file) => {
          formData.append('file', file)
        })
        const res = await importFile(formData)
        if(res.data.code == 100){
          this.$message.success(res.data.msg,2);
          this.fileList = []
          this.uploadLoading = false
          this.visible = false
          this.getAddressBook()
        }else{
          this.$message.warning(res.data.msg);
          this.uploadLoading = false
        }
      }
    },
 
    clearMod(){
      this.fileList = []
    }
  }
}
</script>
 
<style lang="less" scoped>
.delBtn{
  color: @danger
}
</style>