-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.js
executable file
·91 lines (84 loc) · 2.46 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
import Vue from 'vue'
import App from './App'
import request from './common/j-request/request.js'
console.log(request);
var baseUrl = 'http://api.ieclipse.cn/wnl/'
// #ifdef H5
baseUrl = '/wnl/'
// #endif
request.setConfig({
baseUrl: baseUrl,
debug: true
})
// 请求拦截
request.interceptor.request = (config => {
// 给data添加全局请求参数uid
if (!config.data.uid) {
config.data.uid = 100
}
// 给header添加全局请求参数token
if (!config.header.token) {
config.header.token = 'my_token'
}
// 添加一个自定义的参数,默认异常请求都弹出一个toast提示
if (config.toastError === undefined) {
config.toastError = true
}
return config;
})
// 全局的业务拦截
request.interceptor.response = ((res, config) => {
if (res.code === 0) {
//res.success = true;
config.businessSuccess = true;
} else if (res.code === 1001) {
// token失效,需要重新登录
uni.navigateTo({
url: '/pages/loign/login'
})
}
return res;
})
// 全局的错误异常处理
request.interceptor.fail = ((res, config) => {
let ret = res;
let msg = ''
if (res.statusCode === 200) { // 业务错误
msg = res.data.msg
ret = res.data
} else if (res.statusCode > 0) { // HTTP错误
msg = '服务器异常[' + res.statusCode + ']'
} else { // 其它错误
msg = res.errMsg
}
if (config.toastError) {
uni.showToast({
title: msg,
duration: 2000,
icon: 'none'
})
}
return ret;
})
// since 1.2.0 全局请求开始前回调,如果设置,那么内置的请求开始回调不会执行
// request.interceptor.prepare = ((config, extra) => {
// console.log('global prepare');
// extra.start = Date.now()
// })
// // since 1.2.0 全局请求完成的回调,如果设置,那么内置的请求结束回调不会执行
// request.interceptor.complete = ((config, extra, res) => {
// console.log('global complete');
// extra.end = Date.now()
// console.log('request cost time ' + (extra.end - extra.start));
// })
/**
* Assign the request to global VUE.
* @param {Request} $request - The request object.
*/
Vue.prototype.$request = request
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()