index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <wrapper
  3. v-loading="loading"
  4. class="c-detail"
  5. fill
  6. >
  7. <div class="l-flex__none c-detail__header">
  8. <template v-if="device">
  9. <div class="l-flex--row has-padding">
  10. <span
  11. class="c-sibling-item o-status"
  12. :class="statusType"
  13. >
  14. {{ statusTip }}
  15. </span>
  16. <auto-text
  17. class="c-sibling-item o-name"
  18. :text="deviceName"
  19. />
  20. </div>
  21. <el-tabs
  22. v-model="active"
  23. class="c-tabs"
  24. >
  25. <el-tab-pane
  26. label="设备信息"
  27. name="info"
  28. />
  29. <el-tab-pane
  30. label="运行状态"
  31. name="status"
  32. />
  33. <el-tab-pane
  34. label="设备告警"
  35. name="alarm"
  36. />
  37. </el-tabs>
  38. </template>
  39. <el-result
  40. v-if="!loading && !device"
  41. icon="warning"
  42. >
  43. <template #extra>
  44. <el-link
  45. class="u-pointer"
  46. type="warning"
  47. @click="getDevice"
  48. >
  49. 出错了,点击重试
  50. </el-link>
  51. </template>
  52. </el-result>
  53. </div>
  54. <div
  55. v-if="device"
  56. class="l-flex--col l-flex__fill has-padding"
  57. >
  58. <keep-alive>
  59. <component
  60. :is="activeComponent"
  61. class="l-flex__fill c-detail__wrapper has-padding u-overflow-y--auto"
  62. :device="device"
  63. />
  64. </keep-alive>
  65. </div>
  66. </wrapper>
  67. </template>
  68. <script>
  69. import { getDevice } from '@/api/device'
  70. import {
  71. start,
  72. stop,
  73. addListener
  74. } from './monitor'
  75. import DeviceInfo from './components/DeviceInfo'
  76. import DeviceStatus from './components/DeviceStatus'
  77. import DeviceAlarm from './components/DeviceAlarm'
  78. export default {
  79. name: 'DeviceDetail',
  80. components: {
  81. DeviceInfo,
  82. DeviceStatus,
  83. DeviceAlarm
  84. },
  85. data () {
  86. return {
  87. loading: true,
  88. device: null,
  89. isActivated: false,
  90. isOnline: false,
  91. active: 'info'
  92. }
  93. },
  94. computed: {
  95. deviceId () {
  96. return this.$route.params.id
  97. },
  98. statusType () {
  99. return this.isActivated
  100. ? this.isOnline
  101. ? 'success'
  102. : 'error'
  103. : this.device.activate
  104. ? 'primary'
  105. : 'warning'
  106. },
  107. statusTip () {
  108. return this.isActivated
  109. ? this.isOnline
  110. ? '在线'
  111. : '离线'
  112. : this.device.activate
  113. ? '已激活'
  114. : '未激活'
  115. },
  116. deviceName () {
  117. return this.device?.name
  118. },
  119. activeComponent () {
  120. switch (this.active) {
  121. case 'info':
  122. return 'DeviceInfo'
  123. case 'status':
  124. return 'DeviceStatus'
  125. case 'alarm':
  126. return 'DeviceAlarm'
  127. default:
  128. return ''
  129. }
  130. }
  131. },
  132. watch: {
  133. deviceId () {
  134. console.log(this.deviceId)
  135. stop()
  136. this.active = 'info'
  137. this.device = null
  138. this.getDevice()
  139. }
  140. },
  141. created () {
  142. this.getDevice()
  143. },
  144. beforeDestroy () {
  145. stop()
  146. },
  147. methods: {
  148. getDevice () {
  149. this.loading = true
  150. const id = this.deviceId
  151. getDevice(id).then(({ data }) => {
  152. if (this.deviceId === id) {
  153. this.device = data
  154. this.isActivated = data.activate === 2
  155. this.isOnline = data.onlineStatus === 1
  156. start(this.device)
  157. addListener('online', this.onUpdate)
  158. }
  159. }).finally(() => {
  160. if (this.deviceId === id) {
  161. this.loading = false
  162. }
  163. })
  164. },
  165. onUpdate (online) {
  166. this.isActivated = true
  167. this.isOnline = online
  168. }
  169. }
  170. }
  171. </script>
  172. <style lang="scss" scoped>
  173. .c-detail {
  174. &__header {
  175. background-color: #fff;
  176. }
  177. &__wrapper {
  178. border-radius: $radius;
  179. background-color: #fff;
  180. }
  181. }
  182. .o-name {
  183. color: $black;
  184. font-size: 20px;
  185. font-weight: bold;
  186. }
  187. .o-status {
  188. display: inline-block;
  189. padding: 4px 8px;
  190. color: #fff;
  191. font-size: 16px;
  192. line-height: 1;
  193. border-radius: 4px;
  194. background-color: $success--dark;
  195. &.primary {
  196. background-color: $primary;
  197. }
  198. &.error {
  199. background-color: $error;
  200. }
  201. &.warning {
  202. background: $warning;
  203. }
  204. }
  205. </style>