app.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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('servicehotline', res.data[0].servicehotline);
  59. wx.setStorageSync('bannerDataList', res.data)
  60. })
  61. },
  62. initSocket() {
  63. console.log('链接initSocket')
  64. let that = this;
  65. this.globalData.SocketTask = wx.connectSocket({
  66. url: 'wss://www.buwanjia.com/bwj/webSocket/' + wx.getStorageSync('userData').token,
  67. complete: (res) => {
  68. console.log(res)
  69. }
  70. })
  71. this.globalData.SocketTask.onOpen(function (res) {
  72. console.log('WebSocket连接已打开!readyState=' + that.globalData.SocketTask.readyState);
  73. that.globalData.socketEstablish = true;
  74. /* var openid = wx.getStorageSync('openid')
  75. wx.sendSocketMessage({
  76. data: openid
  77. }); */
  78. })
  79. this.globalData.SocketTask.onMessage(function (res) {
  80. that.globalData.callback(res)
  81. })
  82. this.globalData.SocketTask.onError(function (res) {
  83. console.log('readyState=' + that.globalData.SocketTask.readyState)
  84. setTimeout(() => {
  85. that.initSocket()
  86. }, 1000)
  87. })
  88. this.globalData.SocketTask.onClose(function (res) {
  89. console.log('WebSocket连接已关闭!readyState=' + that.globalData.SocketTask.readyState)
  90. if (that.globalData.socketEstablish == false) {
  91. setTimeout(() => {
  92. that.initSocket()
  93. }, 1000)
  94. } else {
  95. that.globalData.socketEstablish = false;
  96. }
  97. })
  98. },
  99. globalData: {
  100. isLogin: false,
  101. myNavBorHeight: 0, //自定义头部导航高度
  102. safeAreaBottom: 0, //底部安全距离
  103. msgFcount: "", //徽标数量
  104. count: 0, //banner 请求次数
  105. socketEstablish: false, //是否已经建立socket
  106. SocketTask: '',
  107. callback: function () {},
  108. }
  109. })