修改全局变量VUE_APP_ENV
Showing
9 changed files
with
26 additions
and
98 deletions
1 | NODE_ENV | 1 | NODE_ENV='development' |
2 | # just a flag | 2 | # must start with VUE_APP_ |
3 | ENV = 'development' | 3 | VUE_APP_ENV = 'development' |
4 | #base url | 4 | #base url |
5 | BASE_URL = https://www.xxx.com/ | 5 | BASE_URL = 'https://www.xxx.com/' |
6 | # base api | 6 | # base api |
7 | VUE_APP_BASE_API = '/dev-api' | 7 | VUE_APP_BASE_API = '/dev-api' |
8 | VUE_CLI_BABEL_TRANSPILE_MODULES = true | 8 | VUE_CLI_BABEL_TRANSPILE_MODULES = true | ... | ... |
... | @@ -3,6 +3,7 @@ import request from '@/utils/request' | ... | @@ -3,6 +3,7 @@ import request from '@/utils/request' |
3 | import { api } from '@/config' | 3 | import { api } from '@/config' |
4 | // api | 4 | // api |
5 | const { common_api } = api | 5 | const { common_api } = api |
6 | |||
6 | // 登录 | 7 | // 登录 |
7 | export function login(params) { | 8 | export function login(params) { |
8 | return request({ | 9 | return request({ | ... | ... |
src/utils/request.1.js
deleted
100644 → 0
1 | import axios from 'axios' | ||
2 | import store from '@/store' | ||
3 | |||
4 | // create an axios instance | ||
5 | const service = axios.create({ | ||
6 | baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url | ||
7 | withCredentials: true, // send cookies when cross-domain requests | ||
8 | timeout: 5000 // request timeout | ||
9 | }) | ||
10 | |||
11 | // request interceptor | ||
12 | service.interceptors.request.use( | ||
13 | config => { | ||
14 | // do something before request is sent | ||
15 | if (store.getters.token) { | ||
16 | // let each request carry token | ||
17 | // ['X-Token'] is a custom headers key | ||
18 | // please modify it according to the actual situation | ||
19 | config.headers['X-Token'] = '' | ||
20 | } | ||
21 | return config | ||
22 | }, | ||
23 | error => { | ||
24 | // do something with request error | ||
25 | console.log(error) // for debug | ||
26 | return Promise.reject(error) | ||
27 | } | ||
28 | ) | ||
29 | |||
30 | // response interceptor | ||
31 | service.interceptors.response.use( | ||
32 | response => { | ||
33 | const res = response.data | ||
34 | |||
35 | // if the custom code is not 20000, it is judged as an error. | ||
36 | if (res.code !== 20000) { | ||
37 | Message({ | ||
38 | message: res.message || 'error', | ||
39 | type: 'error', | ||
40 | duration: 5 * 1000 | ||
41 | }) | ||
42 | |||
43 | // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; | ||
44 | if (res.code === 50008 || res.code === 50012 || res.code === 50014) { | ||
45 | // to re-login | ||
46 | MessageBox.confirm( | ||
47 | 'You have been logged out, you can cancel to stay on this page, or log in again', | ||
48 | 'Confirm logout', | ||
49 | { | ||
50 | confirmButtonText: 'Re-Login', | ||
51 | cancelButtonText: 'Cancel', | ||
52 | type: 'warning' | ||
53 | } | ||
54 | ).then(() => { | ||
55 | store.dispatch('user/resetToken').then(() => { | ||
56 | location.reload() | ||
57 | }) | ||
58 | }) | ||
59 | } | ||
60 | return Promise.reject(res.message || 'error') | ||
61 | } else { | ||
62 | return res | ||
63 | } | ||
64 | }, | ||
65 | error => { | ||
66 | console.log('err' + error) // for debug | ||
67 | Message({ | ||
68 | message: error.message, | ||
69 | type: 'error', | ||
70 | duration: 5 * 1000 | ||
71 | }) | ||
72 | return Promise.reject(error) | ||
73 | } | ||
74 | ) | ||
75 | |||
76 | export default service |
1 | import axios from 'axios' | 1 | import axios from 'axios' |
2 | import store from '@/store' | 2 | import store from '@/store' |
3 | import { Toast } from 'vant' | 3 | import { Toast } from 'vant' |
4 | import { api } from '@/config' | ||
4 | // create an axios instance | 5 | // create an axios instance |
5 | const service = axios.create({ | 6 | const service = axios.create({ |
6 | baseURL: process.env.BASE_URL, // url = base url + request url | 7 | baseURL: api.base_api, // url = base url + request url |
7 | withCredentials: true, // send cookies when cross-domain requests | 8 | withCredentials: true, // send cookies when cross-domain requests |
8 | timeout: 5000 // request timeout | 9 | timeout: 5000 // request timeout |
9 | }) | 10 | }) | ... | ... |
... | @@ -7,30 +7,32 @@ | ... | @@ -7,30 +7,32 @@ |
7 | </template> | 7 | </template> |
8 | 8 | ||
9 | <script> | 9 | <script> |
10 | import { | 10 | // import { api } from '@/config' |
11 | import { | ||
11 | Button | 12 | Button |
12 | } from 'vant' | 13 | } from 'vant' |
13 | export default { | 14 | export default { |
14 | components: { | 15 | components: { |
15 | 'van-button': Button | 16 | 'van-button': Button |
16 | }, | 17 | }, |
17 | 18 | ||
18 | data() { | 19 | data () { |
19 | return {} | 20 | return {} |
20 | }, | 21 | }, |
21 | 22 | ||
22 | computed: {}, | 23 | computed: {}, |
23 | 24 | ||
24 | mounted() {}, | 25 | mounted () { |
26 | console.log(process.env) | ||
27 | }, | ||
25 | 28 | ||
26 | methods: {} | 29 | methods: {} |
27 | } | 30 | } |
28 | 31 | ||
29 | </script> | 32 | </script> |
30 | <style lang='scss' scoped> | 33 | <style lang='scss' scoped> |
31 | h1 { | 34 | h1 { |
32 | background: red; | 35 | background: red; |
33 | width: 375px; | 36 | width: 375px; |
34 | } | 37 | } |
35 | |||
36 | </style> | 38 | </style> | ... | ... |
-
Please register or sign in to post a comment