You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.7 KiB
80 lines
1.7 KiB
const http = uni.$u.http;
|
|
const {
|
|
environment
|
|
} = require('@/config/environment.js')
|
|
|
|
/**
|
|
* 混入默认个性化配置
|
|
* @param {*} config
|
|
* @returns
|
|
*/
|
|
function mixinCustom(config) {
|
|
config.custom = Object.assign({
|
|
auth: true,
|
|
toast: true,
|
|
catch: true,
|
|
loading: true
|
|
}, config.custom || {});
|
|
return config;
|
|
}
|
|
|
|
/**
|
|
* 格式化get请求url参数,将对象解析为字符串
|
|
* @param {*} url
|
|
* @param {*} params
|
|
* @returns
|
|
*/
|
|
function urlFormater(url, params) {
|
|
if (params) {
|
|
let paramList = [];
|
|
for (let key in params) {
|
|
paramList.push(key + '=' + params[key])
|
|
}
|
|
return url.indexOf('?') > -1 ? (url + '&' + paramList.join('&')) : (url + '?' + paramList.join('&'))
|
|
}
|
|
return url;
|
|
}
|
|
|
|
const request = {
|
|
// post提交
|
|
post(url, params, isLoading, config = {}) {
|
|
url = environment.baseApi + url
|
|
config = mixinCustom(config)
|
|
loadingFun(isLoading)
|
|
return http.post(url, params, config);
|
|
},
|
|
// get提交
|
|
get(url, params, isLoading, config = {}) {
|
|
url = environment.baseApi + url
|
|
config = mixinCustom(config)
|
|
let path = urlFormater(url, params)
|
|
loadingFun(isLoading)
|
|
return http.get(path, config);
|
|
},
|
|
|
|
// put提交
|
|
put(url, params, isLoading, config = {}) {
|
|
url = environment.baseApi + url
|
|
config = mixinCustom(config)
|
|
loadingFun(isLoading)
|
|
return http.put(url, params, config);
|
|
},
|
|
|
|
// delete提交
|
|
delete(url, params, isLoading, config = {}) {
|
|
url = environment.baseApi + url
|
|
config = mixinCustom(config)
|
|
loadingFun(isLoading)
|
|
return http.delete(url, params, config);
|
|
},
|
|
};
|
|
|
|
function loadingFun(isLoading) {
|
|
if (isLoading) {
|
|
uni.showLoading({
|
|
title: '加载中...',
|
|
mask: true
|
|
})
|
|
}
|
|
}
|
|
export default request;
|
|
|