123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <script>
- import md5Libs from '@/uni_modules/uview-ui/libs/function/md5';
- import Vue from 'vue';
- let that;
- export default {
- data() {
- return {
- timeout: 30000, // 30s
- timeoutObj: null
- };
- },
- created() {
- // .判断是否已连接
- this.checkOpenSocket();
- // #ifdef APP-PLUS
- plus.navigator.closeSplashscreen();
- // #endif
- },
- onLaunch: async function () {
- console.log('App Launch');
- uni.hideTabBar();
- //加载配置
- uni.getSystemInfo({
- success: function (e) {
- // #ifndef MP
- Vue.prototype.StatusBar = e.statusBarHeight;
- if (e.platform == 'android') {
- Vue.prototype.CustomBar = e.statusBarHeight + 50;
- } else {
- Vue.prototype.CustomBar = e.statusBarHeight + 45;
- }
- // #endif
- // #ifdef MP-WEIXIN
- Vue.prototype.StatusBar = e.statusBarHeight;
- let custom = wx.getMenuButtonBoundingClientRect();
- Vue.prototype.Custom = custom;
- Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
- // #endif
- // #ifdef MP-ALIPAY
- Vue.prototype.StatusBar = e.statusBarHeight;
- Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
- // #endif
- }
- });
- let res = await this.$api.getConfig();
- // console.log(res);
- if (!res.code) {
- return;
- }
- this.vuex_config = res.data;
- },
- onShow: function () {
- console.log('App 开启');
- uni.hideTabBar();
- },
- onHide: function () {
- console.log('App 关闭');
- },
- onLoad: function () {
- that = this;
- uni.hideTabBar();
- },
- methods: {
- // 判断是否已连接
- checkOpenSocket() {
- uni.sendSocketMessage({
- data: 'ping',
- success: (res) => {
- return;
- },
- fail: (err) => {
- // 未连接打开websocket连接
- this.openConnection();
- }
- });
- },
- openConnection() {
- // 打开连接
- // uni.closeSocket(); // 确保已经关闭后再重新打开
- uni.connectSocket({
- url: 'ws://192.168.10.80:8002',
- success(res) {
- console.log('连接成功 connectSocket=', res);
- },
- fail(err) {
- console.log('连接失败 connectSocket=', err);
- }
- });
- uni.onSocketOpen((res) => {
- console.log('连接成功');
- this.send();
- });
- this.onSocketMessage(); // 打开成功监听服务器返回的消息
- },
- send() {
- uni.sendSocketMessage({
- data: '{"command":"getshowpage"}',
- success: (res) => {
- console.log(res);
- console.log('发送成功....');
- },
- fail: (err) => {
- console.log(res);
- console.log('连接失败重新连接....');
- this.openConnection();
- }
- });
- },
- onSocketMessage() {
- // 消息
- this.timeout = 30000;
- this.timeoutObj = null;
- uni.onSocketMessage((res) => {
- console.log(res);
- this.getSocketMsg(res.data); // 监听到有新服务器消息
- });
- },
- // 监听到有新服务器消息
- getSocketMsg(reData) {
- // 监听到服务器消息
- console.log('收到服务器消息', reData);
- if (reData == 'pong') {
- return;
- }
- let data = JSON.parse(reData);
- if (data.command == 'updatepage') {
- if (data.id == 0) {
- uni.navigateTo({
- url: '/pages/index/startPage'
- });
- }
- if (data.id == 1) {
- uni.navigateTo({
- url: '/pages/index/index'
- });
- }
- }
- this.reset(); // 检测心跳reset,防止长时间连接导致连接关闭
- },
- // 检测心跳reset
- reset() {
- clearInterval(this.timeoutObj);
- this.start(); // 启动心跳
- },
- // 启动心跳 start
- start() {
- this.timeoutObj = setInterval(function () {
- uni.sendSocketMessage({
- data: 'ping',
- success: (res) => {
- console.log('连接中....');
- },
- fail: (err) => {
- console.log('连接失败重新连接....');
- that.openConnection();
- }
- });
- }, this.timeout);
- }
- }
- };
- </script>
- <style lang="scss">
- @import '@/uni_modules/uview-ui/index.scss';
- @import 'common/app.css';
- @import './GraceUI5/css/graceUI.css';
- @import './GraceUI5/skin/black.css';
- /* 加载图标字体 - 条件编译模式 */
- /* #ifdef APP-PLUS-NVUE */
- .gui-icons {
- font-family: graceIconfont;
- }
- /* #endif */
- </style>
|