Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
web-project
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
海康威视
web-project
Commits
aa31a4b9
Commit
aa31a4b9
authored
Sep 12, 2024
by
张伯涛
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AItoken的axios封装添加headers变量
parent
202b2912
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
4 additions
and
339 deletions
+4
-339
token.js
src/api/AIStation/token.js
+2
-3
heepThree.js
src/api/heepThree.js
+0
-335
httpTwo.js
src/api/httpTwo.js
+2
-1
No files found.
src/api/AIStation/token.js
View file @
aa31a4b9
import
httpTwo
from
'../httpTwo'
import
heepThree
from
'../heepThree'
import
qs
from
"qs"
;
import
{
encrypt
}
from
'../../utils/jsencrypt.js'
import
http
from
"@/api/http.js"
;
// 获取随机uuid
export
const
getAuthUUID
=
params
=>
{
...
...
@@ -19,7 +17,8 @@ export const createToken = params => {
const
sign
=
qs
.
stringify
({
sign
:
encrypt
(
params
)
})
return
heepThree
.
post
(
`/auth/createToken`
,
sign
).
then
(
res
=>
res
).
catch
(
function
(
error
)
{
const
headers
=
{
'Content-Type'
:
'application/x-www-form-urlencoded'
}
return
httpTwo
.
post
(
`/auth/createToken`
,
sign
,
headers
).
then
(
res
=>
res
).
catch
(
function
(
error
)
{
console
.
log
(
error
);
})
}
...
...
src/api/heepThree.js
deleted
100644 → 0
View file @
202b2912
import
axios
from
'axios'
import
store
from
'../store/index'
// import qs from "qs";
//import { useRouter, useRoute } from 'vue-router'
import
{
useRouter
}
from
"vue-router"
;
//import { ref } from 'vue'
//import { ExternalSERVEICE, WithinSERVEICE } from '../../public/config'
import
{
ElLoading
as
Loading
,
ElMessage
as
Message
}
from
'element-plus'
;
let
loadingInstance
;
let
loadingStatus
=
false
;
const
router
=
useRouter
();
const
AIapi
=
axios
.
create
({
timeout
:
50000
,
baseURL
:
'/AIapi'
,
//ExternalSERVEICE;
withCredentials
:
true
,
headers
:
{
'Content-Type'
:
'application/x-www-form-urlencoded'
}
})
if
(
!
AIapi
.
defaults
.
baseURL
.
endsWith
(
'/'
))
{
AIapi
.
defaults
.
baseURL
+=
"/"
;
}
let
ipAddress
=
AIapi
.
defaults
.
baseURL
;
AIapi
.
interceptors
.
request
.
use
((
config
)
=>
{
config
.
baseURL
=
'/AIapi'
;
console
.
log
(
config
.
baseURL
);
// config.data = qs.stringify(config.data);
return
config
;
},
(
error
)
=>
{
return
Promise
.
reject
(
error
);
});
AIapi
.
interceptors
.
response
.
use
((
res
)
=>
{
closeLoading
();
checkResponse
(
res
);
return
Promise
.
resolve
(
res
);
},
(
error
)
=>
{
closeLoading
();
let
httpMessage
=
''
;
if
(
error
.
response
)
{
alert
(
'1'
)
if
(
error
.
response
.
status
==
'401'
)
{
if
(
error
.
response
.
data
&&
error
.
response
.
data
.
status
==
401
)
{
if
(
!
localStorage
.
getItem
(
'user'
))
{
Message
.
error
({
showClose
:
true
,
message
:
'登陆已过期'
,
type
:
'error'
});
}
toLogin
();
return
;
}
}
if
(
error
.
response
.
status
==
'404'
)
{
httpMessage
=
"未找到请求地址"
;
}
else
if
(
error
.
response
.
data
&&
error
.
response
.
data
.
message
)
{
httpMessage
=
error
.
response
.
data
.
message
;
}
}
if
(
error
.
request
)
{
httpMessage
=
"未连接到远程服务器!"
;
console
.
log
(
httpMessage
+
error
.
message
);
if
(
error
.
config
.
url
!=
'/api/User/getVierificationCode'
){
toLogin
();
}
}
else
{
httpMessage
=
'服务器处理异常'
;
}
redirect
(
httpMessage
);
return
Promise
.
reject
(
error
.
response
||
{},
httpMessage
);
});
function
changeIP
(
IP
){
AIapi
.
defaults
.
baseURL
=
IP
;
}
function
showLoading
(
loading
)
{
if
(
!
loading
||
loadingStatus
)
{
return
;
}
loadingInstance
=
Loading
.
service
({
lock
:
true
,
text
:
'Loading'
,
customClass
:
"http-loading"
,
background
:
typeof
loading
==
"string"
?
loading
:
'正在处理.....'
,
background
:
'rgba(58, 61, 63, 0.32)'
});
}
function
closeLoading
()
{
if
(
loadingInstance
)
{
loadingInstance
.
close
();
}
if
(
loadingStatus
)
{
loadingStatus
=
false
;
if
(
loadingInstance
)
{
loadingInstance
.
close
();
}
}
}
function
checkResponse
(
res
)
{
//刷新token
if
(
!
res
.
headers
)
{
if
(
res
.
getResponseHeader
(
"ams_exp"
)
==
"1"
)
{
replaceToken
();
}
}
else
if
(
res
.
headers
.
ams_exp
==
"1"
)
{
replaceToken
();
}
}
const
_Authorization
=
'Authorization'
;
function
getToken
()
{
// const token = sessionStorage.getItem('token');
// if(token != null)
// {
// return token;
// }else{
// return "";
// }
//console.log("token:" + store.getters.getToken());
const
AIToken
=
localStorage
.
getItem
(
'AIToken'
)
return
AIToken
;
}
/*
url
params请求后台的参数,如:{name:123,values:['a','b','c']}
loading是否显示遮罩层,可以传入true.false.及提示文本
config配置信息,如{timeout:3000,headers:{token:123}}
*/
function
post
(
url
,
params
,
loading
,
config
)
{
showLoading
(
loading
);
//console.log(ipAddress);
AIapi
.
defaults
.
headers
[
_Authorization
]
=
getToken
();
return
new
Promise
((
resolve
,
reject
)
=>
{
AIapi
.
post
(
url
,
params
,
config
)
.
then
(
response
=>
{
resolve
(
response
.
data
);
},
err
=>
{
reject
(
err
&&
err
.
data
&&
err
.
data
.
message
?
err
.
data
.
message
:
'服务器处理异常'
);
})
.
catch
((
error
)
=>
{
reject
(
error
)
})
})
}
//=true异步请求时会显示遮罩层,=字符串,异步请求时遮罩层显示当前字符串
function
get
(
url
,
param
,
loading
,
config
)
{
showLoading
(
loading
);
//console.log(ipAddress);
AIapi
.
defaults
.
headers
[
_Authorization
]
=
getToken
();
return
new
Promise
((
resolve
,
reject
)
=>
{
AIapi
.
get
(
url
,
config
)
.
then
(
response
=>
{
resolve
(
response
.
data
)
},
err
=>
{
reject
(
err
)
})
.
catch
((
error
)
=>
{
reject
(
error
)
})
})
}
//url:url地址
//params:请求参数
//fileName:下载的文件名
//loading:是否显示加载状态
function
download
(
url
,
params
,
fileName
,
loading
,
callback
)
{
fileName
=
fileName
.
replace
(
">"
,
">"
).
replace
(
"<"
,
"<"
);
post
(
url
,
params
,
loading
,
{
responseType
:
'blob'
}).
then
(
content
=>
{
const
blob
=
new
Blob
([
content
])
if
(
'download'
in
document
.
createElement
(
'a'
))
{
// 非IE下载
const
elink
=
document
.
createElement
(
'a'
)
elink
.
download
=
fileName
elink
.
style
.
display
=
'none'
elink
.
href
=
URL
.
createObjectURL
(
blob
)
document
.
body
.
appendChild
(
elink
)
elink
.
click
()
URL
.
revokeObjectURL
(
elink
.
href
)
// 释放URL 对象
document
.
body
.
removeChild
(
elink
)
}
else
{
// IE10+下载
navigator
.
msSaveBlob
(
blob
,
fileName
)
}
callback
&&
callback
();
})
}
function
createXHR
()
{
if
(
XMLHttpRequest
)
{
return
new
XMLHttpRequest
();
}
if
(
ActiveXObject
)
{
if
(
typeof
arguments
.
callee
.
activeXString
!=
"string"
)
{
var
versions
=
[
"MSXML2.XMLHttp.6.0"
,
"MSXML2.XMLHttp"
,
"MSXML2.XMLHttp.3.0"
];
for
(
var
i
=
0
;
i
<
versions
.
length
;
i
++
)
{
try
{
new
ActiveXObject
(
versions
[
i
]);
arguments
.
callee
.
activeXString
=
versions
[
i
];
break
;
}
catch
(
e
)
{
console
.
log
(
e
);
}
}
}
return
new
ActiveXObject
(
arguments
.
callee
.
activeXString
);
}
}
function
redirect
(
responseText
,
message
)
{
try
{
let
responseData
=
typeof
responseText
==
'string'
?
JSON
.
parse
(
responseText
)
:
responseText
;
if
((
responseData
.
hasOwnProperty
(
'status'
)
&&
responseData
.
status
==
401
)
||
(
responseData
.
data
&&
responseData
.
data
.
status
==
401
))
{
closeLoading
();
toLogin
();
}
else
{
if
(
message
)
{
Message
.
error
({
showClose
:
true
,
message
:
message
,
type
:
'error'
});
}
}
}
catch
(
error
)
{
//console.log(error);
Message
.
error
({
showClose
:
true
,
message
:
responseText
,
type
:
'error'
});
}
}
//退回到登录页
function
toLogin
()
{
if
(
window
.
location
.
hash
)
{
window
.
location
.
href
=
window
.
location
.
origin
+
'/#/login'
return
}
window
.
location
.
href
=
window
.
location
.
origin
+
'/login'
}
//动态刷新token
function
replaceToken
()
{
ajax
({
url
:
"/api/User/replaceToken"
,
param
:
{},
json
:
true
,
success
:
function
(
x
)
{
if
(
x
.
success
)
{
let
userInfo
=
store
.
getters
.
getUserInfo
();
userInfo
.
token
=
x
.
data
;
store
.
commit
(
'setUserInfo'
,
userInfo
);
}
else
{
console
.
log
(
x
.
message
);
toLogin
();
}
},
errror
:
function
(
ex
)
{
console
.
log
(
ex
);
toLogin
();
},
type
:
"post"
,
async
:
false
});
}
function
ajax
(
param
)
{
let
httpParam
=
Object
.
assign
({
url
:
''
,
headers
:
{},
param
:
{},
json
:
true
,
success
:
function
()
{
},
errror
:
function
()
{
},
type
:
'post'
,
async
:
true
},
param
);
httpParam
.
url
=
AIapi
.
defaults
.
baseURL
+
httpParam
.
url
.
replace
(
/
\/?
/
,
''
);
httpParam
.
headers
[
_Authorization
]
=
getToken
();
var
xhr
=
createXHR
();
xhr
.
onreadystatechange
=
function
()
{
if
(
xhr
.
status
==
403
||
xhr
.
status
==
401
)
{
redirect
(
xhr
.
responseText
);
return
;
}
checkResponse
(
xhr
);
if
(
xhr
.
readyState
==
4
&&
xhr
.
status
==
200
)
{
httpParam
.
success
(
httpParam
.
json
?
JSON
.
parse
(
xhr
.
responseText
)
:
xhr
.
responseText
);
return
;
}
if
(
xhr
.
status
!=
0
&&
xhr
.
readyState
!=
1
)
{
httpParam
.
errror
(
xhr
);
}
};
//初始化请求
xhr
.
open
(
httpParam
.
type
,
httpParam
.
url
,
httpParam
.
async
);
xhr
.
setRequestHeader
(
"Content-type"
,
"application/json"
);
for
(
const
key
in
httpParam
.
headers
)
{
xhr
.
setRequestHeader
(
key
,
httpParam
.
headers
[
key
]);
}
try
{
xhr
.
send
(
JSON
.
stringify
(
httpParam
.
param
));
}
catch
(
error
)
{
toLogin
();
}
}
ajax
.
post
=
function
(
url
,
param
,
success
,
errror
)
{
ajax
({
url
:
url
,
param
:
param
,
success
:
success
,
error
:
errror
,
type
:
'post'
})
}
ajax
.
get
=
function
(
url
,
param
,
success
,
errror
)
{
ajax
({
url
:
url
,
param
:
param
,
success
:
success
,
error
:
errror
,
type
:
'get'
})
}
export
default
{
post
,
get
,
download
,
ajax
,
ipAddress
,
changeIP
}
src/api/httpTwo.js
View file @
aa31a4b9
...
...
@@ -137,10 +137,11 @@ function getToken () {
loading是否显示遮罩层,可以传入true.false.及提示文本
config配置信息,如{timeout:3000,headers:{token:123}}
*/
function
post
(
url
,
params
,
loading
,
config
)
{
function
post
(
url
,
params
,
headers
,
loading
,
config
)
{
showLoading
(
loading
);
//console.log(ipAddress);
AIapi
.
defaults
.
headers
[
_Authorization
]
=
getToken
();
AIapi
.
defaults
.
headers
=
headers
||
{
'Content-Type'
:
'application/json'
}
return
new
Promise
((
resolve
,
reject
)
=>
{
AIapi
.
post
(
url
,
params
,
config
)
.
then
(
response
=>
{
...
...
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