Blame view

apps/utils/init-client.js 2.99 KB
陈峰 authored
1
import Vue from 'vue';
yyq authored
2
import cookie from 'yoho-cookie';
陈峰 authored
3
import yoho from 'common/yoho';
yyq authored
4
import version from 'utils/version';
陈峰 authored
5
yyq authored
6 7 8
const setStatusBar = (width, height, store) => {
  const statusBar = {
    statusBarStatus: false,
yyq authored
9
    statusBarHeight: 0
yyq authored
10 11 12 13
  };

  // 仅支持ios
  if (yoho.isYohoBuy && yoho.isiOS) {
yyq authored
14
    let isX = (height / width) > 2.1;
yyq authored
15
    let actionBarHeight = isX ? 32 : 0;
yyq authored
16
yyq authored
17 18 19 20 21 22 23 24 25 26
    statusBar.statusBarHeight = isX ? 44 : 22;

    if (store) {
      store.commit('SET_STATUS_BAR_HEIGHT', {
        height: statusBar.statusBarHeight
      });
      store.commit('SET_ACTION_BAR_HEIGHT', {
        height: actionBarHeight
      });
    }
yyq authored
27
yyq authored
28 29
    function getAppVersion(str, split) {
        const match = str.match(new RegExp('(^|)app_version=([^' + split + ']*)(' + split + '|$)'));
yyq authored
30
yyq authored
31 32 33 34 35
        return match && match.length ? match[2] : '';
    }

    const appVersion = getAppVersion(document.cookie, ';') || getAppVersion(location.href, '&');
yyq authored
36
    if (version(appVersion, '6.9.2') >= 0) {
yyq authored
37 38 39 40 41 42 43 44 45
      statusBar.statusBarStatus = true;
      store && store.commit('SET_STATUS_BAR_STATUS', {status: true});
    }
  }

  return statusBar;
};

const initClient = (store) => {
陈峰 authored
46 47 48
  window.onresize = () => {
    const {clientWidth, clientHeight} = document.body;
    store.commit('SET_WINDOW_SIZE', {clientWidth, clientHeight});
yyq authored
49
    setStatusBar(clientWidth, clientHeight, store);
陈峰 authored
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  };
  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});
陈峰 authored
67 68 69 70 71 72 73 74
  store.commit('SET_ENV', {
    env: {
      isApp: yoho.isApp,
      isiOS: yoho.isiOS,
      isAndroid: yoho.isAndroid,
      isYohoBuy: yoho.isYohoBuy,
    }
  });
yyq authored
75
yyq authored
76
  setStatusBar(clientWidth, clientHeight, store);
yyq authored
77
陈峰 authored
78 79 80 81 82 83 84 85 86 87 88
  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';
陈峰 authored
89 90 91 92 93 94

  Vue.$sdk.getUser().then(user => {
    if (user && user.uid) {
      store.commit('SET_LOGIN_INFO', user);
    }
  });
陈峰 authored
95 96 97

  Vue.$yoho.ready(() => {
    Vue.$yoho.setWebview({
陈峰 authored
98 99
      bounces: false,
      clearCacheWhenDestroy: false
陈峰 authored
100 101
    });
  });
陈峰 authored
102
};
yyq authored
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

const getYohoState = () => {
  const {clientWidth, clientHeight} = document.body;

  return {
    context: {
      env: {
        isApp: yoho.isApp,
        isiOS: yoho.isiOS,
        isAndroid: yoho.isAndroid,
        isYohoBuy: yoho.isYohoBuy
      }
    },
    window: setStatusBar(clientWidth, clientHeight)
  }
};

export {
  initClient,
  getYohoState
};