uniapp网络请求uni.request封装
import Vue from 'vue'
export function request(url, data, method = 'get', contentType = 1) {
const baseUrl = Vue.prototype.$baseUrl;
let header = {
'content-type': contentType === 1 ? 'application/json' : 'application/x-www-form-urlencoded',
Authorization: uni.getStorageSync('authorization') != '' ? uni.getStorageSync('authorization') : null
}
for (let property in data) {
if (data[property] == null) {
delete data[property]
}
}
return new Promise((resolve, reject) => {
uni.request({
url: baseUrl + url,
data: data,
method,
header,
success: (res) => {
var code = res.data.code;
var Msg=res.data.msg;
if (res.statusCode == 200) {
if(code==505){
uni.showToast({
icon: 'none',
title: '请先进行登陆',
duration: 2500
});
}
resolve(res.data)
} else if (res.statusCode == 405) {
uni.showToast({
icon: 'none',
title: res.data.message,
duration: 1500
});
}
},
fail: (err) => {
uni.showToast({
icon: 'none',
title: err.errMsg,
duration: 2000
});
reject(err)
}
})
})
}
main.js
import {request} from './common/http.js'
Vue.prototype.$baseUrl = 'https://*******'
vue.prototype.$http=request
页面中使用
getInit(){
this.$http('index/list',{},'get').then(res=>{
})
}