UserManagement.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <div id="main">
  3. <el-table :data="userList" style="width: 100%">
  4. <el-table-column label="ID" prop="ID"/>
  5. <el-table-column :label="lang.account" prop="Account"/>
  6. <el-table-column :label="lang.user_name" prop="Name"/>
  7. <el-table-column :label="lang.disabled" prop="Disabled">
  8. <template #default="scope">
  9. <span>{{ scope.row.Disabled === 1 ? lang.disabled : lang.enabled }}</span>
  10. </template>
  11. </el-table-column>
  12. <el-table-column align="right">
  13. <template #header>
  14. <el-button type="primary" size="small" @click="createUser">
  15. New
  16. </el-button>
  17. </template>
  18. <template #default="scope">
  19. <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
  20. Edit
  21. </el-button>
  22. </template>
  23. </el-table-column>
  24. </el-table>
  25. <div id="paginationBox">
  26. <el-pagination v-model:current-page="currentPage" small background layout="prev, pager, next"
  27. :page-count="totalPage" class="mt-4" @current-change="reflushList"/>
  28. </div>
  29. <el-dialog v-model="userInfoDialog" :title="title" width="500">
  30. <el-form>
  31. <el-form-item label-width="100px" :label="lang.account">
  32. <el-input :disabled="editModel === 'edit'" v-model="editUserInfo.account" autocomplete="off"/>
  33. </el-form-item>
  34. <el-form-item label-width="100px" :label="lang.user_name">
  35. <el-input v-model="editUserInfo.name" autocomplete="off"/>
  36. </el-form-item>
  37. <el-form-item label-width="100px" :label="lang.password">
  38. <el-input :placeholder="lang.resetPwd" v-model="editUserInfo.password" autocomplete="off"/>
  39. </el-form-item>
  40. <div style="display: flex;">
  41. <div
  42. style="display: inline-flex;justify-content: flex-end;align-items: flex-start;flex: 0 0 auto;font-size: var(--el-form-label-font-size); height: 32px;line-height: 32px;padding: 0 12px 0 60px;box-sizing: border-box; ">
  43. <el-switch v-model="editUserInfo.disabled" class="ml-2" :active-text="lang.disabled"
  44. :inactive-text="lang.enabled"/>
  45. </div>
  46. </div>
  47. </el-form>
  48. <template #footer>
  49. <div class="dialog-footer">
  50. <el-button @click="userInfoDialog = false">Cancel</el-button>
  51. <el-button type="primary" @click="submit">
  52. Confirm
  53. </el-button>
  54. </div>
  55. </template>
  56. </el-dialog>
  57. </div>
  58. </template>
  59. <script setup>
  60. import {reactive, ref} from 'vue'
  61. import lang from '../i18n/i18n';
  62. import {http} from "@/utils/axios";
  63. import {ElNotification} from "element-plus";
  64. const userList = reactive([])
  65. const currentPage = ref(1)
  66. const totalPage = ref(1)
  67. const userInfoDialog = ref(false)
  68. const editModel = ref("edit")
  69. const editUserInfo = reactive({
  70. "account": "",
  71. "name": "",
  72. "password": "",
  73. "disabled": false
  74. })
  75. const title = ref(lang.editUser)
  76. const reflushList = function () {
  77. http.post('/api/user/list', {"current_page": currentPage.value, "page_size": 10}).then(res => {
  78. userList.length = 0
  79. totalPage.value = res.data.total_page
  80. userList.push(...res.data["list"])
  81. })
  82. }
  83. const handleEdit = function (idx, row) {
  84. editUserInfo.account = row.Account
  85. editUserInfo.name = row.Name
  86. editUserInfo.disabled = row.Disabled === 1
  87. editUserInfo.password = ""
  88. editModel.value = "edit"
  89. title.value = lang.editUser
  90. userInfoDialog.value = true
  91. }
  92. const createUser = function () {
  93. editUserInfo.account = ""
  94. editUserInfo.name = ""
  95. editUserInfo.disabled = false
  96. editUserInfo.password = ""
  97. editModel.value = "create"
  98. title.value = lang.newUser
  99. userInfoDialog.value = true
  100. }
  101. const submit = function () {
  102. if (editModel.value === 'edit') {
  103. let newData = {
  104. "account": editUserInfo.account,
  105. "username": editUserInfo.name,
  106. "disabled": editUserInfo.disabled ? 1 : 0
  107. }
  108. if (editUserInfo.password !== "") {
  109. newData["password"] = editUserInfo.password
  110. }
  111. http.post('/api/user/edit', newData).then(res => {
  112. ElNotification({
  113. title: res.errorNo === 0 ? lang.succ : lang.fail,
  114. message: res.errorNo === 0 ? "" : res.data,
  115. type: res.errorNo === 0 ? 'success' : 'error',
  116. })
  117. if (res.errorNo === 0) {
  118. reflushList()
  119. userInfoDialog.value = false
  120. }
  121. })
  122. } else {
  123. let newData = {
  124. "account": editUserInfo.account,
  125. "username": editUserInfo.name,
  126. "disabled": editUserInfo.disabled ? 1 : 0,
  127. "password": editUserInfo.password
  128. }
  129. http.post('/api/user/create', newData).then(res => {
  130. ElNotification({
  131. title: res.errorNo === 0 ? lang.succ : lang.fail,
  132. message: res.errorNo === 0 ? "" : res.data,
  133. type: res.errorNo === 0 ? 'success' : 'error',
  134. })
  135. if (res.errorNo === 0) {
  136. reflushList()
  137. userInfoDialog.value = false
  138. }
  139. })
  140. }
  141. }
  142. reflushList()
  143. </script>
  144. <style scoped>
  145. #paginationBox {
  146. margin-top: 10px;
  147. display: flex;
  148. justify-content: center;
  149. }
  150. </style>