import {
  ApiModel
} from "./utils/api";
const _Http = new ApiModel();
App({
  onLaunch() {
    /* 计算tabbar+iphone安全距离  tabbar页面+100rpx*/
    let safeAreaBottom = 0,
      capsule = wx.getMenuButtonBoundingClientRect(),
      height = 200,
      that = this;
    //获取轮播图
    this.getBanner();
    wx.getSystemInfo({
      success(res) {
        //计算底部安全距离高度
        safeAreaBottom += res.screenHeight - res.safeArea.bottom;
        //计算自定义导航的高度
        height = capsule.height + res.statusBarHeight + (capsule.top - res.statusBarHeight) + 8;
        that.globalData.safeAreaBottom = safeAreaBottom;
        //判断是否为平板
        if (res.model.slice(0, 4) == "iPad") return that.globalData.myNavBorHeight = height;
        that.globalData.myNavBorHeight = height * 2;
      }
    })
  },
  getBanner() {
    //轮播图
    _Http.basic({
      "classname": "publicmethod.bannermag.bannermag",
      "method": "query_bannerlocationlist",
      "content": {
        "siteid": "BWJ",
        "fclienttype": "MOBILE"
      }
    }, false).then(res => {
      if (res.msg != '成功') {
        this.globalData.count = this.globalData.count + 1;
        if (this.globalData.count > 3) this.getBanner();
        return;
      }
      wx.setStorageSync('bannerDataList', res.data)
    })
  },
  initSocket() {
    let that = this;
    this.globalData.SocketTask = wx.connectSocket({
      url: 'wss://www.buwanjia.com/bwj/webSocket/' + wx.getStorageSync('userData').token,
      complete: (res) => {
        console.log(res)
      }
    })
    this.globalData.SocketTask.onOpen(function (res) {
      console.log('WebSocket连接已打开！readyState=' + that.globalData.SocketTask.readyState);
      that.globalData.socketEstablish = true;
      /* var openid = wx.getStorageSync('openid')
      wx.sendSocketMessage({
        data: openid
      }); */
    })
    this.globalData.SocketTask.onMessage(function (res) {
      console.log("onMessage",res)
      that.globalData.callback(res)
    })
    this.globalData.SocketTask.onError(function (res) {
      console.log('readyState=' + that.globalData.SocketTask.readyState)
      setTimeout(() => {
        that.initSocket()
      }, 1000)
    })
    this.globalData.SocketTask.onClose(function (res) {
      console.log('WebSocket连接已关闭！readyState=' + that.globalData.SocketTask.readyState)
      if (that.globalData.socketEstablish == false) {
        setTimeout(() => {
          that.initSocket()
        }, 1000)
      } else {
        that.globalData.socketEstablish = false;
      }
    })
  },
  getUserProfile() {
    if (wx.getStorageSync('getUserInfo')) return;
    wx.getUserProfile({
      desc: '用于完善客户信息资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        wx.setStorageSync('getUserInfo', true)
      },
      fail: (err) => {
        wx.setStorageSync('getUserInfo', false)
      }
    })
  },
  globalData: {
    myNavBorHeight: 0, //自定义头部导航高度
    safeAreaBottom: 0, //底部安全距离
    msgFcount: "", //徽标数量
    count: 0, //banner 请求次数
    socketEstablish: false, //是否已经建立socket
    SocketTask: '',
    callback: function () {},
  }
})