github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/views/Automation/Conditions/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 {Pagination, TableColumn} from '@/types/table' 6 import api from "@/api/api"; 7 import {ElButton, ElTag} from 'element-plus' 8 import {ApiCondition} from "@/api/stub"; 9 import {useRouter} from "vue-router"; 10 import {ContentWrap} from "@/components/ContentWrap"; 11 import {parseTime} from "@/utils"; 12 import {EventStateChange} from "@/api/types"; 13 import {UUID} from "uuid-generator-ts"; 14 import {useCache} from "@/hooks/web/useCache"; 15 16 const {push} = useRouter() 17 const {t} = useI18n() 18 const {wsCache} = useCache() 19 20 interface TableObject { 21 tableList: ApiCondition[] 22 params?: any 23 loading: boolean 24 sort?: string 25 } 26 27 interface Params { 28 page?: number; 29 limit?: number; 30 sort?: string; 31 } 32 33 const cachePref = 'conditions' 34 const tableObject = reactive<TableObject>( 35 { 36 tableList: [], 37 loading: false, 38 sort: wsCache.get(cachePref + 'Sort') || '-createdAt' 39 } 40 ); 41 42 const currentID = ref('') 43 44 const onStateChanged = (event: EventStateChange) => { 45 getList() 46 } 47 48 onMounted(() => { 49 const uuid = new UUID() 50 currentID.value = uuid.getDashFreeUUID() 51 52 }) 53 54 onUnmounted(() => { 55 56 }) 57 58 const columns: TableColumn[] = [ 59 { 60 field: 'id', 61 label: t('automation.conditions.id'), 62 sortable: true, 63 width: "60px" 64 }, 65 { 66 field: 'name', 67 label: t('automation.conditions.name'), 68 sortable: true, 69 width: "170px" 70 }, 71 { 72 field: 'areaId', 73 label: t('automation.area'), 74 width: "100px", 75 sortable: true, 76 formatter: (row: ApiCondition) => { 77 return h( 78 'span', 79 row.area?.name 80 ) 81 } 82 }, 83 { 84 field: 'description', 85 label: t('automation.description'), 86 sortable: true, 87 formatter: (row: ApiCondition) => { 88 return h( 89 'span', 90 row?.description || t('automation.nothing') 91 ) 92 } 93 }, 94 { 95 field: 'createdAt', 96 label: t('main.createdAt'), 97 type: 'time', 98 sortable: true, 99 width: "170px", 100 formatter: (row: ApiCondition) => { 101 return h( 102 'span', 103 parseTime(row.createdAt) 104 ) 105 } 106 }, 107 { 108 field: 'updatedAt', 109 label: t('main.updatedAt'), 110 type: 'time', 111 sortable: true, 112 width: "170px", 113 formatter: (row: ApiCondition) => { 114 return h( 115 'span', 116 parseTime(row.updatedAt) 117 ) 118 } 119 }, 120 ] 121 const paginationObj = ref<Pagination>({ 122 currentPage: wsCache.get(cachePref + 'CurrentPage') || 1, 123 pageSize: wsCache.get(cachePref + 'PageSize') || 50, 124 total: 0, 125 pageSizes: [50, 100, 150, 250], 126 }) 127 128 const getList = async () => { 129 tableObject.loading = true 130 131 wsCache.set(cachePref + 'CurrentPage', paginationObj.value.currentPage) 132 wsCache.set(cachePref + 'PageSize', paginationObj.value.pageSize) 133 wsCache.set(cachePref + 'Sort', tableObject.sort) 134 135 let params: Params = { 136 page: paginationObj.value.currentPage, 137 limit: paginationObj.value.pageSize, 138 sort: tableObject.sort, 139 } 140 141 const res = await api.v1.conditionServiceGetConditionList(params) 142 .catch(() => { 143 }) 144 .finally(() => { 145 tableObject.loading = false 146 }) 147 if (res) { 148 const {items, meta} = res.data; 149 tableObject.tableList = items; 150 paginationObj.value.currentPage = meta.pagination.page; 151 paginationObj.value.total = meta.pagination.total; 152 } else { 153 tableObject.tableList = []; 154 } 155 } 156 157 watch( 158 () => paginationObj.value.currentPage, 159 () => { 160 getList() 161 } 162 ) 163 164 watch( 165 () => paginationObj.value.pageSize, 166 () => { 167 getList() 168 } 169 ) 170 171 const sortChange = (data) => { 172 const {column, prop, order} = data; 173 const pref: string = order === 'ascending' ? '+' : '-' 174 tableObject.sort = pref + prop 175 getList() 176 } 177 178 getList() 179 180 const addNew = () => { 181 push('/automation/conditions/new') 182 } 183 184 185 const selectRow = (row) => { 186 if (!row) { 187 return 188 } 189 const {id} = row 190 push(`/automation/conditions/edit/${id}`) 191 } 192 193 </script> 194 195 <template> 196 <ContentWrap> 197 <ElButton class="flex mb-20px items-left" type="primary" @click="addNew()" plain> 198 <Icon icon="ep:plus" class="mr-5px"/> 199 {{ t('automation.conditions.addNew') }} 200 </ElButton> 201 202 <Table 203 :selection="false" 204 v-model:pageSize="paginationObj.pageSize" 205 v-model:currentPage="paginationObj.currentPage" 206 :columns="columns" 207 :data="tableObject.tableList" 208 :loading="tableObject.loading" 209 :pagination="paginationObj" 210 @sort-change="sortChange" 211 style="width: 100%" 212 :showUpPagination="20" 213 > 214 <template #name="{ row }"> 215 <span @click.prevent.stop="selectRow(row)" style="cursor: pointer"> 216 {{ row.name }} 217 </span> 218 </template> 219 220 <template #pluginName="{row}"> 221 <ElTag> 222 {{ row.pluginName }} 223 </ElTag> 224 </template> 225 </Table> 226 227 </ContentWrap> 228 229 </template> 230 231 <style lang="less"> 232 233 </style>