index.vue 10 KB

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