Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
study
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
Damon
study
Commits
9d446897
Commit
9d446897
authored
Jul 12, 2021
by
Damon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: hTTP请求封装
parent
2ad91cea
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
61 deletions
+72
-61
.env.dev.js
.env.dev.js
+1
-1
App.vue
src/App.vue
+13
-7
http.interceptor.js
src/common/http.interceptor.js
+58
-53
No files found.
.env.dev.js
View file @
9d446897
const
UNI_APP
=
{
const
UNI_APP
=
{
apiUrl
:
'http://192.168.
1.22
'
,
apiUrl
:
'http://192.168.
0.208:8760
'
,
qn_base_url
:
'https://qn-static.banshouhui.com/study/'
,
qn_base_url
:
'https://qn-static.banshouhui.com/study/'
,
argoDebugMode
:
1
,
argoDebugMode
:
1
,
version
:
'v1.0.0'
version
:
'v1.0.0'
...
...
src/App.vue
View file @
9d446897
<
script
>
<
script
>
import
"./../.env.js"
export
default
{
export
default
{
onLaunch
:
function
()
{
globalData
:
{
// 开发环境
uniEnv
:
process
.
uniEnv
,
},
onLaunch
:
function
()
{
// 版本更新
// 版本更新
this
.
updateApp
()
this
.
updateApp
()
},
},
onShow
:
function
()
{
onShow
:
function
()
{
console
.
log
(
'App Show'
)
console
.
log
(
'App Show'
)
},
},
onHide
:
function
()
{
onHide
:
function
()
{
console
.
log
(
'App Hide'
)
console
.
log
(
'App Hide'
)
},
},
methods
:
{
methods
:
{
...
@@ -28,7 +33,7 @@ export default {
...
@@ -28,7 +33,7 @@ export default {
// }
// }
// })
// })
updateManager
.
onUpdateReady
(
function
(
res
)
{
updateManager
.
onUpdateReady
(
function
(
res
)
{
uni
.
showModal
({
uni
.
showModal
({
title
:
'更新提示'
,
title
:
'更新提示'
,
content
:
'新版本已经准备好,是否重启应用?'
,
content
:
'新版本已经准备好,是否重启应用?'
,
...
@@ -43,7 +48,7 @@ export default {
...
@@ -43,7 +48,7 @@ export default {
content
:
content
:
'本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~'
,
'本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~'
,
showCancel
:
false
,
showCancel
:
false
,
success
:
function
(
result
)
{
success
:
function
(
result
)
{
if
(
result
.
confirm
)
updateManager
.
applyUpdate
()
if
(
result
.
confirm
)
updateManager
.
applyUpdate
()
},
},
})
})
...
@@ -52,7 +57,7 @@ export default {
...
@@ -52,7 +57,7 @@ export default {
})
})
})
})
updateManager
.
onUpdateFailed
(
function
(
res
)
{
updateManager
.
onUpdateFailed
(
function
(
res
)
{
// 新的版本下载失败
// 新的版本下载失败
uni
.
showModal
({
uni
.
showModal
({
title
:
'已经有新版本了哟~'
,
title
:
'已经有新版本了哟~'
,
...
@@ -63,7 +68,8 @@ export default {
...
@@ -63,7 +68,8 @@ export default {
}
else
{
}
else
{
uni
.
showModal
({
uni
.
showModal
({
title
:
'提示'
,
title
:
'提示'
,
content
:
'当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
,
content
:
'当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
,
showCancel
:
false
,
showCancel
:
false
,
})
})
}
}
...
...
src/common/http.interceptor.js
View file @
9d446897
const
globalUrl
=
process
.
uniEnv
||
{}
const
globalUrl
=
process
.
uniEnv
||
{}
// 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token变量
// 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token变量
const
install
=
(
Vue
,
vm
)
=>
{
const
install
=
(
Vue
,
vm
)
=>
{
const
systemApiUrl
=
globalUrl
.
apiUrl
const
systemApiUrl
=
globalUrl
.
apiUrl
// 此为自定义配置参数,具体参数见上方说明
// 此为自定义配置参数,具体参数见上方说明
Vue
.
prototype
.
$u
.
http
.
setConfig
({
Vue
.
prototype
.
$u
.
http
.
setConfig
({
baseUrl
:
systemApiUrl
,
baseUrl
:
systemApiUrl
,
header
:
{
header
:
{
'content-type'
:
'application/json; charset=utf-8'
channel
:
'Applets'
,
}
,
'content-type'
:
'application/json; charset=utf-8'
,
loadingTime
:
800
,
}
,
// ......
originalData
:
true
});
})
// 请求拦截,配置Token等参数
// 请求拦截,配置Token等参数
Vue
.
prototype
.
$u
.
http
.
interceptor
.
request
=
(
config
)
=>
{
Vue
.
prototype
.
$u
.
http
.
interceptor
.
request
=
(
config
)
=>
{
// 引用token
// 引用token
const
token
=
vm
.
vuex_token
//
const token = vm.vuex_token
const
reg
=
/
\?\w
+/
//
const reg = /\?\w+/
if
(
config
.
url
.
indexOf
(
'access-token='
)
>=
0
&&
config
.
params
[
'access-token'
]){
//
if(config.url.indexOf('access-token=') >=0 && config.params['access-token']){
// 给默认参数增加access-token (Java端)
//
// 给默认参数增加access-token (Java端)
if
(
reg
.
test
(
config
.
url
)){
//
if(reg.test(config.url)){
config
.
url
+=
'&access-token='
+
token
//
config.url += '&access-token=' + token
}
else
{
//
}else{
config
.
url
+=
'?access-token='
+
token
//
config.url += '?access-token=' + token
}
//
}
}
//
}
// 最后需要将config进行return
// 最后需要将config进行return
return
config
;
return
config
// 如果return一个false值,则会取消本次请求
// 如果return一个false值,则会取消本次请求
// if(config.url == '/user/rest') return false; // 取消某次请求
// if(config.url == '/user/rest') return false; // 取消某次请求
}
}
// 响应拦截,判断状态码是否通过
// 响应拦截,判断状态码是否通过
Vue
.
prototype
.
$u
.
http
.
interceptor
.
response
=
(
res
)
=>
{
Vue
.
prototype
.
$u
.
http
.
interceptor
.
response
=
(
res
)
=>
{
if
(
res
.
code
==
200
)
{
const
data
=
res
.
data
// res为服务端返回值,可能有code,result等字段
if
(
res
.
statusCode
===
200
)
{
// 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
if
(
data
.
code
===
0
)
{
// 如果配置了originalData为true,请留意这里的返回值
// res为服务端返回值,可能有code,result等字段
return
res
.
result
;
return
data
.
data
}
else
if
(
res
.
code
==
201
)
{
}
else
if
(
data
.
code
===
401
)
{
// 假设201为token失效,这里跳转登录
vm
.
$u
.
toast
(
data
.
msg
)
vm
.
$u
.
toast
(
'验证失败,请重新登录'
);
let
pages
=
getCurrentPages
()
setTimeout
(()
=>
{
let
route
=
pages
[
pages
.
length
-
1
].
route
// 此为uView的方法,详见路由相关文档
// 如果不是在登录页,则跳转到登录页
vm
.
$u
.
route
(
'/pages/login/index'
)
if
(
route
!==
'pages/login/index'
)
{
},
1500
)
setTimeout
(()
=>
{
return
false
;
vm
.
$u
.
route
(
'/pages/login/index'
)
}
else
{
},
1500
)
// 如果返回false,则会调用Promise的reject回调,
}
// 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值
return
false
return
false
;
}
else
{
// 如果返回false,则会调用Promise的reject回调,
vm
.
$u
.
toast
(
data
.
msg
)
return
false
}
}
else
{
// HTTP状态非200
vm
.
$u
.
toast
(
data
.
msg
||
'服务器错误'
)
}
}
}
}
}
}
export
default
{
export
default
{
install
install
,
}
}
\ No newline at end of file
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