mqttDemo.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. }else{
  71. uni.showModal({
  72. title:"温馨提示",
  73. content:"请登录!",
  74. success() {
  75. uni.navigateTo({
  76. url:'../../login/login'
  77. })
  78. }
  79. })
  80. }
  81. },
  82. //获取播放信息
  83. getPlayInfo() {
  84. this.$store
  85. .dispatch({
  86. type: "moduleMqtt/publishWithType",
  87. mqttType: "get_position",
  88. })
  89. .then((result) => {})
  90. .catch((err) => {
  91. console.warn(err);
  92. });
  93. },
  94. //获取设备信息
  95. getDeviceInfo() {
  96. this.$store
  97. .dispatch({
  98. type: "moduleMqtt/publishWithType",
  99. mqttType: "get_dev_info",
  100. })
  101. .then((result) => {})
  102. .catch((err) => {
  103. console.warn(err);
  104. });
  105. },
  106. //下一曲
  107. next() {
  108. this.$store
  109. .dispatch({
  110. type: "moduleMqtt/publishWithType",
  111. mqttType: "next",
  112. })
  113. .then((result) => {})
  114. .catch((err) => {
  115. console.warn(err);
  116. });
  117. },
  118. //上一曲
  119. previous() {
  120. this.$store
  121. .dispatch({
  122. type: "moduleMqtt/publishWithType",
  123. mqttType: "previous",
  124. })
  125. .then((result) => {})
  126. .catch((err) => {
  127. console.warn(err);
  128. });
  129. },
  130. //播放
  131. play() {
  132. this.$store
  133. .dispatch({
  134. type: "moduleMqtt/publishWithType",
  135. mqttType: "play",
  136. })
  137. .then((result) => {})
  138. .catch((err) => {
  139. console.warn(err);
  140. });
  141. },
  142. //暂停
  143. pause() {
  144. this.$store
  145. .dispatch({
  146. type: "moduleMqtt/publishWithType",
  147. mqttType: "pause",
  148. })
  149. .then((result) => {})
  150. .catch((err) => {
  151. console.warn(err);
  152. });
  153. },
  154. //设置音量
  155. setVolume(event) {
  156. console.log(event.target.value);
  157. this.$store
  158. .dispatch({
  159. type: "moduleMqtt/publishWithType",
  160. mqttType: "volume_set",
  161. other: {
  162. volume: event.target.value,
  163. },
  164. })
  165. .then((result) => {})
  166. .catch((err) => {
  167. console.warn(err);
  168. });
  169. },
  170. //设置频道
  171. setChannel(event) {
  172. console.log(event.target.value);
  173. this.$store
  174. .dispatch({
  175. type: "moduleMqtt/publishWithType",
  176. mqttType: "play",
  177. other: {
  178. user_id: "650633",
  179. timestamp: "", //暂时没有时间戳,先不填
  180. channel_id: event.target.value + "",
  181. url: "",
  182. media_data: "",
  183. order: "",
  184. resource_from: "",
  185. },
  186. })
  187. .then((result) => {})
  188. .catch((err) => {
  189. console.warn(err);
  190. });
  191. },
  192. goDeviceInfo() {
  193. //console.log("goDeviceInfo");
  194. uni.navigateTo({
  195. url: "./deviceInfo",
  196. complete(res) {
  197. console.warn(res);
  198. }
  199. });
  200. },
  201. godoBle() {
  202. //添加设备
  203. // #ifdef MP-WEIXIN||APP-PLUS
  204. uni.navigateTo({
  205. url: "../ble/ScanBleDevice",
  206. });
  207. // #endif
  208. // #ifdef H5
  209. uni.navigateTo({
  210. url: "../ble/ConnectBleDevice",
  211. });
  212. // uni.showToast({
  213. // title:'H5页面不支持扫描设备',
  214. // icon:'none'
  215. // })
  216. // #endif
  217. },
  218. },
  219. computed: {
  220. // deviceList() {
  221. // return this.$store.state.moduleMqtt.deviceList;
  222. // },
  223. ...mapState({
  224. deviceList: (state) => state.moduleMqtt.deviceList,
  225. playInfo: (state) => state.moduleMqtt.playInfo,
  226. currentDevice: (state) => state.moduleMqtt.currentDevice,
  227. }),
  228. },
  229. };
  230. </script>
  231. <style>
  232. .content {
  233. display: flex;
  234. flex-direction: column;
  235. }
  236. .play-info {
  237. display: flex;
  238. flex-direction: column;
  239. align-items: center;
  240. }
  241. .dbox {
  242. display: flex;
  243. flex-direction: row;
  244. width: 100vw;
  245. justify-content: flex-start;
  246. }
  247. .btn {
  248. margin-left: 0rpx;
  249. margin-right: 10rpx;
  250. width: 160rpx;
  251. height: 25vw;
  252. /* display: flex; */
  253. /* justify-content: center;
  254. align-items: center; */
  255. line-height: 25vw;
  256. border: 1rpx solid #333333;
  257. font-size: 120rpx;
  258. }
  259. .text1 {
  260. width: 150px;
  261. word-break: break-all;
  262. /* text-overflow:ellipsis;
  263. overflow: hidden;
  264. white-space: nowrap; */
  265. }
  266. .play-thmub {
  267. display: flex;
  268. align-self: center;
  269. width: 300rpx;
  270. height: 300rpx;
  271. margin-top: 30rpx;
  272. }
  273. .play-ctrl {
  274. display: flex;
  275. flex-direction: row;
  276. justify-content: space-around;
  277. margin-top: 40rpx;
  278. }
  279. .next-icon {
  280. width: 54rpx;
  281. height: 60rpx;
  282. }
  283. .progress {
  284. align-self: center;
  285. width: 80vw;
  286. }
  287. </style>