Blame view

ant-design-vue-jeecg/src/components/jeecgbiz/modal/JSelectUserByDepModal.vue 9.39 KB
1
<template>
2
  <j-modal
3
4
5
    :width="modalWidth"
    :visible="visible"
    :title="title"
6
    switchFullscreen
7
    wrapClassName="j-user-select-modal"
8
9
    @ok="handleSubmit"
    @cancel="close"
10
    style="top:50px"
11
12
13
14
15
16
17
18
    cancelText="关闭"
  >
    <a-row :gutter="10" style="background-color: #ececec; padding: 10px; margin: -10px">
      <a-col :md="6" :sm="24">
        <a-card :bordered="false">
          <!--组织机构-->
          <a-directory-tree
            selectable
19
            :selectedKeys="selectedDepIds"
20
21
22
            :checkStrictly="true"
            :dropdownStyle="{maxHeight:'200px',overflow:'auto'}"
            :treeData="departTree"
23
24
25
            :expandAction="false"
            :expandedKeys.sync="expandedKeys"
            @select="onDepSelect"
26
27
28
29
30
31
32
33
          />
        </a-card>
      </a-col>
      <a-col :md="18" :sm="24">
        <a-card :bordered="false">
          用户账号:
          <a-input-search
            :style="{width:'150px',marginBottom:'15px'}"
34
            placeholder="请输入账号"
35
36
            v-model="queryParam.username"
            @search="onSearch"
37
38
          ></a-input-search>
          <a-button @click="searchReset(1)" style="margin-left: 20px" icon="redo">重置</a-button>
39
40
41
42
43
44
45
46
47
          <!--用户列表-->
          <a-table
            ref="table"
            :scroll="scrollTrigger"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
48
            :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type: getType}"
49
            :loading="loading"
50
51
52
53
54
            @change="handleTableChange">
          </a-table>
        </a-card>
      </a-col>
    </a-row>
55
  </j-modal>
56
57
58
</template>

<script>
59
  import { pushIfNotExist, filterObj } from '@/utils/util'
60
  import {queryDepartTreeList, getUserList, queryUserByDepId} from '@/api/api'
61
  import { getAction } from '@/api/manage'
