index.vue 10 KB

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