device.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <script setup lang="ts">
  2. import { reactive, onMounted, ref, nextTick, watch } from "vue";
  3. import { getStationDeviceList, removeDevice, getStationList } from "@/api/station";
  4. import { useRenderIcon } from "@/components/ReIcon/src/hooks";
  5. import { useRoute, useRouter } from "vue-router";
  6. import { ElMessage, ElMessageBox } from "element-plus";
  7. import { ExtDLabel, ExtDSelect } from "@/components/ExtForm";
  8. import DeviceDialog from "./device-dialog.vue";
  9. defineOptions({
  10. name: "AdminStationDevice"
  11. });
  12. const route = useRoute();
  13. const router = useRouter();
  14. const queryRef = ref();
  15. const tableRef = ref();
  16. const dialogRef = ref();
  17. const state = reactive({
  18. stationId: (route.query.stationId as string) || "",
  19. stationOptions: [] as Array<{ stationId: string; stationName: string }>,
  20. formQuery: {
  21. stationId: "",
  22. shortId: "",
  23. deviceConfigName: "",
  24. productKey: "",
  25. deviceName: "",
  26. state: "",
  27. hasFoam: "",
  28. hasWater: ""
  29. },
  30. pageQuery: {
  31. pageNum: 1,
  32. pageSize: 10,
  33. total: 0
  34. },
  35. tableData: {
  36. height: 500,
  37. data: [] as Array<any>,
  38. loading: false,
  39. columns: [
  40. { label: "设备编号", prop: "shortId", width: 100 },
  41. { label: "站点名称", prop: "stationName", width: 180 },
  42. { label: "设备配置名", prop: "deviceConfigName", width: 120 },
  43. { label: "ProductKey", prop: "productKey", width: 180 },
  44. { label: "DeviceName", prop: "deviceName", width: 180 },
  45. { label: "状态", prop: "state", width: 90 },
  46. { label: "运行时长", prop: "uptimeMs", width: 120 },
  47. { label: "功能", prop: "functions", width: 180 },
  48. { label: "是否有泡沫", prop: "hasFoam", width: 100 },
  49. { label: "是否有水", prop: "hasWater", width: 100 },
  50. { label: "创建时间", prop: "createTime", width: 160 },
  51. { label: "更新时间", prop: "updateTime", width: 160 }
  52. ]
  53. }
  54. });
  55. watch(
  56. () => route.query.stationId,
  57. (newStationId) => {
  58. if (newStationId) {
  59. state.stationId = newStationId as string;
  60. state.formQuery.stationId = newStationId as string;
  61. loadData(true);
  62. }
  63. }
  64. );
  65. onMounted(() => {
  66. loadStationOptions();
  67. if (state.stationId) {
  68. state.formQuery.stationId = state.stationId;
  69. }
  70. loadData();
  71. nextTick(() => {
  72. const bodyHeight = document.body.clientHeight;
  73. const queryHeight = queryRef.value?.$el?.clientHeight || 0;
  74. state.tableData.height = bodyHeight - queryHeight - 280;
  75. });
  76. });
  77. const loadStationOptions = () => {
  78. getStationList({ pageSize: 1000 }).then((res: any) => {
  79. const { list } = res || {};
  80. state.stationOptions = list || [];
  81. });
  82. };
  83. const loadData = (refresh: boolean = false) => {
  84. if (refresh) {
  85. state.pageQuery.pageNum = 1;
  86. }
  87. state.tableData.loading = true;
  88. getStationDeviceList({
  89. ...state.formQuery,
  90. ...state.pageQuery
  91. })
  92. .then((res: any) => {
  93. const { list, total } = res || {};
  94. state.tableData.data = list || [];
  95. state.pageQuery.total = total || 0;
  96. })
  97. .catch(() => {
  98. state.tableData.data = [];
  99. })
  100. .finally(() => {
  101. state.tableData.loading = false;
  102. });
  103. };
  104. const handleSizeChange = (size: number) => {
  105. state.pageQuery.pageSize = size;
  106. loadData(true);
  107. };
  108. const handleCurrentChange = (page: number) => {
  109. state.pageQuery.pageNum = page;
  110. loadData();
  111. };
  112. const handleSearch = () => {
  113. loadData(true);
  114. };
  115. const handleReset = () => {
  116. state.formQuery = {
  117. stationId: state.stationId || "",
  118. shortId: "",
  119. deviceConfigName: "",
  120. productKey: "",
  121. deviceName: "",
  122. state: "",
  123. hasFoam: "",
  124. hasWater: ""
  125. };
  126. loadData(true);
  127. };
  128. const handleBack = () => {
  129. router.push("/admin/station/list");
  130. };
  131. const handleAdd = () => {
  132. dialogRef.value?.open("add", { stationId: state.formQuery.stationId });
  133. };
  134. const handleEdit = (row: any) => {
  135. dialogRef.value?.open("edit", row);
  136. };
  137. const handleDelete = (row: any) => {
  138. ElMessageBox.confirm(`此操作将永久删除设备『${row.shortId}』,是否继续?`, "提示", {
  139. confirmButtonText: "确定",
  140. cancelButtonText: "取消",
  141. type: "warning"
  142. }).then(() => {
  143. removeDevice(row.id)
  144. .then(() => {
  145. ElMessage.success("删除成功");
  146. loadData(true);
  147. })
  148. .catch(() => {
  149. ElMessage.error("删除失败");
  150. });
  151. });
  152. };
  153. const formatDuration = (ms: number) => {
  154. if (!ms) return "-";
  155. const seconds = Math.floor(ms / 1000);
  156. const days = Math.floor(seconds / 86400);
  157. const hours = Math.floor((seconds % 86400) / 3600);
  158. const minutes = Math.floor((seconds % 3600) / 60);
  159. const secs = seconds % 60;
  160. const parts = [];
  161. if (days > 0) parts.push(`${days}天`);
  162. if (hours > 0) parts.push(`${hours}时`);
  163. if (minutes > 0) parts.push(`${minutes}分`);
  164. if (secs > 0 || parts.length === 0) parts.push(`${secs}秒`);
  165. return parts.join("");
  166. };
  167. </script>
  168. <template>
  169. <div class="page-container">
  170. <el-card shadow="hover">
  171. <template #header>
  172. <div class="card-header">
  173. <span>设备清单</span>
  174. <el-button :icon="useRenderIcon('ri/arrow-left-line')" @click="handleBack">
  175. 返回站点列表
  176. </el-button>
  177. </div>
  178. </template>
  179. <el-form
  180. ref="queryRef"
  181. :model="state.formQuery"
  182. inline
  183. class="search-form"
  184. >
  185. <el-form-item label="站点">
  186. <el-select
  187. v-model="state.formQuery.stationId"
  188. placeholder="请选择站点"
  189. clearable
  190. filterable
  191. @change="handleSearch"
  192. >
  193. <el-option
  194. v-for="item in state.stationOptions"
  195. :key="item.stationId"
  196. :label="item.stationName"
  197. :value="item.stationId"
  198. />
  199. </el-select>
  200. </el-form-item>
  201. <el-form-item label="设备编号">
  202. <el-input
  203. v-model="state.formQuery.shortId"
  204. placeholder="请输入设备编号"
  205. clearable
  206. @keyup.enter="handleSearch"
  207. />
  208. </el-form-item>
  209. <el-form-item label="设备配置名">
  210. <el-input
  211. v-model="state.formQuery.deviceConfigName"
  212. placeholder="请输入设备配置名"
  213. clearable
  214. @keyup.enter="handleSearch"
  215. />
  216. </el-form-item>
  217. <el-form-item label="状态">
  218. <ExtDSelect
  219. v-model="state.formQuery.state"
  220. type="Device.state"
  221. placeholder="请选择状态"
  222. @on-change="handleSearch"
  223. />
  224. </el-form-item>
  225. <el-form-item label="是否有泡沫">
  226. <ExtDSelect
  227. v-model="state.formQuery.hasFoam"
  228. type="YesNo"
  229. placeholder="请选择"
  230. @on-change="handleSearch"
  231. />
  232. </el-form-item>
  233. <el-form-item label="是否有水">
  234. <ExtDSelect
  235. v-model="state.formQuery.hasWater"
  236. type="YesNo"
  237. placeholder="请选择"
  238. @on-change="handleSearch"
  239. />
  240. </el-form-item>
  241. <el-form-item>
  242. <el-button
  243. type="primary"
  244. :icon="useRenderIcon('ri/search-line')"
  245. @click="handleSearch"
  246. >
  247. 查询
  248. </el-button>
  249. <el-button
  250. :icon="useRenderIcon('ri/refresh-line')"
  251. @click="handleReset"
  252. >
  253. 重置
  254. </el-button>
  255. <el-button
  256. type="success"
  257. :icon="useRenderIcon('ri/add-line')"
  258. @click="handleAdd"
  259. >
  260. 新增
  261. </el-button>
  262. </el-form-item>
  263. </el-form>
  264. <el-table
  265. ref="tableRef"
  266. v-loading="state.tableData.loading"
  267. :data="state.tableData.data"
  268. :height="state.tableData.height"
  269. border
  270. stripe
  271. >
  272. <template #empty>
  273. <el-empty description="暂无数据" />
  274. </template>
  275. <el-table-column
  276. v-for="col in state.tableData.columns"
  277. :key="col.prop"
  278. :prop="col.prop"
  279. :label="col.label"
  280. :width="col.width"
  281. show-overflow-tooltip
  282. >
  283. <template #default="{ row }">
  284. <template v-if="col.prop === 'stationName'">
  285. <div class="station-name-cell">
  286. <div class="station-id">{{ row.stationId }}</div>
  287. <el-divider direction="horizontal" />
  288. <div class="station-name">{{ row.stationName }}</div>
  289. </div>
  290. </template>
  291. <template v-else-if="col.prop === 'state'">
  292. <ExtDLabel type="Device.state" :model-value="row.state" />
  293. </template>
  294. <template v-else-if="col.prop === 'uptimeMs'">
  295. {{ formatDuration(row.uptimeMs) }}
  296. </template>
  297. <template v-else-if="col.prop === 'hasFoam' || col.prop === 'hasWater'">
  298. <ExtDLabel type="YesNo" :model-value="row[col.prop]" />
  299. </template>
  300. <template v-else>
  301. {{ row[col.prop] }}
  302. </template>
  303. </template>
  304. </el-table-column>
  305. <el-table-column label="操作" width="180" fixed="right">
  306. <template #default="{ row }">
  307. <el-button type="warning" link size="small" @click="handleEdit(row)">
  308. 编辑
  309. </el-button>
  310. <el-button type="primary" link size="small">
  311. 结算
  312. </el-button>
  313. <el-button type="danger" link size="small" @click="handleDelete(row)">
  314. 删除
  315. </el-button>
  316. </template>
  317. </el-table-column>
  318. </el-table>
  319. <div class="pagination-container">
  320. <el-pagination
  321. v-model:current-page="state.pageQuery.pageNum"
  322. v-model:page-size="state.pageQuery.pageSize"
  323. :total="state.pageQuery.total"
  324. :page-sizes="[10, 20, 50, 100]"
  325. layout="total, sizes, prev, pager, next, jumper"
  326. @size-change="handleSizeChange"
  327. @current-change="handleCurrentChange"
  328. />
  329. </div>
  330. </el-card>
  331. <DeviceDialog ref="dialogRef" @refresh="loadData(true)" />
  332. </div>
  333. </template>
  334. <style scoped lang="scss">
  335. .page-container {
  336. padding: 15px;
  337. }
  338. .card-header {
  339. display: flex;
  340. justify-content: space-between;
  341. align-items: center;
  342. }
  343. .search-form {
  344. margin-bottom: 15px;
  345. }
  346. .pagination-container {
  347. display: flex;
  348. justify-content: flex-end;
  349. margin-top: 15px;
  350. }
  351. .station-name-cell {
  352. text-align: center;
  353. .station-id {
  354. color: #909399;
  355. font-size: 12px;
  356. }
  357. .station-name {
  358. font-weight: 500;
  359. }
  360. .el-divider {
  361. margin: 4px 0;
  362. }
  363. }
  364. </style>