app.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. console.log('websocket:', 'wss://www.buwanjia.com/bwj/webSocket/' + wx.getStorageSync('userData').token)
  47. let that = this;
  48. this.globalData.SocketTask = wx.connectSocket({
  49. url: 'wss://www.buwanjia.com/bwj/webSocket/bwj/webSocket/' + wx.getStorageSync('userData').token,
  50. complete: (res) => {
  51. console.log(res)
  52. }
  53. })
  54. this.globalData.SocketTask.onOpen(function (res) {
  55. console.log('WebSocket连接已打开!readyState=' + that.globalData.SocketTask.readyState);
  56. that.globalData.socketEstablish = true;
  57. /* var openid = wx.getStorageSync('openid')
  58. wx.sendSocketMessage({
  59. data: openid
  60. }); */
  61. })
  62. this.globalData.SocketTask.onMessage(function (res) {
  63. that.globalData.callback(res)
  64. })
  65. this.globalData.SocketTask.onError(function (res) {
  66. console.log('readyState=' + that.globalData.SocketTask.readyState)
  67. // that.initSocket()
  68. })
  69. this.globalData.SocketTask.onClose(function (res) {
  70. console.log('WebSocket连接已关闭!readyState=' + that.globalData.SocketTask.readyState)
  71. /* if (that.globalData.socketEstablish == false) {
  72. that.initSocket()
  73. } else {
  74. that.globalData.socketEstablish = false;
  75. } */
  76. })
  77. },
  78. globalData: {
  79. myNavBorHeight: 0, //自定义头部导航高度
  80. safeAreaBottom: 0, //底部安全距离
  81. msgFcount: "", //徽标数量
  82. count: 0, //banner 请求次数
  83. socketEstablish: false, //是否已经建立socket
  84. SocketTask: '',
  85. callback: function () {},
  86. }
  87. })