list.vue 16 KB

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