Skip to content

Commit 9405bda

Browse files
committed
VUE_APP_ANOTHER_BASE_URI
1 parent 404f6fe commit 9405bda

File tree

9 files changed

+201
-11
lines changed

9 files changed

+201
-11
lines changed

vue-element-admin/.env.development

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ ENV = 'development'
33

44
# base api
55
# VUE_APP_BASE_API = '/dev-api'
6-
VUE_APP_BASE_API = 'http://www.cirest.com:8890/api/v2/'
6+
# VUE_APP_BASE_API = 'http://www.cirest.com:8890/api/v2/'
7+
VUE_APP_BASE_API = '/api/v2'
8+
VUE_APP_ANOTHER_BASE_API = '/apix/v2'
9+
10+
# base url
11+
VUE_APP_BASE_URL = 'http://www.cirest.com:8890'
12+
VUE_APP_ANOTHER_BASE_URL = 'http://www.cirest.com:8891'

vue-element-admin/.env.production

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ ENV = 'production'
44
# base api
55
# VUE_APP_BASE_API = '/prod-api'
66
# http://www.cirest.com:8890/api/v2/
7-
VUE_APP_BASE_API = 'http://www.cirest.com:8890/api/v2/'
7+
# VUE_APP_BASE_API = 'http://www.cirest.com:8890/api/v2/'
8+
9+
VUE_APP_BASE_API = '/api/v2'
10+
ANOTHER_VUE_APP_BASE_API = '/apix/v2'
11+
12+
VUE_APP_BASE_URL = 'http://www.cirest.com:8890'
13+
ANOTHER_VUE_APP_BASE_URL = 'http://www.cirest.com:8891'

vue-element-admin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"postcss-loader": "^3.0.0",
4444
"postcss-url": "^8.0.0",
4545
"pretty-checkbox-vue": "^1.1.9",
46+
"sass-loader": "^7.3.1",
4647
"screenfull": "4.2.0",
4748
"script-loader": "0.7.2",
4849
"sortablejs": "1.8.4",
@@ -84,7 +85,6 @@
8485
"plop": "2.3.0",
8586
"runjs": "^4.3.2",
8687
"sass": "^1.26.9",
87-
"sass-loader": "^7.1.0",
8888
"script-ext-html-webpack-plugin": "2.1.3",
8989
"serve-static": "^1.13.2",
9090
"svg-sprite-loader": "4.1.3",

vue-element-admin/src/api/transaction.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import request from '@/utils/request'
2+
import anotherRequest from '@/utils/anotherRequest'
23

