<script>
import { up, down } from '@/api/monitor'
export default {
  onLaunch: function () {
    console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！')
    console.log('App Launch')
  },

  onShow: function () {
    console.log('App Show')
    console.log(this.$fileBasUrl, 'appppp$fileBasUrl')
    if (wx.getStorageSync('token')) {
      up()
    }
    // this.connectSocket()

    // this.timer = setInterval(() => {
    //   wx.request({
    //     url: 'http://192.168.0.109:3000/heartbeat?userId=' + wx.getStorageSync('userId'),
    //   })
    // }, 5000)

    // uni.hideTabBar()
  },
  onHide: function () {
    console.log('App Hide')
    if (wx.getStorageSync('token')) {
      down()
    }
    // close socket
    // wx.closeSocket()
    // clearInterval(this.timer)
    // wx.request({
    //   url: 'http://192.168.0.109:3000/hide?userId=' + wx.getStorageSync('userId'),
    // })
  },
  globalData: {
    fileBasUrl: 'https://mall.rongtongh.cn/storage/img',
    hasLogin: false,
    inviteId: null,
    orderConfirmIds: [],
    selectAddrss: false,
    selectCoupon: false,
  },
  methods: {
    connectSocket() {
      function connect() {
        wx.connectSocket({
          url: 'ws://192.168.0.109:3000',
        })

        wx.onSocketOpen(function (res) {
          console.log('WebSocket连接已打开！')
          let user = {
            name: 'user123',
          }
          wx.sendSocketMessage({
            data: JSON.stringify(user),
          })
        })

        wx.onSocketClose(function (res) {
          console.log('WebSocket连接已关闭！')
          setTimeout(() => {
            console.log('re connect')
            // connect()
          }, 2000)
        })
      }
      connect()
    },
  },
}
</script>

<style lang="scss">
@import './uni_modules/jc-tailwind/index.scss';
/*每个页面公共css */
@import '@/uni_modules/uni-scss/index.scss';
/* #ifndef APP-NVUE */
@import '@/static/customicons.css';
@import '@/static/style.scss';
// 设置整个项目的背景色
page {
  background-color: #f5f5f5;
}

/* #endif */
.example-info {
  font-size: 14px;
  color: #333;
  padding: 10px;
}
</style>
