index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <div class="l-flex--col c-debug has-padding">
  3. <div class="l-flex__none l-flex--row has-bottom-padding">
  4. <div class="l-flex__auto l-flex--row c-sibling-item">
  5. <span>开启:</span>
  6. <el-switch
  7. v-model="opend"
  8. class="l-flex__none c-sibling-item"
  9. active-color="#13ce66"
  10. inactive-color="#ff4949"
  11. @change="onChange"
  12. />
  13. <button
  14. v-if="opend"
  15. class="l-flex__none c-sibling-item o-button"
  16. @click="publish"
  17. >
  18. <i class="o-button__icon el-icon-share" />
  19. 发送消息
  20. </button>
  21. </div>
  22. <el-select
  23. v-model="device"
  24. class="l-flex__none c-sibling-item"
  25. placeholder="请选择设备"
  26. >
  27. <el-option
  28. v-for="item in devices"
  29. :key="item.id"
  30. :label="item.name"
  31. :value="item.id"
  32. />
  33. </el-select>
  34. <button
  35. class="l-flex__none c-sibling-item o-button"
  36. @click="clear"
  37. >
  38. <i class="o-button__icon el-icon-delete" />
  39. 清空
  40. </button>
  41. </div>
  42. <div class="l-flex__auto">
  43. <div
  44. v-for="message in messages"
  45. :key="message.id"
  46. >
  47. <div v-if="message.topic">
  48. {{ message.time }} {{ message.topic }}
  49. </div>
  50. <div>{{ message.desc }}</div>
  51. </div>
  52. </div>
  53. <el-dialog
  54. title="发送消息"
  55. :visible.sync="mqtt.show"
  56. custom-class="c-dialog"
  57. >
  58. <div class="c-form">
  59. <div class="c-form__section">
  60. <span class="c-form__label required">topic:</span>
  61. <el-input
  62. v-model.trim="mqtt.topic"
  63. class="c-form__item"
  64. />
  65. </div>
  66. <div class="c-form__section">
  67. <span class="c-form__label">message:</span>
  68. <el-input
  69. v-model.trim="mqtt.message"
  70. class="c-form__item"
  71. type="textarea"
  72. />
  73. </div>
  74. <div class="c-form__section">
  75. <span class="c-form__label">加密:</span>
  76. <el-switch
  77. v-model="mqtt.encode"
  78. class="c-form__item"
  79. active-color="#13ce66"
  80. inactive-color="#ff4949"
  81. />
  82. </div>
  83. </div>
  84. <template #footer>
  85. <button
  86. class="o-button"
  87. @click="send"
  88. >
  89. 发送
  90. </button>
  91. </template>
  92. </el-dialog>
  93. </div>
  94. </template>
  95. <script>
  96. import {
  97. devices,
  98. deviceMessage,
  99. start,
  100. stop,
  101. clear,
  102. send
  103. } from './debug'
  104. export default {
  105. name: 'Debug',
  106. data () {
  107. return {
  108. opend: true,
  109. device: 'all',
  110. devices,
  111. deviceMessage,
  112. mqtt: {
  113. show: false,
  114. topic: '',
  115. message: '',
  116. encode: true
  117. }
  118. }
  119. },
  120. computed: {
  121. messages () {
  122. if (this.device === 'all') {
  123. return this.deviceMessage.map(item => {
  124. return {
  125. id: item.id,
  126. desc: `${item.name}共${item.list.length}条消息`
  127. }
  128. })
  129. }
  130. return this.deviceMessage.find(item => item.id === this.device).list
  131. }
  132. },
  133. created () {
  134. start()
  135. },
  136. beforeDestroy () {
  137. stop()
  138. },
  139. methods: {
  140. onChange () {
  141. if (this.opend) {
  142. start()
  143. } else {
  144. stop()
  145. }
  146. },
  147. clear () {
  148. clear()
  149. },
  150. publish () {
  151. this.mqtt = {
  152. show: true,
  153. topic: '',
  154. message: '',
  155. encode: true
  156. }
  157. },
  158. send () {
  159. if (!this.mqtt.topic) {
  160. this.$message({
  161. type: 'warning',
  162. message: '请填写topic'
  163. })
  164. return
  165. }
  166. send(this.mqtt.topic, this.mqtt.message, this.mqtt.encode).then(() => {
  167. this.$message({
  168. type: 'success',
  169. message: '发送成功'
  170. })
  171. }, () => {
  172. this.$message({
  173. type: 'error',
  174. message: '发送失败'
  175. })
  176. })
  177. }
  178. }
  179. }
  180. </script>
  181. <style lang="scss" scoped>
  182. .c-debug {
  183. color: $black;
  184. font-size: 14px;
  185. line-height: 24px;
  186. overflow-y: auto;
  187. }
  188. </style>