34
export function fetchList(query) {
45
return request({
@@ -7,3 +8,11 @@ export function fetchList(query) {
78
params: query
89
})
910
}
11+
12+
export function fetchListx(query) {
13+
return anotherRequest({
14+
url: '/sys/blog',
15+
method: 'get',
16+
params: query
17+
})
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import axios from 'axios'
2+
import { MessageBox, Message } from 'element-ui'
3+
import store from '@/store'
4+
import { getToken, getRefreshToken } from '@/utils/auth'
5+
console.log('......',process.env);
6+
7+
console.log('anotherRequest......', process.env.VUE_APP_ANOTHER_BASE_URL + process.env.VUE_APP_ANOTHER_BASE_API);
8+
// create an axios instance
9+
const service = axios.create({
10+
baseURL: process.env.VUE_APP_ANOTHER_BASE_URL + process.env.VUE_APP_ANOTHER_BASE_API, // url = base url + request url
11+
// withCredentials: true, // send cookies when cross-domain requests
12+
timeout: 5000 // request timeout
13+
})
14+
15+
// request interceptor
16+
service.interceptors.request.use(
17+
config => {
18+
// Do something before request is sent
19+
if (store.getters.token) {
20+
// 让每个请求携带token-- ['Authorization']为自定义key 请根据实际情况自行修改
21+
config.headers['Authorization'] = 'Bearer ' + getToken()
22+
// console.log('getToken', getToken())
23+
}
24+
// 监听 是否 /sys/user/refreshtoken 是则重置token为刷新token
25+
const url = config.url
26+
if (url.split('/').pop() === 'refreshtoken') {
27+
// console.log('config.url', config.url, getRefreshToken())
28+
config.headers['Authorization'] = 'Bearer ' + getRefreshToken()
29+
}
30+
31+
return config
32+
},
33+
error => {
34+
// Do something with request error
35+
console.log(error) // for debug
36+
Promise.reject(error)
37+
}
38+
)
39+
40+
// response interceptor
41+
service.interceptors.response.use(
42+
/**
43+
* If you want to get http information such as headers or status
44+
* Please return response => response
45+
*/
46+
47+
/**
48+
* 下面的注释为通过在response里,自定义code来标示请求状态
49+
* 当code返回如下情况则说明权限有问题,登出并返回到登录页
50+
* 如想通过 xmlhttprequest 来状态码标识 逻辑可写在下面error中
51+
* 以下代码均为样例,请结合自生需求加以修改,若不需要,则可删除
52+
*/
53+
response => {
54+
const res = response.data
55+
// console.log('response interceptor', response)
56+
if (res.code !== 20000) {
57+
Message({
58+
message: res.message,
59+
type: 'error',
60+
duration: 5 * 1000
61+
})
62+
63+
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; 50015: refresh_token过期
64+
if (res.code === 50008 || res.code === 50012 || res.code === 50015) {
65+
// 请自行在引入 MessageBox
66+
// import { Message, MessageBox } from 'element-ui'
67+
console.log(' refresh_token过期 超时......')
68+
MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', {
69+
confirmButtonText: '重新登录',
70+
cancelButtonText: '取消',
71+
type: 'warning'
72+
}).then(() => {
73+
store.dispatch('user/FedLogOut').then(() => {
74+
location.reload() // 为了重新实例化vue-router对象 避免bug
75+
})
76+
})
77+
}
78+
return Promise.reject(new Error(res.message || 'Error'))
79+
} else {
80+
return response.data
81+
}
82+
},
83+
error => {
84+
// Error对象可能log出来并不是你想象的那种以对象的样子出现
85+
console.log(error.response) // console.log(error) 401 再判断 error.response.data.code
86+
// // let config = error.response.config
87+
// console.log('err' + error) // for debug
88+
89+
// Message({
90+
// message: error.message,
91+
// type: 'error',
92+
// duration: 5 * 1000
93+
// })
94+
95+
// 拦截网络连接非 200 及 401 响应的错误, eg. Status Code: 500 Internal Server Error
96+
if (error.response.status !== 200 && error.response.status !== 401) {
97+
Message({
98+
message: 'Status Code: ' + error.response.status + ' ' + error.response.statusText,
99+
type: 'error',
100+
duration: 3 * 1000
101+
})
102+
return
103+
}
104+
105+
if (error.response.status === 401 && error.response.data.code === 50014) {
106+
// Message({
107+
// message: 'access_token过期,自动续期',
108+
// type: 'error',
109+
// duration: 3* 1000
110+
// })
111+
return againRequest(error)
112+
}
113+
114+
if (error.response.status === 401 && error.response.data.code === 50015) {
115+
// Message({
116+
// message: 'refresh_token过期,重定向登录',
117+
// type: 'error',
118+
// duration: 3* 1000
119+
// })
120+
console.log('refresh_token过期 超时......')
121+
MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', {
122+
confirmButtonText: '重新登录',
123+
cancelButtonText: '取消',
124+
type: 'warning'
125+
}).then(() => {
126+
store.dispatch('user/FedLogOut').then(() => {
127+
location.reload() // 为了重新实例化vue-router对象 避免bug
128+
})
129+
})
130+
}
131+
132+
return Promise.reject(error)
133+
}
134+
) // response 拦截结束
135+
136+
async function againRequest(error) {
137+
await store.dispatch('user/handleCheckRefreshToken')
138+
139+
const config = error.response.config
140+
config.headers['Authorization'] = 'Bearer ' + getToken()
141+
142+
const res = await axios.request(config)
143+
// console.log('againRequest...............................', res)
144+
return res.data // 以error.response.config重新请求返回的数据包是在函数内是 被封装在data里面
145+
}
146+
147+
export default service

