init-client.js 1.47 KB
import Vue from 'vue';
import yoho from 'common/yoho';

export default store => {
  window.onresize = () => {
    const {clientWidth, clientHeight} = document.body;

    store.commit('SET_WINDOW_SIZE', {clientWidth, clientHeight});
  };
  const {clientWidth, clientHeight} = document.body;

  store.commit('SET_WINDOW_SIZE', {clientWidth, clientHeight});
  let supportsPassive = false;

  try {
    const opts = Object.defineProperty({}, 'passive', {
      get() {
        supportsPassive = true;
        return true;
      }
    });

    window.addEventListener('test', null, opts);
  } catch (e) {} //eslint-disable-line
  store.commit('SET_SUPPORT_PASSIVE', {supportsPassive});
  store.commit('SET_ENV', {
    env: {
      isApp: yoho.isApp,
      isiOS: yoho.isiOS,
      isAndroid: yoho.isAndroid,
      isYohoBuy: yoho.isYohoBuy,
    }
  });
  if (/yoho/i.test(navigator.userAgent) && /supportWebp/i.test(navigator.userAgent)) {
    store.commit('SET_SUPPORT_WEBP', {supportWebp: true});
  }
  let img = new Image();

  img.onload = () => {
    if (img.width > 0 && img.height > 0) {
      store.commit('SET_SUPPORT_WEBP', {supportWebp: true});
    }
  };
  img.src = 'data:image/webp;base64,UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA';

  Vue.$sdk.getUser().then(user => {
    if (user && user.uid) {
      store.commit('SET_LOGIN_INFO', user);
    }
  });

  Vue.$yoho.ready(() => {
    Vue.$yoho.setWebview({
      bounces: false,
      clearCacheWhenDestroy: false
    });
  });
};