mqttDemo.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="content">
  3. <!-- <view class="dbox">
  4. <button @click="addDevice">添加设备</button>
  5. <button @click="getPlayInfo">获取播放信息</button>
  6. <button @click="getDeviceInfo">获取设备信息</button>
  7. </view> -->
  8. <view class="dbox">
  9. <button @click="addDevice" class="btn">+</button>
  10. <device-card v-for="(device, index) in deviceList" :key="index" :device="device"></device-card>
  11. <!-- <device-card v-if="currentDevice" :device="currentDevice"/> -->
  12. </view>
  13. <view class="play-info">
  14. <image class="play-thmub" :src="playInfo.albumURI" mode="aspectFit" />
  15. <text style="margin-top: 20rpx">{{ playInfo.title }}</text>
  16. <text style="margin-top: 20rpx">{{ playInfo.artist }}</text>
  17. </view>
  18. <view class="play-ctrl">
  19. <image src="../../../static/demo/previous.png" class="next-icon" @click="previous" />
  20. <image v-if="playInfo.PlayState != 1" src="../../../static/demo/play.png" class="next-icon" @click="play" />
  21. <image v-if="playInfo.PlayState == 1" src="../../../static/demo/pause.png" class="next-icon"
  22. @click="pause" />
  23. <image src="../../../static/demo/next.png" class="next-icon" @click="next" />
  24. </view>
  25. <text style="margin-top: 20rpx; margin-left: 50rpx">音量</text>
  26. <slider :value="currentDevice && currentDevice.Volume ? currentDevice.Volume : 0" class="progress" min="0"
  27. max="100" step="1" @change="setVolume" show-value></slider>
  28. <text style="margin-top: 10rpx; margin-left: 50rpx">频道</text>
  29. <slider :value="playInfo.channel" class="progress" min="0" max="11" step="1" @change="setChannel" show-value>
  30. </slider>
  31. </view>
  32. </template>
  33. <script>
  34. import deviceCard from "../../../components/demo/deviceCard.vue";
  35. import {
  36. mapState
  37. } from "vuex";
  38. export default {
  39. components: {
  40. deviceCard
  41. },
  42. data() {
  43. return {
  44. //deviceList:[]
  45. };
  46. },
  47. onLoad() {
  48. //this.deviceList = this.$store.state.moduleMqtt.deviceList;
  49. uni.$on("mqtt_onoffline", this.onofflineCallback);
  50. },
  51. onUnload() {
  52. uni.$off("mqtt_onoffline", this.onofflineCallback);
  53. },
  54. methods: {
  55. onofflineCallback(value) {
  56. //console.log("onofflineCallback");
  57. },
  58. //测试添加设备
  59. addDevice() {
  60. if (getApp().globalData.uid) {
  61. //添加设备
  62. // this.$store.dispatch({
  63. // type: "moduleMqtt/addDevice",
  64. // clientId: `wx_${getApp().globalData.uid}`,
  65. // device: {
  66. // devName: "MW-V",
  67. // uuid: "89860474192070498495",
  68. // },
  69. // });
  70. uni.navigateTo({
  71. url:'../../addDevice/selectDevice/selectDevice'
  72. })
  73. }else{
  74. uni.showModal({
  75. title:"温馨提示",
  76. content:"请登录!",
  77. success() {
  78. uni.navigateTo({
  79. url:'../../login/login'
  80. })
  81. }
  82. })
  83. }
  84. },
  85. //获取播放信息
  86. getPlayInfo() {
  87. this.$store
  88. .dispatch({
  89. type: "moduleMqtt/publishWithType",
  90. mqttType: "get_position",
  91. })
  92. .then((result) => {})
  93. .catch((err) => {
  94. console.warn(err);
  95. });
  96. },
  97. //获取设备信息
  98. getDeviceInfo() {
  99. this.$store
  100. .dispatch({
  101. type: "moduleMqtt/publishWithType",
  102. mqttType: "get_dev_info",
  103. })
  104. .then((result) => {})
  105. .catch((err) => {
  106. console.warn(err);
  107. });
  108. },
  109. //下一曲
  110. next() {
  111. this.$store
  112. .dispatch({
  113. type: "moduleMqtt/publishWithType",
  114. mqttType: "next",
  115. })
  116. .then((result) => {})
  117. .catch((err) => {
  118. console.warn(err);
  119. });
  120. },
  121. //上一曲
  122. previous() {
  123. this.$store
  124. .dispatch({
  125. type: "moduleMqtt/publishWithType",
  126. mqttType: "previous",
  127. })
  128. .then((result) => {})
  129. .catch((err) => {
  130. console.warn(err);
  131. });
  132. },
  133. //播放
  134. play() {
  135. this.$store
  136. .dispatch({
  137. type: "moduleMqtt/publishWithType",
  138. mqttType: "play",
  139. })
  140. .then((result) => {})
  141. .catch((err) => {
  142. console.warn(err);
  143. });
  144. },
  145. //暂停
  146. pause() {
  147. this.$store
  148. .dispatch({
  149. type: "moduleMqtt/publishWithType",
  150. mqttType: "pause",
  151. })
  152. .then((result) => {})
  153. .catch((err) => {
  154. console.warn(err);
  155. });
  156. },
  157. //设置音量
  158. setVolume(event) {
  159. console.log(event.target.value);
  160. this.$store
  161. .dispatch({
  162. type: "moduleMqtt/publishWithType",
  163. mqttType: "volume_set",
  164. other: {
  165. volume: event.target.value,
  166. },
  167. })
  168. .then((result) => {})
  169. .catch((err) => {
  170. console.warn(err);
  171. });
  172. },
  173. //设置频道
  174. setChannel(event) {
  175. console.log(event.target.value);
  176. this.$store
  177. .dispatch({
  178. type: "moduleMqtt/publishWithType",
  179. mqttType: "play",
  180. other: {
  181. user_id: "650633",
  182. timestamp: "", //暂时没有时间戳,先不填
  183. channel_id: event.target.value + "",
  184. url: "",
  185. media_data: "",
  186. order: "",
  187. resource_from: "",
  188. },
  189. })
  190. .then((result) => {})
  191. .catch((err) => {
  192. console.warn(err);
  193. });
  194. },
  195. goDeviceInfo() {
  196. //console.log("goDeviceInfo");
  197. uni.navigateTo({
  198. url: "./deviceInfo",
  199. complete(res) {
  200. console.warn(res);
  201. }
  202. });
  203. },
  204. godoBle() {
  205. //添加设备
  206. // #ifdef MP-WEIXIN||APP-PLUS
  207. uni.navigateTo({
  208. url: "../ble/ScanBleDevice",
  209. });
  210. // #endif
  211. // #ifdef H5
  212. uni.navigateTo({
  213. url: "../ble/ConnectBleDevice",
  214. });
  215. // uni.showToast({
  216. // title:'H5页面不支持扫描设备',
  217. // icon:'none'
  218. // })
  219. // #endif
  220. },
  221. },
  222. computed: {
  223. // deviceList() {
  224. // return this.$store.state.moduleMqtt.deviceList;
  225. // },
  226. ...mapState({
  227. deviceList: (state) => state.moduleMqtt.deviceList,
  228. playInfo: (state) => state.moduleMqtt.playInfo,
  229. currentDevice: (state) => state.moduleMqtt.currentDevice,
  230. }),
  231. },
  232. };
  233. </script>
  234. <style>
  235. .content {
  236. display: flex;
  237. flex-direction: column;
  238. }
  239. .play-info {
  240. display: flex;
  241. flex-direction: column;
  242. align-items: center;
  243. }
  244. .dbox {
  245. display: flex;
  246. flex-direction: row;
  247. width: 100vw;
  248. justify-content: flex-start;
  249. }
  250. .btn {
  251. margin-left: 0rpx;
  252. margin-right: 10rpx;
  253. width: 160rpx;
  254. height: 25vw;
  255. /* display: flex; */
  256. /* justify-content: center;
  257. align-items: center; */
  258. line-height: 25vw;
  259. border: 1rpx solid #333333;
  260. font-size: 120rpx;
  261. }
  262. .text1 {
  263. width: 150px;
  264. word-break: break-all;
  265. /* text-overflow:ellipsis;
  266. overflow: hidden;
  267. white-space: nowrap; */
  268. }
  269. .play-thmub {
  270. display: flex;
  271. align-self: center;
  272. width: 300rpx;
  273. height: 300rpx;
  274. margin-top: 30rpx;
  275. }
  276. .play-ctrl {
  277. display: flex;
  278. flex-direction: row;
  279. justify-content: space-around;
  280. margin-top: 40rpx;
  281. }
  282. .next-icon {
  283. width: 54rpx;
  284. height: 60rpx;
  285. }
  286. .progress {
  287. align-self: center;
  288. width: 80vw;
  289. }
  290. </style>