index.vue 6.0 KB

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