62
63
  export default {
64
    name: 'JSelectUserByDepModal',
65
    components: {},
66
    props: ['modalWidth', 'multi', 'userIds', 'store', 'text'],
67
68
    data() {
      return {
69
        queryParam: {
70
          username: "",
71
        },
72
73
74
75
76
77
78
        columns: [
          {
            title: '用户账号',
            align: 'center',
            dataIndex: 'username'
          },
          {
79
            title: '用户姓名',
80
81
82
83
84
85
86
            align: 'center',
            dataIndex: 'realname'
          },
          {
            title: '性别',
            align: 'center',
            dataIndex: 'sex',
87
            customRender: function (text) {
88
89
90
91
92
93
94
95
96
97
              if (text === 1) {
                return '男'
              } else if (text === 2) {
                return '女'
              } else {
                return text
              }
            }
          },
          {
98
            title: '手机',
99
100
101
102
            align: 'center',
            dataIndex: 'phone'
          },
          {
103
            title: '部门',
104
            align: 'center',
105
            dataIndex: 'orgCodeTxt'
106
107
108
109
          }
        ],
        scrollTrigger: {},
        dataSource: [],
110
        selectionRows: [],
111
112
113
        selectedRowKeys: [],
        selectUserRows: [],
        selectUserIds: [],
114
        title: '根据部门选择用户',
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        isorter: {
          column: 'createTime',
          order: 'desc'
        },
130
        selectedDepIds: [],
131
132
        departTree: [],
        visible: false,
133
134
135
        form: this.$form.createForm(this),
        loading: false,
        expandedKeys: [],
136
137
      }
    },
138
139
140
141
142
143
144
    computed: {
      // 计算属性的 getter
      getType: function () {
        return this.multi == true ? 'checkbox' : 'radio';
      }
    },
    watch: {
145
146
147
148
149
150
      userIds: {
        immediate: true,
        handler() {
          this.initUserNames()
        }
      },
151
    },
152
153
154
    created() {
      // 该方法触发屏幕自适应
      this.resetScreenSize();
155
      this.loadData()
156
157
    },
    methods: {
158
159
      initUserNames() {
        if (this.userIds) {
160
161
          // 这里最后加一个 , 的原因是因为无论如何都要使用 in 查询,防止后台进行了模糊匹配,导致查询结果不准确
          let values = this.userIds.split(',') + ','
162
163
164
165
166
167
168
169
          let param = {[this.store]: values}
          getAction('/sys/user/getMultiUser', param).then((list)=>{
            this.selectionRows = []
            let selectedRowKeys = []
            let textArray = []
            if(list && list.length>0){
              for(let user of list){
                textArray.push(user[this.text])
170
                selectedRowKeys.push(user['id'])
171
                this.selectionRows.push(user)
172
              }
173
            }
174
175
            this.selectedRowKeys = selectedRowKeys
            this.$emit('initComp', textArray.join(','))
176
          })
177
178
        } else {
179
          // JSelectUserByDep组件bug issues/I16634
180
          this.$emit('initComp', '')
181
182
          // 前端用户选择单选无法置空的问题 #2610
          this.selectedRowKeys = []
183
184
185
        }
      },
      async loadData(arg) {
186
187
188
        if (arg === 1) {
          this.ipagination.current = 1;
        }
189
190
191
192
193
194
195
196
197
198
        let params = this.getQueryParams()//查询条件
        this.loading = true
        getAction('/sys/user/queryUserComponentData', params).then(res=>{
          if (res.success) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total
          }
        }).finally(() => {
          this.loading = false
        })
199
200
201
202
203
      },
      // 触发屏幕自适应
      resetScreenSize() {
        let screenWidth = document.body.clientWidth;
        if (screenWidth < 500) {
204
          this.scrollTrigger = {x: 800};
205
206
207
208
209
210
211
        } else {
          this.scrollTrigger = {};
        }
      },
      showModal() {
        this.visible = true;
        this.queryDepartTree();
212
        this.initUserNames()
213
        this.loadData();
214
215
216
217
218
219
220
        this.form.resetFields();
      },
      getQueryParams() {
        let param = Object.assign({}, this.queryParam, this.isorter);
        param.field = this.getQueryField();
        param.pageNo = this.ipagination.current;
        param.pageSize = this.ipagination.pageSize;
221
        param.departId = this.selectedDepIds.join(',')
222
223
224
225
226
227
228
229
230
231
232
        return filterObj(param);
      },
      getQueryField() {
        let str = 'id,';
        for (let a = 0; a < this.columns.length; a++) {
          str += ',' + this.columns[a].dataIndex;
        }
        return str;
      },
      searchReset(num) {
        let that = this;
233
234
235
        that.selectedRowKeys = [];
        that.selectUserIds = [];
        that.selectedDepIds = [];
236
        if (num !== 0) {
237
238
          that.queryParam = {};
          that.loadData(1);
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
        }
      },
      close() {
        this.searchReset(0);
        this.visible = false;
      },
      handleTableChange(pagination, filters, sorter) {
        //TODO 筛选
        if (Object.keys(sorter).length > 0) {
          this.isorter.column = sorter.field;
          this.isorter.order = 'ascend' === sorter.order ? 'asc' : 'desc';
        }
        this.ipagination = pagination;
        this.loadData();
      },
      handleSubmit() {
        let that = this;
256
        this.getSelectUserRows();
257
        that.$emit('ok', that.selectUserRows);
258
        that.searchReset(0)
259
260
        that.close();
      },
261
      //获取选择用户信息
262
263
264
265
266
      getSelectUserRows() {
        this.selectUserRows = []
        for (let row of this.selectionRows) {
          if (this.selectedRowKeys.includes(row.id)) {
            this.selectUserRows.push(row)
267
268
          }
        }
269
        this.selectUserIds = this.selectUserRows.map(row => row.username).join(',')
270
271
      },
      // 点击树节点,筛选出对应的用户
272
273
274
275
      onDepSelect(selectedDepIds) {
        if (selectedDepIds[0] != null) {
          if (this.selectedDepIds[0] !== selectedDepIds[0]) {
            this.selectedDepIds = [selectedDepIds[0]];
276
          }
277
          this.loadData(1);
278
279
280
281
        }
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        this.selectedRowKeys = selectedRowKeys;
282
        selectionRows.forEach(row => pushIfNotExist(this.selectionRows, row, 'id'))
283
284
285
286
287
      },
      onSearch() {
        this.loadData(1);
      },
      // 根据选择的id来查询用户信息
288
      initQueryUserByDepId(selectedDepIds) {
289
290
        this.loading = true
        return queryUserByDepId({id: selectedDepIds.toString()}).then((res) => {
291
292
293
294
          if (res.success) {
            this.dataSource = res.result;
            this.ipagination.total = res.result.length;
          }
295
296
        }).finally(() => {
          this.loading = false
297
298
299
300
301
302
        })
      },
      queryDepartTree() {
        queryDepartTreeList().then((res) => {
          if (res.success) {
            this.departTree = res.result;
303
304
            // 默认展开父节点
            this.expandedKeys = this.departTree.map(item => item.id)
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
          }
        })
      },
      modalFormOk() {
        this.loadData();
      }
    }
  }
</script>

<style scoped>
  .ant-table-tbody .ant-table-row td {
    padding-top: 10px;
    padding-bottom: 10px;
  }

  #components-layout-demo-custom-trigger .trigger {
    font-size: 18px;
    line-height: 64px;
    padding: 0 24px;
    cursor: pointer;
    transition: color .3s;
  }
</style>