entry-client.js
3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import Vue from 'vue';
import {
ROUTE_CHANGE,
} from 'store/yoho/types';
import {createApp} from './app';
import {createApi} from 'create-api';
import {Style, Toast, Dialog, DatePicker, ImagePreview} from 'cube-ui'; //eslint-disable-line
import {get} from 'lodash';
import Lazy from 'vue-lazyload';
import yoho from 'common/yoho';
import OrderPayType from 'components/order-pay-type';
import sdk from 'yoho-activity-sdk';
import 'statics/scss/common.scss';
import 'statics/font/iconfont.css';
import 'statics/font/ufofont.css';
const $app = document.getElementById('app');
const isDegrade = Boolean(!($app && $app.attributes['data-server-rendered']));
const context = get(window, '__INITIAL_STATE__.yoho.context');
const {app, router, store} = createApp(context);
const api = createApi();
if (window.__INITIAL_STATE__) {
store.replaceState(window.__INITIAL_STATE__);
}
window._router = get(store, 'state.yoho.context.route');
Vue.prop('yoho', yoho);
Vue.prop('sdk', sdk);
Vue.use(Toast);
Vue.use(DatePicker);
Vue.use(Dialog);
Vue.use(ImagePreview);
Vue.prop('api', api);
Vue.use(Lazy, {error: ''});
Vue.use(OrderPayType);
yoho.auth = async(loginUrl) => {
let user = await sdk.getUser();
if (user && user.uid) {
return user;
} else {
location.href = loginUrl || `${location.origin}/xianyu/passport/login/taobao`;
return;
}
}
yoho.authRealName = async() => {
if (await yoho.auth()) {
let res = await api.get('/api/ufo/sellerOrder/entryStatus');
if (res.code === 200) {
store.commit('SET_USER_SELLER_INFO', res.data);
if (!get(res.data, 'isZhiMaCertWithPhoto')) {
router.push({
name: 'passport.auth'
});
}
}
return res;
}
return {
code: 403,
message: '未登录'
};
}
const fetchAsycData = (matched, r) => {
const asyncDataPromises = matched
.map(({asyncData}) => asyncData && asyncData({store, router: r})).
filter(p => p);
return Promise.all(asyncDataPromises);
};
const trackPage = (path) => {
if (window._hmt) {
try {
window._hmt.push(['_trackPageview', path]);
} catch (error) {
console.error(error);
}
}
};
router.onReady(() => {
store.dispatch('reportYas', {
params: {
appop: 'YB_H5_PAGE_OPEN_L',
param: {
F_URL: `${location.origin}${router.currentRoute.fullPath}`,
PAGE_URL: '',
PAGE_NAME: router.currentRoute.name
}
}
});
if (isDegrade) {
fetchAsycData(router.getMatchedComponents(), router.currentRoute);
}
router.beforeResolve((to, from, next) => {
try {
trackPage(to.fullPath);
const matched = router.getMatchedComponents(to);
store.commit(ROUTE_CHANGE, {to, from});
store.dispatch('reportYas', {
params: {
appop: 'YB_H5_PAGE_OPEN_L',
param: {
F_URL: `${location.origin}${to.fullPath}`,
PAGE_URL: `${location.origin}${from.fullPath}`,
PAGE_NAME: from.name
}
}
});
fetchAsycData(matched, to)
.then(next)
.catch(e => {
store.dispatch('reportError', {error: e});
console.error(e);
return next();
});
} 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'});
});