index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <view class="content">
  3. <view class="header">
  4. <!-- <view class="header_left">
  5. {{isSlecte?'机器人':'储充设备'}}
  6. </view> -->
  7. <view class="tabar_btn">
  8. <view class="btn" :class="isSlecte?'active':''" @click="isSlecte=true">
  9. 机器人
  10. </view>
  11. <view class="btn" :class="!isSlecte?'active':''" @click="isSlecte=false">
  12. 储充设备
  13. </view>
  14. </view>
  15. </view>
  16. <view class="main">
  17. <view class="main_one" v-if="isSlecte">
  18. <automatic :robotInfoVo = "robotInfoVo" :robotcode = 'robotcode' ></automatic>
  19. </view>
  20. <view class="main_two" v-if="!isSlecte">
  21. <manual :storeInfoLists = "storeInfoLists"></manual>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import automatic from "./components/automatic.vue"
  28. import manual from "./components/manual.vue"
  29. export default {
  30. components: {
  31. manual,
  32. automatic
  33. },
  34. data() {
  35. return {
  36. isSlecte: true,//切换页面
  37. getStationCode: null,//这个暂时没有
  38. ws: null,//websuokit 链接器
  39. timer: null,//记录链接的方法 方便清除
  40. storeInfoLists:[],//储充设备数据
  41. robotInfoVo:{},//机器人数据
  42. robotcode:0,//机器人模式
  43. }
  44. },
  45. mounted() {
  46. this.InitWs()
  47. },
  48. onUnload() {
  49. console.log('onUnload')
  50. clearInterval(this.timer)
  51. this.timer = null
  52. this.ws.close()
  53. },
  54. computed: {
  55. pageState() {
  56. if (this.swapState === -1) {
  57. return 0
  58. } else if (this.swapState === 1) {
  59. return 1 //可以发起换电操作
  60. } else {
  61. return 2 //换电中,不能发起换电
  62. }
  63. }
  64. },
  65. watch: {
  66. pageState(val) {
  67. if (val === 0) {
  68. uni.showLoading({
  69. title: ''
  70. });
  71. } else {
  72. uni.hideLoading();
  73. }
  74. }
  75. },
  76. methods: {
  77. //判断websocket是否链接上
  78. InitWs() {
  79. let token = this.$storage.getJson("token");
  80. let users = this.$storage.getJson("users");
  81. // if (users == null || token == null) {
  82. // uni.navigateTo("public/login");
  83. // return false;
  84. // }
  85. if (this.ws != null) {
  86. this.ws.close()
  87. this.ws = null
  88. }
  89. if (this.timer != null) {
  90. clearInterval(this.timer)
  91. this.timer = null
  92. }
  93. console.log(this.$config.web_socket_url, 'this.$config.web_socket_url')
  94. this.ws = uni.connectSocket({
  95. url: this.$config.web_socket_url + token,
  96. header: {
  97. 'content-type': 'application/json'
  98. },
  99. complete: () => {}
  100. })
  101. this.ws.onOpen(() => {
  102. // uni.showLoading({
  103. // title: '数据加载中...'
  104. // });
  105. let wsMessage={
  106. type: 'page',
  107. payload: 'equipment-monitoring'
  108. }
  109. let data = JSON.stringify(wsMessage)
  110. this.ws.send({data})
  111. })
  112. this.ws.onMessage((res) => {
  113. let data = JSON.parse(res.data)
  114. // console.log(data,'resws')
  115. if (res) {
  116. uni.hideLoading()
  117. }
  118. console.log(data)
  119. //请求成功 开始赋值
  120. if (data.robotInfoVo) {
  121. this.robotInfoVo = data.robotInfoVo;
  122. this.robotcode = data.batChooseMode;
  123. // console.log(this.robotcode,'robotInfoVos99999')
  124. }
  125. if (data.storeInfoList) {
  126. this.storeInfoLists = data.storeInfoList;
  127. }
  128. })
  129. // 监听WebSocket关闭事件
  130. this.ws.onClose(() => {
  131. console.log('WebSocket连接已关闭!');
  132. })
  133. this.ws.onError(() => {
  134. console.log("WebSocket连接错误")
  135. })
  136. if (this.timer == null) {
  137. this.timer = setInterval(() => {
  138. if (this.ws.readyState != 1) {
  139. clearInterval(this.timer)
  140. this.timer = null
  141. this.ws.close()
  142. this.InitWs()
  143. }
  144. }, 2000)
  145. }
  146. },
  147. // sendExchangeMsg(msg) {
  148. // this.ws.send(msg)
  149. // },
  150. jumpError(title) {
  151. uni.redirectTo({
  152. url: '/pages/public/404?error=' + title
  153. })
  154. }
  155. }
  156. }
  157. </script>
  158. <style lang="scss" scoped>
  159. .content {
  160. width: 100%;
  161. color: white;
  162. font-size: 24px;
  163. .header {
  164. display: flex;
  165. justify-content: space-between;
  166. // margin-bottom: 16px;
  167. .header_left {
  168. font-size: 16px;
  169. font-family: PingFang SC-Semibold, PingFang SC;
  170. font-weight: 600;
  171. color: rgba(255, 255, 255, 0.85);
  172. text-shadow: 0px 0px 4px #0027D8;
  173. }
  174. .tabar_btn {
  175. display: flex;
  176. .btn {
  177. width: 100px;
  178. height: 48px;
  179. background: #263042;
  180. border-radius: 0px 2px 2px 0px;
  181. font-size: 16px;
  182. text-align: center;
  183. line-height: 48px;
  184. }
  185. .active {
  186. background: #91FDB9;
  187. border-radius: 2px 0px 0px 2px;
  188. color: black;
  189. }
  190. }
  191. }
  192. .main {
  193. display: flex;
  194. width: 100%;
  195. height: 650px;
  196. .main_one,
  197. .main_two {
  198. width: 100%;
  199. height: 100%;
  200. }
  201. }
  202. }
  203. </style>