app.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. console.log("onMessage",res)
  63. that.globalData.callback(res)
  64. })
  65. this.globalData.SocketTask.onError(function (res) {
  66. console.log('readyState=' + that.globalData.SocketTask.readyState)
  67. setTimeout(() => {
  68. that.initSocket()
  69. }, 1000)
  70. })
  71. this.globalData.SocketTask.onClose(function (res) {
  72. console.log('WebSocket连接已关闭!readyState=' + that.globalData.SocketTask.readyState)
  73. if (that.globalData.socketEstablish == false) {
  74. setTimeout(() => {
  75. that.initSocket()
  76. }, 1000)
  77. } else {
  78. that.globalData.socketEstablish = false;
  79. }
  80. })
  81. },
  82. getUserProfile() {
  83. if (wx.getStorageSync('getUserInfo')) return;
  84. wx.getUserProfile({
  85. desc: '用于完善客户信息资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  86. success: (res) => {
  87. wx.setStorageSync('getUserInfo', true)
  88. },
  89. fail: (err) => {
  90. wx.setStorageSync('getUserInfo', false)
  91. }
  92. })
  93. },
  94. globalData: {
  95. myNavBorHeight: 0, //自定义头部导航高度
  96. safeAreaBottom: 0, //底部安全距离
  97. msgFcount: "", //徽标数量
  98. count: 0, //banner 请求次数
  99. socketEstablish: false, //是否已经建立socket
  100. SocketTask: '',
  101. callback: function () {},
  102. }
  103. })