user-select.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <!-- 授权用户 -->
  3. <el-dialog title="选择用户" :visible.sync="visible" width="800px" top="5vh" append-to-body :close-on-click-modal="false">
  4. <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
  5. <el-form-item label="用户名称" prop="userName">
  6. <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable @keyup.enter.native="handleQuery" />
  7. </el-form-item>
  8. <el-form-item>
  9. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  10. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  11. </el-form-item>
  12. </el-form>
  13. <el-row>
  14. <el-table
  15. ref="table"
  16. :data="userList"
  17. border
  18. stripe
  19. header-row-class-name="headBackground"
  20. @row-click="clickRow"
  21. @selection-change="handleSelectionChange"
  22. >
  23. <el-table-column type="selection" width="55" />
  24. <el-table-column label="用户名称" prop="userName" :show-overflow-tooltip="true" />
  25. <el-table-column label="用户状态" prop="state" :show-overflow-tooltip="true">
  26. <template slot-scope="scope">
  27. <span>{{ convertUserStatus(scope.row.state) }}</span>
  28. </template>
  29. </el-table-column>
  30. <el-table-column label="用户类型" prop="userType" :show-overflow-tooltip="true">
  31. <template slot-scope="scope">
  32. <span>{{ convertUserType(scope.row.userType) }}</span>
  33. </template>
  34. </el-table-column>
  35. <el-table-column label="最后登录时间" prop="lastLoginTime" width="160" />
  36. <el-table-column label="创建时间" prop="createTime" width="160" />
  37. </el-table>
  38. <pagination
  39. v-show="total > 0"
  40. :total="total"
  41. align="right"
  42. :page.sync="queryParams.page"
  43. :limit.sync="queryParams.limit"
  44. @pagination="getList"
  45. />
  46. </el-row>
  47. <div slot="footer" class="dialog-footer">
  48. <el-button type="primary" size="mini" @click="handleSelectUser">确定</el-button>
  49. <el-button size="mini" @click="visible = false">取消</el-button>
  50. </div>
  51. </el-dialog>
  52. </template>
  53. <script>
  54. import { getUserList, addUserToAuthRole } from '@/api/system/user'
  55. export default {
  56. props: {
  57. // 角色编号
  58. roleId: {
  59. type: [Number, String]
  60. }
  61. },
  62. data() {
  63. return {
  64. // 遮罩层
  65. visible: false,
  66. // 选中数组值
  67. userIds: [],
  68. // 总条数
  69. total: 0,
  70. // 未授权用户数据
  71. userList: [],
  72. // 用户状态
  73. userStatus: this.$store.getters.userStatus,
  74. // 用户类型
  75. userType: this.$store.getters.userType,
  76. // 查询参数
  77. queryParams: {
  78. page: 1,
  79. limit: 10,
  80. roleId: undefined,
  81. userName: undefined
  82. }
  83. }
  84. },
  85. created() {
  86. this.getList()
  87. },
  88. methods: {
  89. /** 用户状态转换 */
  90. convertUserStatus(val) {
  91. let result = ''
  92. this.userStatus.forEach((e) => {
  93. if (e.value === val) {
  94. result = e.name
  95. }
  96. })
  97. return result
  98. },
  99. /** 用户类型转换 */
  100. convertUserType(val) {
  101. let result = ''
  102. this.userType.forEach((e) => {
  103. if (e.value === val) {
  104. result = e.name
  105. }
  106. })
  107. return result
  108. },
  109. /** 显示弹框 */
  110. show() {
  111. this.queryParams.roleId = this.roleId
  112. this.visible = true
  113. this.resetQuery()
  114. },
  115. /** 行点击事件 */
  116. clickRow(row) {
  117. this.$refs.table.toggleRowSelection(row)
  118. },
  119. /** 多选框选中数据 */
  120. handleSelectionChange(selection) {
  121. this.userIds = selection.map(item => item.userId)
  122. },
  123. /** 查询用户列表 */
  124. getList() {
  125. getUserList(this.queryParams).then(res => {
  126. if (res.code === 200) {
  127. const data = res.data
  128. data.userList.forEach(item => {
  129. item.lastLoginTime ? item.lastLoginTime : item.lastLoginTime = '暂未登录'
  130. })
  131. this.userList = data.userList
  132. this.total = data.count
  133. }
  134. })
  135. },
  136. /** 搜索按钮操作 */
  137. handleQuery() {
  138. this.queryParams.page = 1
  139. this.getList()
  140. },
  141. /** 重置按钮操作 */
  142. resetQuery() {
  143. if (this.$refs['queryForm']) {
  144. this.$refs['queryForm'].resetFields()
  145. }
  146. this.handleQuery()
  147. },
  148. /** 选择授权用户操作 */
  149. handleSelectUser() {
  150. const query = {
  151. roleId: Number(this.queryParams.roleId),
  152. userIdList: this.userIds
  153. }
  154. if (this.userIds.length === 0) {
  155. this.$message({
  156. message: '请选择要分配的用户',
  157. type: 'warning'
  158. })
  159. return
  160. } else {
  161. addUserToAuthRole(query).then(res => {
  162. if (res.code === 200) {
  163. this.$message({
  164. message: '分配用户成功',
  165. type: 'success'
  166. })
  167. this.visible = false
  168. this.$emit('updateUserList')
  169. }
  170. })
  171. }
  172. }
  173. }
  174. // watch: {
  175. // //弹出框表格数据刷新滚动条置顶
  176. // userList: {
  177. // handler () {
  178. // this.$nextTick(() => {
  179. // // 滚动到顶部
  180. // this.$refs.table.bodyWrapper.scrollTop = 0;
  181. // });
  182. // },
  183. // deep: true,
  184. // immediate: true,
  185. // }
  186. // }
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. .el-dialog {
  191. height: 78vh;
  192. overflow: auto;
  193. }
  194. </style>