app.vue 4.56 KB
<template>
  <div id="app">
    <transition
      :name="`route-view-${yoho.direction}`">
      <keep-alive>
        <router-view :key="$route.fullPath"></router-view>
      </keep-alive>
    </transition>
    <div id="fps" class="fps" v-if="showFps"></div>
  </div>
</template>

<script>
import Share from 'plugins/share';
import {get, find} from 'lodash';
import {mapState, mapMutations} from 'vuex';

export default {
  name: 'App',
  data() {
    return {
      showFps: false,
      fpsTick: 0,
      fps: 0
    };
  },
  computed: {
    ...mapState(['yoho']),
  },
  mounted() {
    this.$yoho.ready(() => {
      this.$yoho.getNavStatus('', res => {
        if (res === 'N') {
          this.SET_STATUS_BAR_STATUS({status: false});
        }
      });
    });

    const barColor = get(this.$route, 'meta.statusBarColor');

    if (barColor) {
      this.SET_STATUS_BAR_COLOR({
        color: barColor
      });
    }

    document.addEventListener('visibilitychange', () => {
      if (!document.hidden) {
        const child = find(this.$children, ['_inactive', false]);

        if (child && child.pageVisibileEvent) {
          child.pageVisibileEvent();
        }

        const {isApp, isiOS} = this.yoho.context.env || {};
        const {statusBarColor} = this.yoho.window || {};

        if (isApp && isiOS && statusBarColor) {
          this.$yoho.setStatusBar({statusBarColor});
        }
      }
    });

    if (process.env.NODE_ENV === 'development') {
      this.showFps = true;
      this.frameCallback();
    }
    if (this.yoho.context.needLogin) {
      this.$yoho.ready(() => {
        this.$sdk.goLogin();
      });
    }

    if (!this.$yoho.isApp) {
      Share.init();
    }

    this.reportEnter();
  },
  methods: {
    ...mapMutations(['SET_STATUS_BAR_COLOR', 'SET_STATUS_BAR_STATUS']),
    frameCallback() {
      window.requestAnimationFrame(() => {
        const now = Date.now();

        if (!this.time) {
          this.time = now;
        }
        if (now - this.time >= 1000) {
          this.time = now;
          document.getElementById('fps').innerText = this.fpsTick;
          this.fpsTick = 0;
        }
        this.fpsTick++;
        this.frameCallback();
      });
    },
    reportEnter() {
      const route = this.$route;

      this.$store.dispatch('reportYas', {
        params: {
          appop: 'YB_PAGE_ENTER',
          param: {
            PAGE_ID: route.name,
            TYPE_ID: route.params.id || route.params.topicId,
            SOURCE_ID: '',
            PAGE_NEW_CREATE: 'Y'
          }
        }
      });
    }
  },
  watch: {
    'yoho.context.needLogin': function(newVal) {
      if (newVal) {
        this.$sdk.goLogin();
      }
    },
    'yoho.window.statusBarColor': function(value) {
      setTimeout(() => {
        this.$yoho.setStatusBar({
          statusBarColor: value
        });
      }, 100);
    }
  }
};
</script>

<style lang="scss">
// 浏览器模式下切换动效
.route-view-forword-enter-active,
.route-view-forword-leave-active,
.route-view-back-enter-active,
.route-view-back-leave-active {
  will-change: transform;
  width: 100%;
  height: 100%;
  position: absolute;
  backface-visibility: hidden;
  perspective: 1000;
}

.route-view-forword-leave-active,
.route-view-back-leave-active {
  transition: all 300ms ease;
}

.route-view-forword-enter-active,
.route-view-back-enter-active {
  transition: all 300ms cubic-bezier(0.165, 0.84, 0.44, 1);
}

.route-view-forword-enter {
  transform: translate3d(100%, 0, 0);
}

.route-view-forword-enter-active {
  z-index: 2;
}

.route-view-forword-leave-active {
  z-index: -1;
  transform: translate3d(-30%, 0, 0);
}

.route-view-back-enter {
  transform: translate3d(-30%, 0, 0);
}

.route-view-back-enter-active {
  z-index: -1;
}

.route-view-back-leave-active {
  transform: translate3d(100%, 0, 0);
  z-index: 2;
}

// protocol模式下切换动效
.route-view-local-forword-enter-active {
  will-change: transform;
  width: 100%;
  height: 100%;
  position: absolute;
  backface-visibility: hidden;
  perspective: 1000;
  transition: all 300ms cubic-bezier(0.165, 0.84, 0.44, 1);
}

.route-view-local-forword-leave-active {
  transition: all 300ms ease;
  transform: translate3d(-30%, 0, 0);
}

.route-view-local-forword-enter {
  transform: translate3d(100%, 0, 0);
}

.route-view-local-forword-enter-active,
.route-view-local-back-enter-active {
  z-index: 20;
}

.route-view-local-forword-leave-active,
.route-view-local-back-leave-active {
  z-index: -1;
}

.fps {
  position: absolute;
  z-index: 99;
  left: 0;
  top: 0;
  width: 20PX;
  height: 20PX;
  background-color: white;
  color: black;
}
</style>