Commit 9d446897 authored by Damon's avatar Damon

feat: hTTP请求封装

parent 2ad91cea
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/',
argoDebugMode: 1,
version: 'v1.0.0'
......
<script>
import "./../.env.js"
export default {
onLaunch: function () {
globalData: {
// 开发环境
uniEnv: process.uniEnv,
},
onLaunch: function() {
// 版本更新
this.updateApp()
},
onShow: function () {
onShow: function() {
console.log('App Show')
},
onHide: function () {
onHide: function() {
console.log('App Hide')
},
methods: {
......@@ -28,7 +33,7 @@ export default {
// }
// })
updateManager.onUpdateReady(function (res) {
updateManager.onUpdateReady(function(res) {
uni.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
......@@ -43,7 +48,7 @@ export default {
content:
'本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
showCancel: false,
success: function (result) {
success: function(result) {
if (result.confirm) updateManager.applyUpdate()
},
})
......@@ -52,7 +57,7 @@ export default {
})
})
updateManager.onUpdateFailed(function (res) {
updateManager.onUpdateFailed(function(res) {
// 新的版本下载失败
uni.showModal({
title: '已经有新版本了哟~',
......@@ -63,7 +68,8 @@ export default {
} else {
uni.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
content:
'当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
showCancel: false,
})
}
......
const globalUrl = process.uniEnv || {}
// 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token变量
const install = (Vue, vm) => {
const systemApiUrl = globalUrl.apiUrl
// 此为自定义配置参数,具体参数见上方说明
Vue.prototype.$u.http.setConfig({
baseUrl: systemApiUrl,
header: {
'content-type': 'application/json; charset=utf-8'
},
loadingTime: 800,
// ......
});
// 请求拦截,配置Token等参数
Vue.prototype.$u.http.interceptor.request = (config) => {
// 引用token
const token = vm.vuex_token
const reg = /\?\w+/
if(config.url.indexOf('access-token=') >=0 && config.params['access-token']){
// 给默认参数增加access-token (Java端)
if(reg.test(config.url)){
config.url += '&access-token=' + token
}else{
config.url += '?access-token=' + token
}
}
const systemApiUrl = globalUrl.apiUrl
// 此为自定义配置参数,具体参数见上方说明
Vue.prototype.$u.http.setConfig({
baseUrl: systemApiUrl,
header: {
channel: 'Applets',
'content-type': 'application/json; charset=utf-8',
},
originalData: true
})
// 请求拦截,配置Token等参数
Vue.prototype.$u.http.interceptor.request = (config) => {
// 引用token
// const token = vm.vuex_token
// const reg = /\?\w+/
// if(config.url.indexOf('access-token=') >=0 && config.params['access-token']){
// // 给默认参数增加access-token (Java端)
// if(reg.test(config.url)){
// config.url += '&access-token=' + token
// }else{
// config.url += '?access-token=' + token
// }
// }
// 最后需要将config进行return
return config;
// 如果return一个false值,则会取消本次请求
// if(config.url == '/user/rest') return false; // 取消某次请求
}
// 响应拦截,判断状态码是否通过
Vue.prototype.$u.http.interceptor.response = (res) => {
if(res.code == 200) {
// res为服务端返回值,可能有code,result等字段
// 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
// 如果配置了originalData为true,请留意这里的返回值
return res.result;
} else if(res.code == 201) {
// 假设201为token失效,这里跳转登录
vm.$u.toast('验证失败,请重新登录');
setTimeout(() => {
// 此为uView的方法,详见路由相关文档
vm.$u.route('/pages/login/index')
}, 1500)
return false;
} else {
// 如果返回false,则会调用Promise的reject回调,
// 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值
return false;
// 最后需要将config进行return
return config
// 如果return一个false值,则会取消本次请求
// if(config.url == '/user/rest') return false; // 取消某次请求
}
// 响应拦截,判断状态码是否通过
Vue.prototype.$u.http.interceptor.response = (res) => {
const data = res.data
if (res.statusCode === 200) {
if (data.code === 0) {
// res为服务端返回值,可能有code,result等字段
return data.data
} else if (data.code === 401) {
vm.$u.toast(data.msg)
let pages = getCurrentPages()
let route = pages[pages.length - 1].route
// 如果不是在登录页,则跳转到登录页
if (route !== 'pages/login/index') {
setTimeout(() => {
vm.$u.route('/pages/login/index')
}, 1500)
}
return false
} else {
// 如果返回false,则会调用Promise的reject回调,
vm.$u.toast(data.msg)
return false
}
} else { // HTTP状态非200
vm.$u.toast(data.msg || '服务器错误')
}
}
}
}
export default {
install
}
\ No newline at end of file
install,
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment