-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.js
176 lines (155 loc) · 4.02 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
//import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//Vue.use(ElementUI)
import { Pagination,Button,ButtonGroup, Select,Dialog,Menu, Submenu, MenuItem, MenuItemGroup,Input, InputNumber,Table,
TableColumn, Tooltip, Form, FormItem, Alert, Icon, Row, Col, Upload, Card, Progress,Container, Header, Aside, Main, Footer,
Loading, MessageBox, Message, Notification} from 'element-ui'
Vue.use(Pagination)
Vue.use(Dialog)
Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItem)
Vue.use(MenuItemGroup)
Vue.use(Input)
Vue.use(InputNumber)
Vue.use(Select)
Vue.use(Button)
Vue.use(ButtonGroup)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(Tooltip)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Alert)
Vue.use(Icon)
Vue.use(Row)
Vue.use(Col)
Vue.use(Upload)
Vue.use(Card)
Vue.use(Progress)
Vue.use(Container)
Vue.use(Header)
Vue.use(Aside)
Vue.use(Main)
Vue.use(Footer)
Vue.use(Loading.directive)
//element ui
Vue.prototype.$loading = Loading.service
Vue.prototype.$msgbox = MessageBox
Vue.prototype.$alert = MessageBox.alert
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$prompt = MessageBox.prompt
Vue.prototype.$notify = Notification
Vue.prototype.$message = Message
Vue.config.productionTip = false
Vue.prototype.$http = axios
Vue.prototype.$user = {
isLogin: function () {
//1 for true, 0 false
var v = localStorage.getItem("isLogin");
return v === "true" ? true : false;
},
userId: function () {
return localStorage.getItem("userId");
},
account: function () {
return localStorage.getItem("account");
},
token: function () {
return localStorage.getItem("token");
},
createTime: function () {
return localStorage.getItem("createTime");
},
lastTime: function () {
return localStorage.getItem("lastTime");
},
clean(){
localStorage.clear();
}
}
Vue.prototype.showErrorMsg = function (msg) {
Vue.prototype.$message({
showClose: true,
message: msg,
type: 'error',
duration: 3000
});
}
Vue.prototype.showSuccessMsg = function (msg) {
Vue.prototype.$message({
showClose: true,
duration: 3000,
message: msg,
type: 'success'
});
}
Vue.prototype.showInfoMsg = function (msg) {
Vue.prototype.$message({
showClose: true,
duration: 3000,
message: msg,
type: 'info'
});
}
function arraybuffer2str(buf) {
var enc = new TextDecoder();
return enc.decode(buf);
}
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
//统一处理token无效
if (error.response.status === 401) {
window.localStorage["isLogin"] = false;
var toWhere = {
name: 'route-login',
params: {
account: ''
}
};
if (Vue.prototype.$user.account() == undefined){
Vue.prototype.showErrorMsg('请先登录');
}else{
Vue.prototype.showErrorMsg('会话过期,请重新登录');
toWhere.params.account = Vue.prototype.$user.account();
}
router.push(toWhere);
} else {
if (error.config.responseType === "arraybuffer") {
//下载文件返回错误
var str = arraybuffer2str(error.response.data);
var obj = JSON.parse(str);
Vue.prototype.showErrorMsg(obj.msg);
return;
}
//存在服务端返回的msg则显示msg
if (error.response.data.msg) {
Vue.prototype.showErrorMsg(error.response.data.msg);
} else {
//否则展示状态码
var pre = '';
if (error.response.status >= 500){
pre = '服务异常,请刷新重试!';
}
Vue.prototype.showErrorMsg(pre + error.response.status + " " + error.response.statusText)
}
}
//Promise.resolve(error.response)
return new Promise(() => {});
});
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
template: '<App/>',
components: {
App
}
})