import { ApiModel } from "./utils/api"; const _Http = new ApiModel(); App({ onLaunch() { //判断是否存在登录 _Http.basic({ "accesstoken": wx.getStorageSync('userData').token, "classname": "customer.usercenter.usermsg.usermsg", "method": "query_usermsg", "content": {} }).then(res => { console.log("onLaunch", res) if (res.msg == '成功') { this.globalData.isLogin = true; setTimeout(()=>{ this.initSocket();//存在登录 链接webSocket },500) } else { } }) /* 计算tabbar+iphone安全距离 tabbar页面+100rpx*/ let safeAreaBottom = 0, capsule = wx.getMenuButtonBoundingClientRect(), height = 200, that = this; //获取轮播图 this.getBanner(); wx.getSystemInfo({ success(res) { //计算底部安全距离高度 safeAreaBottom += res.screenHeight - res.safeArea.bottom; //计算自定义导航的高度 height = capsule.height + res.statusBarHeight + (capsule.top - res.statusBarHeight) + 8; that.globalData.safeAreaBottom = safeAreaBottom; //判断是否为平板 if (res.model.slice(0, 4) == "iPad") return that.globalData.myNavBorHeight = height; that.globalData.myNavBorHeight = height * 2; } }) }, getBanner() { //轮播图 _Http.basic({ "classname": "publicmethod.bannermag.bannermag", "method": "query_bannerlocationlist", "content": { "siteid": "BWJ", "fclienttype": "MOBILE" } }, false).then(res => { if (res.msg != '成功') { this.globalData.count += 1; if (this.globalData.count < 5) setTimeout(() => this.getBanner(), 200) return; } wx.setStorageSync('servicehotline', res.data[0].servicehotline); wx.setStorageSync('bannerDataList', res.data) }) }, initSocket() { console.log('链接initSocket') let that = this; this.globalData.SocketTask = wx.connectSocket({ url: 'wss://www.buwanjia.com/bwj/webSocket/' + wx.getStorageSync('userData').token, complete: (res) => { console.log(res) } }) this.globalData.SocketTask.onOpen(function (res) { console.log('WebSocket连接已打开!readyState=' + that.globalData.SocketTask.readyState); that.globalData.socketEstablish = true; /* var openid = wx.getStorageSync('openid') wx.sendSocketMessage({ data: openid }); */ }) this.globalData.SocketTask.onMessage(function (res) { that.globalData.callback(res) }) this.globalData.SocketTask.onError(function (res) { console.log('readyState=' + that.globalData.SocketTask.readyState) setTimeout(() => { that.initSocket() }, 1000) }) this.globalData.SocketTask.onClose(function (res) { console.log('WebSocket连接已关闭!readyState=' + that.globalData.SocketTask.readyState) if (that.globalData.socketEstablish == false) { setTimeout(() => { that.initSocket() }, 1000) } else { that.globalData.socketEstablish = false; } }) }, globalData: { isLogin: false, myNavBorHeight: 0, //自定义头部导航高度 safeAreaBottom: 0, //底部安全距离 msgFcount: "", //徽标数量 count: 0, //banner 请求次数 socketEstablish: false, //是否已经建立socket SocketTask: '', callback: function () {}, } })