app.js 3.9 KB

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