home.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view class="container">
  3. <view class="pages">
  4. <Home ref="home" v-if="tabName=='首页'" />
  5. <Equipment ref="equipment" v-if="tabName=='设备'" />
  6. <Commodity ref="commodity" v-if="tabName=='商品'" />
  7. <Account v-if="tabName=='我的'" />
  8. </view>
  9. <u-tabbar v-if="menu&&menu.length>0" :value="current" @change="tabChange" :fixed="true" :placeholder="false"
  10. :safeAreaInsetBottom="true">
  11. <u-tabbar-item :text="item.name" v-for="(item,index) in menu" :key="item.id" @click="tabClick(item)">
  12. <image class="u-page__item__slot-icon" slot="inactive-icon" :src="tabIcon[item.name][0]"></image>
  13. <image class="u-page__item__slot-icon" slot="active-icon" :src="tabIcon[item.name][1]"></image>
  14. </u-tabbar-item>
  15. </u-tabbar>
  16. </view>
  17. </template>
  18. <script>
  19. import Home from './components/home.vue'
  20. import Equipment from './components/equipment.vue'
  21. import Commodity from './components/commodity.vue'
  22. import Account from './components/account.vue'
  23. let mqtt = require('../../static/js/mqtt.min.js')
  24. export default {
  25. components: {
  26. Home,
  27. Equipment,
  28. Commodity,
  29. Account
  30. },
  31. data() {
  32. return {
  33. current: 0,
  34. tabIcon: {
  35. '首页': [require('../../static/images/tabbar/home.png'), require(
  36. '../../static/images/tabbar/home_.png')],
  37. '设备': [require('../../static/images/tabbar/equipment.png'), require(
  38. '../../static/images/tabbar/equipment_.png')],
  39. '商品': [require('../../static/images/tabbar/commodity.png'), require(
  40. '../../static/images/tabbar/commodity_.png')],
  41. '我的': [require('../../static/images/tabbar/mine.png'), require(
  42. '../../static/images/tabbar/mine_.png')],
  43. },
  44. client: null,
  45. tabName: null
  46. }
  47. },
  48. onShow() {
  49. // 版本自动更新代码
  50. const updateManager = wx.getUpdateManager()
  51. updateManager.onUpdateReady(function() {
  52. wx.showModal({
  53. title: '更新检测',
  54. content: '检测到新版本,是否重启小程序?',
  55. success: function(res) {
  56. if (res.confirm) {
  57. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  58. updateManager.applyUpdate()
  59. }
  60. }
  61. })
  62. })
  63. updateManager.onUpdateFailed(function() {
  64. // 新的版本下载失败
  65. wx.showModal({
  66. title: '已有新版本咯',
  67. content: '请您删除当前小程序,重新打开呦~',
  68. showCancel: false
  69. })
  70. })
  71. //onshow刷新设备状态
  72. if(this.current==0){
  73. this.$refs.home.onshow()
  74. }
  75. if (this.current == 1) {
  76. this.$refs.equipment.onshow()
  77. }
  78. if(this.current==2){
  79. this.$refs.commodity.onshow()
  80. }
  81. },
  82. computed: {
  83. menu() {
  84. return JSON.parse(this.$store.state.permission.permissions_menu)
  85. },
  86. },
  87. watch: {
  88. menu: {
  89. handler(newVal, oldVal) {
  90. if (newVal.length > 0) {
  91. this.tabName = newVal[0].name
  92. } else {
  93. this.tabName = null
  94. }
  95. },
  96. deep: true,
  97. immediate: true
  98. }
  99. },
  100. onLoad(o) {
  101. if (o.tabName) { //公库添加商品到私库完毕,显示商品界面
  102. this.tabName = o.tabName;
  103. if (this.menu && this.menu.length > 0) {
  104. this.menu.forEach((item, index) => {
  105. if (item.name == o.tabName) {
  106. this.current = index
  107. }
  108. })
  109. }
  110. }
  111. // this.mqttConnect()
  112. },
  113. methods: {
  114. mqttConnect() {
  115. let _this = this;
  116. //测试用,生产通过接口(/sys/user-info/webUserMqtt)获取options
  117. const options = {
  118. clean: true, // true: 清除会话, false: 保留会话
  119. connectTimeout: 4000, // 超时时间
  120. // 认证信息
  121. clientId: 'web-user-wxc-1',
  122. username: 'webuser',
  123. password: 'xy20220101',
  124. }
  125. // 连接字符串, 通过协议指定使用的连接方式
  126. // ws 未加密 WebSocket 连接
  127. // wss 加密 WebSocket 连接
  128. // mqtt 未加密 TCP 连接
  129. // mqtts 加密 TCP 连接
  130. // wxs 微信小程序连接
  131. // alis 支付宝小程序连接
  132. const connectUrl = 'wxs://mqtt.mxrvending.com:8084/mqtt'
  133. this.client = mqtt.connect(connectUrl, options)
  134. this.client.on('connect', function() {
  135. //订阅消息
  136. _this.client.subscribe('web-user-wxc-1', function(err) {
  137. if (!err) {
  138. console.log('订阅成功', err)
  139. } else {
  140. console.log('订阅失败', err)
  141. }
  142. })
  143. }).on('reconnect', function() {
  144. console.log('正在重连')
  145. }).on('error', function() {
  146. console.log('错误')
  147. }).on('end', function() {
  148. console.log('连接结束')
  149. }).on('message', function(topic, message) {
  150. console.log('接收到的消息==========>>>>>>>>', message.toString())
  151. })
  152. },
  153. tabChange(e) {
  154. this.current = e
  155. },
  156. tabClick(e) {
  157. this.tabName = e.name
  158. }
  159. }
  160. }
  161. </script>
  162. <style lang="scss">
  163. .container {
  164. .pages {
  165. height: 100%;
  166. }
  167. .u-page__item__slot-icon {
  168. width: 44rpx;
  169. height: 44rpx;
  170. }
  171. }
  172. </style>