Blame view

apps/entry-client.js 4.33 KB
陈峰 authored
1 2 3 4 5 6
import Vue from 'vue';
import {
  ROUTE_CHANGE,
} from 'store/yoho/types';
import {createApp} from './app';
import {createApi} from 'create-api';
陈峰 authored
7
import {Style, Toast, Dialog, ActionSheet} from 'cube-ui'; //eslint-disable-line
yyq authored
8
import Prompt from 'plugins/grass-prompt';
TaoHuang authored
9 10
import bus from 'plugins/bus';
yyq authored
11
import {get, find} from 'lodash';
陈峰 authored
12
import yoho from 'common/yoho';
陈峰 authored
13
import sdk from 'common/sdk';
陈峰 authored
14
import links from 'utils/links';
yyq authored
15
import {initClient, getYohoState} from 'utils/init-client';
陈峰 authored
16
import 'statics/scss/common.scss';
yyq authored
17
import 'statics/scss/grass-prompt.scss';
陈峰 authored
18
import 'statics/font/iconfont.css';
yyq authored
19
import 'statics/font/cube-icon.css';
陈峰 authored
20 21 22 23 24

const $app = document.getElementById('app');

const isDegrade = Boolean(!($app && $app.attributes['data-server-rendered']));
const context = get(window, '__INITIAL_STATE__.yoho.context');
yyq authored
25
const {app, router, store} = createApp(context, {yoho: getYohoState()});
陈峰 authored
26 27 28 29 30

if (window.__INITIAL_STATE__) {
  store.replaceState(window.__INITIAL_STATE__);
}
陈峰 authored
31
陈峰 authored
32
window._router = get(store, 'state.yoho.context.route') || 'offline-route';
陈峰 authored
33 34 35

Vue.prop('yoho', yoho);
Vue.prop('sdk', sdk);
陈峰 authored
36
Vue.prop('links', links);
yyq authored
37
Vue.prop('auth', function() {
陈峰 authored
38
  if (!get(this.$store, '$context.isLogin')) {
yyq authored
39 40 41 42
    this.$sdk && this.$sdk.goLogin && this.$sdk.goLogin();
    return false;
  }
陈峰 authored
43
  return true;
yyq authored
44
});
TaoHuang authored
45 46

Vue.prop('bus', bus());
yyq authored
47
Vue.use(Prompt);
陈峰 authored
48 49
Vue.use(Toast);
Vue.use(Dialog);
陈峰 authored
50
Vue.use(ActionSheet);
陈峰 authored
51
Vue.prop('api', createApi(context, store));
陈峰 authored
52
yyq authored
53
initClient(store);
陈峰 authored
54
yyq authored
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
const getReportParamsKey = (name) => {
  let key;

  switch (name) {
    case 'article.comment':
      key = 'articleId';
      break;
    case 'topic':
    case 'topic.share':
      key = 'topicId';
      break;
    default:
      key = 'id';
      break;
  }

  return key;
};
陈峰 authored
74 75 76 77 78 79 80 81 82 83 84
const trackPage = (path) => {
  if (window._hmt) {
    try {
      window._hmt.push(['_trackPageview', path]);
    } catch (error) {
      console.error(error);
    }
  }
};

router.onReady(() => {
陈峰 authored
85 86 87
  if (isDegrade) {
    store.commit(ROUTE_CHANGE, {to: router.currentRoute});
  }
陈峰 authored
88
  router.beforeResolve((to, from, next) => {
yyq authored
89 90 91 92
    store.commit('SET_STATUS_BAR_COLOR', {
      color: get(to, 'meta.statusBarColor')
    });
陈峰 authored
93 94 95 96 97 98 99 100
    try {
      trackPage(to.fullPath);
      store.commit(ROUTE_CHANGE, {to, from});

      store.dispatch('reportYas', {
        params: {
          appop: 'YB_H5_PAGE_OPEN_L',
          param: {
yyq authored
101 102 103 104 105 106 107 108 109 110
            PAGE_URL: `${location.origin}${to.fullPath}`,
            PAGE_NAME: to.name
          }
        }
      });

      store.dispatch('reportYas', {
        params: {
          appop: 'YB_H5_PAGE_FLR_C',
          param: {
yyq authored
111 112 113
            F_URL: `${location.origin}${from.fullPath}`,
            PAGE_URL: `${location.origin}${to.fullPath}`,
            PAGE_NAME: to.name
陈峰 authored
114 115 116
          }
        }
      });
yyq authored
117 118 119 120 121 122 123 124

      store.dispatch('reportYas', {
        params: {
          appop: 'YB_PAGE_ENTER',
          param: {
            PAGE_ID: to.name,
            TYPE_ID: to.params[getReportParamsKey(to.name)],
            SOURCE_ID: from.name,
陈峰 authored
125
            PAGE_NEW_CREATE: find(get(store, 'state.yoho.historys'), history => {
yyq authored
126
              return history.path === to.path;
yyq authored
127
            }) ? 'Y' : 'N'
yyq authored
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
          }
        }
      });

      store.dispatch('reportYas', {
        params: {
          appop: 'YB_PAGE_EXIT',
          param: {
            PAGE_ID: from.name,
            TYPE_ID: from.params[getReportParamsKey(from.name)]
          }
        }
      });

      if (get(store, 'state.yoho.direction') === 'back') {
        store.dispatch('reportYas', {
          params: {
            appop: 'YB_PAGE_FINISH',
            param: {
              PAGE_ID: from.name,
              TYPE_ID: from.params[getReportParamsKey(from.name)]
            }
          }
        });
      }
yyq authored
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      if (to.query.report_yas) {
        let reportParam = to.query.report_param || {};

        store.dispatch('reportYas', {
          params: {
            appop: to.query.report_yas,
            param: {
              ...reportParam,
              FP_NAME: from.name,
              FP_PARAM: from.params[getReportParamsKey(from.name)],
            }
          }
        });
      }
陈峰 authored
169
      return next();
陈峰 authored
170 171 172 173 174 175 176 177 178 179 180 181 182
    } catch (e) {
      store.dispatch('reportError', {error: e});
      return next();
    }
  });
  app.$mount(isDegrade ? '#degrade-app' : '#app');
});

router.onError(e => {
  store.dispatch('reportError', {error: e});
  router.push({name: 'error.500'});
});