物流端
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.
yyt-mall-wl/utils/http.js

106 lines
2.4 KiB

var app = getApp(); //引入全局app.js,我们可以在globalData中定义一些公用的数据,比如baseUrl、token
const request = function(url, options) {
let header = {
'content-type': 'application/json;charset=utf-8'
};
if (app.globalData.token) {
header.Authorization = app.globalData.token;
}
// 检查外部传递的请求头是否存在
if (options.header && typeof options.header === 'object') {
// 合并外部传递的请求头
header = Object.assign({}, header, options.header);
}
return new Promise((resolve, reject) => {
wx.request({
url: app.globalData.baseUrl + url,
method: options.method,
data: options.data,
header: header,
success: (res) => {
if (res.data.code == 500) {
wx.showModal({
showCancel: false,
title: '提示',
content: res.data.message
});
reject(res.data.message);
} else {
resolve(res);
}
},
fail: (err) => {
reject(err);
}
});
});
};
const get = function(url, data, header) {
return request(url, {
method: "GET",
data,
header
});
}
const post = function(url, data, header) {
return request(url, {
method: "POST",
data,
header
});
}
/**
* 上传图片文件
* @param {*} url 请求地址
* @param {*} imgSrc 本地图片临时路径
*/
const upImgFile = function(url, imgSrc) {
let header = {
'content-type': 'application/json;charset=utf-8'
};
if (app.globalData.token) {
header.Authorization = app.globalData.token;
}
// 检查外部传递的请求头是否存在
// if (options.header && typeof options.header === 'object') {
// // 合并外部传递的请求头
// header = Object.assign({}, header, options.header);
// }
return new Promise((resolve, reject) => {
wx.uploadFile({
url: app.globalData.baseUrl + url,
name: "file",
filePath: imgSrc,
header: header,
formData: {
"file": "upimagefile",
},
success: function (res) {
if (res.data.code == 500) {
wx.showModal({
showCancel: false,
title: '提示',
content: res.data.message
});
reject(res.data.message);
} else {
resolve(res);
}
}
});
});
};
//暴露方法
module.exports = {
get,
post,
upImgFile
};