Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
bdc
/
bdcjg-web
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
888d73dc
authored
2023-02-15 10:02:56 +0800
by
任超
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge branch 'master' of
http://yun.pashanhoo.com:9090/bdc/bdcjg-web
2 parents
6eece206
4c25ea7a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
83 additions
and
83 deletions
src/api/login.js
src/layout/components/Navbar.vue
src/utils/request.js
src/views/login/index.vue
src/api/login.js
View file @
888d73d
...
...
@@ -7,7 +7,7 @@ export function loginIn(username, password) {
method
:
"post"
,
});
}
export
function
log
inO
ut
()
{
export
function
log
o
ut
()
{
return
request
({
url
:
`
${
SERVER
.
MANAGEMENTAPI
}
/logout`
,
method
:
"post"
,
...
...
src/layout/components/Navbar.vue
View file @
888d73d
...
...
@@ -22,7 +22,7 @@
<
script
>
import
sidebarLeft
from
"./Sidebar/sidebarLeft"
;
import
sidebarRight
from
"./Sidebar/sidebarRight"
;
import
{
log
inO
ut
}
from
"@/api/login.js"
;
import
{
log
o
ut
}
from
"@/api/login.js"
;
import
{
mapGetters
}
from
"vuex"
;
export
default
{
components
:
{
...
...
@@ -34,10 +34,15 @@ export default {
},
methods
:
{
onCancel
()
{
log
inO
ut
()
log
o
ut
()
.
then
((
res
)
=>
{
sessionStorage
.
removeItem
(
"token"
);
this
.
$router
.
push
(
"/login"
);
this
.
$router
.
replace
({
path
:
"/login"
,
query
:
{
redirect
:
this
.
$route
.
fullPath
,
},
});
})
.
catch
((
error
)
=>
{
// console.dir(error);
...
...
src/utils/request.js
View file @
888d73d
/**
* 此文件主要创建 axios 实例,然后添加请求拦截器和响应拦截器
*/
import
axios
from
'axios'
import
{
Message
}
from
'element-ui'
import
{
endLoadingSubCount
}
from
'./requestLoading'
import
axios
from
"axios"
;
import
{
Message
}
from
"element-ui"
;
import
{
endLoadingSubCount
}
from
"./requestLoading"
;
import
router
from
"../router"
;
// create an axios instance
const
service
=
axios
.
create
({
baseURL
:
process
.
env
.
VUE_APP_BASE_API
,
withCredentials
:
true
,
//是否允许跨域
headers
:
{
'Content-Type'
:
'application/json; charset=utf-8'
},
timeout
:
15000
})
baseURL
:
process
.
env
.
VUE_APP_BASE_API
,
withCredentials
:
true
,
//是否允许跨域
headers
:
{
"Content-Type"
:
"application/json; charset=utf-8"
,
},
timeout
:
15000
,
})
;
// request interceptor
service
.
interceptors
.
request
.
use
(
config
=>
{
//调用登录接口时无token,也不需要传token,其他接口都传入token
config
.
headers
.
Authorization
=
sessionStorage
.
getItem
(
"token"
)
||
""
;
config
.
headers
.
Accept
=
'application/json'
return
config
},
error
=>
{
Message
.
error
(
'请求超时!'
);
return
Promise
.
reject
(
error
)
}
)
(
config
)
=>
{
//调用登录接口时无token,也不需要传token,其他接口都传入token
config
.
headers
.
Authorization
=
sessionStorage
.
getItem
(
"token"
)
||
""
;
config
.
headers
.
Accept
=
"application/json"
;
return
config
;
},
(
error
)
=>
{
Message
.
error
(
"请求超时!"
);
return
Promise
.
reject
(
error
);
}
)
;
// response interceptor
service
.
interceptors
.
response
.
use
(
response
=>
{
/**
* 对响应数据判断:
* 如果成功返回数据,就通过return把数据返出去
* 如果请求不成功,就在拦截器这里统一处理(组件的代码就不用关注错误的情况了)
*/
if
(
response
.
status
==
200
)
{
// if (response.data.code == 500) {
// Message({
// message: response.data.message,
// type: 'error',
// duration: 5 * 1000
// })
// }
return
response
.
data
;
}
else
{
handleErrorData
(
response
.
data
);
}
return
response
;
},
error
=>
{
endLoadingSubCount
()
// 对响应错误做点什么
Message
({
message
:
'服务器异常,请联系管理员'
,
type
:
'error'
,
duration
:
5
*
1000
})
return
Promise
.
reject
(
error
);
}
)
//对错误信息的处理函数
function
handleErrorData
(
errMes
)
{
if
(
errMes
.
message
)
{
Message
.
error
(
errMes
.
message
);
(
response
)
=>
{
/**
* 对响应数据判断:
* 如果成功返回数据,就通过return把数据返出去
* 如果请求不成功,就在拦截器这里统一处理(组件的代码就不用关注错误的情况了)
*/
if
(
response
.
status
==
200
)
{
return
response
.
data
;
}
else
{
switch
(
errMes
.
code
)
{
case
401
:
Message
.
error
(
"由于长时间未操作,请重新登录!"
);
window
.
location
.
href
=
`http://
${
window
.
location
.
host
}
`
break
;
case
403
:
Message
.
error
(
"拒绝访问"
);
break
;
case
404
:
Message
.
error
(
"很抱歉,资源未找到!"
);
break
;
case
500
:
Message
.
error
(
"服务器错误!"
);
break
;
default
:
Message
.
error
(
"服务正在联调中,请稍后!"
);
break
;
}
// 对响应错误做点什么
Message
({
message
:
"请求失败"
,
type
:
"error"
,
duration
:
5
*
1000
,
});
}
return
response
;
},
(
error
)
=>
{
handleErrorData
(
error
.
response
.
status
);
endLoadingSubCount
();
return
Promise
.
reject
(
error
);
}
);
//对错误信息的处理函数
function
handleErrorData
(
status
)
{
switch
(
status
)
{
case
401
:
Message
.
error
(
"由于长时间未操作,请重新登录!"
);
router
.
replace
({
path
:
"/login"
,
query
:
{
redirect
:
router
.
history
.
current
.
fullPath
,
},
});
break
;
case
403
:
Message
.
error
(
"拒绝访问"
);
break
;
case
404
:
Message
.
error
(
"很抱歉,资源未找到!"
);
break
;
case
500
:
Message
.
error
(
"服务器错误!"
);
break
;
default
:
Message
.
error
(
"服务正在联调中,请稍后!"
);
break
;
}
}
export
default
service
export
default
service
;
...
...
src/views/login/index.vue
View file @
888d73d
...
...
@@ -79,10 +79,12 @@ export default {
loginIn
(
self
.
user
.
account
,
self
.
user
.
password
)
.
then
((
res
)
=>
{
if
(
res
.
status
===
1
)
{
//存储token
sessionStorage
.
setItem
(
"token"
,
`Bearer
${
res
.
content
}
`
);
this
.
$router
.
push
(
'/'
);
//路由判断有无
this
.
$router
.
replace
(
this
.
$route
.
query
.
redirect
||
'/'
)
}
else
{
alert
(
res
.
message
);
//错误处理
}
})
.
catch
((
error
)
=>
{
...
...
Please
register
or
sign in
to post a comment