Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mt-education-web
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王旭
mt-education-web
Commits
959d762b
Commit
959d762b
authored
Jun 26, 2019
by
curryft
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
token
parent
3cd2ae00
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
146 additions
and
75 deletions
+146
-75
App.vue
src/App.vue
+53
-23
request.js
src/utils/request.js
+70
-42
vue.config.js
vue.config.js
+23
-10
No files found.
src/App.vue
View file @
959d762b
...
@@ -7,15 +7,18 @@
...
@@ -7,15 +7,18 @@
</
template
>
</
template
>
<
script
>
<
script
>
import
Vue
from
'vue'
import
zhCN
from
'ant-design-vue/lib/locale-provider/zh_CN'
;
import
zhCN
from
'ant-design-vue/lib/locale-provider/zh_CN'
;
import
{
AppDeviceEnquire
}
from
'@/utils/mixin'
;
import
{
AppDeviceEnquire
}
from
'@/utils/mixin'
;
import
{
sendUserCodeAndCorpId
}
from
'@/api/user'
;
import
{
sendUserCodeAndCorpId
,
getToken
}
from
'@/api/user'
;
function
GetQueryString
(
name
)
{
function
GetQueryString
(
name
)
{
var
reg
=
new
RegExp
(
'(^|&)'
+
name
+
'=([^&]*)(&|$)'
,
'i'
);
var
reg
=
new
RegExp
(
'(^|&)'
+
name
+
'=([^&]*)(&|$)'
,
'i'
);
var
r
=
window
.
location
.
search
.
substr
(
1
).
match
(
reg
);
// 获取url中"?"符后的字符串并正则匹配
var
r
=
window
.
location
.
search
.
substr
(
1
).
match
(
reg
);
// 获取url中"?"符后的字符串并正则匹配
var
context
=
''
;
var
context
=
''
;
if
(
r
!=
null
)
{
context
=
r
[
2
];
}
if
(
r
!=
null
)
{
context
=
r
[
2
];
}
reg
=
null
;
reg
=
null
;
r
=
null
;
r
=
null
;
return
context
==
null
||
context
===
''
||
context
===
'undefined'
?
''
:
context
;
return
context
==
null
||
context
===
''
||
context
===
'undefined'
?
''
:
context
;
...
@@ -29,29 +32,56 @@
...
@@ -29,29 +32,56 @@
};
};
},
},
methods
:
{
methods
:
{
getUserInfoFromServer
()
{
// getUserInfoFromServer() {
const
userCode
=
GetQueryString
(
'userCode'
);
// const userCode = GetQueryString('userCode');
const
corpId
=
GetQueryString
(
'corpId'
);
// const corpId = GetQueryString('corpId');
// const userId = GetQueryString('userId');
// // const userId = GetQueryString('userId');
this
.
$ls
.
set
(
'corpId'
,
corpId
)
// this.$ls.set('corpId', corpId)
// this.$ls.set('userId', userId)
// // this.$ls.set('userId', userId)
console
.
log
(
userCode
,
corpId
);
// console.log(userCode, corpId);
const
data
=
{
// const data = {
code
:
userCode
,
corpId
// code: userCode
};
// };
return
sendUserCodeAndCorpId
(
data
).
then
(
res
=>
{
// getToken(data).then((res) => {
if
(
res
.
code
===
'M0000'
)
{
// this.$ls.set('accessToken', res.data.access_token)
const
userId
=
res
.
data
.
corpUsers
[
0
].
id
;
// })
this
.
$ls
.
set
(
'userId'
,
userId
)
// return sendUserCodeAndCorpId(data).then(res => {
alert
(
userId
);
// if (res.code === 'M0000') {
}
else
{
// const userId = res.data.corpUsers[0].id;
alert
(
'获取失败'
);
// this.$ls.set('userId', userId)
}
// alert(userId);
});
// } else {
}
// alert('获取失败');
// }
// });
// }
},
},
async
mounted
()
{
async
mounted
()
{
await
this
.
getUserInfoFromServer
();
await
this
.
getUserInfoFromServer
();
const
userCode
=
GetQueryString
(
"userCode"
)
const
corpId
=
GetQueryString
(
"corpId"
)
Vue
.
ls
.
setItem
(
'corpId'
,
corpId
)
console
.
log
(
userCode
,
corpId
);
const
data
=
{
code
:
userCode
}
//获取
await
axios
.
get
(
'/api/oauth/getTokenByCode'
,{
params
:
data
,
headers
:
{
'clientType'
:
'app'
}}).
then
(
res
=>
{
Vue
.
ls
.
set
(
'accessToken'
,
res
.
data
.
access_token
)
})
await
sendUserCodeAndCorpId
(
data
).
then
(
res
=>
{
if
(
res
.
code
===
'M0000'
)
{
const
userId
=
res
.
data
.
corpUsers
[
0
].
id
const
accessToken
=
res
.
data
.
access_token
const
refreshToken
=
res
.
data
.
refresh_token
Vue
.
ls
.
set
(
'userId'
,
userId
)
Vue
.
ls
.
set
(
'accessToken'
,
accessToken
)
Vue
.
ls
.
set
(
'refreshToken'
,
refreshToken
)
alert
(
userId
)
}
else
{
alert
(
'获取失败'
)
}
})
}
}
};
};
</
script
>
</
script
>
...
...
src/utils/request.js
View file @
959d762b
import
Vue
from
'vue'
import
Vue
from
'vue'
;
import
axios
from
'axios'
import
axios
from
'axios'
;
import
store
from
'@/store'
import
store
from
'@/store'
;
import
{
import
{
VueAxios
VueAxios
}
from
'./axios'
}
from
'./axios'
;
import
notification
from
'ant-design-vue/es/notification'
import
notification
from
'ant-design-vue/es/notification'
;
import
{
import
{
ACCESS_TOKEN
ACCESS_TOKEN
}
from
'@/store/mutation-types'
}
from
'@/store/mutation-types'
;
// 创建 axios 实例
// 创建 axios 实例
const
service
=
axios
.
create
({
const
service
=
axios
.
create
({
// baseURL: 'http://106.2.13.200:8032', // api base_url
// baseURL: 'http://106.2.13.200:8032', // api base_url
baseURL
:
'
http://192.168.9.23:8080
'
,
// api base_url
baseURL
:
'
api/task/v1
'
,
// api base_url
timeout
:
6000
// 请求超时时间
timeout
:
6000
// 请求超时时间
})
});
// 刷新token的请求方法
function
getRefreshToken
()
{
const
params
=
{
refreshToken
:
this
.
$ls
.
getItem
(
'refreshToken'
)
};
return
axios
.
get
(
'http://192.168.5.41/api/task/v1/task/tb-user/refreshToken'
,
{
params
,
headers
:
{
'clientType'
:
'app'
}
});
}
const
err
=
(
error
)
=>
{
const
err
=
(
error
)
=>
{
if
(
error
.
response
)
{
if
(
error
.
response
)
{
const
data
=
error
.
response
.
data
// const data = error.response.data
const
token
=
Vue
.
ls
.
get
(
ACCESS_TOKEN
)
// const token = Vue.ls.get(ACCESS_TOKEN)
if
(
error
.
response
.
status
===
403
)
{
// if (error.response.status === 403) {
notification
.
error
({
// notification.error({
message
:
'Forbidden'
,
// message: 'Forbidden',
description
:
data
.
msg
// description: data.msg
})
// })
}
// }
if
(
error
.
response
.
status
===
401
&&
!
(
data
.
result
&&
data
.
result
.
isLogin
))
{
// if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
notification
.
error
({
// notification.error({
message
:
'Unauthorized'
,
// message: 'Unauthorized',
description
:
'Authorization verification failed'
// description: 'Authorization verification failed'
})
// })
if
(
token
)
{
// if (token) {
store
.
dispatch
(
'Logout'
).
then
(()
=>
{
// store.dispatch('Logout').then(() => {
setTimeout
(()
=>
{
// setTimeout(() => {
window
.
location
.
reload
()
// window.location.reload()
},
1500
)
// }, 1500)
})
// })
}
// }
}
}
}
return
Promise
.
reject
(
error
)
return
Promise
.
reject
(
error
)
;
}
}
;
// request interceptor
// request interceptor
service
.
interceptors
.
request
.
use
(
config
=>
{
service
.
interceptors
.
request
.
use
(
config
=>
{
const
token
=
Vue
.
ls
.
get
(
ACCESS_TOKEN
)
// const token = Vue.ls.get(ACCESS_TOKEN)
if
(
token
)
{
// if (token) {
config
.
headers
[
'Access-Token'
]
=
token
// 让每个请求携带自定义 token 请根据实际情况自行修改
// config.headers['Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
// }
config
.
headers
[
'clientType'
]
=
'app'
;
// 如果不为空的话就设置 因为一开始获取token的接口是没有的
if
(
Vue
.
ls
.
getItem
(
'accessToken'
)
!=
''
&&
Vue
.
ls
.
getItem
(
'accessToken'
)
!=
undefined
)
{
config
.
headers
[
'Authorization'
]
=
'Bearer '
+
Vue
.
ls
.
getItem
(
'accessToken'
);
}
}
return
config
return
config
;
},
err
)
},
err
)
;
// response interceptor
// response interceptor
service
.
interceptors
.
response
.
use
((
response
)
=>
{
service
.
interceptors
.
response
.
use
((
response
)
=>
{
return
response
.
data
const
resConfig
=
response
.
config
;
},
err
)
console
.
log
(
resConfig
);
const
res
=
response
.
data
;
if
(
res
.
code
==
'G_T_001'
)
{
console
.
log
(
'refresh'
);
return
getRefreshToken
()
.
then
((
res
)
=>
{
const
data
=
res
.
data
;
console
.
log
(
data
);
console
.
log
(
'重新请求'
);
Vue
.
ls
.
set
(
'accessToken'
,
data
.
access_token
);
Vue
.
ls
.
set
(
'refreshToken'
,
data
.
refresh_token
);
resConfig
.
headers
[
'Authorization'
]
=
'Bearer '
+
Vue
.
ls
.
get
(
'refreshToken'
);
return
axios
(
resConfig
);
});
}
return
res
;
},
err
);
const
installer
=
{
const
installer
=
{
vm
:
{},
vm
:
{},
install
(
Vue
)
{
install
(
Vue
)
{
Vue
.
use
(
VueAxios
,
service
)
Vue
.
use
(
VueAxios
,
service
)
;
}
}
}
}
;
export
{
export
{
installer
as
VueAxios
,
installer
as
VueAxios
,
service
as
axios
service
as
axios
}
}
;
vue.config.js
View file @
959d762b
const
path
=
require
(
'path'
)
const
path
=
require
(
'path'
)
;
const
webpack
=
require
(
'webpack'
)
const
webpack
=
require
(
'webpack'
)
;
function
resolve
(
dir
)
{
function
resolve
(
dir
)
{
return
path
.
join
(
__dirname
,
dir
)
return
path
.
join
(
__dirname
,
dir
)
;
}
}
// vue.config.js
// vue.config.js
module
.
exports
=
{
module
.
exports
=
{
/*
/*
Vue-cli3:
Vue-cli3:
Crashed when using Webpack `import()` #2463
Crashed when using Webpack `import()` #2463
...
@@ -21,7 +22,9 @@ module.exports = {
...
@@ -21,7 +22,9 @@ module.exports = {
}
}
},
},
*/
*/
publicPath
:
"./"
,
publicPath
:
process
.
env
.
NODE_ENV
===
'production'
?
'/zx-task-manager'
:
'/'
,
configureWebpack
:
{
configureWebpack
:
{
module
:
{
module
:
{
rules
:
[
rules
:
[
...
@@ -55,10 +58,10 @@ module.exports = {
...
@@ -55,10 +58,10 @@ module.exports = {
.
set
(
'@comp'
,
resolve
(
'src/components'
))
.
set
(
'@comp'
,
resolve
(
'src/components'
))
.
set
(
'@views'
,
resolve
(
'src/views'
))
.
set
(
'@views'
,
resolve
(
'src/views'
))
.
set
(
'@layout'
,
resolve
(
'src/layout'
))
.
set
(
'@layout'
,
resolve
(
'src/layout'
))
.
set
(
'@static'
,
resolve
(
'src/static'
))
.
set
(
'@static'
,
resolve
(
'src/static'
))
;
const
svgRule
=
config
.
module
.
rule
(
'svg'
)
const
svgRule
=
config
.
module
.
rule
(
'svg'
)
;
svgRule
.
uses
.
clear
()
svgRule
.
uses
.
clear
()
;
svgRule
svgRule
.
oneOf
(
'inline'
)
.
oneOf
(
'inline'
)
.
resourceQuery
(
/inline/
)
.
resourceQuery
(
/inline/
)
...
@@ -71,7 +74,7 @@ module.exports = {
...
@@ -71,7 +74,7 @@ module.exports = {
.
loader
(
'file-loader'
)
.
loader
(
'file-loader'
)
.
options
({
.
options
({
name
:
'assets/[name].[hash:8].[ext]'
name
:
'assets/[name].[hash:8].[ext]'
})
})
;
/* svgRule.oneOf('inline')
/* svgRule.oneOf('inline')
.resourceQuery(/inline/)
.resourceQuery(/inline/)
.use('vue-svg-loader')
.use('vue-svg-loader')
...
@@ -107,6 +110,16 @@ module.exports = {
...
@@ -107,6 +110,16 @@ module.exports = {
devServer
:
{
devServer
:
{
// development server port 8000
// development server port 8000
port
:
8000
,
port
:
8000
,
proxy
:
{
'/api'
:
{
target
:
'http://192.168.5.41'
,
changeOrigin
:
true
,
ws
:
true
,
pathRewrite
:
{
'^/api'
:
'/api'
}
}
}
// host: '192.168.1.108',
// host: '192.168.1.108',
// disableHostCheck: true
// disableHostCheck: true
// proxy: {
// proxy: {
...
@@ -124,4 +137,4 @@ module.exports = {
...
@@ -124,4 +137,4 @@ module.exports = {
lintOnSave
:
undefined
,
lintOnSave
:
undefined
,
// babel-loader no-ignore node_modules/*
// babel-loader no-ignore node_modules/*
transpileDependencies
:
[]
transpileDependencies
:
[]
}
}
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment