index.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <template>
  2. <view class="container">
  3. <!-- 顶部搜索与筛选 -->
  4. <view class="search-bar">
  5. <input v-model="filters.search" placeholder="搜索:名称 / 编号 / 所属农场" class="search-input" />
  6. <picker mode="selector" :range="machineTypeDisplayOptions" @change="onTypeChange" class="filter-picker">
  7. <view class="filter-label">类型:{{ selectedTypeLabel }}</view>
  8. </picker>
  9. <picker mode="selector" :range="onlineStatusDisplayOptions" @change="onStatusChange" class="filter-picker">
  10. <view class="filter-label">状态:{{ selectedStatusLabel }}</view>
  11. </picker>
  12. <button @click="onSearch" class="search-btn">搜索</button>
  13. </view>
  14. <!-- 列表 -->
  15. <scroll-view scroll-y class="list-wrapper" :style="{height: listHeight+'px'}">
  16. <view v-if="machines.length === 0 && !loading" class="empty-tip">暂无农机数据</view>
  17. <view v-for="machine in machines" :key="machine.id" class="machine-card" @click="navigateToDetail(machine)">
  18. <image :src="machine.imageUrl || '/static/icons/machine_default.png'" class="machine-image" mode="aspectFill" />
  19. <view class="machine-info">
  20. <view class="row">
  21. <text class="machine-name">{{ machine.machineName }}</text>
  22. <text class="machine-code">({{ machine.machineCode }})</text>
  23. </view>
  24. <view class="row meta-row">
  25. <text class="meta-item">类型:{{ getMachineTypeLabel(machine.machineType) }}</text>
  26. <text class="meta-item">农场:{{ machine.deptName || '-' }}</text>
  27. <text class="meta-item">地块:{{ machine.currentField || '-' }}</text>
  28. </view>
  29. <view class="row status-row">
  30. <view :class="['status-badge', onlineClass(machine.onlineStatus)]">{{ onlineStatusLabel(machine.onlineStatus) }}</view>
  31. <text class="meta-item">保养:{{ maintenanceLabel(machine.maintenanceStatus) }}</text>
  32. <text class="meta-item">定位:{{ locationLabel(machine.locationStatus) }}</text>
  33. <text class="alarm-count">告警:{{ machine.alarmCount || 0 }}</text>
  34. </view>
  35. <view class="row footer-row">
  36. <text class="manager">负责人:{{ machine.managerName || '-' }}</text>
  37. <text class="date">启用:{{ formatDate(machine.purchaseDate) }}</text>
  38. </view>
  39. </view>
  40. </view>
  41. <view v-if="loading" class="loading-tip">加载中...</view>
  42. <!-- 分页/加载更多 -->
  43. <button v-if="!allLoaded && !loading" @click="loadMore" class="load-more-btn">加载更多</button>
  44. <text v-if="allLoaded" class="end-tip">已加载全部</text>
  45. </scroll-view>
  46. </view>
  47. </template>
  48. <script setup>
  49. import { ref, reactive } from 'vue'
  50. import { onLoad, onPullDownRefresh } from '@dcloudio/uni-app'
  51. import { machinesDeviceList } from "@/api/services/agriculturalMachines.js"
  52. import storage from "@/utils/storage.js"
  53. // Reactive data
  54. const machines = ref([])
  55. const loading = ref(false)
  56. const pageNum = ref(1)
  57. const pageSize = ref(10)
  58. const total = ref(0)
  59. const allLoaded = ref(false)
  60. const listHeight = ref(0)
  61. const filters = reactive({
  62. search: '',
  63. machineType: '', // numeric or string code from backend
  64. onlineStatus: '' // filter by onlineStatus
  65. })
  66. const machineTypeDisplayOptions = ref(['全部','其他','拖拉机','收割机','播种机','喷雾机','农业智能体'])
  67. const machineTypeCodeOptions = ref(['', '0','1','2','3','4','5'])
  68. const onlineStatusDisplayOptions = ref(['全部','离线','在线','待命','作业中','维护中','故障'])
  69. const onlineStatusCodeOptions = ref(['', '0','1','2','3','4','5'])
  70. const selectedTypeLabel = ref('全部')
  71. const selectedStatusLabel = ref('全部')
  72. // Methods
  73. const formatDate = (dateStr) => {
  74. if (!dateStr) return '-'
  75. const d = new Date(dateStr)
  76. if (isNaN(d.getTime())) return '-'
  77. const y = d.getFullYear()
  78. const m = (`0${d.getMonth() + 1}`).slice(-2)
  79. const day = (`0${d.getDate()}`).slice(-2)
  80. return `${y}-${m}-${day}`
  81. }
  82. const getMachineTypeLabel = (type) => {
  83. // backend uses numbers or strings; coerce to number if possible
  84. const map = {
  85. '0': '其他',
  86. '1': '拖拉机',
  87. '2': '收割机',
  88. '3': '播种机',
  89. '4': '喷雾机',
  90. '5': '农业智能体'
  91. }
  92. return map[String(type)] || (type ? String(type) : '其他')
  93. }
  94. const onlineStatusLabel = (code) => {
  95. const map = {
  96. '0': '离线',
  97. '1': '在线',
  98. '2': '待命',
  99. '3': '作业中',
  100. '4': '维护中',
  101. '5': '故障'
  102. }
  103. return map[String(code)] || '-'
  104. }
  105. const onlineClass = (code) => {
  106. const c = Number(code)
  107. if (c === 1) return 'status-online'
  108. if (c === 0) return 'status-offline'
  109. if (c === 5) return 'status-error'
  110. return 'status-idle'
  111. }
  112. const maintenanceLabel = (code) => {
  113. const map = { '1': '正常', '2': '需保养', '3': '待修复' }
  114. return map[code] || (code ? code : '-')
  115. }
  116. const locationLabel = (code) => {
  117. const map = { '1': '良好', '2': '异常', '3': '无信号' }
  118. return map[code] || (code ? code : '-')
  119. }
  120. const onTypeChange = (e) => {
  121. const idx = Number(e.detail.value)
  122. selectedTypeLabel.value = machineTypeDisplayOptions.value[idx]
  123. filters.machineType = machineTypeCodeOptions.value[idx] || ''
  124. }
  125. const onStatusChange = (e) => {
  126. const idx = Number(e.detail.value)
  127. selectedStatusLabel.value = onlineStatusDisplayOptions.value[idx]
  128. filters.onlineStatus = onlineStatusCodeOptions.value[idx] || ''
  129. }
  130. const onSearch = () => {
  131. pageNum.value = 1
  132. machines.value = []
  133. allLoaded.value = false
  134. fetchMachines()
  135. }
  136. const fetchMachines = () => {
  137. if (loading.value || allLoaded.value) return
  138. loading.value = true
  139. uni.showLoading({ title: '加载中...' })
  140. const params = {
  141. pageNum: pageNum.value,
  142. pageSize: pageSize.value,
  143. machineName: filters.search || undefined,
  144. machineCode: filters.search || undefined,
  145. deptName: filters.search || undefined,
  146. machineType: filters.machineType || undefined,
  147. onlineStatus: filters.onlineStatus || undefined
  148. }
  149. machinesDeviceList(params)
  150. .then(res => {
  151. if (res && res.data.code === 200 && res.data.rows) {
  152. const data = res.data.rows
  153. if (pageNum.value === 1) {
  154. machines.value = data
  155. } else {
  156. machines.value = machines.value.concat(data)
  157. }
  158. total.value = res.data.total
  159. if (machines.value.length >= total.value) {
  160. allLoaded.value = true
  161. } else {
  162. allLoaded.value = false
  163. }
  164. } else {
  165. uni.showToast({ title: (res && res.data.rows && res.data.msg) || '获取数据失败', icon: 'none' })
  166. }
  167. })
  168. .catch(err => {
  169. console.error('fetchMachines error', err)
  170. uni.showToast({ title: '请求失败', icon: 'none' })
  171. })
  172. .finally(() => {
  173. loading.value = false
  174. uni.hideLoading()
  175. })
  176. }
  177. const loadMore = () => {
  178. if (loading.value || allLoaded.value) return
  179. pageNum.value += 1
  180. fetchMachines()
  181. }
  182. const navigateToDetail = (machine) => {
  183. // Placeholder navigation - create detail page separately if needed
  184. uni.navigateTo({
  185. url: '/pages/device/device-list/detail-machine?id=' + machine.id,
  186. success: (res) => {
  187. setTimeout(() => {
  188. uni.$emit('agriculturalMachinesData', {
  189. machineId: machine.id,
  190. machineCode: machine.machineCode,
  191. machineName: machine.machineName,
  192. onlineStatus: machine.onlineStatus,
  193. updateTime: machine.updateTime
  194. })
  195. }, 100)
  196. }
  197. })
  198. }
  199. // Lifecycle hooks
  200. onLoad(() => {
  201. // compute list height to make scroll-view fill screen (simple heuristic)
  202. const systemInfo = uni.getSystemInfoSync()
  203. // subtract header/search height approx 160px
  204. listHeight.value = systemInfo.windowHeight - 160
  205. // initial load
  206. fetchMachines()
  207. })
  208. onPullDownRefresh(() => {
  209. pageNum.value = 1
  210. machines.value = []
  211. allLoaded.value = false
  212. fetchMachines()
  213. setTimeout(() => {
  214. uni.stopPullDownRefresh()
  215. }, 800)
  216. })
  217. </script>
  218. <style scoped>
  219. .container {
  220. padding: 24rpx;
  221. background-color: #F9FCFA;
  222. min-height: 100vh;
  223. box-sizing: border-box;
  224. }
  225. .search-bar {
  226. display: flex;
  227. align-items: center;
  228. gap: 12rpx;
  229. margin-bottom: 18rpx;
  230. }
  231. .search-input {
  232. flex: 1;
  233. height: 68rpx;
  234. border-radius: 12rpx;
  235. padding: 0 20rpx;
  236. background: #fff;
  237. border: 1rpx solid #EFEFEF;
  238. }
  239. .filter-picker {
  240. width: 180rpx;
  241. height: 68rpx;
  242. background: #fff;
  243. border-radius: 12rpx;
  244. display: flex;
  245. align-items: center;
  246. justify-content: center;
  247. border: 1rpx solid #EFEFEF;
  248. }
  249. .filter-label {
  250. color: #666;
  251. }
  252. .search-btn {
  253. background: #3BB44A;
  254. color: #fff;
  255. padding: 14rpx 20rpx;
  256. border-radius: 12rpx;
  257. }
  258. .list-wrapper {
  259. width: 100%;
  260. }
  261. .machine-card {
  262. display: flex;
  263. background: #fff;
  264. border-radius: 20rpx;
  265. padding: 20rpx;
  266. margin-bottom: 18rpx;
  267. box-shadow: 0 8rpx 24rpx rgba(0,0,0,0.04);
  268. align-items: center;
  269. }
  270. .machine-image {
  271. width: 140rpx;
  272. height: 100rpx;
  273. border-radius: 12rpx;
  274. margin-right: 18rpx;
  275. background: #f6f6f6;
  276. }
  277. .machine-info {
  278. flex: 1;
  279. }
  280. .row {
  281. display: flex;
  282. align-items: center;
  283. margin-bottom: 8rpx;
  284. }
  285. .machine-name {
  286. font-size: 30rpx;
  287. font-weight: 600;
  288. color: #333;
  289. margin-right: 8rpx;
  290. }
  291. .machine-code {
  292. color: #999;
  293. font-size: 24rpx;
  294. }
  295. .meta-row .meta-item {
  296. margin-right: 18rpx;
  297. color: #666;
  298. font-size: 24rpx;
  299. }
  300. .status-row .status-badge {
  301. padding: 8rpx 12rpx;
  302. border-radius: 8rpx;
  303. color: #fff;
  304. font-size: 22rpx;
  305. margin-right: 12rpx;
  306. }
  307. .status-online { background: #4CAF50; }
  308. .status-offline { background: #F56C6C; }
  309. .status-error { background: #FF9800; }
  310. .status-idle { background: #9E9E9E; }
  311. .alarm-count {
  312. margin-left: 12rpx;
  313. color: #F56C6C;
  314. font-weight: 600;
  315. }
  316. .footer-row {
  317. justify-content: space-between;
  318. color: #999;
  319. font-size: 22rpx;
  320. }
  321. .loading-tip, .end-tip {
  322. text-align: center;
  323. color: #999;
  324. margin: 18rpx 0;
  325. }
  326. .empty-tip {
  327. text-align: center;
  328. color: #999;
  329. margin-top: 60rpx;
  330. }
  331. .load-more-btn {
  332. width: 80%;
  333. margin: 18rpx auto;
  334. background: #fff;
  335. border: 1rpx solid #EFEFEF;
  336. padding: 14rpx 0;
  337. border-radius: 12rpx;
  338. }
  339. </style>