董国庆
2025-06-26 1a61525947d43fd7c8a6df637bf5c1c8f9cc37f3
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
332
333
334
335
336
337
338
339
340
341
342
343
<template>
    <el-dialog @open="openDialog" class="select-member" :z-index="1000" :visible.sync="visible" width="53.33%"
        :close-on-click-modal="false" :show-close="false">
        <template #title>
            <div>{{ title }}</div>
        </template>
        <div class="select-member-content">
            <el-row :gutter="16">
                <el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8">
                    <div class="select-member-content-left">
                        <div class="select-member-content-left-search">
                            <div class="select-member-content-left-search-input">
                                <img src="@/assets/public/search-outline@2x.png" />
                                <el-input v-model="search" placeholder="请输入" />
                                <div class="select-member-content-left-search-input-close">
                                    <img @click.stop="searchRole" v-if="search"
                                        src="@/assets/public/close-circle-fill@2x.png" />
                                </div>
                            </div>
                            <el-button type="primary" @click="filterRoleList">搜索</el-button>
                        </div>
                        <div class="select-member-content-left-list">
                            <div class="select-member-content-left-list-title">角色列表</div>
                            <div class="select-member-content-left-list-itemBox">
                                <div @click="searchUserList(item.roleId)" v-for="item in filteredRoleList"
                                    :key="item.roleId" class="select-member-content-left-list-itemBox-item"
                                    :class="roleId == item.roleId && 'active'">
                                    {{ item.roleName }}
                                </div>
                            </div>
                        </div>
                    </div>
                </el-col>
                <el-col :xs="24" :sm="24" :md="16" :lg="16" :xl="16">
                    <div class="select-member-content-right">
                        <div class="select-member-content-right-header">
                            <div class="select-member-content-right-header-text">人员列表</div>
                            <div class="select-member-content-right-header-search">
                                <el-input clearable v-model="nickNameOrPhone" placeholder="请输入姓名/手机号"
                                    @keyup.enter.native="searchUserList(roleId)" />
                                <el-button type="primary" @click="searchUserList(roleId)">搜索</el-button>
                            </div>
                        </div>
                        <Table ref="memberTable" :row-key="row => row.userId" height="300" :data="tableData" :total="0"
                            @selection-change="handleSelectionChange" :row-class-name="tableRowClassName"
                            @row-click="handleRowClick">
                            <el-table-column type="selection" width="55" v-if="!singleSelect" />
                            <el-table-column label="角色" prop="roleName" />
                            <el-table-column label="姓名" prop="nickName" />
                            <el-table-column label="创建时间" prop="createTime" />
                        </Table>
                    </div>
                </el-col>
            </el-row>
        </div>
        <div class="select-member-footer">
            <el-button @click="close" type="default">关闭</el-button>
            <el-button type="primary" @click="submit">确认选择</el-button>
        </div>
    </el-dialog>
</template>
 
