<script>
  import Vue from 'vue';
  import {
    getUserInfo
  } from "@/common/utils/util"
  import api from '@/common/http/'
  var getUserValTimeId = null
  export default {
    onLaunch: function() {

      // #ifdef MP-WEIXIN
      // 小程序更新
      const updateManager = uni.getUpdateManager();
      // 请求完新版本信息
      updateManager.onCheckForUpdate((res) => {
        // console.log(res.hasUpdate);
      });
      // 新的版本已经下载好
      updateManager.onUpdateReady((res) => {
        uni.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success(res) {
            if (res.confirm) {
              // 调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate();
            }
          },
        });
      });
      // 新的版本下载失败
      updateManager.onUpdateFailed((res) => {});
      // #endif

      uni.$on('updateUserInfo', () => {
        this.updateUserInfo();
      });

      uni.$on('getSendMsg', () => {
        this.getSendMsg();
      });

      if (this.$store.state.user.token) {
        this.getSendMsg();
      }

      uni.getSystemInfo({
        success: function(e) {
          console.log(e);
          Vue.prototype.StatusBar = e.statusBarHeight;
          // #ifdef MP-WEIXIN
          let custom = wx.getMenuButtonBoundingClientRect();
          Vue.prototype.Custom = custom;
          Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight + 4;
          // #endif
          // #ifndef MP-WEIXIN
          Vue.prototype.CustomBar = 48
          // #endif
          //用来判断是否iphoneX类型的全面屏设备
          if (e.model.indexOf('iPhone X') == 0) {
            Vue.prototype.isIphoneX = 68;
          } else {
            Vue.prototype.isIphoneX = 0;
          }
        },
      });
    },
    onShow: function() {

    },
    onHide: function() {

    },
    methods: {
      async getSendMsg() {
        // #ifndef H5
        (function getUserVal() {
          if (getUserValTimeId) {
            clearTimeout(getUserValTimeId)
          }
          api.get('/user/talk/unread').then((res) => {
              uni.setTabBarBadge({
                index: 2,
                text: res.data
              })
              getUserValTimeId = setTimeout(getUserVal, 2000)
            })
            .catch((error) => {});
        })()
        // #endif
      },
      async updateUserInfo() {

      },
    },
  }
</script>

<style lang="scss">
  /*每个页面公共css */
  @import "uview-ui/index.scss";

  @import "styles/iconfont.css";
</style>