github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Users/index.vue (about)

     1  <script setup lang="ts">
     2  import {useI18n} from '@/hooks/web/useI18n'
     3  import {Table} from '@/components/Table'
     4  import {h, reactive, ref, watch} from 'vue'
     5  import {useAppStore} from "@/store/modules/app";
     6  import {Pagination, TableColumn} from '@/types/table'
     7  import api from "@/api/api";
     8  import {ElButton} from 'element-plus'
     9  import {ApiUserShot, ApiLog} from "@/api/stub";
    10  import {useForm} from "@/hooks/web/useForm";
    11  import {useRouter} from "vue-router";
    12  import {parseTime} from "@/utils";
    13  import {ContentWrap} from "@/components/ContentWrap";
    14  import {useCache} from "@/hooks/web/useCache";
    15  
    16  const {push, currentRoute} = useRouter()
    17  const remember = ref(false)
    18  const {register, elFormRef, methods} = useForm()
    19  const appStore = useAppStore()
    20  const {t} = useI18n()
    21  const { wsCache } = useCache()
    22  
    23  interface TableObject {
    24    tableList: ApiUserShot[]
    25    params?: any
    26    loading: boolean
    27    sort?: string
    28  }
    29  
    30  interface Params {
    31    page?: number;
    32    limit?: number;
    33    sort?: string;
    34  }
    35  
    36  const cachePref = 'users'
    37  const tableObject = reactive<TableObject>(
    38      {
    39        tableList: [],
    40        loading: false,
    41        sort: wsCache.get(cachePref+'Sort') || '-id'
    42      },
    43  );
    44  
    45  const columns: TableColumn[] = [
    46    {
    47      field: 'id',
    48      label: t('users.id'),
    49      width: "60px",
    50      sortable: true
    51    },
    52    {
    53      field: 'nickname',
    54      label: t('users.nickname'),
    55      sortable: true
    56    },
    57    {
    58      field: 'role',
    59      label: t('users.role'),
    60      sortable: true,
    61      formatter: (row: ApiUserShot) => {
    62        return h(
    63            'span',
    64            row.roleName
    65        )
    66      }
    67    },
    68    {
    69      field: 'email',
    70      label: t('users.email'),
    71      sortable: true
    72    },
    73    {
    74      field: 'status',
    75      label: t('users.status'),
    76      sortable: true
    77    },
    78    {
    79      field: 'createdAt',
    80      label: t('main.createdAt'),
    81      type: 'time',
    82      sortable: true,
    83      width: "170px",
    84      formatter: (row: ApiUserShot) => {
    85        return h(
    86            'span',
    87            parseTime(row.createdAt)
    88        )
    89      }
    90    },
    91    {
    92      field: 'updatedAt',
    93      label: t('main.updatedAt'),
    94      type: 'time',
    95      sortable: true,
    96      width: "170px",
    97      formatter: (row: ApiUserShot) => {
    98        return h(
    99            'span',
   100            parseTime(row.updatedAt)
   101        )
   102      }
   103    },
   104  ]
   105  const paginationObj = ref<Pagination>({
   106    currentPage: wsCache.get(cachePref+'CurrentPage') || 1,
   107    pageSize: wsCache.get(cachePref+'PageSize') || 50,
   108    total: 0,
   109  })
   110  const currentID = ref('')
   111  
   112  const getList = async () => {
   113    tableObject.loading = true
   114  
   115    wsCache.set(cachePref+'CurrentPage', paginationObj.value.currentPage)
   116    wsCache.set(cachePref+'PageSize', paginationObj.value.pageSize)
   117    wsCache.set(cachePref+'Sort', tableObject.sort)
   118  
   119    let params: Params = {
   120      page: paginationObj.value.currentPage,
   121      limit: paginationObj.value.pageSize,
   122      sort: tableObject.sort,
   123    }
   124  
   125    const res = await api.v1.userServiceGetUserList(params)
   126        .catch(() => {
   127        })
   128        .finally(() => {
   129          tableObject.loading = false
   130        })
   131    if (res) {
   132      const {items, meta} = res.data;
   133      tableObject.tableList = items;
   134      paginationObj.value.currentPage = meta.pagination.page;
   135      paginationObj.value.total = meta.pagination.total;
   136    } else {
   137      tableObject.tableList = [];
   138    }
   139  }
   140  
   141  watch(
   142      () => paginationObj.value.currentPage,
   143      () => {
   144        getList()
   145      }
   146  )
   147  
   148  watch(
   149      () => paginationObj.value.pageSize,
   150      () => {
   151        getList()
   152      }
   153  )
   154  
   155  const sortChange = (data) => {
   156    const {column, prop, order} = data;
   157    const pref: string = order === 'ascending' ? '+' : '-'
   158    tableObject.sort = pref + prop
   159    getList()
   160  }
   161  
   162  getList()
   163  
   164  const addNew = () => {
   165    push('/etc/users/new')
   166  }
   167  
   168  const selectRow = (row) => {
   169    if (!row) {
   170      return
   171    }
   172    const {id} = row
   173    push(`/etc/users/edit/${id}`)
   174  }
   175  
   176  </script>
   177  
   178  <template>
   179  
   180    <ContentWrap>
   181    <ElButton class="flex mb-20px items-left" type="primary" @click="addNew()" plain>
   182      <Icon icon="ep:plus" class="mr-5px"/>
   183      {{ t('users.addNew') }}
   184    </ElButton>
   185    <Table
   186        :selection="false"
   187        v-model:pageSize="paginationObj.pageSize"
   188        v-model:currentPage="paginationObj.currentPage"
   189        :showUpPagination="20"
   190        :columns="columns"
   191        :data="tableObject.tableList"
   192        :loading="tableObject.loading"
   193        :pagination="paginationObj"
   194        @sort-change="sortChange"
   195        style="width: 100%"
   196        @current-change="selectRow"
   197    />
   198    </ContentWrap>
   199  
   200  </template>
   201  
   202  <style lang="less">
   203  
   204  .el-table__row {
   205    cursor: pointer;
   206  }
   207  </style>