index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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=false">
  9. 储充设备
  10. </view>
  11. <view class="btn" :class="isSlecte?'active':''" @click="isSlecte=true">
  12. 机器人
  13. </view>
  14. </view>
  15. </view>
  16. <view class="main">
  17. <view class="main_one" v-show="isSlecte">
  18. <automatic :robotInfoVo="robotInfoVo" :robotcode='robotcode'></automatic>
  19. </view>
  20. <view class="main_two" v-show="!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: false, //切换页面
  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. destroyed(){
  61. clearInterval(this.timer)
  62. this.ws.close()
  63. },
  64. onUnload() {
  65. console.log('onUnload')
  66. if(this.timer!=null){
  67. clearInterval(this.timer)
  68. this.timer = null
  69. }
  70. if(this.ws!=null){
  71. this.ws.close()
  72. this.ws=null
  73. }
  74. if(this.heartbeatTimer!=null){
  75. this.heartbeatTimer.close()
  76. this.heartbeatTimer=null
  77. }
  78. },
  79. methods: {
  80. //判断websocket是否链接上
  81. InitWs() {
  82. uni.showLoading({
  83. title:'数据加载中...'
  84. })
  85. let token = this.$storage.getJson("token");
  86. let users = this.$storage.getJson("users");
  87. // if (users == null || token == null) {
  88. // uni.navigateTo("public/login");
  89. // return false;
  90. // }
  91. if (this.ws != null) {
  92. this.ws.close()
  93. this.ws = null
  94. }
  95. if (this.timer != null) {
  96. clearInterval(this.timer)
  97. this.timer = null
  98. }
  99. // console.log(this.$config.web_socket_url, 'this.$config.web_socket_url')
  100. this.ws = uni.connectSocket({
  101. url: this.$config.web_socket_url + token,
  102. header: {
  103. 'content-type': 'application/json'
  104. },
  105. complete: () => {}
  106. })
  107. this.ws.onOpen(() => {
  108. // uni.showLoading({
  109. // title: '数据加载中...'
  110. // });
  111. let wsMessage = {
  112. type: 'page',
  113. payload: 'equipment-monitoring'
  114. }
  115. let data = JSON.stringify(wsMessage)
  116. this.ws.send({
  117. data
  118. })
  119. })
  120. this.ws.onMessage((res) => {
  121. this.onMessageTime = new Date();
  122. let data = JSON.parse(res.data)
  123. // console.log(data)
  124. //请求成功 开始赋值
  125. if (data.robotInfoVo) {
  126. this.robotInfoVo = data.robotInfoVo;
  127. this.robotcode = data.batChooseMode;
  128. // console.log(this.robotcode,'robotInfoVos99999')
  129. }
  130. if (data.storeInfoList) {
  131. data.storeInfoList.forEach(item=>{
  132. if(item.chgType == 0){
  133. item.showCodeNum = 0 //仓内充电 显示电池
  134. }else{
  135. if(!item.batInfoOneVo.sn && item.batInfoTwoVo.sn){ //A枪不存在数据B枪有数据
  136. item.showCodeNum = 2
  137. }else{
  138. item.showCodeNum = 1
  139. }
  140. }
  141. })
  142. this.storeInfoLists = data.storeInfoList;
  143. }
  144. })
  145. // 监听WebSocket关闭事件
  146. this.ws.onClose(() => {
  147. console.log('WebSocket连接已关闭!');
  148. })
  149. this.ws.onError(() => {
  150. console.log("WebSocket连接错误")
  151. })
  152. if(this.timer == null){
  153. //检查断开重连
  154. this.timer = setInterval(() => {
  155. if(this.ws==null){
  156. console.log('ws为null,检查断开重连')
  157. this.InitWs()
  158. }else if(this.ws.readyState != 1){
  159. this.ws.close()
  160. this.ws = null
  161. console.log('未连接成功,检查断开重连')
  162. this.InitWs()
  163. }
  164. }, 2000)
  165. }
  166. },
  167. // sendExchangeMsg(msg) {
  168. // this.ws.send(msg)
  169. // },
  170. jumpError(title) {
  171. uni.redirectTo({
  172. url: '/pages/public/404?error=' + title
  173. })
  174. }
  175. }
  176. }
  177. </script>
  178. <style lang="scss" scoped>
  179. .content {
  180. width: 100%;
  181. color: white;
  182. font-size: 24px;
  183. .header {
  184. display: flex;
  185. justify-content: space-between;
  186. // margin-bottom: 16px;
  187. .header_left {
  188. font-size: 16px;
  189. font-family: PingFang SC-Semibold, PingFang SC;
  190. font-weight: 600;
  191. color: rgba(255, 255, 255, 0.85);
  192. text-shadow: 0px 0px 4px #0027D8;
  193. }
  194. .tabar_btn {
  195. display: flex;
  196. .btn {
  197. width: 100px;
  198. height: 48px;
  199. background: #263042;
  200. border-radius: 0px 2px 2px 0px;
  201. font-size: 16px;
  202. text-align: center;
  203. line-height: 48px;
  204. }
  205. .active {
  206. background: #91FDB9;
  207. border-radius: 2px 0px 0px 2px;
  208. color: black;
  209. }
  210. }
  211. }
  212. .main {
  213. display: flex;
  214. width: 100%;
  215. height: 650px;
  216. .main_one,
  217. .main_two {
  218. width: 100%;
  219. height: 100%;
  220. }
  221. }
  222. }
  223. </style>