zzw
昨天 781b8b3e072a4e00b1cee56f961280c753cbb460
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
<template>
  <div class="page">
    <div class="page-title">
      <div><h2>用户管理</h2><p>左侧查询用户,右侧设置该用户拥有的角色,一个用户可设置多个角色</p></div>
    </div>
    <div class="user-layout">
      <section class="user-left">
        <div class="toolbar">
          <el-button type="primary" plain icon="el-icon-s-check" :disabled="!selectedRows.length" v-permission="'system:users:assign'" @click="openBatchDialog">批量设置角色</el-button>
          <span v-if="selectedRows.length" class="selected-count">已选 {{ selectedRows.length }} 人</span>
          <el-input v-model="filters.keyword" clearable placeholder="用户名 / 账户名" style="width:190px" @keyup.enter.native="search"></el-input>
          <el-select v-model="filters.deptId" clearable filterable placeholder="所属部门" style="width:200px">
            <el-option v-for="item in depts" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
          <el-select v-model="filters.roleId" clearable placeholder="角色" style="width:170px">
            <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
          <el-button type="primary" icon="el-icon-search" v-permission="'system:users:query'" @click="search">查询</el-button>
          <el-button icon="el-icon-refresh-left" @click="reset">重置</el-button>
        </div>
        <el-table ref="userTable" row-key="userId"
          v-loading="loading"
          :data="rows"
          border
          stripe
          highlight-current-row
          height="calc(100vh - 250px)"
          @selection-change="selectedRows = $event"
          @current-change="selectUser"
        >
          <el-table-column type="selection" width="46" align="center"></el-table-column>
          <el-table-column prop="nickName" label="用户名" min-width="110"></el-table-column>
          <el-table-column prop="userName" label="账户名" min-width="140"></el-table-column>
          <el-table-column prop="deptName" label="所属部门" min-width="180" show-overflow-tooltip></el-table-column>
          <el-table-column label="角色权限" min-width="220">
            <template slot-scope="{ row }">
              <el-tag v-for="role in row.roles" :key="role.roleId" size="mini" class="role-tag">{{ role.roleName }}</el-tag>
              <span v-if="!row.roles || !row.roles.length" class="muted">未分配</span>
            </template>
          </el-table-column>
          <el-table-column prop="phoneNumber" label="手机号" width="130"></el-table-column>
          <el-table-column label="状态" width="80" align="center">
            <template slot-scope="{ row }">
              <el-tag size="mini" :type="isDisabled(row) ? 'info' : 'success'">{{ isDisabled(row) ? '停用' : '正常' }}</el-tag>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pager"
          background
          layout="total, sizes, prev, pager, next"
          :total="total"
          :current-page="current"
          :page-size="size"
          :page-sizes="[20, 50, 100]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        ></el-pagination>
      </section>
      <section class="user-right">
        <div class="panel-head">
          <h3>角色授权</h3>
          <span v-if="selected" class="selected-user">{{ selected.nickName || selected.userName }}({{ selected.userName }})</span>
        </div>
        <div v-if="!selected" class="empty-tip">请选择左侧用户</div>
        <template v-else>
          <el-checkbox-group v-model="checkedRoleIds" class="role-list">
            <el-checkbox v-for="item in roles" :key="item.id" :label="item.id" class="role-item">
              <span class="role-name">{{ item.name }}</span>
              <span class="role-code">{{ item.code }}</span>
            </el-checkbox>
          </el-checkbox-group>
          <el-button
            type="primary"
            icon="el-icon-check"
            class="save-button"
            v-permission="'system:users:assign'"
            :loading="saving"
            @click="saveRoles"
          >保存授权</el-button>
        </template>
      </section>
    </div>
      <el-dialog title="批量设置角色" :visible.sync="batchVisible" width="520px" append-to-body :close-on-click-modal="false">
        <div class="batch-summary">已选择 {{ selectedRows.length }} 个用户</div>
        <el-radio-group v-model="batchMode" class="batch-mode">
          <el-radio label="append">追加角色</el-radio>
          <el-radio label="replace">覆盖角色</el-radio>
        </el-radio-group>
        <el-alert :title="batchMode === 'append' ? '保留所选用户原有角色,并追加勾选角色' : '使用勾选角色覆盖所选用户的原有角色'" type="warning" :closable="false" show-icon></el-alert>
        <el-checkbox-group v-model="batchRoleIds" class="batch-role-list">
          <el-checkbox v-for="item in roles" :key="item.id" :label="item.id" class="role-item">
            <span class="role-name">{{ item.name }}</span>
            <span class="role-code">{{ item.code }}</span>
          </el-checkbox>
        </el-checkbox-group>
        <span slot="footer">
          <el-button @click="batchVisible = false">取消</el-button>
          <el-button type="primary" :loading="batchSaving" v-permission="'system:users:assign'" @click="saveBatchRoles">保存</el-button>
        </span>
      </el-dialog>
  </div>
