Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
micro-system
/
system
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit e7dc2d78
authored
Oct 26, 2020
by
Margaret
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
merge
1 parent
e863a9fc
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
68 deletions
src/App.vue
src/config/config.js
src/utils/http.js
src/App.vue
View file @
e7dc2d7
...
@@ -42,7 +42,7 @@ export default {
...
@@ -42,7 +42,7 @@ export default {
this
.
$root
.
parentRouter
.
push
(
path
);
this
.
$root
.
parentRouter
.
push
(
path
);
}
}
},
},
moun
ted
()
{
crea
ted
()
{
const
baseService
=
new
BaseService
();
const
baseService
=
new
BaseService
();
baseService
.
getBaseConfig
().
then
((
res
)
=>
{
baseService
.
getBaseConfig
().
then
((
res
)
=>
{
// console.log(res);
// console.log(res);
...
...
src/config/config.js
View file @
e7dc2d7
...
@@ -6,7 +6,7 @@ const env = process.env.NODE_ENV
...
@@ -6,7 +6,7 @@ const env = process.env.NODE_ENV
// https://oss-config.test.glsx.com.cn/mock/5f8fda237ec90706c0f89d05/service
// https://oss-config.test.glsx.com.cn/mock/5f8fda237ec90706c0f89d05/service
// http://192.168.0.63:8880
// http://192.168.0.63:8880
if
(
!
BASE_URL
)
{
if
(
!
BASE_URL
)
{
BASE_URL
=
env
===
'development'
?
'http
s://oss-config.test.glsx.com.cn/mock/5f8fda237ec90706c0f89d05/service
'
BASE_URL
=
env
===
'development'
?
'http
://192.168.0.63:8880
'
:
''
:
''
}
}
...
...
src/utils/http.js
View file @
e7dc2d7
import
axios
from
'axios'
import
axios
from
'axios'
;
import
qs
from
'qs'
// import store from '@/store';
// import store from '@/store/index'
// const baseURL = store.state.baseURL
// import { checkStatus } from './checkStatus';
// let loadingInstance = null // 加载全局的loading
const
instance
=
axios
.
create
({
//
创建axios实例,在这里可以设置请求的默认配置
const
instance
=
axios
.
create
({
//创建axios实例,在这里可以设置请求的默认配置
timeout
:
60000
,
// 设置超时时间60s
timeout
:
60000
,
// 设置超时时间60s
// baseURL
retry
:
3
,
retryDelay
:
1000
})
})
// 文档中的统一设置post请求头。
// 统一设置post请求头
// instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
instance
.
defaults
.
headers
.
post
[
'Content-Type'
]
=
'application/json'
;
let
httpCode
=
{
601
:
'(601)请求参数错误'
,
704
:
'(704)权限不足, 请重新登录'
,
703
:
'(703)服务器拒绝本次访问'
,
// 404: '(404)请求资源未找到',
// 500: '(500)内部服务器错误',
707
:
'(707)服务器不支持该请求中使用的方法'
,
// 502: '(502)网关错误',
// 504: '(504)网关超时'
}
/** 添加请求拦截器 **/
/** 添加请求拦截器 **/
instance
.
interceptors
.
request
.
use
(
config
=>
{
instance
.
interceptors
.
request
.
use
(
config
=>
{
/**
config
.
headers
[
'Authorization'
]
=
localStorage
.
getItem
(
'token'
)
||
''
;
* 在这里:可以根据业务需求可以在发送请求之前做些什么。
// store.dispatch('app/showLoading', true);
* config.headers['token'] = sessionStorage.getItem('token') || ''
*/
// loadingInstance = Loading.service({ // 发起请求时加载全局loading,请求失败或有响应时会关闭
// spinner: 'fa fa-spinner fa-spin fa-3x fa-fw',
// text: '拼命加载中...'
// })
return
config
return
config
},
error
=>
{
},
error
=>
{
// 对请求错误做些什么
console
.
log
(
'请求错误,原因:'
,
error
)
console
.
log
(
'请求错误,原因:'
,
error
)
// store.dispatch('app/showLoading', false);
return
Promise
.
reject
(
error
)
return
Promise
.
reject
(
error
)
})
})
/** 添加响应拦截器 **/
/** 添加响应拦截器 **/
instance
.
interceptors
.
response
.
use
(
response
=>
{
instance
.
interceptors
.
response
.
use
(
response
=>
{
// loadingInstance.close()
// store.dispatch('app/showLoading', false);
if
(
response
.
data
.
code
===
200
||
response
.
status
===
200
)
{
// 响应结果里的status: 200是我与后台的约定,大家可以根据实际情况去做对应的判断
const
serverCode
=
response
.
data
.
code
;
if
(
response
.
status
)
{
if
(
serverCode
===
200
||
response
.
status
===
200
)
{
return
Promise
.
resolve
(
response
)
if
(
serverCode
!==
200
)
{
// checkStatus(serverCode);
}
if
(
response
.
status
)
{
return
Promise
.
resolve
(
response
);
}
}
return
Promise
.
resolve
(
response
.
data
)
return
Promise
.
resolve
(
response
.
data
);
}
else
{
}
else
{
// Message({
// checkStatus(serverCode);
// message: response.data.message,
// type: 'error'
// })
console
.
log
(
response
.
data
.
message
)
return
Promise
.
reject
(
response
.
data
.
message
)
return
Promise
.
reject
(
response
.
data
.
message
)
}
}
},
error
=>
{
},
error
=>
{
// loadingInstance.close()
// store.dispatch('app/showLoading', false);
if
(
error
.
response
)
{
const
errRes
=
error
.
response
;
/**
if
(
errRes
)
{
* 在这里:可以根据业务需求可以在请求失败后做什么。
const
{
data
,
config
,
status
,
statusText
}
=
errRes
;
* 根据请求失败的http状态码去给用户相应的提示
// checkStatus(status);
*/
console
.
log
(
`
${
config
.
url
}
接口请求出错,状态码
${
status
}
`
);
let
tips
=
error
.
response
.
status
in
httpCode
?
httpCode
[
error
.
response
.
status
]
:
error
.
response
.
data
.
message
return
Promise
.
reject
(
error
);
// Message({
// message: tips,
// type: 'error'
// })
console
.
log
(
tips
)
return
Promise
.
reject
(
error
)
}
else
{
}
else
{
// Message({
// checkStatus('fail');
// message: '请求失败',
console
.
log
(
'http request fail 请求失败'
);
// type: 'error'
// })
return
Promise
.
reject
(
new
Error
(
'请求失败'
))
return
Promise
.
reject
(
new
Error
(
'请求失败'
))
}
}
//超时处理
var
config
=
error
.
config
;
if
(
!
config
||
!
config
.
retry
)
return
Promise
.
reject
(
error
);
// 设置用于跟踪重试次数的变量
config
.
__retryCount
=
config
.
__retryCount
||
0
;
// 检查我们是否已将重试总数最大化
if
(
config
.
__retryCount
>=
config
.
retry
)
{
// 错误拒绝
return
Promise
.
reject
(
error
);
}
// 增加重试次数
config
.
__retryCount
+=
1
;
// 创造新的承诺来处理指数退避
var
backoff
=
new
Promise
(
function
(
resolve
)
{
setTimeout
(
function
()
{
resolve
();
},
config
.
retryDelay
||
1
);
});
// 返回承诺,其中将撤回axios以重试请求
return
backoff
.
then
(
function
()
{
return
axios
(
config
);
});
})
})
export
default
class
HTTP
{
export
default
class
HTTP
{
...
@@ -85,26 +87,22 @@ export default class HTTP {
...
@@ -85,26 +87,22 @@ export default class HTTP {
instance
({
instance
({
url
:
options
.
url
,
url
:
options
.
url
,
method
:
'post'
,
method
:
'post'
,
header
:
{
data
:
JSON
.
stringify
(
options
.
data
)
'Content-Type'
:
'application/x-www-form-urlencoded'
},
data
:
qs
.
stringify
(
options
.
data
)
}).
then
((
res
)
=>
{
}).
then
((
res
)
=>
{
options
.
success
(
res
.
data
)
options
.
success
(
res
.
data
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
)
=>
{
options
.
error
(
err
)
options
.
error
(
err
);
})
})
}
}
axiosGet
(
options
)
{
axiosGet
(
options
)
{
instance
({
instance
({
url
:
options
.
url
,
url
:
options
.
url
,
method
:
'get'
,
method
:
'get'
params
:
options
.
data
}).
then
((
res
)
=>
{
}).
then
((
res
)
=>
{
options
.
success
(
res
.
data
)
options
.
success
(
res
.
data
);
}).
catch
((
err
)
=>
{
}).
catch
((
err
)
=>
{
options
.
error
(
err
)
options
.
error
(
err
);
})
})
}
}
}
}
\ No newline at end of file
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment