index.vue 5.9 KB

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