OnlineDuration.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <div class="l-flex--col c-device-grid-item has-border radius">
  3. <div class="l-flex__none l-flex--row c-sibling-item--v">
  4. <i class="l-flex__none c-sibling-item o-icon" />
  5. <span class="l-flex__fill c-sibling-item near u-color--info u-ellipsis">在线总时长</span>
  6. <div class="u-color--info u-font-size--xs">{{ timestamp }}</div>
  7. <!-- <i
  8. class="el-icon-date"
  9. @click="open()"
  10. /> -->
  11. <!-- <el-dialog
  12. title="历史在线时长"
  13. custom-class="c-dialog xl"
  14. :visible.sync="dialogVisible"
  15. width="30%"
  16. close-on-press-escape="true"
  17. >
  18. <div class="picker_top">
  19. <el-date-picker
  20. v-model="date"
  21. format="yyyy-MM-dd"
  22. value-format="yyyy-MM-dd"
  23. type="daterange"
  24. range-separator="至"
  25. start-placeholder="开始日期"
  26. end-placeholder="结束日期"
  27. @change="getTime()"
  28. />
  29. </div>
  30. <div
  31. id="chart"
  32. class="content"
  33. style="width:1250px;height:1000px "
  34. />
  35. </el-dialog> -->
  36. </div>
  37. <div class="l-flex__fill l-flex--row center u-color--black u-text--center">
  38. <div
  39. v-if="duration"
  40. class="u-font-size--lg"
  41. >
  42. {{ duration }}
  43. </div>
  44. <i
  45. v-else
  46. class="el-icon-loading"
  47. />
  48. </div>
  49. </div>
  50. </template>
  51. <script>
  52. import * as echarts from 'echarts'
  53. import { getOnlineDurationByIds } from '@/api/platform'
  54. export default {
  55. name: 'OnlineDuration',
  56. props: {
  57. device: {
  58. type: Object,
  59. required: true
  60. }
  61. },
  62. data () {
  63. return {
  64. duration: '',
  65. timestamp: '',
  66. datearr: [],
  67. publication: [],
  68. date: [],
  69. dialogVisible: false
  70. }
  71. },
  72. created () {
  73. this.$running = true
  74. this.$timer = -1
  75. this.startRun()
  76. },
  77. mounted () {
  78. this.datearr = this.getDayAll(this.getDay(-1), this.getDay(0))
  79. },
  80. beforeDestroy () {
  81. this.$running = false
  82. clearTimeout(this.$timer)
  83. },
  84. methods: {
  85. startRun () {
  86. if (!this.$running) {
  87. return
  88. }
  89. getOnlineDurationByIds([this.device.id], { custom: true }).then(({ data }) => {
  90. if (data?.[0]) {
  91. if (data[0].powerSeconds === '0') {
  92. this.duration = this.transformDuration(data[0].onlineSeconds)
  93. this.timestamp = data[0].onlineSecondsUpdateTime
  94. } else {
  95. this.duration = this.transformDuration(data[0].powerSeconds)
  96. this.timestamp = data[0].powerSecondsUpdateTime
  97. }
  98. if (!this.duration) {
  99. this.duration = '暂无统计数据'
  100. }
  101. } else {
  102. this.duration = '暂无统计数据'
  103. this.timestamp = ''
  104. }
  105. }).finally(() => {
  106. if (this.$running) {
  107. this.$timer = setTimeout(this.startRun, 60000)
  108. }
  109. })
  110. },
  111. transformDuration (duration) {
  112. duration = Number(duration)
  113. return [
  114. { value: duration / (24 * 3600) | 0, unit: '天' },
  115. { value: (duration % (24 * 3600)) / 3600 | 0, unit: '小时' },
  116. { value: (duration / 3600) / 60 | 0, unit: '分' },
  117. { value: duration % 60, unit: '秒' }
  118. ].reduce((curr, { value, unit }) => value ? `${curr}${value}${unit}` : curr, '')
  119. },
  120. getDayAll (starDay, endDay) {
  121. const arr = []
  122. const dates = []
  123. const db = new Date(starDay)
  124. const de = new Date(endDay)
  125. const s = db.getTime() - 24 * 60 * 60 * 1000
  126. const d = de.getTime() - 24 * 60 * 60 * 1000
  127. for (let i = s; i <= d;) {
  128. i += 24 * 60 * 60 * 1000
  129. arr.push(parseInt(i))
  130. }
  131. for (const j in arr) {
  132. if (arr[j] !== null) {
  133. const time = new Date(arr[j])
  134. const year = time.getFullYear(time)
  135. const mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : (`0${time.getMonth() + 1}`)
  136. const day = time.getDate() >= 10 ? time.getDate() : (`0${time.getDate()}`)
  137. const YYMMDD = `${year}-${mouth}-${day}`
  138. dates.push(YYMMDD)
  139. }
  140. }
  141. return dates
  142. },
  143. getDay (setmonth) {
  144. const date = new Date()
  145. const seperator1 = '-'
  146. const year = date.getFullYear()
  147. let month = date.getMonth() + 1 + setmonth
  148. let strDate = date.getDate()
  149. if (month >= 1 && month <= 9) {
  150. month = `0${month}`
  151. }
  152. if (strDate >= 0 && strDate <= 9) {
  153. strDate = `0${strDate}`
  154. }
  155. const currentdate = year + seperator1 + month + seperator1 + strDate
  156. return currentdate
  157. },
  158. getTime () {
  159. this.datearr = this.getDayAll(this.date[0], this.date[1])
  160. this.setChart()
  161. },
  162. open () {
  163. this.dialogVisible = true
  164. this.setChart()
  165. },
  166. setChart () {
  167. setTimeout(() => {
  168. const option = {
  169. tooltip: {
  170. trigger: 'axis'
  171. },
  172. legend: {
  173. data: ['设备1', '设备2', '设备3', '设备4', '设备5']
  174. },
  175. grid: {
  176. left: '3%',
  177. right: '4%',
  178. bottom: '3%',
  179. containLabel: true
  180. },
  181. xAxis: {
  182. type: 'category',
  183. boundaryGap: false,
  184. data: this.datearr
  185. },
  186. yAxis: {
  187. type: 'value'
  188. },
  189. series: [
  190. {
  191. name: '设备1',
  192. type: 'line',
  193. stack: 'Total',
  194. data: [120, 132, 101, 134, 90, 230, 210]
  195. },
  196. {
  197. name: '设备2',
  198. type: 'line',
  199. stack: 'Total',
  200. data: [220, 182, 191, 234, 290, 330, 310]
  201. },
  202. {
  203. name: '设备3',
  204. type: 'line',
  205. stack: 'Total',
  206. data: [150, 232, 201, 154, 190, 330, 410]
  207. },
  208. {
  209. name: '设备4',
  210. type: 'line',
  211. stack: 'Total',
  212. data: [320, 332, 301, 334, 390, 330, 320]
  213. },
  214. {
  215. name: '设备5',
  216. type: 'line',
  217. stack: 'Total',
  218. data: [820, 932, 901, 934, 1290, 1330, 1320]
  219. }
  220. ]
  221. }
  222. const chartDom = document.getElementById('chart')
  223. console.log('chart', document.getElementById('chart'))
  224. const myChart = echarts.init(chartDom)
  225. myChart.setOption(option)
  226. }, 100)
  227. }
  228. }
  229. }
  230. </script>
  231. <style lang="scss" scoped>
  232. .picker_top {
  233. margin-left: 30px;
  234. }
  235. .o-icon {
  236. background-image: url("~@/assets/icon_online.png");
  237. }
  238. </style>