index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <template>
  2. <view class="container">
  3. <!-- 地块信息 -->
  4. <view class="plot-section">
  5. <text class="plot-text">当前查看地块:</text>
  6. <text class="plot-name">{{ currentPlot }}</text>
  7. </view>
  8. <!-- 顶部区域:概览统计 -->
  9. <view class="overview-section">
  10. <view class="stat-item">
  11. <view class="stat-icon-wrapper">
  12. <image src="/static/icons/device_num.png" mode="aspectFit" class="stat-icon-img"></image>
  13. </view>
  14. <text class="stat-value">{{ totalDevices }}</text>
  15. <text class="stat-label">总数</text>
  16. </view>
  17. <view class="stat-item">
  18. <view class="stat-icon-wrapper online">
  19. <image src="/static/icons/device_online.png" mode="aspectFit" class="stat-icon-img"></image>
  20. </view>
  21. <text class="stat-value online">{{ onlineDevices }}</text>
  22. <text class="stat-label">在线</text>
  23. </view>
  24. <view class="stat-item">
  25. <view class="stat-icon-wrapper offline">
  26. <image src="/static/icons/device_offline.png" mode="aspectFit" class="stat-icon-img"></image>
  27. </view>
  28. <text class="stat-value offline">{{ offlineDevices }}</text>
  29. <text class="stat-label">离线</text>
  30. </view>
  31. <view class="stat-item">
  32. <view class="stat-icon-wrapper alert">
  33. <image src="/static/icons/device_alert.png" mode="aspectFit" class="stat-icon-img"></image>
  34. </view>
  35. <text class="stat-value alert">{{ alertDevices }}</text>
  36. <text class="stat-label">告警</text>
  37. </view>
  38. </view>
  39. <!-- 设备网格布局 -->
  40. <view class="device-grid">
  41. <view
  42. v-for="(item, index) in deviceList"
  43. :key="index"
  44. @click="navigateToDeviceList(item.type)"
  45. class="device-card"
  46. :class="{'has-alert': item.alerts > 0}"
  47. hover-class="device-card-hover"
  48. >
  49. <!-- 告警角标 -->
  50. <view v-if="item.alerts > 0" class="alert-badge">{{ item.alerts }}</view>
  51. <!-- 设备图标 -->
  52. <view class="device-icon-container">
  53. <image :src="item.icon" mode="aspectFit" class="device-icon"></image>
  54. </view>
  55. <!-- 设备名称 -->
  56. <text class="device-name">{{ item.name }}</text>
  57. <!-- 设备状态信息区域 -->
  58. <view class="device-status-container">
  59. <!-- 在线状态 -->
  60. <view class="status-info">
  61. <view class="status-dot online-dot"></view>
  62. <text class="status-text">在线</text>
  63. <text class="status-num online-num">{{ item.online }}</text>
  64. </view>
  65. <!-- 离线状态 -->
  66. <view class="status-info">
  67. <view class="status-dot offline-dot"></view>
  68. <text class="status-text">离线</text>
  69. <text class="status-num offline-num">{{ item.offline }}</text>
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. </view>
  75. </template>
  76. <script>
  77. // 导入API服务
  78. import { forEach } from "../../utils/lib/request/utils";
  79. import { fetchDeviceOverview } from "@/api/services/device.js";
  80. import storage from "@/utils/storage.js";
  81. export default {
  82. data() {
  83. return {
  84. deviceList: [],
  85. currentPlot: "加载中...",
  86. totalDevices: 0,
  87. onlineDevices: 0,
  88. offlineDevices: 0,
  89. alertDevices: 0,
  90. loading: false,
  91. currentFieldId: null, // 当前选中的地块ID
  92. typeOnline: 0,
  93. typeOffline: 0
  94. }
  95. },
  96. methods: {
  97. // 初始化地块信息
  98. initFieldInfo() {
  99. const fieldInfo = storage.getPlots();
  100. console.log("ggg ");
  101. if (fieldInfo) {
  102. const plotData = JSON.parse(fieldInfo);
  103. this.currentFieldId = plotData.id;
  104. this.currentPlot = plotData.name || "未选择地块";
  105. } else {
  106. this.currentPlot = "未选择地块";
  107. }
  108. },
  109. // 获取设备数据
  110. fetchDeviceData() {
  111. if (this.loading) return;
  112. this.loading = true;
  113. uni.showLoading({
  114. title: '加载中...'
  115. });
  116. // 调用API获取设备概览数据
  117. fetchDeviceOverview(this.currentFieldId)
  118. .then(res => {
  119. if (res.data.code === 200 && res.data.data) {
  120. const data = res.data.data;
  121. // 更新设备总览数据
  122. this.totalDevices = data.totalDevices || 0;
  123. this.onlineDevices = data.onlineDevices || 0;
  124. this.offlineDevices = data.offlineDevices || 0;
  125. this.alertDevices = data.alertDevices || 0;
  126. // 更新设备类型列表
  127. if (data.deviceList && data.deviceList.length > 0) {
  128. this.deviceList = data.deviceList;
  129. }
  130. console.log('设备概览数据加载成功');
  131. } else {
  132. this.handleApiError(res);
  133. }
  134. })
  135. .catch(error => {
  136. console.error('获取设备概览数据失败', error);
  137. uni.showToast({
  138. title: '获取设备数据失败',
  139. icon: 'none'
  140. });
  141. })
  142. .finally(() => {
  143. this.loading = false;
  144. uni.hideLoading();
  145. });
  146. },
  147. // 处理API错误
  148. handleApiError(res) {
  149. console.error('API错误', res);
  150. uni.showToast({
  151. title: res.data.msg || '获取数据失败',
  152. icon: 'none'
  153. });
  154. },
  155. // 跳转到对应设备列表页面
  156. navigateToDeviceList(type) {
  157. console.log("type",type);
  158. // 传递指定设备类型的在线、离线数量
  159. this.deviceList.forEach((item, index) => {
  160. if(item.type === type){
  161. this.typeOnline = item.online
  162. this.typeOffline = item.offline
  163. }
  164. });
  165. if(type === 'tractor'){
  166. uni.navigateTo({
  167. url: `/pages/device/device-list/agricultural/index?type=${type}&typeOnline=${this.typeOnline}&typeOffline=${this.typeOffline}`
  168. });
  169. }else{
  170. uni.navigateTo({
  171. url: `/pages/device/device-list/index?type=${type}&typeOnline=${this.typeOnline}&typeOffline=${this.typeOffline}`
  172. });
  173. }
  174. },
  175. // 切换地块
  176. changePlot() {
  177. uni.navigateTo({
  178. url: '/pages/field-selector/index?callback=deviceCenter'
  179. });
  180. },
  181. },
  182. // 页面导航配置
  183. onShow() {
  184. this.initFieldInfo();
  185. this.fetchDeviceData();
  186. },
  187. // 下拉刷新
  188. onPullDownRefresh() {
  189. this.fetchDeviceData();
  190. setTimeout(() => {
  191. uni.stopPullDownRefresh();
  192. }, 1000);
  193. }
  194. }
  195. </script>
  196. <style scoped>
  197. .container {
  198. padding: 30rpx;
  199. background-color: #F9FCFA;
  200. min-height: 100vh;
  201. box-sizing: border-box;
  202. }
  203. /* 地块选择区域 */
  204. .plot-section {
  205. background-color: #FFFFFF;
  206. border-radius: 20rpx;
  207. padding: 26rpx 30rpx;
  208. margin-bottom: 30rpx;
  209. display: flex;
  210. align-items: center;
  211. justify-content: center;
  212. box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.04);
  213. }
  214. .plot-text {
  215. font-size: 28rpx;
  216. color: #606060;
  217. }
  218. .plot-name {
  219. font-size: 32rpx;
  220. color: #333333;
  221. font-weight: 600;
  222. margin: 0 12rpx;
  223. }
  224. /* 顶部概览统计区域 */
  225. .overview-section {
  226. background-color: #F2F7F3;
  227. border-radius: 20rpx;
  228. padding: 36rpx 40rpx;
  229. margin-bottom: 34rpx;
  230. display: flex;
  231. justify-content: space-between;
  232. box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.03);
  233. }
  234. .stat-item {
  235. display: flex;
  236. flex-direction: column;
  237. align-items: center;
  238. position: relative;
  239. }
  240. .stat-icon-wrapper {
  241. width: 90rpx;
  242. height: 90rpx;
  243. border-radius: 50%;
  244. background-color: #F0F0F0;
  245. display: flex;
  246. justify-content: center;
  247. align-items: center;
  248. margin-bottom: 14rpx;
  249. box-shadow: 0 6rpx 12rpx rgba(0, 0, 0, 0.04);
  250. }
  251. .stat-icon-wrapper.online {
  252. background: linear-gradient(135deg, #E8F5EA, #D6ECD8);
  253. }
  254. .stat-icon-wrapper.offline {
  255. background: linear-gradient(135deg, #FDEAEA, #FDDCDC);
  256. }
  257. .stat-icon-wrapper.alert {
  258. background: linear-gradient(135deg, #FFF6E5, #FFEFD0);
  259. }
  260. .stat-icon-img {
  261. width: 48rpx;
  262. height: 48rpx;
  263. display: block;
  264. }
  265. .stat-value {
  266. font-size: 38rpx;
  267. color: #333333;
  268. font-weight: 600;
  269. margin: 8rpx 0;
  270. line-height: 1.2;
  271. }
  272. .stat-value.online {
  273. color: #4CAF50;
  274. }
  275. .stat-value.offline {
  276. color: #F44336;
  277. }
  278. .stat-value.alert {
  279. color: #FF9800;
  280. }
  281. .stat-label {
  282. font-size: 24rpx;
  283. color: #888888;
  284. font-weight: 400;
  285. }
  286. /* 设备网格区域 */
  287. .device-grid {
  288. display: flex;
  289. flex-wrap: wrap;
  290. justify-content: space-between;
  291. width: 100%;
  292. }
  293. .device-card {
  294. width: 48%;
  295. border-radius: 24rpx; /* 加大卡片圆角 */
  296. background-color: #FFFFFF;
  297. box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.05); /* 柔和阴影 */
  298. padding: 30rpx 0 24rpx 0;
  299. margin-bottom: 30rpx; /* 增加卡片间距 */
  300. position: relative;
  301. display: flex;
  302. flex-direction: column;
  303. align-items: center;
  304. transition: all 0.25s ease;
  305. }
  306. .device-card-hover {
  307. background-color: #f2fef6;
  308. box-shadow: 0 12rpx 32rpx rgba(59, 180, 74, 0.1);
  309. transform: translateY(-3rpx);
  310. }
  311. .device-card.has-alert {
  312. box-shadow: 0 10rpx 30rpx rgba(245, 108, 108, 0.08);
  313. }
  314. /* 告警角标 */
  315. .alert-badge {
  316. position: absolute;
  317. top: 16rpx; /* 6px */
  318. right: 16rpx; /* 6px */
  319. width: 40rpx; /* 20px */
  320. height: 40rpx; /* 20px */
  321. border-radius: 50%;
  322. background-color: #F56C6C;
  323. color: white;
  324. font-size: 24rpx; /* 12px */
  325. font-weight: 600;
  326. display: flex;
  327. align-items: center;
  328. justify-content: center;
  329. box-shadow: 0 4rpx 8rpx rgba(245, 108, 108, 0.3);
  330. z-index: 2;
  331. }
  332. /* 设备图标 */
  333. .device-icon-container {
  334. width: 72rpx; /* 36px */
  335. height: 72rpx; /* 36px */
  336. border-radius: 50%;
  337. background: linear-gradient(135deg, #66CC6A 0%, #3BB44A 100%);
  338. display: flex;
  339. align-items: center;
  340. justify-content: center;
  341. margin-bottom: 18rpx;
  342. box-shadow: 0 6rpx 12rpx rgba(59, 180, 74, 0.15);
  343. }
  344. .device-icon {
  345. width: 40rpx;
  346. height: 40rpx;
  347. filter: brightness(0) invert(1);
  348. }
  349. /* 设备名称 */
  350. .device-name {
  351. font-size: 32rpx; /* 16px */
  352. color: #333333;
  353. font-weight: 600;
  354. text-align: center;
  355. margin-bottom: 24rpx;
  356. padding: 0 20rpx;
  357. }
  358. /* 设备状态信息区域 */
  359. .device-status-container {
  360. width: 100%;
  361. display: flex;
  362. flex-direction: row;
  363. justify-content: center;
  364. padding: 16rpx 0;
  365. border-top: 1rpx solid #EEEEEE;
  366. }
  367. /* 状态信息项 */
  368. .status-info {
  369. display: flex;
  370. align-items: center;
  371. margin: 0 16rpx;
  372. }
  373. /* 状态指示点 */
  374. .status-dot {
  375. width: 8rpx;
  376. height: 8rpx;
  377. border-radius: 50%;
  378. margin-right: 6rpx;
  379. }
  380. .online-dot {
  381. background-color: #4CAF50;
  382. }
  383. .offline-dot {
  384. background-color: #F56C6C;
  385. }
  386. /* 状态文本 */
  387. .status-text {
  388. font-size: 26rpx;
  389. color: #999999;
  390. margin-right: 6rpx;
  391. }
  392. /* 状态数字 */
  393. .status-num {
  394. font-size: 26rpx;
  395. font-weight: 600;
  396. }
  397. .online-num {
  398. color: #4CAF50;
  399. }
  400. .offline-num {
  401. color: #F56C6C;
  402. }
  403. </style>