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.
126 lines
2.9 KiB
126 lines
2.9 KiB
1 year ago
|
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
|
||
|
});
|
||
|
}
|
||
|
|
||
|
|
||
|
//上传图片文件
|
||
|
const upImgFile = function(url, imgSrc) {
|
||
|
let header = {
|
||
|
// 'content-type': 'multipart/form-data'
|
||
|
'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);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// 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);
|
||
|
// }
|
||
|
// });
|
||
|
});
|
||
|
};
|
||
|
|
||
|
//暴露出去
|
||
|
module.exports = {
|
||
|
get,
|
||
|
post,
|
||
|
upImgFile
|
||
|
};
|