index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <div class="pa-3 white">
  3. <m-search :items="searchItems" v-model="searchValues" class="mb-3" @search="search"></m-search>
  4. <MTable
  5. v-loading="loading"
  6. :items="items"
  7. :headers="headers"
  8. :page-size="pageInfo.size"
  9. :page-current="pageInfo.current"
  10. :total="total"
  11. @page-change="handlePageChange"
  12. >
  13. <template #card-tools>
  14. <m-button type="orange" size="small" icon="el-icon-plus" @click="onAdd">新增</m-button>
  15. </template>
  16. <template #createdTime="scope">
  17. {{ dateFormat(scope.row.createdTime) }}
  18. </template>
  19. <template #state="scope">
  20. <el-tag size="small" :type="scope.row.state ? 'default' : 'success'">{{ scope.row.state ? '未启用' : '已启用' }}</el-tag>
  21. </template>
  22. <template #role="scope">
  23. <template v-if="scope.row.companyInfo?.homeUserId === scope.row.id">
  24. <el-tag size="small" type="danger">超级管理员</el-tag>
  25. </template>
  26. <template v-if="scope.row.role && scope.row.role.length">
  27. <el-tag
  28. v-for="item in scope.row.role"
  29. :key="item.id"
  30. size="small"
  31. >{{ item.roleName }}</el-tag>
  32. </template>
  33. </template>
  34. <template #actions="scope">
  35. <template v-if="scope.row.companyInfo?.homeUserId !== scope.row.id">
  36. <m-button type="primary" class="pa-0" text @click="onEdit(scope.row)">编辑</m-button>
  37. <m-button type="primary" class="pa-0" text @click="onSetRole(scope.row)">角色分配</m-button>
  38. <m-button type="warning" class="pa-0" text @click="onReset(scope.row)">重置密码</m-button>
  39. <m-button
  40. :type="scope.row.state ? 'success' : 'danger'"
  41. class="pa-0"
  42. text
  43. @click="onStatus(scope.row)"
  44. >
  45. {{ scope.row.state ? '启用' : '禁用' }}
  46. </m-button>
  47. <m-button type="danger" class="pa-0" text @click="onDelete(scope.row)">删除</m-button>
  48. </template>
  49. </template>
  50. </MTable>
  51. <UserEdit ref="dialog" @refresh="init"></UserEdit>
  52. <UserRole ref="userRefs" @refresh="init"></UserRole>
  53. <UserReset ref="userReset" @refresh="init"></UserReset>
  54. </div>
  55. </template>
  56. <script>
  57. import UserEdit from './userEdit.vue'
  58. import UserRole from './userRole.vue'
  59. import UserReset from './userReset'
  60. import util from '@/utils/base64ToFile'
  61. import {
  62. getUserList,
  63. deleteUser,
  64. downloadUserTemplate,
  65. userExcelExport,
  66. blockUser
  67. } from '@/api/user'
  68. import { dateFormat } from '@/utils/date'
  69. export default {
  70. name: 'user-list',
  71. components: {
  72. UserEdit,
  73. UserRole,
  74. UserReset
  75. },
  76. data () {
  77. return {
  78. searchItems: [
  79. {
  80. label: '用户查找',
  81. options: {
  82. placeholder: '请输入用户昵称 / 账号'
  83. },
  84. prop: 'searchKey',
  85. type: 'input'
  86. }
  87. ],
  88. searchValues: {
  89. searchKey: null
  90. },
  91. headers: [
  92. { label: '用户昵称', prop: 'name', width: 200 },
  93. { label: '账号', prop: 'username', width: 200 },
  94. { label: '人员编码', prop: 'employeeCode' },
  95. { label: '角色', prop: 'role' },
  96. { label: '状态', prop: 'state' },
  97. { label: '上次登录时间', prop: 'lastLoginTime', width: 150 },
  98. { label: '创建时间', prop: 'createdTime', width: 200 },
  99. { label: '操作', prop: 'actions', fixed: 'right', width: 300 }
  100. ],
  101. items: [],
  102. total: 0,
  103. pageInfo: {
  104. current: 1,
  105. size: 10
  106. },
  107. loading: false,
  108. showReset: false,
  109. show: false,
  110. uploadLoading: false
  111. }
  112. },
  113. async created () {
  114. await this.init()
  115. },
  116. methods: {
  117. dateFormat (str) {
  118. const date = new Date(+str * 1000)
  119. return dateFormat('YYYY-mm-dd HH:MM:SS', date)
  120. },
  121. async init () {
  122. try {
  123. this.loading = true
  124. const { data } = await getUserList({ ...this.pageInfo, ...this.searchValues })
  125. this.items = data.records
  126. this.total = data.total
  127. } catch (error) {
  128. this.$message.error(error)
  129. } finally {
  130. this.loading = false
  131. }
  132. },
  133. search (val) {
  134. this.pageInfo.current = 1
  135. this.init()
  136. },
  137. async onStatus (item) {
  138. const state = item.state ? 1 : 0
  139. try {
  140. await blockUser({
  141. userId: item.id,
  142. state: 1 ^ state
  143. })
  144. this.$message.success('保存成功')
  145. this.init()
  146. } catch (error) {
  147. this.$message.error(error)
  148. }
  149. },
  150. onEdit (item) {
  151. this.$refs.dialog.open(item)
  152. },
  153. onAdd () {
  154. this.$refs.dialog.open()
  155. },
  156. onSetRole (item) {
  157. this.$refs.userRefs.open(item)
  158. },
  159. onReset (item) {
  160. this.$refs.userReset.open(item)
  161. },
  162. onDelete (item) {
  163. this.$confirm('是否确定删除该选项', '提示')
  164. .then(async () => {
  165. try {
  166. await deleteUser({ userId: item.id })
  167. this.$message.success('删除成功')
  168. this.init()
  169. } catch (error) {
  170. this.$message.error(error)
  171. }
  172. })
  173. .catch(_ => {})
  174. },
  175. handlePageChange (page) {
  176. this.pageInfo.current = page
  177. this.init()
  178. },
  179. // 下载批量上传文件模板
  180. async downloadTemplate () {
  181. try {
  182. const { data } = await downloadUserTemplate()
  183. util.downloadFileByByte(data, '批量上传文件模板.xls')
  184. } catch (error) {
  185. this.$message.error(error)
  186. }
  187. },
  188. // 批量上传用户
  189. async batchImportUsers (file) {
  190. const formData = new FormData()
  191. formData.append('file', file)
  192. this.uploadLoading = true
  193. try {
  194. await userExcelExport(formData)
  195. this.$message.success('上传成功')
  196. this.init()
  197. } catch (error) {
  198. this.$message.error(error)
  199. } finally {
  200. this.uploadLoading = false
  201. }
  202. }
  203. }
  204. }
  205. </script>
  206. <style lang="scss" scoped>
  207. </style>