<script>
import { getRoleList, getUserList,getUsersList } from './service'
export default {
    props: {
        projectId: {
            type: [String, Number],
            default: null
        },
        singleSelect: {
            type: Boolean,
            default: false
        },
        title: {
            type: String,
            default: '选择参与人员'
        },
    },
    data() {
        return {
            visible: false,
            search: '',
            nickNameOrPhone: '',
            tableData: [],
            selectData: [],
            roleList: [], // 原始角色列表
            filteredRoleList: [], // 过滤后的角色列表
            roleId: null,
        }
    },
    methods: {
        setSelection(selected) {
            this.selectData = selected
            this.$nextTick(() => {
                // 设置新选中
                this.tableData.forEach(row => {
                    if (selected.some(i => i.userId === row.userId)) {
                        this.$refs.memberTable.toggleRowSelection(row, true)
                    }
                })
            })
        },
        openDialog() {
            // 获取角色列表并根据项目组ID进行过滤
            getRoleList().then(res => {
                let roles = [];
                if (this.title == '选择菌种实验员') {
                    if (this.title == '选择菌种实验员') {
                        roles = res.filter(item => item.roleId == 48)
                    }
                } else {
                    if (this.projectId) {
                        // 过滤出实验员和化验师角色
                        roles = res.filter(item => item.roleId == 4 || item.roleId == 5);
                    } else {
                        roles = res.filter(item => item.roleId !== 1); // 排除 roleId 为 1 的角色
                    }
                }
 
 
 
 
                this.roleList = roles;
                this.filterRoleList(); // 获取后先进行一次过滤
            });
            this.searchUserList(null);
        },
        handleSelectionChange(val) {
            if (!this.singleSelect) {
                // 获取当前表格中未被选中的用户ID
                const unselectedInCurrentTable = this.tableData.filter(row => !val.some(selected => selected.userId === row.userId)).map(row => row.userId);
                // 从总的selectData中移除这些未选中的用户,保留其他角色已选中的用户
                const otherSelected = this.selectData.filter(item => !unselectedInCurrentTable.includes(item.userId));
                // 合并当前表格的选中项和其他角色已选中的项
                this.selectData = [...otherSelected.filter(item => !val.some(v => v.userId === item.userId)), ...val];
            }
        },
        async searchUserList(roleId) {
            this.roleId = roleId
            // 根据是否有项目组ID来决定调用不同的接口
            let params = {
                roleIds: roleId ? [roleId] : [],
                nickNameOrPhone: this.nickNameOrPhone, // 使用 nickNameOrPhone 进行搜索
                pageSize: 9999,
                pageNum: 1,
            };
 
            if (this.projectId) {
                params.projectId = this.projectId;
            }
            let res = ''
            if(this.title == '选择菌种实验员'){
                params.roleType = 2
                params.projectId = this.projectId;
                res = await getUsersList(params);
                this.tableData = res.data.records;
            }else{
                res = await getUserList(params);
                this.tableData = res.records;
            }
 
 
            // 数据加载完成后重新应用选中状态
            this.$nextTick(() => {
                this.tableData.forEach(row => {
                    if (this.selectData.some(i => i.userId === row.userId)) {
                        this.$refs.memberTable.toggleRowSelection(row, true)
                    }
                })
            })
        },
        searchRole() {
            this.search = '';
            this.filterRoleList(); // 清空搜索词后重新过滤
        },
        filterRoleList() {
            if (this.search) {
                this.filteredRoleList = this.roleList.filter(item => item.roleName.includes(this.search));
            } else {
                this.filteredRoleList = [...this.roleList];
            }
        },
        open() {
            this.visible = true
        },
        close() {
            this.visible = false
        },
        submit() {
            this.$emit('submit', this.selectData)
        },
        tableRowClassName({ row, rowIndex }) {
            if (this.selectData.findIndex(item => item.userId === row.userId) != -1) {
                return 'select-row';
            }
            return '';
        },
        handleRowClick(row) {
            if (this.singleSelect) {
                // 单选模式:直接设置选中项
                this.selectData = [row];
                // 更新表格选中状态
                this.$nextTick(() => {
                    this.tableData.forEach(item => {
                        this.$refs.memberTable.toggleRowSelection(item, item.userId === row.userId);
                    });
                });
            }
        }
    }
}
</script>
 
<style scoped lang="less">
// to fix el-message弹框被el-dialog覆盖的问题。
.select-member-content {
    .select-member-content-left {
        box-shadow: 0px 4px 12px 4px rgba(0, 0, 0, 0.08);
        border-radius: 10px;
        margin-bottom: 31px;
 
        &-search {
            padding: 6px 7px 6px 10px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid rgba(238, 238, 238, 1);
 
            &-input {
                display: flex;
                align-items: center;
                background: rgba(0, 0, 0, 0.04);
                border-radius: 4px;
                margin-right: 10px;
                flex: 1;
 
                ::v-deep .el-input__inner {
                    background-color: transparent;
                    border: none;
                    height: 36px;
                    line-height: 36px;
                    padding: 0;
                    width: 100%;
                }
 
                img {
                    flex-shrink: 0;
                    width: 19px;
                    height: 20px;
                    margin: 0 5px;
                }
 
                &-close {
                    width: 19px;
                    height: 20px;
                    margin: 0 9px;
                    flex-shrink: 0;
 
                    img {
                        cursor: pointer;
                    }
                }
            }
        }
 
        &-list {
            padding: 15px 13px;
 
            &-title {
                font-weight: 500;
                font-size: 16px;
                line-height: 16px;
                color: #222222;
                font-family: 'SourceHanSansCN-Medium';
                margin-bottom: 8px;
            }
 
            &-itemBox {
                height: 451px;
                background: #F4F6F8;
                border-radius: 10px;
                padding: 12px 10px;
                overflow-y: auto;
 
                &-item {
                    cursor: pointer;
                    font-weight: 400;
                    font-size: 14px;
                    line-height: 22px;
                    color: rgba(0, 0, 0, 0.88);
                    font-family: 'PingFangSCRegular';
                    background: #FFFFFF;
                    border-radius: 4px;
                    padding: 9px 15px;
                    margin-bottom: 10px;
 
                    &:last-child {
                        margin-bottom: 0;
                    }
 
                    &:hover,
                    &.active {
                        background: rgba(4, 156, 154, 0.1);
                        color: #049C9A;
                    }
                }
            }
        }
    }
 
    .select-member-content-right {
        margin-bottom: 31px;
 
        &-header {
            display: flex;
            align-items: center;
            margin-top: 5px;
            justify-content: space-between;
            margin-bottom: 21px;
 
            &-text {
                flex-shrink: 0;
                font-weight: 500;
                font-size: 16px;
                line-height: 16px;
                color: #222222;
                font-family: 'SourceHanSansCN-Medium';
                margin-right: 20px;
            }
 
            &-search {
                display: flex;
                align-items: center;
 
                ::v-deep .el-input {
                    margin-right: 12px;
                }
            }
        }
    }
}
</style>