-
Notifications
You must be signed in to change notification settings - Fork 109
/
main.js
119 lines (109 loc) · 2.93 KB
/
main.js
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
/**
*@des
*@author yiiframe [email protected]
*@blog http://www.yiiframe.com
*@date 2019/1/1 07:25:00
*/
import Vue from 'vue';
import App from './App';
// 引入全局存储
import store from '@/store';
// 引入全局配置
import $mAssetsPath from '@/config/assets.config.js';
import $mConfig from '@/config/index.config.js';
import $mRoutesConfig from '@/config/routes.config.js';
import $mFormRule from '@/config/formRule.config.js';
import $mConstDataConfig from '@/config/constData.config.js';
import $mSettingConfig from '@/config/setting.config.js';
// 引入全局方法
import $mHelper from '@/utils/helper';
import $mRouter from '@/utils/router';
// 全局组件
// 网络状态监听
uni.getNetworkType({
success: res => {
store.dispatch('networkStateChange', res.networkType);
}
});
uni.onNetworkStatusChange(function (res) {
store.dispatch('networkStateChange', res.networkType);
});
// 挂载全局自定义方法
Vue.prototype.$mStore = store;
// Vue.prototype.$http = http;
Vue.prototype.$mConfig = $mConfig;
Vue.prototype.$mAssetsPath = $mAssetsPath;
Vue.prototype.$mFormRule = $mFormRule;
Vue.prototype.$mRoutesConfig = $mRoutesConfig;
Vue.prototype.$mConstDataConfig = $mConstDataConfig;
Vue.prototype.$mSettingConfig = $mSettingConfig;
Vue.prototype.$mHelper = $mHelper;
Vue.prototype.$mRouter = $mRouter;
if (process.env.NODE_ENV === 'production') {
Vue.config.productionTip = false;
}
// 路由导航
$mRouter.beforeEach((navType, to) => {
if (to.route === undefined) {
throw '路由钩子函数中没有找到to对象,路由信息:' + JSON.stringify(to);
}
if (to.route === $mRoutesConfig.login.path && store.getters.hasLogin) {
uni.reLaunch({
url: $mHelper.objParseUrlAndParam($mRoutesConfig.main.path)
});
return;
}
// 过滤需要权限的页面
if (to.route.requiresAuth) {
if (store.getters.hasLogin) {
// 已经登录
uni[navType]({
url: $mHelper.objParseUrlAndParam(to.route.path, to.query)
});
} else {
// 登录成功后的重定向地址和参数
const query = {
redirectUrl: to.route.path,
...to.query
};
// 没有登录 是否强制登录?
if (store.state.forcedLogin) {
uni.redirectTo({
url: $mHelper.objParseUrlAndParam($mRoutesConfig.login.path, query)
});
} else {
uni.navigateTo({
url: $mHelper.objParseUrlAndParam($mRoutesConfig.login.path, query)
});
}
}
} else {
uni[navType]({
url: $mHelper.objParseUrlAndParam(to.route, to.query)
});
}
});
App.mpType = 'app';
Vue.mixin({
computed: {
themeColor: {
get () {
return store.getters.themeColor;
},
set (val) {
store.state.themeColor = val;
}
}
}
});
Vue.prototype.moneySymbol = $mConstDataConfig.moneySymbol;
Vue.prototype.singleSkuText = $mConstDataConfig.singleSkuText;
// 保留小数点后两位
Vue.filter('keepTwoDecimal', value => {
return (Math.floor((value || 0) * 100) / 100).toFixed(2);
});
const app = new Vue({
...App,
store: store
});
app.$mount();