1.h5端配置manifest.json文件
"devServer" : {
"https" : false,
"proxy" : {
"/api" : {
"target" : "http://pets.wgzero.com/api",
"changeOrigin" : true,
"secure" : false,
"pathRewrite" : {
"^/api" : "/"
}
}
}
}
2.条件编译: 从上到下执行
let urlAll = null
urlAll = baseUrl + url
urlAll = url
3.request.js文件
let baseUrl = "http://pets.wgzero.com"
export default class Request {
http(param) {
console.log("xx", param)
let url = param.url,
method = param.method,
header = {},
data = param.data || {},
token = param.token || "",
hideLoading = param.hideLoading || false;
let urlAll = null
urlAll = baseUrl + url
urlAll = url
console.log("urlAll", urlAll)
if (method) {
method = method.toUpperCase();
if (method == "POST") {
header = {
'content-type': "application/x-www-form-urlencoded"
};
} else {
header = {
'content-type': "application/json"
};
}
}
if (!hideLoading) {
uni.showLoading({
title: '加载中...'
});
}
return new Promise((resolve, reject) => {
uni.request({
url: urlAll,
data: data,
method: method,
header: header,
success: (res) => {
console.log("axios", res)
if (res.statusCode && res.statusCode != 200) {
uni.showToast({
title: "api错误" + res.errMsg,
icon: 'none'
});
return;
}
if (res.data.code) {
if (res.data.code != '200') {
uni.showToast({
title: "" + res.data.msg,
icon: 'none'
});
return;
}
} else {
uni.showToast({
title: "code!=200" + res.data.msg,
icon: 'none'
});
return;
}
resolve(res.data)
},
fail: (e) => {
uni.showToast({
title: "" + e.data.msg,
icon: 'none'
});
resolve(e.data);
},
complete() {
if (!hideLoading) {
uni.hideLoading();
}
resolve();
return;
}
})
})
}
}
4.http.js文件
import Request from '../request/index.js'
let request = new Request().http
export default {
getBanner: function(data) {
return request({
url: "/api/getBanner",
method: "GET",
data: data,
})
},
getDetail: function(data) {
return request({
url: "/api/getCategoryDetail",
method: "POST",
data: data,
})
},
}
5.main.js里面引用+页面使用
import http from "./http/http.js"
Vue.prototype.$http = http
handleClickPost(){
let data = {
id: 1,
pid: 1
}
this.$http.getDetail(data).then(res => {
console.log("post", res)
})
},