最近写项目,发现这个封装请求超叼,记录一下
新建个http.js文件
import store from '../store/index.js'
var ipAddress;
if (process.env.NODE_ENV === 'development') {
ipAddress = ""自己的接口
} else {
ipAddress = ""自己的接口
}
export function post(url, data, loading) {
return request(url, 'POST', data, loading);
}
export async function get(url, data, loading) {
return request(url, 'GET', data, loading);
}
export function getToken() {
return store.getters.getToken();
}
export function request(url, method, data, loading) {
if (loading) {
uni.showLoading({
title: typeof loading == 'boolean' ? "正在处理..." : loading
})
}
if (url.startsWith("/")) {
url = url.substr(1)
}
url = ipAddress + url;
var _header = {};
var _token = getToken();
if (_token) {
_header['Authorization'] = _token;
}
return new Promise((reslove, reject) => {
_header.uapp = '1';
uni.request({
url: url,
method: method,
data: data,
header: _header,
success: (res) => {
if (loading) {
uni.hideLoading();
}
if (res.statusCode == 500) {
console.log(JSON.stringify(res))
uni.showToast({
icon: "none",
title: "服务器内部错误"
})
return;
}
if (res.statusCode == 404) {
uni.showToast({
icon: "none",
title: "未找到请求接口"
})
return
}
if (res.statusCode == 202 || res.statusCode == 401) {
if (res.data && res.data.message && res.data.code != '401') {
uni.showToast({
icon: "none",
title: res.data.message
})
return;
};
uni.reLaunch({
url: "/pages/login/login"
})
return;
}
if (res.header.vol_exp == "1") {
post('自己的接口', "POST").then(async result => {
let userInfo = store.getters.getUserInfo();
userInfo.token = result.data;
store.commit('setUserInfo', userInfo);
});
}
reslove(res.data)
},
fail: (err) => {
if (loading) {
uni.hideLoading();
}
console.log(JSON.stringify(err))
if ((err.hasOwnProperty('statusCode') && err.statusCode == 401) ||
(err.data && err.data.code == 401)) {
uni.reLaunch({
url: "/pages/login/login"
})
return;
}
uni.showToast({
icon: "none",
title: "请求接口失败" + JSON.stringify(err)
})
}
});
})
}
export default {
get,
post,
request,
ipAddress
}
再到main.js文件写
import http from './util/http.js'
Vue.prototype.http=http;
使用方法: params是参数
this.http.post("自己的接口", params, true).then(result => {
})