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

     1  <script setup lang="ts">
     2  import {useI18n} from '@/hooks/web/useI18n'
     3  import {Table} from '@/components/Table'
     4  import {h, onMounted, onUnmounted, 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, ElTag} from 'element-plus'
     9  import {ApiScript, ApiStatistics} 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 Statistics from "@/components/Statistics/Statistics.vue";
    15  import {FormSchema} from "@/types/form";
    16  import {Form} from '@/components/Form'
    17  import {UUID} from "uuid-generator-ts";
    18  import stream from "@/api/stream";
    19  import {useCache} from "@/hooks/web/useCache";
    20  import {Infotip} from "@/components/Infotip";
    21  
    22  const {push, currentRoute} = useRouter()
    23  const remember = ref(false)
    24  const statistic = ref<Nullable<ApiStatistics>>(null)
    25  const {register, elFormRef, methods} = useForm()
    26  const appStore = useAppStore()
    27  const {t} = useI18n()
    28  const {wsCache} = useCache()
    29  
    30  const cachePref = 'scripts'
    31  
    32  interface TableObject {
    33    tableList: ApiScript[]
    34    params?: any
    35    loading: boolean
    36    sort?: string
    37    query?: string
    38  }
    39  
    40  interface Params {
    41    page?: number;
    42    limit?: number;
    43    sort?: string;
    44  }
    45  
    46  const tableObject = reactive<TableObject>(
    47      {
    48        tableList: [],
    49        loading: false,
    50        sort: wsCache.get(cachePref + 'Sort') || '-createdAt',
    51        query: wsCache.get(cachePref + 'Query'),
    52      },
    53  );
    54  
    55  const columns: TableColumn[] = [
    56    {
    57      field: 'id',
    58      label: t('scripts.id'),
    59      width: "60px",
    60      sortable: true
    61    },
    62    {
    63      field: 'name',
    64      label: t('scripts.name'),
    65      width: "170px",
    66      sortable: true
    67    },
    68    {
    69      field: 'lang',
    70      label: t('scripts.lang'),
    71      width: "200px",
    72      sortable: true
    73    },
    74    {
    75      field: 'description',
    76      label: t('scripts.description'),
    77      sortable: true
    78    },
    79    {
    80      field: 'createdAt',
    81      label: t('main.createdAt'),
    82      type: 'time',
    83      width: "170px",
    84      formatter: (row: ApiScript) => {
    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: ApiScript) => {
    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 getStatistic = async () => {
   113  
   114    const res = await api.v1.scriptServiceGetStatistic()
   115        .catch(() => {
   116        })
   117        .finally(() => {
   118  
   119        })
   120    if (res) {
   121      statistic.value = res.data
   122    }
   123  }
   124  
   125  const getList = async () => {
   126    getStatistic()
   127  
   128    tableObject.loading = true
   129  
   130    wsCache.set(cachePref + 'CurrentPage', paginationObj.value.currentPage)
   131    wsCache.set(cachePref + 'PageSize', paginationObj.value.pageSize)
   132    wsCache.set(cachePref + 'Sort', tableObject.sort)
   133    wsCache.set(cachePref + 'Query', tableObject.query)
   134  
   135    let params: Params = {
   136      page: paginationObj.value.currentPage,
   137      limit: paginationObj.value.pageSize,
   138      sort: tableObject.sort,
   139      query: tableObject.query || undefined,
   140    }
   141  
   142    const res = await api.v1.scriptServiceGetScriptList(params)
   143        .catch(() => {
   144        })
   145        .finally(() => {
   146          tableObject.loading = false
   147        })
   148    if (res) {
   149      const {items, meta} = res.data;
   150      tableObject.tableList = items;
   151      paginationObj.value.currentPage = meta.pagination.page;
   152      paginationObj.value.total = meta.pagination.total;
   153    } else {
   154      tableObject.tableList = [];
   155    }
   156  }
   157  
   158  onMounted(() => {
   159    const uuid = new UUID()
   160    currentID.value = uuid.getDashFreeUUID()
   161  
   162    setTimeout(() => {
   163      stream.subscribe('event_removed_script_model', currentID.value, getList)
   164      stream.subscribe('event_created_script_model', currentID.value, getList)
   165    }, 1000)
   166  })
   167  
   168  onUnmounted(() => {
   169    stream.unsubscribe('event_removed_script_model', currentID.value)
   170    stream.unsubscribe('event_created_script_model', currentID.value)
   171  })
   172  
   173  watch(
   174      () => paginationObj.value.currentPage,
   175      () => {
   176        getList()
   177      }
   178  )
   179  
   180  watch(
   181      () => paginationObj.value.pageSize,
   182      () => {
   183        getList()
   184      }
   185  )
   186  
   187  const sortChange = (data) => {
   188    const {column, prop, order} = data;
   189    const pref: string = order === 'ascending' ? '+' : '-'
   190    tableObject.sort = pref + prop
   191    getList()
   192  }
   193  
   194  getList()
   195  
   196  const addNew = () => {
   197    push('/scripts/new')
   198  }
   199  
   200  const selectRow = (row) => {
   201    if (!row) {
   202      return
   203    }
   204    const {id} = row
   205    push(`/scripts/edit/${id}`)
   206  }
   207  
   208  // search form
   209  const schema = reactive<FormSchema[]>([
   210    {
   211      field: 'name',
   212      label: t('scripts.search'),
   213      component: 'Input',
   214      colProps: {
   215        span: 24
   216      },
   217      componentProps: {
   218        placeholder: t('scripts.search'),
   219        onChange: (val: string) => {
   220          tableObject.query = val || undefined
   221          getList()
   222        }
   223      }
   224    },
   225  ])
   226  
   227  
   228  const {setValues, setSchema} = methods
   229  if (wsCache.get(cachePref + 'Query')) {
   230    setValues({
   231      name: wsCache.get(cachePref + 'Query')
   232    })
   233  }
   234  
   235  </script>
   236  
   237  <template>
   238  
   239    <Statistics v-model="statistic" :cols="6"/>
   240  
   241    <ContentWrap>
   242      <ElButton class="flex mb-20px items-left" type="primary" @click="addNew()" plain>
   243        <Icon icon="ep:plus" class="mr-5px"/>
   244        {{ t('scripts.addNew') }}
   245      </ElButton>
   246  
   247      <Infotip
   248          :show-index="false"
   249          title="INFO"
   250          :schema="[
   251        {
   252          label: t('scripts.info4'),
   253        },
   254      ]"
   255      />
   256  
   257      <Form
   258          id="search-form-scripts"
   259          :schema="schema"
   260          label-position="top"
   261          label-width="auto"
   262          hide-required-asterisk
   263          @register="register"
   264      />
   265      <Table
   266          :selection="false"
   267          v-model:pageSize="paginationObj.pageSize"
   268          v-model:currentPage="paginationObj.currentPage"
   269          :showUpPagination="20"
   270          :columns="columns"
   271          :data="tableObject.tableList"
   272          :loading="tableObject.loading"
   273          :pagination="paginationObj"
   274          @sort-change="sortChange"
   275          style="width: 100%"
   276          @current-change="selectRow"
   277      >
   278        <template #lang="{row}">
   279          <ElTag>
   280            {{ row.lang }}
   281          </ElTag>
   282        </template>
   283      </Table>
   284    </ContentWrap>
   285  </template>
   286  
   287  <style lang="less" scoped>
   288  
   289  .el-table__row {
   290    cursor: pointer;
   291  }
   292  
   293  :deep(#search-form-scripts .el-col) {
   294    padding-left: 0 !important;
   295    padding-right: 0 !important;
   296  }
   297  
   298  </style>