index_backoff1.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view class="content">
  3. <view>
  4. <button type="primary" @click="connectOrDisconnect">{{connectText}}</button>
  5. <button @click="godoBle">添加设备</button>
  6. </view>
  7. <!-- <image class="logo" src="/static/logo.png"></image> -->
  8. <text class="title">发布消息</text>
  9. <view class="br"></view>
  10. <view class="text-area">
  11. <text>Topic</text>
  12. <input class="edittext" v-model="publishTopic" />
  13. </view>
  14. <view class="br"></view>
  15. <view class="text-area">
  16. <text>Payload</text>
  17. <textarea class="edittext" v-model="publishMsg" />
  18. </view>
  19. <view class="br"></view>
  20. <button @click="publish">publish</button>
  21. <text class="title">订阅主题</text>
  22. <view class="text-area">
  23. <!-- 订阅 -->
  24. <input class="edittext" v-model="subscribeTopic" />
  25. </view>
  26. <view class="br"></view>
  27. <button @click="subscribe">subscribe</button>
  28. <view class="br"></view>
  29. <text class="title">收到的Message</text>
  30. <view class="br"></view>
  31. <view v-for="(item, index) in subList" :key="index">
  32. <text>Topic:{{ item.topic }} ,Payload: {{ item.msg }}</text>
  33. </view>
  34. <view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. //import store from '@/store/index.js';//需要引入store
  40. export default {
  41. data() {
  42. return {
  43. publishMsg: "",
  44. publishTopic: "",
  45. subscribeTopic: "",
  46. subList: [],
  47. }
  48. },
  49. onLoad() {
  50. console.log('index.vue onload')
  51. this.init();
  52. },
  53. created() {
  54. //this.init();
  55. },
  56. computed: {
  57. connectText() {
  58. return (this.$store.getters.isConnect) ? '断开' : '连接'
  59. }
  60. },
  61. methods: {
  62. publish() {
  63. //发布消息
  64. if (this.$store.getters.isConnect) {
  65. this.$store.state.mqttClient.publish(this.publishTopic, this.publishMsg, (err) => {
  66. if (err) {
  67. //alert(`发布消息失败`);
  68. uni.showToast({
  69. title: "发布消息失败",
  70. duration: 2000,
  71. icon: "none"
  72. })
  73. }
  74. });
  75. }
  76. },
  77. subscribe() {
  78. //console.log(this.$root)
  79. console.log(`subscribe isconnect = ${this.$store.getters.isConnect}`)
  80. if (this.$store.getters.isConnect) {
  81. //订阅消息
  82. this.$store.state.mqttClient.subscribe(this.subscribeTopic, (err) => {
  83. console.log(err);
  84. if (!err) {
  85. uni.showToast({
  86. title: `订阅${this.subscribeTopic}主题成功`,
  87. duration: 2000,
  88. icon: "none"
  89. })
  90. } else {
  91. uni.showToast({
  92. title: `订阅${this.subscribeTopic}主题失败`,
  93. duration: 2000,
  94. icon: "none"
  95. })
  96. }
  97. });
  98. }
  99. },
  100. init() {
  101. //连接mqtt服务器
  102. //this.$store.commit('connect')
  103. //this.mqttListen()
  104. },
  105. mqttListen() {
  106. const that = this;
  107. if (this.$store.state.mqttClient !== undefined) {
  108. //收到消息的回调
  109. this.$store.state.mqttClient.on("message", function(topic, message) {
  110. console.log(`message = ${message.toString()}`);
  111. that.subList.push({
  112. topic: topic.toString(),
  113. msg: message.toString(),
  114. });
  115. });
  116. }
  117. },
  118. connectOrDisconnect() { //连接或者断开mqtt服务
  119. if (this.$store.getters.isConnect === false) {
  120. //连接mqtt服务器
  121. this.$store.commit('connect')
  122. this.mqttListen()
  123. } else {
  124. //断开连接
  125. this.$store.commit('disconnect')
  126. }
  127. },
  128. godoBle() { //添加设备
  129. // #ifdef MP-WEIXIN||APP-PLUS
  130. uni.navigateTo({
  131. url: '../ble/ScanBleDevice'
  132. })
  133. // #endif
  134. // #ifdef H5
  135. uni.navigateTo({
  136. url: '../ble/ConnectBleDevice'
  137. })
  138. // uni.showToast({
  139. // title:'H5页面不支持扫描设备',
  140. // icon:'none'
  141. // })
  142. // #endif
  143. }
  144. }
  145. }
  146. </script>
  147. <style>
  148. .content {
  149. display: flex;
  150. flex-direction: column;
  151. /* align-items: center; */
  152. justify-content: center;
  153. margin: 20rpx;
  154. }
  155. .logo {
  156. height: 200rpx;
  157. width: 200rpx;
  158. margin-top: 200rpx;
  159. margin-left: auto;
  160. margin-right: auto;
  161. margin-bottom: 50rpx;
  162. }
  163. .br {
  164. height: 10rpx;
  165. }
  166. .text-area {
  167. display: flex;
  168. /* justify-content: center; */
  169. }
  170. .title {
  171. font-size: 20rpx;
  172. color: #2c3e50;
  173. }
  174. textarea {
  175. height: 50rpx;
  176. }
  177. .edittext {
  178. display: flex;
  179. border: 1rpx solid #333333;
  180. }
  181. </style>