vue-element-admin/src/utils/request.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import axios from 'axios'
22
import { MessageBox, Message } from 'element-ui'
33
import store from '@/store'
44
import { getToken, getRefreshToken } from '@/utils/auth'
5-
5+
console.log('request.....', process.env.VUE_APP_BASE_URL + process.env.VUE_APP_BASE_API);
66
// create an axios instance
77
const service = axios.create({
8-
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
8+
baseURL: process.env.VUE_APP_BASE_URL + process.env.VUE_APP_BASE_API, // url = base url + request url
99
// withCredentials: true, // send cookies when cross-domain requests
10-
timeout: 5000, // request timeout
10+
timeout: 5000 // request timeout
1111
})
1212

1313
// request interceptor

vue-element-admin/src/views/dashboard/admin/components/TransactionTable.vue

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
<script>
2424
import { fetchList } from '@/api/transaction'
25+
import { fetchListx } from '@/api/transaction'
2526
2627
export default {
2728
filters: {
@@ -38,7 +39,8 @@ export default {
3839
},
3940
data() {
4041
return {
41-
list: null
42+
list: null,
43+
listx: null
4244
}
4345
},
4446
created() {
@@ -49,6 +51,9 @@ export default {
4951
fetchList().then(response => {
5052
this.list = response.data.items.slice(0, 8)
5153
})
54+
fetchListx().then(response => {
55+
this.listx = response.data
56+
})
5257
}
5358
}
5459
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
22
<div style="padding:30px;">
3-
<el-alert :closable="false" title="menu 2" />
3+
<el-alert :closable="false" title="menu 2" />
44
</div>
5-
</template>
5+
</template>

vue-element-admin/vue.config.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const name = defaultSettings.title || 'vue Element Admin' // page title
1414
// You can change the port by the following method:
1515
// port = 9527 npm run dev OR npm run dev --port = 9527
1616
const port = process.env.port || process.env.npm_config_port || 9527 // dev port
17-
17+
// console.log(process.env)
1818
// All configuration item explanations can be find in https://cli.vuejs.org/config/
1919
module.exports = {
2020
/**
@@ -36,8 +36,25 @@ module.exports = {
3636
warnings: false,
3737
errors: true
3838
},
39+
proxy: {
40+
[process.env.VUE_APP_BASE_API]: {
41+
target: process.env.VUE_APP_BASE_URL,
42+
changeOrigin: true,
43+
pathRewrite: {
44+
['^' + process.env.VUE_APP_BASE_API]: ''
45+
}
46+
},
47+
[process.env.VUE_APP_ANOTHER_BASE_API]: {
48+
target: process.env.VUE_APP_ANOTHER_BASE_URL,
49+
changeOrigin: true,
50+
pathRewrite: {
51+
['^' + process.env.VUE_APP_ANOTHER_BASE_API]: ''
52+
}
53+
},
54+
},
3955
before: require('./mock/mock-server.js')
4056
},
57+
4158
configureWebpack: {
4259
// provide the app's title in webpack's name field, so that
4360
// it can be accessed in index.html to inject the correct title.
@@ -95,7 +112,7 @@ module.exports = {
95112
.plugin('ScriptExtHtmlWebpackPlugin')
96113
.after('html')
97114
.use('script-ext-html-webpack-plugin', [{
98-
// `runtime` must same as runtimeChunk name. default is `runtime`
115+
// `runtime` must same as runtimeChunk name. default is `runtime`
99116
inline: /runtime\..*\.js$/
100117
}])
101118
.end()

0 commit comments

Comments
 (0)