internetDetail.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <template>
  2. <u-popup :show="visible" mode="bottom" :round="20" :closeable="true" :close-on-click-overlay="false"
  3. @close="closePopup">
  4. <view class="internet-detail-container">
  5. <!-- 标题栏 -->
  6. <view class="popup-header">
  7. <text class="popup-title">设备详情</text>
  8. </view>
  9. <scroll-view scroll-y class="popup-body">
  10. <!-- 仪表盘区域 -->
  11. <view class="gauge-section">
  12. <view class="section-title">实时数据</view>
  13. <view class="gauge-grid">
  14. <view class="gauge-item" v-for="(item, index) in getGaugeData" :key="index">
  15. <view class="gauge-card" v-for="(listItem, j) in item.list" :key="j">
  16. <GaugeChart height="250px" :value="listItem.value" :min="listItem.min"
  17. :max="listItem.max" :title="listItem.name" :unit="listItem.unit"
  18. :colors="['#5470C6', '#91CC75', '#FAC858', '#EE6666']"
  19. :options="{ pointer: { itemStyle: { color: '#5470C6' } } }"
  20. :ref="'gaugeChartRef'" />
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. <!-- 实时数据列表 -->
  26. <view class="data-section">
  27. <view class="section-title">运行状态</view>
  28. <view class="data-grid">
  29. <view class="data-item" v-for="(obj, key) in realData" :key="key">
  30. <view class="data-label">{{ obj.name }}:</view>
  31. <view class="data-value"
  32. v-if="obj.dataType&&(obj.dataType.type == 'enum' || obj.dataType.type == 'bool')">
  33. {{ obj.specs[obj.value] }}
  34. </view>
  35. <view class="data-value" v-else>
  36. {{ obj.value }} <text v-if="obj.unit" class="data-unit">{{ obj.unit }}</text>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. <!-- 历史折线图 -->
  42. <view class="history-section">
  43. <view class="section-title">历史趋势</view>
  44. <view class="chart-card" v-for="(item, index) in historyData" :key="index">
  45. <view class="chart-header">
  46. <text class="chart-name">{{ item.name }}</text>
  47. <view class="chart-stats">
  48. <text class="stat-item">最高值<text class="stat-value">{{ item.maxValue }}
  49. {{ item.unit }}</text></text>
  50. <text class="stat-item">最低值<text class="stat-value">{{ item.minValue }}
  51. {{ item.unit }}</text></text>
  52. <text class="stat-item">平均值<text class="stat-value avg">{{ item.avgValue }}
  53. {{ item.unit }}</text></text>
  54. </view>
  55. </view>
  56. <view class="chart-tools">
  57. <dy-date-time-picker v-model="item.chartTime"
  58. :typeEnabled="{ date: item.timeType=='date', month: item.timeType=='month', year: item.timeType=='year', week:false }"
  59. placeholder="" />
  60. <view class="time-type">
  61. <text :class="{ active: item.timeType === 'date' }"
  62. @click="dateClick(item, 'date')">日</text>
  63. <text :class="{ active: item.timeType === 'month' }"
  64. @click="dateClick(item, 'month')">月</text>
  65. <text :class="{ active: item.timeType === 'year' }"
  66. @click="dateClick(item, 'year')">年</text>
  67. </view>
  68. <u-button type="primary" size="mini" @click="refreshSingleChart(item)">查询</u-button>
  69. </view>
  70. <LineChart :data="item.trendData" title="" xAxisName="时间" :yAxisName="item.unit" height="300px"
  71. :options="{}" />
  72. </view>
  73. </view>
  74. </scroll-view>
  75. </view>
  76. </u-popup>
  77. </template>
  78. <script>
  79. import GaugeChart from './GaugeChart.vue';
  80. import LineChart from './LineChart.vue';
  81. import {
  82. getRealData,
  83. getHistoryData,
  84. getPhysicalModel
  85. } from '@/api/ledgerAssets/equipment.js';
  86. import {
  87. getMonday
  88. } from '@/utils/utils.js';
  89. import {
  90. getAssetInfo
  91. } from '@/api/repair';
  92. import dayjs from 'dayjs'
  93. export default {
  94. name: 'InternetDetail',
  95. components: {
  96. GaugeChart,
  97. LineChart
  98. },
  99. props: {},
  100. data() {
  101. return {
  102. visible: false,
  103. loading: false,
  104. realData: [],
  105. iotDashboardPoint: [],
  106. info: null,
  107. dict: {
  108. chartTime: {
  109. date: 3,
  110. month: 2,
  111. year: 1
  112. }
  113. },
  114. gaugeData: [],
  115. historyData: [],
  116. id: null,
  117. };
  118. },
  119. computed: {
  120. getGaugeData() {
  121. let list = [];
  122. let gaugeData = this.iotDashboardPoint.length ?
  123. this.gaugeData.filter((item) => this.iotDashboardPoint.find((Point) => Point.identifier == item
  124. .identifier && Point.checked)) :
  125. this.gaugeData.filter((item, index) => index < 6);
  126. const perRow = 1;
  127. for (let i = 0; i < gaugeData.length; i += perRow) {
  128. list.push({
  129. list: gaugeData.slice(i, i + perRow)
  130. });
  131. }
  132. // this.$nextTick(() => {
  133. // console.log(this.$refs,'dadas')
  134. // // return
  135. // gaugeData.forEach((item, index) => {
  136. // if (this.$refs.gaugeChartRef[index]) {
  137. // this.$refs.gaugeChartRef[index].handleResize();
  138. // }
  139. // });
  140. // });
  141. return list;
  142. },
  143. },
  144. methods: {
  145. closePopup() {
  146. this.visible = false;
  147. },
  148. async open(id) {
  149. this.id = id;
  150. const {
  151. iotDashboardPoint
  152. } = await getAssetInfo(id);
  153. this.iotDashboardPoint = iotDashboardPoint;
  154. this.info = await getPhysicalModel(id);
  155. await this.getRealData();
  156. this.visible = true;
  157. },
  158. async getRealData() {
  159. this.loading = true;
  160. try {
  161. const res = await getRealData(this.id);
  162. this.realData = res;
  163. const tempData = this.info.properties || [];
  164. this.realData.forEach((item) => {
  165. tempData?.forEach((property) => {
  166. if (property.identifier == item.identifier) {
  167. item.max = property.dataType.specs.max;
  168. item.min = property.dataType.specs.min;
  169. item.propertyUnit = property.dataType.specs.unit;
  170. item.unitName = property.dataType.specs.unitName;
  171. item.specs = property.dataType.specs;
  172. item.dataType = property.dataType;
  173. item.propertyName = property.name;
  174. item.accessMode = property.accessMode;
  175. item.saveHistory = property.saveHistory;
  176. item.required = property.required;
  177. item.chartTime = dayjs(new Date()).format('YYYY-MM');
  178. // item.chartTime =dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss') ;
  179. item.timeType = 'month';
  180. }
  181. });
  182. });
  183. const gaugeData = this.realData.filter((item) => item.max !== undefined && item.min !== undefined);
  184. this.gaugeData = gaugeData;
  185. const historyPromises = this.gaugeData.map((item) => item.saveHistory ? this.getDefaultHistoryData(
  186. item) : null);
  187. this.historyData = await Promise.all(historyPromises);
  188. } catch (error) {
  189. console.error('获取数据失败:', error);
  190. } finally {
  191. this.loading = false;
  192. }
  193. },
  194. async getDefaultHistoryData(item) {
  195. const chartTime = item.chartTime
  196. const timeType = item.timeType || 'month';
  197. let time = this.getTimeList(chartTime, timeType);
  198. // if (!time || !time.startTime || !time.endTime) {
  199. // const defaultDate = new Date().toISOString().split('T')[0];
  200. // time = {
  201. // startTime: defaultDate + ' 00:00:00',
  202. // endTime: defaultDate + ' 23:59:59'
  203. // };
  204. // }
  205. return {
  206. identifier: item.identifier || '',
  207. name: item.name || '',
  208. unit: item.unit || '',
  209. unitName: item.unitName || '',
  210. chartTime,
  211. timeType,
  212. trendData: {
  213. xAxis: [],
  214. series: [{
  215. name: '',
  216. data: [],
  217. color: '#5470C6'
  218. }]
  219. },
  220. timeHistoryList: []
  221. };
  222. },
  223. async getHistoryDatas(item) {
  224. try {
  225. const chartTime = item.chartTime
  226. const timeType = item.timeType || 'month';
  227. let time = this.getTimeList(chartTime, timeType);
  228. // if (!time || !time.startTime || !time.endTime) {
  229. // const defaultDate = new Date().toISOString().split('T')[0];
  230. // time = {
  231. // startTime: defaultDate + ' 00:00:00',
  232. // endTime: defaultDate + ' 23:59:59'
  233. // };
  234. // }
  235. // const apiTimeType = this.dict?.chartTime?.[timeType] || 'month';
  236. const res = await getHistoryData({
  237. startTime: time.startTime,
  238. endTime: time.endTime,
  239. property: item.identifier,
  240. substanceId: this.id,
  241. timeType: this.dict.chartTime[timeType]
  242. });
  243. const response = res || {};
  244. const trendData = {
  245. xAxis: [],
  246. series: [{
  247. name: response.name || item.name,
  248. data: [],
  249. color: '#5470C6'
  250. }]
  251. };
  252. const timeHistoryList = Array.isArray(response.timeHistoryList) ? response.timeHistoryList : [];
  253. timeHistoryList.forEach((i) => {
  254. if (i && i.time !== undefined && i.value !== undefined) {
  255. trendData.xAxis.push(i.time);
  256. trendData.series[0].data.push(i.value);
  257. }
  258. });
  259. return {
  260. ...response,
  261. identifier: item.identifier || '',
  262. name: item.name || '',
  263. unit: item.unit || '',
  264. unitName: item.unitName || '',
  265. chartTime,
  266. timeType,
  267. trendData,
  268. timeHistoryList
  269. };
  270. } catch (error) {
  271. console.error(`获取属性 ${item.name || item.identifier} 的历史数据失败:`, error);
  272. return {
  273. identifier: item.identifier || '',
  274. name: item.name || '',
  275. unit: item.unit || '',
  276. unitName: item.unitName || '',
  277. chartTime: item.chartTime || new Date(),
  278. timeType: item.timeType || 'month',
  279. trendData: {
  280. xAxis: [],
  281. series: [{
  282. name: item.name || '',
  283. data: [],
  284. color: '#5470C6'
  285. }]
  286. },
  287. timeHistoryList: []
  288. };
  289. }
  290. },
  291. async refreshSingleChart(item) {
  292. try {
  293. const updatedData = await this.getHistoryDatas(item);
  294. const index = this.historyData.findIndex((d) => d.identifier === item.identifier);
  295. if (index !== -1) {
  296. this.$set(this.historyData, index, updatedData);
  297. }
  298. } catch (error) {
  299. console.error(`刷新图表 ${item.name || item.identifier} 数据失败:`, error);
  300. }
  301. },
  302. dateClick(item, dateType) {
  303. switch (dateType) {
  304. case 'date':
  305. item.chartTime = dayjs(new Date()).format('YYYY-MM-DD');
  306. break;
  307. case 'month':
  308. item.chartTime = dayjs(new Date()).format('YYYY-MM');
  309. break;
  310. case 'year':
  311. item.chartTime = dayjs(new Date()).format('YYYY');
  312. break;
  313. default:
  314. break;
  315. }
  316. item.timeType = dateType;
  317. },
  318. getTimeList(date, timeType) {
  319. console.log(date,'date')
  320. // const fnNum = (num) => num < 10 ? '0' + num : num;
  321. let startTime, endTime;
  322. // const ndate = date ? new Date(date) : new Date();
  323. // const year = ndate.getFullYear();
  324. // const month = ndate.getMonth() + 1;
  325. // const day = ndate.getDate();
  326. switch (timeType) {
  327. case 'date':
  328. startTime = date.date|| date + ' 00:00:00';
  329. endTime = date.date|| date + ' 23:59:59';
  330. break;
  331. case 'month':
  332. const mon = getMonday(date.month || date);
  333. startTime = mon[0] + ' 00:00:00';
  334. endTime = mon[1] + ' 23:59:59';
  335. break;
  336. case 'year':
  337. startTime = date.year|| date + '-01-01 00:00:00';
  338. endTime = date.year|| date + '-12-31 23:59:59';
  339. break;
  340. default:
  341. break;
  342. }
  343. return {
  344. startTime,
  345. endTime
  346. };
  347. },
  348. },
  349. };
  350. </script>
  351. <style scoped lang="scss">
  352. .internet-detail-container {
  353. // display: flex;
  354. // flex-direction: column;
  355. height: calc(100vh - 100rpx);
  356. background: #f5f7fa;
  357. }
  358. .popup-header {
  359. display: flex;
  360. justify-content: center;
  361. align-items: center;
  362. padding: 30rpx 0;
  363. background: #fff;
  364. border-bottom: 1rpx solid #eee;
  365. .popup-title {
  366. font-size: 36rpx;
  367. font-weight: bold;
  368. color: #333;
  369. }
  370. }
  371. .popup-body {
  372. flex: 1;
  373. padding: 20rpx;
  374. height: 100%;
  375. }
  376. .section-title {
  377. font-size: 32rpx;
  378. font-weight: bold;
  379. color: #303133;
  380. margin-bottom: 20rpx;
  381. padding-left: 20rpx;
  382. border-left: 8rpx solid #1890ff;
  383. }
  384. .gauge-section {
  385. margin-bottom: 30rpx;
  386. }
  387. .gauge-grid {
  388. display: flex;
  389. flex-wrap: wrap;
  390. gap: 20rpx;
  391. }
  392. .gauge-item {
  393. display: flex;
  394. gap: 20rpx;
  395. width: 100%;
  396. }
  397. .gauge-card {
  398. flex: 1;
  399. min-width: 200rpx;
  400. background: #fff;
  401. border-radius: 20rpx;
  402. padding: 20rpx;
  403. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  404. }
  405. .data-section {
  406. margin-bottom: 30rpx;
  407. }
  408. .data-grid {
  409. display: grid;
  410. grid-template-columns: 1fr 1fr;
  411. gap: 20rpx;
  412. }
  413. .data-item {
  414. display: flex;
  415. align-items: center;
  416. background: #fff;
  417. padding: 24rpx;
  418. border-radius: 16rpx;
  419. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
  420. }
  421. .data-label {
  422. font-size: 28rpx;
  423. color: #666;
  424. margin-right: 10rpx;
  425. }
  426. .data-value {
  427. font-size: 30rpx;
  428. color: #333;
  429. font-weight: bold;
  430. }
  431. .data-unit {
  432. font-size: 24rpx;
  433. color: #999;
  434. }
  435. .history-section {
  436. margin-bottom: 30rpx;
  437. }
  438. .chart-card {
  439. background: #fff;
  440. border-radius: 20rpx;
  441. padding: 24rpx;
  442. margin-bottom: 24rpx;
  443. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
  444. }
  445. .chart-header {
  446. margin-bottom: 20rpx;
  447. }
  448. .chart-name {
  449. font-size: 30rpx;
  450. font-weight: bold;
  451. color: #333;
  452. display: block;
  453. margin-bottom: 16rpx;
  454. }
  455. .chart-stats {
  456. display: flex;
  457. flex-wrap: wrap;
  458. gap: 20rpx;
  459. }
  460. .stat-item {
  461. font-size: 26rpx;
  462. color: #666;
  463. }
  464. .stat-value {
  465. font-weight: bold;
  466. color: #ff3e52;
  467. margin-left: 8rpx;
  468. &.avg {
  469. color: #0840d5;
  470. }
  471. }
  472. .chart-tools {
  473. display: flex;
  474. align-items: center;
  475. flex-wrap: wrap;
  476. gap: 16rpx;
  477. margin-bottom: 20rpx;
  478. }
  479. .time-type {
  480. display: flex;
  481. gap: 16rpx;
  482. text {
  483. font-size: 28rpx;
  484. color: #666;
  485. padding: 8rpx 20rpx;
  486. border-radius: 8rpx;
  487. background: #f0f0f0;
  488. &.active {
  489. background: #1890ff;
  490. color: #fff;
  491. }
  492. }
  493. }
  494. </style>