</template>
<script>
import api from '@/api'
export default {
  name: 'UserManage',
  data() {
    return {
      rows: [],
      total: 0,
      current: 1,
      size: 20,
      loading: false,
      saving: false,
      batchSaving: false,
      batchVisible: false,
      batchRoleIds: [],
      batchMode: 'append',
      selectedRows: [],
      depts: [],
      roles: [],
      selected: null,
      checkedRoleIds: [],
      filters: { keyword: '', deptId: null, roleId: null }
    }
  },
  created() {
    this.loadRoles()
    this.loadDepts()
    this.load()
  },
  methods: {
    load() {
      this.clearSelection()
      this.loading = true
      api.get('/system/users', {
        params: {
          keyword: this.filters.keyword || undefined,
          deptId: this.filters.deptId || undefined,
          roleId: this.filters.roleId || undefined,
          current: this.current,
          size: this.size
        }
      }).then(res => {
        const data = res.data || {}
        this.rows = data.records || []
        this.total = data.total || 0
        if (this.selected) {
          const matched = this.rows.find(item => item.userId === this.selected.userId)
          this.selected = matched || null
          if (!matched) this.checkedRoleIds = []
        }
      }).finally(() => { this.loading = false })
    },
    loadRoles() {
      api.get('/system/roles').then(res => { this.roles = res.data || [] })
    },
    loadDepts() {
      api.get('/system/depts').then(res => { this.depts = res.data || [] })
    },
    search() {
      this.current = 1
      this.load()
    },
    reset() {
      this.filters = { keyword: '', deptId: null, roleId: null }
      this.search()
    },
    handleCurrentChange(page) {
      this.current = page
      this.load()
    },
    handleSizeChange(size) {
      this.size = size
      this.current = 1
      this.load()
    },
    clearSelection() {
      this.selectedRows = []
      this.$nextTick(() => { if (this.$refs.userTable) this.$refs.userTable.clearSelection() })
    },
    openBatchDialog() {
      if (!this.selectedRows.length) return
      this.batchRoleIds = []
      this.batchMode = 'append'
      this.batchVisible = true
    },
    saveBatchRoles() {
      if (!this.selectedRows.length) return
      const userIds = this.selectedRows.map(item => item.userId)
      const roleIds = this.batchRoleIds.slice()
      if (this.batchMode === 'append' && !roleIds.length) {
        this.$message.warning('请选择要追加的角色')
        return
      }
      const message = this.batchMode === 'append'
        ? '将向 ' + userIds.length + ' 个用户追加所选角色,是否继续?'
        : (roleIds.length ? '将使用所选角色覆盖 ' + userIds.length + ' 个用户的现有角色,是否继续?' : '未选择角色,将清空 ' + userIds.length + ' 个用户的全部角色,是否继续?')
      this.$confirm(message, '批量设置角色', { type: 'warning' }).then(() => {
        this.batchSaving = true
        api.put('/system/users/roles/batch', { userIds, roleIds, mode: this.batchMode }).then(() => {
          this.$message.success('批量角色设置成功')
          this.batchVisible = false
          this.load()
          if (this.selected) this.selectUser(this.selected)
        }).finally(() => { this.batchSaving = false })
      }).catch(() => {})
    },
    selectUser(row) {
      this.selected = row || null
      this.checkedRoleIds = []
      if (!row) return
      api.get('/system/users/' + row.userId + '/roles').then(res => {
        this.checkedRoleIds = res.data || []
      })
    },
    saveRoles() {
      if (!this.selected) return
      this.saving = true
      api.put('/system/users/' + this.selected.userId + '/roles', { roleIds: this.checkedRoleIds }).then(() => {
        this.$message.success('角色授权已保存')
        this.load()
      }).finally(() => { this.saving = false })
    },
    isDisabled(row) {
      return row.status === '1'
    }
  }
}
</script>
<style scoped>
.user-layout { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 12px; }
.user-left, .user-right { min-width: 0; padding: 14px; border: 1px solid #dce4ec; border-radius: 4px; background: #fff; }
.user-right { min-height: calc(100vh - 190px); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.selected-count { align-self: center; color: #5c6f82; font-size: 12px; }
.batch-summary { margin-bottom: 10px; color: #31465d; font-size: 14px; }
.batch-role-list { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 12px; max-height: 320px; margin-top: 14px; overflow: auto; }
.pager { margin-top: 12px; text-align: right; }
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 14px; padding-bottom: 10px; border-bottom: 1px solid #edf1f5; }
.panel-head h3 { margin: 0; color: #31465d; font-size: 15px; }
.selected-user { color: #5c6f82; font-size: 13px; }
.empty-tip { padding: 60px 0; color: #93a2b1; text-align: center; }
.role-list { display: flex; flex-direction: column; gap: 4px; max-height: calc(100vh - 320px); overflow: auto; }
.role-item { display: flex; align-items: center; height: 34px; margin-right: 0; }
.role-name { color: #31465d; }
.role-code { margin-left: 8px; color: #93a2b1; font-size: 12px; }
.save-button { width: 100%; margin-top: 16px; }
.role-tag { margin: 2px 4px 2px 0; }
.muted { color: #a3afbc; }
@media (max-width: 1100px) { .user-layout { grid-template-columns: 1fr; } }
</style>