app.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {
  2. ApiModel
  3. } from "./utils/api";
  4. const _Http = new ApiModel();
  5. App({
  6. onLaunch() {
  7. /* 计算tabbar+iphone安全距离 tabbar页面+100rpx*/
  8. let safeAreaBottom = 0,
  9. capsule = wx.getMenuButtonBoundingClientRect(),
  10. height = 200,
  11. that = this;
  12. //获取轮播图
  13. this.getBanner();
  14. wx.getSystemInfo({
  15. success(res) {
  16. //计算底部安全距离高度
  17. safeAreaBottom += res.screenHeight - res.safeArea.bottom;
  18. //计算自定义导航的高度
  19. height = capsule.height + res.statusBarHeight + (capsule.top - res.statusBarHeight) + 8;
  20. that.globalData.safeAreaBottom = safeAreaBottom;
  21. //判断是否为平板
  22. if (res.model.slice(0, 4) == "iPad") return that.globalData.myNavBorHeight = height;
  23. that.globalData.myNavBorHeight = height * 2;
  24. }
  25. })
  26. },
  27. getBanner() {
  28. //轮播图
  29. _Http.basic({
  30. "classname": "publicmethod.bannermag.bannermag",
  31. "method": "query_bannerlocationlist",
  32. "content": {
  33. "siteid": "BWJ",
  34. "fclienttype": "MOBILE"
  35. }
  36. }, false).then(res => {
  37. if (res.msg != '成功') {
  38. this.globalData.count = this.globalData.count + 1;
  39. if (this.globalData.count > 3) this.getBanner();
  40. return;
  41. }
  42. wx.setStorageSync('bannerDataList', res.data)
  43. })
  44. },
  45. initSocket() {
  46. let that = this;
  47. this.globalData.SocketTask = wx.connectSocket({
  48. url: 'wss://www.buwanjia.com/bwj/webSocket/' + wx.getStorageSync('userData').token,
  49. complete: (res) => {
  50. console.log(res)
  51. }
  52. })
  53. this.globalData.SocketTask.onOpen(function (res) {
  54. console.log('WebSocket连接已打开!readyState=' + that.globalData.SocketTask.readyState);
  55. that.globalData.socketEstablish = true;
  56. /* var openid = wx.getStorageSync('openid')
  57. wx.sendSocketMessage({
  58. data: openid
  59. }); */
  60. })
  61. this.globalData.SocketTask.onMessage(function (res) {
  62. that.globalData.callback(res)
  63. })
  64. this.globalData.SocketTask.onError(function (res) {
  65. console.log('readyState=' + that.globalData.SocketTask.readyState)
  66. setTimeout(() => {
  67. that.initSocket()
  68. }, 1000)
  69. })
  70. this.globalData.SocketTask.onClose(function (res) {
  71. console.log('WebSocket连接已关闭!readyState=' + that.globalData.SocketTask.readyState)
  72. if (that.globalData.socketEstablish == false) {
  73. that.initSocket()
  74. } else {
  75. that.globalData.socketEstablish = false;
  76. }
  77. })
  78. },
  79. globalData: {
  80. myNavBorHeight: 0, //自定义头部导航高度
  81. safeAreaBottom: 0, //底部安全距离
  82. msgFcount: "", //徽标数量
  83. count: 0, //banner 请求次数
  84. socketEstablish: false, //是否已经建立socket
  85. SocketTask: '',
  86. callback: function () {},
  87. }
  88. })