list.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. <template>
  2. <view class="device-list-container">
  3. <view class="search-bar">
  4. <view class="search-input-wrap">
  5. <AppIcon name="search" size="16" color="#999999" />
  6. <input
  7. type="text"
  8. placeholder="请输入设备名称或编号"
  9. v-model="searchKeyword"
  10. @confirm="handleSearch"
  11. />
  12. </view>
  13. <button class="search-btn" @click="handleSearch">搜索</button>
  14. </view>
  15. <view class="filter-bar">
  16. <view class="segmented-control">
  17. <view
  18. v-for="(option, index) in filterOptions"
  19. :key="index"
  20. class="segment-item"
  21. :class="{ active: activeFilter === index }"
  22. @click="activeFilter = index; handleFilterChange(index)"
  23. >
  24. <text>{{ option.label }}</text>
  25. </view>
  26. </view>
  27. </view>
  28. <view class="device-list">
  29. <view
  30. v-for="device in deviceList"
  31. :key="device.id || device.deviceId"
  32. class="device-item"
  33. @click="viewDeviceDetail(getDeviceId(device))"
  34. >
  35. <view class="device-header">
  36. <view class="device-info">
  37. <text class="device-name">
  38. {{ device.name }}
  39. <text v-if="device.hasPa" class="pa-badge-list">PA</text>
  40. </text>
  41. <view class="device-meta">
  42. <text class="device-id">ID: {{ device.shortId }}</text>
  43. <view class="device-status" :style="getDeviceStatusStyle(getDeviceStatusValue(device))">
  44. <text class="status-dot"></text>
  45. <text>{{ getDeviceStatusText(getDeviceStatusValue(device)) }}</text>
  46. </view>
  47. <view class="device-busy-tag" :class="getDeviceBusyStatusClass(device)">
  48. <text>{{ getDeviceBusyStatus(device) }}</text>
  49. </view>
  50. </view>
  51. </view>
  52. <button
  53. class="config-btn"
  54. @click.stop="toggleConfigDropdown(getDeviceId(device))"
  55. >
  56. 设备配置
  57. </button>
  58. </view>
  59. <view class="device-body">
  60. <view class="device-row">
  61. <text class="row-label">所属站点</text>
  62. <text class="row-value">{{ device.stationName || '未分配站点' }}</text>
  63. </view>
  64. <view class="device-row">
  65. <text class="row-label">价格配置</text>
  66. <text class="row-value">{{ device.deviceConfigName || '未绑定配置' }}</text>
  67. </view>
  68. </view>
  69. <view class="device-footer" v-if="isDeviceOnline(getDeviceStatusValue(device))">
  70. <button
  71. class="stop-btn"
  72. @click.stop="handleStopDevice(device)"
  73. >
  74. 停止设备
  75. </button>
  76. </view>
  77. <view v-if="showConfigDropdown === getDeviceId(device)" class="config-dropdown">
  78. <view class="dropdown-header">
  79. <text class="dropdown-title">选择配置</text>
  80. <view class="dropdown-close" @click.stop="closeConfigDropdown">
  81. <AppIcon name="x" size="16" color="#999999" />
  82. </view>
  83. </view>
  84. <view class="dropdown-content">
  85. <view
  86. v-for="config in configList"
  87. :key="config.id"
  88. class="config-option"
  89. @click.stop="selectConfig(getDeviceId(device), config.id, config.name)"
  90. >
  91. <text class="config-option-name">{{ config.name }}</text>
  92. <text class="config-option-desc">{{ config.remark || '无描述' }}</text>
  93. </view>
  94. <view v-if="configList.length === 0" class="no-config">
  95. <text>暂无配置,请先添加</text>
  96. </view>
  97. </view>
  98. </view>
  99. </view>
  100. </view>
  101. <view class="load-more" v-if="deviceList.length > 0">
  102. <text v-if="loadMoreStatus === 'loading'">正在加载...</text>
  103. <text v-else-if="loadMoreStatus === 'noMore'">— 没有更多数据了 —</text>
  104. <text v-else>上拉加载更多</text>
  105. </view>
  106. <view class="empty-state" v-if="deviceList.length === 0 && !loading">
  107. <AppIcon name="smartphone" size="48" color="#BFBFBF" />
  108. <text class="empty-text">暂无设备数据</text>
  109. <button class="empty-refresh-btn" @click="loadDeviceList">刷新</button>
  110. </view>
  111. <view class="loading-state" v-if="loading && deviceList.length === 0">
  112. <view class="loading-spinner"></view>
  113. <text class="loading-text">加载中...</text>
  114. </view>
  115. </view>
  116. </template>
  117. <script setup>
  118. import { ref, onMounted, computed } from 'vue'
  119. import { onReachBottom } from '@dcloudio/uni-app'
  120. import { getDeviceList, stopDevice, getDeviceConfigList, batchModifyDeviceConfig } from '../../api/device.js'
  121. import { showToast, fmtDictName, getDictColor } from '../../utils/index.js'
  122. import dictUtil, { loadDicts } from '../../utils/dict.js'
  123. const deviceList = ref([])
  124. const loading = ref(true)
  125. const page = ref(1)
  126. const pageSize = ref(10)
  127. const hasMore = ref(true)
  128. const loadMoreStatus = ref('more')
  129. const activeFilter = ref(0)
  130. const searchKeyword = ref('')
  131. const configList = ref([])
  132. const showConfigDropdown = ref(null)
  133. const loadingConfig = ref(false)
  134. const filterOptions = computed(() => dictUtil.getDictFilterOptions('WashDevice.status'))
  135. onMounted(async () => {
  136. await loadDicts()
  137. loadDeviceList()
  138. })
  139. const loadDeviceList = async (isLoadMore = false) => {
  140. if (!isLoadMore) {
  141. page.value = 1
  142. deviceList.value = []
  143. loading.value = true
  144. }
  145. try {
  146. const params = {
  147. page: page.value,
  148. pageSize: pageSize.value,
  149. keyword: searchKeyword.value,
  150. status: activeFilter.value === 0 ? '' : getStatusValue(activeFilter.value)
  151. }
  152. const res = await getDeviceList(params)
  153. if (res && res.code === 200) {
  154. const data = res.data
  155. const records = data.records || data.list || data
  156. const totalPages = data.pages || data.totalPages || 1
  157. if (isLoadMore) {
  158. deviceList.value = [...deviceList.value, ...records]
  159. } else {
  160. deviceList.value = records
  161. }
  162. hasMore.value = page.value < totalPages
  163. loadMoreStatus.value = hasMore.value ? 'more' : 'noMore'
  164. if (isLoadMore) page.value++
  165. }
  166. } catch (error) {
  167. showToast('获取设备列表失败')
  168. } finally {
  169. loading.value = false
  170. }
  171. }
  172. const loadMore = () => {
  173. if (hasMore.value && !loading.value && loadMoreStatus.value !== 'loading') {
  174. loadMoreStatus.value = 'loading'
  175. loadDeviceList(true)
  176. }
  177. }
  178. onReachBottom(() => {
  179. loadMore()
  180. })
  181. const handleSearch = () => {
  182. loadDeviceList()
  183. }
  184. const handleFilterChange = (index) => {
  185. activeFilter.value = index
  186. loadDeviceList()
  187. }
  188. const viewDeviceDetail = (deviceId) => {
  189. uni.navigateTo({ url: `/pages/device/detail?id=${deviceId}` })
  190. }
  191. const handleStopDevice = (device) => {
  192. uni.showModal({
  193. title: '停止设备',
  194. content: `确定要停止设备「${device.name || device.shortId}」吗?`,
  195. success: async (res) => {
  196. if (res.confirm) {
  197. try {
  198. await stopDevice(device.shortId)
  199. showToast('设备已停止', 'success')
  200. loadDeviceList()
  201. } catch (error) {
  202. showToast('停止设备失败')
  203. }
  204. }
  205. }
  206. })
  207. }
  208. const loadConfigList = async () => {
  209. if (loadingConfig.value) return
  210. loadingConfig.value = true
  211. try {
  212. const res = await getDeviceConfigList({ page: 1, pageSize: 1000 })
  213. if (res && res.code === 200) {
  214. const data = res.data
  215. configList.value = data.list || data.records || (Array.isArray(data) ? data : [])
  216. }
  217. } catch (error) {
  218. showToast('无法连接服务器')
  219. } finally {
  220. loadingConfig.value = false
  221. }
  222. }
  223. const toggleConfigDropdown = (deviceId) => {
  224. if (showConfigDropdown.value === deviceId) {
  225. closeConfigDropdown()
  226. } else {
  227. loadConfigList()
  228. showConfigDropdown.value = deviceId
  229. }
  230. }
  231. const closeConfigDropdown = () => {
  232. showConfigDropdown.value = null
  233. }
  234. const selectConfig = async (deviceId, configId, configName) => {
  235. try {
  236. const res = await batchModifyDeviceConfig({
  237. deviceIds: [deviceId],
  238. deviceConfigId: configId
  239. })
  240. if (res && res.code === 200) {
  241. showToast(`成功绑定: ${configName}`, 'success')
  242. closeConfigDropdown()
  243. setTimeout(() => loadDeviceList(), 500)
  244. } else {
  245. showToast(res.message || '绑定失败')
  246. }
  247. } catch (error) {
  248. showToast('网络连接失败')
  249. }
  250. }
  251. const getDeviceId = (device) => {
  252. if (!device) return null
  253. return device.id || device.deviceId || device.device_id || device.shortId || null
  254. }
  255. const getDeviceStatusValue = (device) => {
  256. if (!device) return null
  257. return device.status ?? device.state ?? device.stateCode ?? device.deviceStatus ?? null
  258. }
  259. const getDeviceStatusText = (status) => fmtDictName('WashDevice.status', status)
  260. const getDeviceBusyStatus = (device) => {
  261. const status = getDeviceStatusValue(device)
  262. const onlineValue = dictUtil.getDictValue('WashDevice.status', '在线')
  263. if (status != onlineValue) return ''
  264. const hasCurrentOrder = device.isBusy || device.currentOrder || device.workingStatus === 'BUSY'
  265. return hasCurrentOrder || (device.todayOrders > 0)
  266. ? dictUtil.getDictLabel('deviceBusyStatus', '1')
  267. : dictUtil.getDictLabel('deviceBusyStatus', '0')
  268. }
  269. const getDeviceBusyStatusClass = (device) => {
  270. const label = getDeviceBusyStatus(device)
  271. if (!label) return ''
  272. return label !== dictUtil.getDictLabel('deviceBusyStatus', '0') ? 'busy' : 'idle'
  273. }
  274. const getDeviceStatusStyle = (status) => {
  275. const color = getDictColor('WashDevice.status', status)
  276. if (color) return { color: color, backgroundColor: `${color}1A` }
  277. return {}
  278. }
  279. const isDeviceOnline = (status) => {
  280. const onlineValue = dictUtil.getDictValue('WashDevice.status', '在线')
  281. return status == onlineValue
  282. }
  283. const getStatusValue = (filterIndex) => {
  284. if (filterIndex === 0) return ''
  285. const options = dictUtil.getDictOptions('WashDevice.status')
  286. const idx = filterIndex - 1
  287. return options[idx] ? options[idx].value : ''
  288. }
  289. </script>
  290. <style scoped>
  291. .device-list-container {
  292. padding: 20rpx 28rpx;
  293. background-color: #F5F7FA;
  294. min-height: 100vh;
  295. box-sizing: border-box;
  296. padding-bottom: 100rpx;
  297. }
  298. /* ===== Search Bar ===== */
  299. .search-bar {
  300. display: flex;
  301. gap: 16rpx;
  302. margin-bottom: 20rpx;
  303. }
  304. .search-input-wrap {
  305. flex: 1;
  306. display: flex;
  307. align-items: center;
  308. gap: 12rpx;
  309. background: #F5F5F5;
  310. border-radius: 16rpx;
  311. padding: 0 24rpx;
  312. }
  313. .search-input-wrap input {
  314. flex: 1;
  315. height: 72rpx;
  316. border: none;
  317. outline: none;
  318. font-size: 28rpx;
  319. color: #1A1A1A;
  320. background: transparent;
  321. }
  322. .search-btn {
  323. height: 72rpx;
  324. line-height: 72rpx;
  325. padding: 0 36rpx;
  326. background: #C6171E;
  327. color: #FFFFFF;
  328. border: none;
  329. border-radius: 16rpx;
  330. font-size: 28rpx;
  331. font-weight: 500;
  332. transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  333. }
  334. .search-btn:active {
  335. background: #A81212;
  336. transform: scale(0.97);
  337. }
  338. /* ===== Filter Bar ===== */
  339. .filter-bar {
  340. margin-bottom: 20rpx;
  341. }
  342. .segmented-control {
  343. display: flex;
  344. background: #FFFFFF;
  345. border-radius: 16rpx;
  346. padding: 6rpx;
  347. gap: 6rpx;
  348. }
  349. .segment-item {
  350. flex: 1;
  351. text-align: center;
  352. padding: 16rpx 0;
  353. font-size: 26rpx;
  354. color: #666666;
  355. border-radius: 12rpx;
  356. font-weight: 500;
  357. transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  358. }
  359. .segment-item.active {
  360. color: #FFFFFF;
  361. font-weight: 600;
  362. background: #C6171E;
  363. box-shadow: 0 4rpx 12rpx rgba(198, 23, 30, 0.3);
  364. }
  365. /* ===== Device List ===== */
  366. .device-list {
  367. display: flex;
  368. flex-direction: column;
  369. gap: 16rpx;
  370. }
  371. .device-item {
  372. background: #FFFFFF;
  373. border-radius: 20rpx;
  374. padding: 24rpx;
  375. position: relative;
  376. transition: box-shadow 0.25s cubic-bezier(0.4, 0, 0.2, 1), transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  377. }
  378. .device-item:active {
  379. transform: translateY(-2rpx);
  380. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.06);
  381. }
  382. .device-header {
  383. display: flex;
  384. justify-content: space-between;
  385. align-items: flex-start;
  386. gap: 16rpx;
  387. margin-bottom: 18rpx;
  388. }
  389. .device-info {
  390. flex: 1;
  391. min-width: 0;
  392. }
  393. .device-name {
  394. font-size: 30rpx;
  395. font-weight: 600;
  396. color: #1A1A1A;
  397. display: block;
  398. margin-bottom: 10rpx;
  399. }
  400. .pa-badge-list {
  401. display: inline-block;
  402. margin-left: 10rpx;
  403. padding: 2rpx 10rpx;
  404. background: #C6171E;
  405. color: #FFFFFF;
  406. border-radius: 6rpx;
  407. font-size: 20rpx;
  408. font-weight: 700;
  409. vertical-align: middle;
  410. }
  411. .device-meta {
  412. display: flex;
  413. align-items: center;
  414. gap: 12rpx;
  415. flex-wrap: wrap;
  416. }
  417. .device-id {
  418. font-size: 24rpx;
  419. color: #999999;
  420. }
  421. .device-status {
  422. display: flex;
  423. align-items: center;
  424. font-size: 22rpx;
  425. font-weight: 600;
  426. padding: 4rpx 16rpx;
  427. border-radius: 100px;
  428. white-space: nowrap;
  429. }
  430. .status-dot {
  431. display: inline-block;
  432. width: 10rpx;
  433. height: 10rpx;
  434. border-radius: 50%;
  435. margin-right: 6rpx;
  436. background-color: currentColor;
  437. }
  438. .device-busy-tag {
  439. display: flex;
  440. align-items: center;
  441. font-size: 20rpx;
  442. font-weight: 600;
  443. padding: 4rpx 14rpx;
  444. border-radius: 100px;
  445. flex-shrink: 0;
  446. }
  447. .device-busy-tag.busy {
  448. color: #F44336;
  449. background-color: rgba(244, 67, 54, 0.08);
  450. }
  451. .device-busy-tag.idle {
  452. color: #52C41A;
  453. background-color: rgba(82, 196, 26, 0.08);
  454. }
  455. /* ===== Device Body ===== */
  456. .device-body {
  457. padding: 14rpx 0;
  458. border-top: 1px solid #F0F0F0;
  459. border-bottom: 1px solid #F0F0F0;
  460. margin-bottom: 16rpx;
  461. }
  462. .device-row {
  463. display: flex;
  464. align-items: center;
  465. padding: 6rpx 0;
  466. }
  467. .row-label {
  468. font-size: 24rpx;
  469. color: #999999;
  470. width: 120rpx;
  471. flex-shrink: 0;
  472. }
  473. .row-value {
  474. font-size: 24rpx;
  475. color: #1A1A1A;
  476. font-weight: 500;
  477. flex: 1;
  478. white-space: nowrap;
  479. overflow: hidden;
  480. text-overflow: ellipsis;
  481. }
  482. /* ===== Device Footer ===== */
  483. .device-footer {
  484. padding-top: 16rpx;
  485. border-top: 1px solid #F0F0F0;
  486. }
  487. .stop-btn {
  488. width: 100%;
  489. padding: 14rpx 0;
  490. background: #FFFFFF;
  491. color: #1A1A1A;
  492. border: 1px solid #E0E0E0;
  493. border-radius: 12rpx;
  494. font-size: 24rpx;
  495. font-weight: 500;
  496. transition: border-color 0.25s, color 0.25s;
  497. }
  498. .stop-btn:active {
  499. border-color: #C6171E;
  500. color: #C6171E;
  501. }
  502. /* ===== Config Button (header top-right) ===== */
  503. .config-btn {
  504. padding: 8rpx 20rpx;
  505. background: #C6171E;
  506. color: #FFFFFF;
  507. border: none;
  508. border-radius: 12rpx;
  509. font-size: 22rpx;
  510. font-weight: 500;
  511. flex-shrink: 0;
  512. transition: background 0.25s, transform 0.15s;
  513. }
  514. .config-btn:active {
  515. background: #A81212;
  516. transform: scale(0.97);
  517. }
  518. /* ===== Config Dropdown ===== */
  519. .config-dropdown {
  520. position: absolute;
  521. top: 60rpx;
  522. right: 0;
  523. width: 340rpx;
  524. background: #FFFFFF;
  525. border-radius: 16rpx;
  526. box-shadow: 0 10px 15px rgba(0, 0, 0, 0.08), 0 4px 6px rgba(0, 0, 0, 0.05);
  527. z-index: 100;
  528. overflow: hidden;
  529. }
  530. .dropdown-header {
  531. display: flex;
  532. justify-content: space-between;
  533. align-items: center;
  534. padding: 18rpx 22rpx;
  535. background: #F5F7FA;
  536. border-bottom: 1px solid #F0F0F0;
  537. }
  538. .dropdown-title {
  539. font-size: 26rpx;
  540. font-weight: 600;
  541. color: #1A1A1A;
  542. }
  543. .dropdown-close {
  544. padding: 4rpx;
  545. }
  546. .dropdown-content {
  547. max-height: 360rpx;
  548. overflow-y: auto;
  549. }
  550. .config-option {
  551. padding: 18rpx 22rpx;
  552. border-bottom: 1px solid #F0F0F0;
  553. transition: background 0.2s;
  554. }
  555. .config-option:last-child {
  556. border-bottom: none;
  557. }
  558. .config-option:active {
  559. background: #F5F7FA;
  560. }
  561. .config-option-name {
  562. font-size: 26rpx;
  563. font-weight: 500;
  564. color: #1A1A1A;
  565. display: block;
  566. margin-bottom: 4rpx;
  567. }
  568. .config-option-desc {
  569. font-size: 22rpx;
  570. color: #999999;
  571. display: block;
  572. }
  573. .no-config {
  574. padding: 40rpx 20rpx;
  575. text-align: center;
  576. color: #999999;
  577. font-size: 24rpx;
  578. }
  579. /* ===== Load More ===== */
  580. .load-more {
  581. text-align: center;
  582. padding: 32rpx;
  583. color: #666666;
  584. font-size: 26rpx;
  585. margin-top: 24rpx;
  586. }
  587. /* ===== Empty ===== */
  588. .empty-state {
  589. display: flex;
  590. flex-direction: column;
  591. align-items: center;
  592. justify-content: center;
  593. padding: 120rpx 0;
  594. }
  595. .empty-text {
  596. font-size: 28rpx;
  597. color: #999999;
  598. margin: 24rpx 0 32rpx;
  599. }
  600. .empty-refresh-btn {
  601. padding: 16rpx 48rpx;
  602. background: #C6171E;
  603. color: #FFFFFF;
  604. border: none;
  605. border-radius: 44rpx;
  606. font-size: 28rpx;
  607. font-weight: 500;
  608. }
  609. /* ===== Loading ===== */
  610. .loading-state {
  611. display: flex;
  612. flex-direction: column;
  613. align-items: center;
  614. padding: 120rpx 0;
  615. }
  616. .loading-spinner {
  617. width: 56rpx;
  618. height: 56rpx;
  619. border: 4rpx solid #F0F0F0;
  620. border-top-color: #C6171E;
  621. border-radius: 50%;
  622. animation: spin 0.8s linear infinite;
  623. }
  624. @keyframes spin {
  625. to { transform: rotate(360deg); }
  626. }
  627. .loading-text {
  628. font-size: 26rpx;
  629. color: #999999;
  630. margin-top: 20rpx;
  631. }
  632. </style>