App.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <script>
  2. import md5Libs from '@/uni_modules/uview-ui/libs/function/md5';
  3. import Vue from 'vue';
  4. let that;
  5. export default {
  6. data() {
  7. return {
  8. timeout: 30000, // 30s
  9. timeoutObj: null
  10. };
  11. },
  12. created() {
  13. // .判断是否已连接
  14. this.checkOpenSocket();
  15. // #ifdef APP-PLUS
  16. plus.navigator.closeSplashscreen();
  17. // #endif
  18. },
  19. onLaunch: async function () {
  20. console.log('App Launch');
  21. uni.hideTabBar();
  22. //加载配置
  23. uni.getSystemInfo({
  24. success: function (e) {
  25. // #ifndef MP
  26. Vue.prototype.StatusBar = e.statusBarHeight;
  27. if (e.platform == 'android') {
  28. Vue.prototype.CustomBar = e.statusBarHeight + 50;
  29. } else {
  30. Vue.prototype.CustomBar = e.statusBarHeight + 45;
  31. }
  32. // #endif
  33. // #ifdef MP-WEIXIN
  34. Vue.prototype.StatusBar = e.statusBarHeight;
  35. let custom = wx.getMenuButtonBoundingClientRect();
  36. Vue.prototype.Custom = custom;
  37. Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
  38. // #endif
  39. // #ifdef MP-ALIPAY
  40. Vue.prototype.StatusBar = e.statusBarHeight;
  41. Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
  42. // #endif
  43. }
  44. });
  45. let res = await this.$api.getConfig();
  46. // console.log(res);
  47. if (!res.code) {
  48. return;
  49. }
  50. this.vuex_config = res.data;
  51. },
  52. onShow: function () {
  53. console.log('App 开启');
  54. uni.hideTabBar();
  55. },
  56. onHide: function () {
  57. console.log('App 关闭');
  58. },
  59. onLoad: function () {
  60. that = this;
  61. uni.hideTabBar();
  62. },
  63. methods: {
  64. // 判断是否已连接
  65. checkOpenSocket() {
  66. uni.sendSocketMessage({
  67. data: 'ping',
  68. success: (res) => {
  69. return;
  70. },
  71. fail: (err) => {
  72. // 未连接打开websocket连接
  73. this.openConnection();
  74. }
  75. });
  76. },
  77. openConnection() {
  78. // 打开连接
  79. // uni.closeSocket(); // 确保已经关闭后再重新打开
  80. uni.connectSocket({
  81. url: 'ws://192.168.10.80:8002',
  82. success(res) {
  83. console.log('连接成功 connectSocket=', res);
  84. },
  85. fail(err) {
  86. console.log('连接失败 connectSocket=', err);
  87. }
  88. });
  89. uni.onSocketOpen((res) => {
  90. console.log('连接成功');
  91. this.send();
  92. });
  93. this.onSocketMessage(); // 打开成功监听服务器返回的消息
  94. },
  95. send() {
  96. uni.sendSocketMessage({
  97. data: '{"command":"getshowpage"}',
  98. success: (res) => {
  99. console.log(res);
  100. console.log('发送成功....');
  101. },
  102. fail: (err) => {
  103. console.log(res);
  104. console.log('连接失败重新连接....');
  105. this.openConnection();
  106. }
  107. });
  108. },
  109. onSocketMessage() {
  110. // 消息
  111. this.timeout = 30000;
  112. this.timeoutObj = null;
  113. uni.onSocketMessage((res) => {
  114. console.log(res);
  115. this.getSocketMsg(res.data); // 监听到有新服务器消息
  116. });
  117. },
  118. // 监听到有新服务器消息
  119. getSocketMsg(reData) {
  120. // 监听到服务器消息
  121. console.log('收到服务器消息', reData);
  122. if (reData == 'pong') {
  123. return;
  124. }
  125. let data = JSON.parse(reData);
  126. if (data.command == 'updatepage') {
  127. if (data.id == 0) {
  128. uni.navigateTo({
  129. url: '/pages/index/startPage'
  130. });
  131. }
  132. if (data.id == 1) {
  133. uni.navigateTo({
  134. url: '/pages/index/index'
  135. });
  136. }
  137. }
  138. this.reset(); // 检测心跳reset,防止长时间连接导致连接关闭
  139. },
  140. // 检测心跳reset
  141. reset() {
  142. clearInterval(this.timeoutObj);
  143. this.start(); // 启动心跳
  144. },
  145. // 启动心跳 start
  146. start() {
  147. this.timeoutObj = setInterval(function () {
  148. uni.sendSocketMessage({
  149. data: 'ping',
  150. success: (res) => {
  151. console.log('连接中....');
  152. },
  153. fail: (err) => {
  154. console.log('连接失败重新连接....');
  155. that.openConnection();
  156. }
  157. });
  158. }, this.timeout);
  159. }
  160. }
  161. };
  162. </script>
  163. <style lang="scss">
  164. @import '@/uni_modules/uview-ui/index.scss';
  165. @import 'common/app.css';
  166. @import './GraceUI5/css/graceUI.css';
  167. @import './GraceUI5/skin/black.css';
  168. /* 加载图标字体 - 条件编译模式 */
  169. /* #ifdef APP-PLUS-NVUE */
  170. .gui-icons {
  171. font-family: graceIconfont;
  172. }
  173. /* #endif */
  174. </style>