index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. heartbeatTimer:null,
  44. onMessageTime:null,
  45. }
  46. },
  47. mounted() {
  48. this.onMessageTime = new Date();
  49. this.InitWs()
  50. this.heartbeatTimer = setInterval(()=>{
  51. let newDate = new Date();
  52. if(newDate-this.onMessageTime>3000){
  53. if(this.ws!=null){
  54. this.ws.close()
  55. this.ws=null
  56. }
  57. }
  58. },3000)
  59. },
  60. onUnload() {
  61. console.log('onUnload')
  62. if(this.timer!=null){
  63. clearInterval(this.timer)
  64. this.timer = null
  65. }
  66. if(this.ws!=null){
  67. this.ws.close()
  68. this.ws=null
  69. }
  70. if(this.heartbeatTimer!=null){
  71. this.heartbeatTimer.close()
  72. this.heartbeatTimer=null
  73. }
  74. },
  75. computed: {
  76. pageState() {
  77. if (this.swapState === -1) {
  78. return 0
  79. } else if (this.swapState === 1) {
  80. return 1 //可以发起换电操作
  81. } else {
  82. return 2 //换电中,不能发起换电
  83. }
  84. }
  85. },
  86. watch: {
  87. pageState(val) {
  88. if (val === 0) {
  89. uni.showLoading({
  90. title: ''
  91. });
  92. } else {
  93. uni.hideLoading();
  94. }
  95. }
  96. },
  97. methods: {
  98. //判断websocket是否链接上
  99. InitWs() {
  100. let token = this.$storage.getJson("token");
  101. let users = this.$storage.getJson("users");
  102. // if (users == null || token == null) {
  103. // uni.navigateTo("public/login");
  104. // return false;
  105. // }
  106. if (this.ws != null) {
  107. this.ws.close()
  108. this.ws = null
  109. }
  110. if (this.timer != null) {
  111. clearInterval(this.timer)
  112. this.timer = null
  113. }
  114. console.log(this.$config.web_socket_url, 'this.$config.web_socket_url')
  115. this.ws = uni.connectSocket({
  116. url: this.$config.web_socket_url + token,
  117. header: {
  118. 'content-type': 'application/json'
  119. },
  120. complete: () => {}
  121. })
  122. this.ws.onOpen(() => {
  123. // uni.showLoading({
  124. // title: '数据加载中...'
  125. // });
  126. let wsMessage={
  127. type: 'page',
  128. payload: 'equipment-monitoring'
  129. }
  130. let data = JSON.stringify(wsMessage)
  131. this.ws.send({data})
  132. })
  133. this.ws.onMessage((res) => {
  134. this.onMessageTime = new Date();
  135. let data = JSON.parse(res.data)
  136. // console.log(data,'resws')
  137. if (res) {
  138. uni.hideLoading()
  139. }
  140. console.log(data)
  141. //请求成功 开始赋值
  142. if (data.robotInfoVo) {
  143. this.robotInfoVo = data.robotInfoVo;
  144. this.robotcode = data.batChooseMode;
  145. // console.log(this.robotcode,'robotInfoVos99999')
  146. }
  147. if (data.storeInfoList) {
  148. this.storeInfoLists = data.storeInfoList;
  149. }
  150. })
  151. // 监听WebSocket关闭事件
  152. this.ws.onClose(() => {
  153. console.log('WebSocket连接已关闭!');
  154. })
  155. this.ws.onError(() => {
  156. console.log("WebSocket连接错误")
  157. })
  158. if(this.timer == null){
  159. //检查断开重连
  160. this.timer = setInterval(() => {
  161. if(this.ws==null){
  162. console.log('ws为null,检查断开重连')
  163. this.InitWs()
  164. }else if(this.ws.readyState != 1){
  165. this.ws.close()
  166. this.ws = null
  167. console.log('未连接成功,检查断开重连')
  168. this.InitWs()
  169. }
  170. }, 2000)
  171. }
  172. },
  173. // sendExchangeMsg(msg) {
  174. // this.ws.send(msg)
  175. // },
  176. jumpError(title) {
  177. uni.redirectTo({
  178. url: '/pages/public/404?error=' + title
  179. })
  180. }
  181. }
  182. }
  183. </script>
  184. <style lang="scss" scoped>
  185. .content {
  186. width: 100%;
  187. color: white;
  188. font-size: 24px;
  189. .header {
  190. display: flex;
  191. justify-content: space-between;
  192. // margin-bottom: 16px;
  193. .header_left {
  194. font-size: 16px;
  195. font-family: PingFang SC-Semibold, PingFang SC;
  196. font-weight: 600;
  197. color: rgba(255, 255, 255, 0.85);
  198. text-shadow: 0px 0px 4px #0027D8;
  199. }
  200. .tabar_btn {
  201. display: flex;
  202. .btn {
  203. width: 100px;
  204. height: 48px;
  205. background: #263042;
  206. border-radius: 0px 2px 2px 0px;
  207. font-size: 16px;
  208. text-align: center;
  209. line-height: 48px;
  210. }
  211. .active {
  212. background: #91FDB9;
  213. border-radius: 2px 0px 0px 2px;
  214. color: black;
  215. }
  216. }
  217. }
  218. .main {
  219. display: flex;
  220. width: 100%;
  221. height: 650px;
  222. .main_one,
  223. .main_two {
  224. width: 100%;
  225. height: 100%;
  226. }
  227. }
  228. }
  229. </style>