import { basePicUrl, baseUrl, baseFileUrl, baseIconUrl, mode } from '../config/index';
|
|
function request(fun, hostUrl) {
|
const bUrl = hostUrl ? hostUrl : baseUrl;
|
if (fun.params != undefined) {
|
var param = '';
|
Object.keys(fun.params).forEach(key => {
|
var value = fun.params[key];
|
if (param == '') {
|
param += key + '=' + value;
|
} else {
|
param += '&' + key + '=' + value;
|
}
|
});
|
var url = fun.url;
|
fun.url = bUrl + url + '?' + param;
|
} else {
|
var url = fun.url;
|
fun.url = bUrl + url;
|
}
|
|
return new Promise((resolve, reject) => {
|
wx.request({
|
url: fun.url,
|
data: fun.data,
|
method: fun.method,
|
success(res) {
|
if (mode == 'debug') {
|
console.log(
|
'|------------------------------------------------------------------------------------------------------------',
|
);
|
console.log('|--访问: ', fun.url);
|
console.log('|--数据: ', fun.data);
|
console.log('|--结果: ', res);
|
}
|
if (res.statusCode == 200) {
|
resolve(res);
|
} else {
|
reject(res.statusCode);
|
}
|
},
|
fail(err) {
|
if (mode == 'debug') {
|
console.log('--------------请求错误----------------' + fun.url);
|
console.log(err);
|
}
|
// wx.showToast({
|
// title: '请求失败',
|
// icon: 'none',
|
// duration: 2000,
|
// });
|
reject(err);
|
},
|
});
|
});
|
}
|
|
export function get(fun, hostUrl) {
|
fun['method'] = 'GET';
|
return request(fun, hostUrl);
|
}
|
export function post(fun, hostUrl) {
|
fun['method'] = 'POST';
|
return request(fun, hostUrl);
|
}
|
|
export function put(fun, hostUrl) {
|
fun['method'] = 'PUT';
|
return request(fun, hostUrl);
|
}
|