index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div class="page-container">
  3. <el-card class="search-card" shadow="never">
  4. <el-form :model="query" inline>
  5. <el-form-item label="关键字">
  6. <el-input
  7. v-model="query.keyword"
  8. placeholder="分类名称/备注"
  9. clearable
  10. style="width: 220px"
  11. @keyup.enter="handleSearch"
  12. />
  13. </el-form-item>
  14. <el-form-item>
  15. <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
  16. <el-button :icon="Refresh" @click="handleReset">重置</el-button>
  17. </el-form-item>
  18. </el-form>
  19. </el-card>
  20. <el-card class="table-card" shadow="never">
  21. <div class="toolbar">
  22. <el-button type="primary" :icon="Plus" @click="handleAdd">新增用户分类</el-button>
  23. </div>
  24. <el-table v-loading="loading" :data="tableData" border stripe>
  25. <el-table-column label="序号" type="index" width="70" align="center" />
  26. <el-table-column label="分类ID" prop="id" width="180" align="center" />
  27. <el-table-column label="分类名称" prop="name" min-width="180" />
  28. <el-table-column label="备注" prop="remark" min-width="260" show-overflow-tooltip>
  29. <template #default="{ row }">{{ row.remark || '-' }}</template>
  30. </el-table-column>
  31. <el-table-column label="创建时间" prop="createTime" width="170" align="center" :formatter="dateTimeFormatter" />
  32. <el-table-column label="操作" width="150" align="center" fixed="right">
  33. <template #default="{ row }">
  34. <el-button type="primary" link size="small" @click="handleEdit(row)">编辑</el-button>
  35. <el-button type="danger" link size="small" @click="handleDelete(row)">删除</el-button>
  36. </template>
  37. </el-table-column>
  38. </el-table>
  39. <div class="pagination">
  40. <el-pagination
  41. v-model:current-page="query.pageNum"
  42. v-model:page-size="query.pageSize"
  43. :page-sizes="[10, 20, 50]"
  44. :total="total"
  45. :hide-on-single-page="false"
  46. layout="total, sizes, prev, pager, next, jumper"
  47. background
  48. @size-change="loadData"
  49. @current-change="loadData"
  50. />
  51. </div>
  52. </el-card>
  53. <el-dialog
  54. v-model="formVisible"
  55. :title="formMode === 'add' ? '新增用户分类' : '编辑用户分类'"
  56. width="520px"
  57. :close-on-click-modal="false"
  58. @close="handleFormClose"
  59. >
  60. <el-form ref="formRef" :model="formData" :rules="formRules" label-width="90px">
  61. <el-form-item label="分类名称" prop="name">
  62. <el-input v-model.trim="formData.name" placeholder="请输入分类名称" maxlength="50" show-word-limit />
  63. </el-form-item>
  64. <el-form-item label="备注" prop="remark">
  65. <el-input
  66. v-model="formData.remark"
  67. type="textarea"
  68. :rows="3"
  69. placeholder="请输入备注"
  70. maxlength="255"
  71. show-word-limit
  72. />
  73. </el-form-item>
  74. </el-form>
  75. <template #footer>
  76. <el-button @click="formVisible = false">取消</el-button>
  77. <el-button type="primary" :loading="submitLoading" @click="handleSubmit">确定</el-button>
  78. </template>
  79. </el-dialog>
  80. </div>
  81. </template>
  82. <script setup>
  83. import { ref, reactive, onMounted } from 'vue'
  84. import { ElMessage, ElMessageBox } from 'element-plus'
  85. import { Search, Refresh, Plus } from '@element-plus/icons-vue'
  86. import { userCategoryApi } from '@/api'
  87. import { dateTimeFormatter } from '@/utils/date'
  88. import { parsePageData } from '@/utils/pagination'
  89. const loading = ref(false)
  90. const tableData = ref([])
  91. const total = ref(0)
  92. const query = reactive({
  93. keyword: '',
  94. pageNum: 1,
  95. pageSize: 10
  96. })
  97. async function loadData() {
  98. loading.value = true
  99. try {
  100. const res = await userCategoryApi.getUserCategoryPage({
  101. keyword: query.keyword || undefined,
  102. pageNum: query.pageNum,
  103. pageSize: query.pageSize
  104. })
  105. const { records, total: pageTotal } = parsePageData(res.data)
  106. tableData.value = records
  107. total.value = pageTotal
  108. } finally {
  109. loading.value = false
  110. }
  111. }
  112. function handleSearch() {
  113. query.pageNum = 1
  114. loadData()
  115. }
  116. function handleReset() {
  117. query.keyword = ''
  118. query.pageNum = 1
  119. loadData()
  120. }
  121. const formVisible = ref(false)
  122. const formMode = ref('add')
  123. const submitLoading = ref(false)
  124. const formRef = ref()
  125. const formData = reactive({
  126. id: null,
  127. name: '',
  128. remark: ''
  129. })
  130. const formRules = {
  131. name: [
  132. { required: true, message: '请输入分类名称', trigger: 'blur' },
  133. { max: 50, message: '分类名称最长50字符', trigger: 'blur' }
  134. ]
  135. }
  136. function resetForm() {
  137. Object.assign(formData, {
  138. id: null,
  139. name: '',
  140. remark: ''
  141. })
  142. }
  143. function handleAdd() {
  144. formMode.value = 'add'
  145. resetForm()
  146. formVisible.value = true
  147. }
  148. function handleEdit(row) {
  149. formMode.value = 'edit'
  150. Object.assign(formData, {
  151. id: row.id,
  152. name: row.name,
  153. remark: row.remark || ''
  154. })
  155. formVisible.value = true
  156. }
  157. function handleFormClose() {
  158. formRef.value?.resetFields()
  159. }
  160. async function handleSubmit() {
  161. await formRef.value?.validate()
  162. submitLoading.value = true
  163. try {
  164. if (formMode.value === 'add') {
  165. await userCategoryApi.addUserCategory({ ...formData })
  166. ElMessage.success('新增用户分类成功')
  167. } else {
  168. await userCategoryApi.updateUserCategory({ ...formData })
  169. ElMessage.success('修改成功')
  170. }
  171. formVisible.value = false
  172. loadData()
  173. } finally {
  174. submitLoading.value = false
  175. }
  176. }
  177. async function handleDelete(row) {
  178. await ElMessageBox.confirm(`确认删除用户分类【${row.name}】吗?此操作不可恢复`, '警告', {
  179. confirmButtonText: '确认删除',
  180. cancelButtonText: '取消',
  181. type: 'warning'
  182. })
  183. await userCategoryApi.deleteUserCategory(row.id)
  184. ElMessage.success('删除成功')
  185. loadData()
  186. }
  187. onMounted(() => {
  188. loadData()
  189. })
  190. </script>
  191. <style lang="scss" scoped>
  192. .page-container {
  193. display: flex;
  194. flex-direction: column;
  195. gap: 16px;
  196. }
  197. .search-card :deep(.el-card__body) {
  198. padding-bottom: 0;
  199. }
  200. .toolbar {
  201. display: flex;
  202. align-items: center;
  203. margin-bottom: 16px;
  204. }
  205. .pagination {
  206. margin-top: 16px;
  207. display: flex;
  208. justify-content: flex-end;
  209. }
  210. </style>