userTable.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <div class="m-user-table">
  3. <div class="header">
  4. <el-form :inline="true" :model="formInline" ref="ruleFormRef">
  5. <el-form-item label="用户名" prop="username">
  6. <el-input v-model="formInline.username" placeholder="请输入用户名" />
  7. </el-form-item>
  8. <el-form-item>
  9. <el-button type="primary" @click="onSubmit" :icon="Search">查询</el-button>
  10. <el-button @click="reset(ruleFormRef)">重置</el-button>
  11. </el-form-item>
  12. </el-form>
  13. </div>
  14. <div class="footer">
  15. <div class="util">
  16. <el-button type="primary" @click="addHandler">
  17. <el-icon>
  18. <Plus />
  19. </el-icon>
  20. 新增用户
  21. </el-button>
  22. </div>
  23. <div class="table-inner">
  24. <el-table v-loading="loading" :data="tableData" style="width: 100%;height: 100%" border>
  25. <el-table-column prop="username" label="用户名" align="center" width="100" />
  26. <el-table-column prop="nickname" label="昵称" align="center" />
  27. <el-table-column prop="sex" label="性别" align="center">
  28. <template #default="scope">
  29. <span v-if="scope.row.sex === 0">女</span>
  30. <span v-else-if="scope.row.sex === 1">男</span>
  31. </template>
  32. </el-table-column>
  33. <!-- <el-table-column prop="password" label="密码" align="center" width="120"/> -->
  34. <el-table-column prop="role" label="关联角色" align="center" width="120">
  35. <template #default="scope">
  36. <span v-if="scope.row.role === 1">超级管理员</span>
  37. <span v-else-if="scope.row.role === 2">管理员</span>
  38. </template>
  39. </el-table-column>
  40. <el-table-column prop="phone" label="手机号" align="center" width="120" />
  41. <el-table-column prop="status" label="用户状态" align="center">
  42. <template #default="scope">
  43. <el-switch inline-prompt :model-value="scope.row.status" :active-text="'启用'" :inactive-text="'禁用'"
  44. :active-value="1" :inactive-value="0" @change="changeStatus(scope.row)" />
  45. </template>
  46. </el-table-column>
  47. <el-table-column prop="describe" :show-overflow-tooltip="true" width="180" label="用户描述" align="center" />
  48. <el-table-column prop="createTime" label="创建时间" align="center" width="180">
  49. <template #default="{ row }">
  50. {{ dformatTime(row.createTime) }}
  51. </template>
  52. </el-table-column>
  53. <el-table-column prop="operator" label="操作" width="200px" align="center" fixed="right">
  54. <template #default="scope">
  55. <el-button type="primary" size="small" icon="Edit" @click="editHandler(scope.row)">
  56. 编辑
  57. </el-button>
  58. <el-button @click="del(scope.row)" type="danger" size="small" icon="Delete">
  59. 删除
  60. </el-button>
  61. </template>
  62. </el-table-column>
  63. </el-table>
  64. </div>
  65. <div class="pagination">
  66. <el-pagination v-model:currentPage="currentPage1" :page-size="10" background
  67. layout="total, sizes, prev, pager, next, jumper" :total="1000" @size-change="handleSizeChange"
  68. @current-change="handleCurrentChange" />
  69. </div>
  70. </div>
  71. <UserDialog ref="userDialog" :callback="addUser" :editCallback="editUser" />
  72. </div>
  73. </template>
  74. <script lang="ts" setup>
  75. import { ElMessageBox, ElMessage, FormInstance } from 'element-plus'
  76. import { Search } from '@element-plus/icons-vue'
  77. import { onBeforeMount, onMounted, reactive, ref } from 'vue'
  78. import { useUserStore } from "@/store/modules/user"
  79. import { userData } from '@/mock/system'
  80. import UserDialog from './userDialog.vue'
  81. import systemApi from '@/api/system'
  82. import { formatTime } from '@/utils'
  83. const tableData = ref([{}])
  84. const dialogVisible = ref(false)
  85. const userDialog = ref()
  86. const ruleFormRef = ref<FormInstance>()
  87. const formInline = reactive({})
  88. const loading = ref(true)
  89. const currentPage1 = ref(1)
  90. const UserStore = useUserStore()
  91. onBeforeMount(() => {
  92. systemApi.userList({ "userName": UserStore.username }).then((resp) => {
  93. tableData.value = resp.data.result;
  94. });
  95. });
  96. const onSubmit = () => {
  97. console.log('submit!', formInline)
  98. loading.value = true
  99. setTimeout(() => {
  100. loading.value = false
  101. }, 1000)
  102. }
  103. const reset = (formEl: FormInstance | undefined) => {
  104. loading.value = true
  105. setTimeout(() => {
  106. loading.value = false
  107. }, 1000)
  108. }
  109. const dformatTime = (time) => {
  110. const date = new Date(time);
  111. const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
  112. return formattedDate;
  113. }
  114. const addHandler = () => {
  115. userDialog.value.show()
  116. }
  117. const editHandler = (row) => {
  118. userDialog.value.show(row)
  119. }
  120. const del = (row) => {
  121. ElMessageBox.confirm('你确定要删除当前项吗?', '温馨提示', {
  122. confirmButtonText: '确定',
  123. cancelButtonText: '取消',
  124. type: 'warning',
  125. draggable: true,
  126. })
  127. .then(async () => {
  128. systemApi.delUser({ "username": row.username }).then((resp) => {
  129. console.log(resp);
  130. if (resp.data.code == 200) {
  131. let index = tableData.value.indexOf(row);
  132. tableData.value.splice(index, 1);
  133. ElMessage({
  134. message: "删除成功",
  135. type: 'success',
  136. duration: 3000
  137. });
  138. } else {
  139. ElMessage({
  140. message: "删除失败",
  141. type: 'error',
  142. duration: 3000
  143. });
  144. }
  145. });
  146. })
  147. .catch(() => { })
  148. }
  149. const changeStatus = (row) => {
  150. if (row.username != undefined) {
  151. ElMessageBox.confirm(
  152. `确定要${!row.status ? '禁用' : '启用'} ${row.username} 账户吗?`,
  153. '温馨提示',
  154. {
  155. confirmButtonText: '确定',
  156. cancelButtonText: '取消',
  157. type: 'warning',
  158. },
  159. )
  160. .then(async () => {
  161. systemApi.editUserStatus({ "username": row.username }).then((resp) => {
  162. if (resp.data.result) {
  163. row.status = row.status == 0 ? 1 : 0;
  164. }
  165. });
  166. })
  167. }
  168. }
  169. const handleSizeChange = (val: number) => {
  170. console.log(`${val} items per page`)
  171. }
  172. const handleCurrentChange = (val: number) => {
  173. currentPage1.value = val
  174. }
  175. const addUser = (user: any) => {
  176. tableData.value.push(user)
  177. }
  178. const editUser = (user: any) => {
  179. const index = tableData.value.findIndex(item => item.username == user.username);
  180. if (index != -1) {
  181. user.createTime=tableData.value[index].createTime;
  182. tableData.value[index] = user;
  183. }else{
  184. console.log("未找到对象");
  185. }
  186. }
  187. onMounted(() => {
  188. setTimeout(() => {
  189. loading.value = false
  190. }, 1000)
  191. })
  192. </script>
  193. <style lang="scss" scoped>
  194. @import "../index";
  195. </style>