index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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({
  132. data
  133. })
  134. })
  135. this.ws.onMessage((res) => {
  136. this.onMessageTime = new Date();
  137. let data = JSON.parse(res.data)
  138. // console.log(data,'resws')
  139. if (res) {
  140. uni.hideLoading()
  141. }
  142. console.log(data)
  143. //请求成功 开始赋值
  144. if (data.robotInfoVo) {
  145. this.robotInfoVo = data.robotInfoVo;
  146. this.robotcode = data.batChooseMode;
  147. // console.log(this.robotcode,'robotInfoVos99999')
  148. }
  149. if (data.storeInfoList) {
  150. data.storeInfoList.forEach(item=>{
  151. if(item.chgType == 0){
  152. item.showCodeNum = 0 //仓内充电 显示电池
  153. }else{
  154. if(!item.batInfoOneVo.sn && item.batInfoTwoVo.sn){ //A枪不存在数据B枪有数据
  155. item.showCodeNum = 2
  156. }else{
  157. item.showCodeNum = 1
  158. }
  159. }
  160. })
  161. this.storeInfoLists = data.storeInfoList;
  162. }
  163. })
  164. // 监听WebSocket关闭事件
  165. this.ws.onClose(() => {
  166. console.log('WebSocket连接已关闭!');
  167. })
  168. this.ws.onError(() => {
  169. console.log("WebSocket连接错误")
  170. })
  171. if(this.timer == null){
  172. //检查断开重连
  173. this.timer = setInterval(() => {
  174. if(this.ws==null){
  175. console.log('ws为null,检查断开重连')
  176. this.InitWs()
  177. }else if(this.ws.readyState != 1){
  178. this.ws.close()
  179. this.ws = null
  180. console.log('未连接成功,检查断开重连')
  181. this.InitWs()
  182. }
  183. }, 2000)
  184. }
  185. },
  186. // sendExchangeMsg(msg) {
  187. // this.ws.send(msg)
  188. // },
  189. jumpError(title) {
  190. uni.redirectTo({
  191. url: '/pages/public/404?error=' + title
  192. })
  193. }
  194. }
  195. }
  196. </script>
  197. <style lang="scss" scoped>
  198. .content {
  199. width: 100%;
  200. color: white;
  201. font-size: 24px;
  202. .header {
  203. display: flex;
  204. justify-content: space-between;
  205. // margin-bottom: 16px;
  206. .header_left {
  207. font-size: 16px;
  208. font-family: PingFang SC-Semibold, PingFang SC;
  209. font-weight: 600;
  210. color: rgba(255, 255, 255, 0.85);
  211. text-shadow: 0px 0px 4px #0027D8;
  212. }
  213. .tabar_btn {
  214. display: flex;
  215. .btn {
  216. width: 100px;
  217. height: 48px;
  218. background: #263042;
  219. border-radius: 0px 2px 2px 0px;
  220. font-size: 16px;
  221. text-align: center;
  222. line-height: 48px;
  223. }
  224. .active {
  225. background: #91FDB9;
  226. border-radius: 2px 0px 0px 2px;
  227. color: black;
  228. }
  229. }
  230. }
  231. .main {
  232. display: flex;
  233. width: 100%;
  234. height: 650px;
  235. .main_one,
  236. .main_two {
  237. width: 100%;
  238. height: 100%;
  239. }
  240. }
  241. }
  242. </style>