app.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 += 1;
  39. if (this.globalData.count < 5) setTimeout(() => this.getBanner(), 200)
  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. setTimeout(() => {
  74. that.initSocket()
  75. }, 1000)
  76. } else {
  77. that.globalData.socketEstablish = false;
  78. }
  79. })
  80. },
  81. globalData: {
  82. myNavBorHeight: 0, //自定义头部导航高度
  83. safeAreaBottom: 0, //底部安全距离
  84. msgFcount: "", //徽标数量
  85. count: 0, //banner 请求次数
  86. socketEstablish: false, //是否已经建立socket
  87. SocketTask: '',
  88. callback: function () {},
  89. }
  90. })