index.vue 4.2 KB

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