const baseURL = "接口名";
const request = params => {
const token = wx.getStorageSync("token").token;
return new Promise((resolve, reject) => {
wx.showLoading();
wx.request({
url: baseURL + params.url,
data: params.data,
method: params.method,
header: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
exhibitionId: 1,
token: token
},
success(res) {
const pages = getCurrentPages();
// const app = getApp();
if (res.statusCode === 200) {
if (res.data.code === 200) {
resolve(res.data);
} else if (res.data.code === 401) {
wx.navigateTo({
url: '/pages/login/index',
})
} else {
if (res.data.code == 1104) {
wx.clearStorageSync()
wx.redirectTo({
url: '/pages/login/index',
})
} else {
reject(res.data);
}
}
}
},
fail(err) {
reject(err);
},
complete() {
wx.hideLoading();
}
});
});
};
const _upload = (filePath, type) => {
const token = wx.getStorageSync("token").access_token;
return new Promise((resolve, reject) => {
wx.showLoading();
wx.uploadFile({
url: baseURL + "upload_file", //仅为示例,非真实的接口地址
filePath,
name: "file",
header: {
authorization: token ? "Bearer " + token : undefined
},
formData: {
type
},
success(res) {
resolve(JSON.parse(res.data));
},
fail(err) {
reject(err);
},
complete() {
wx.hideLoading();
}
});
});
};
const _get = (url, data) => {
return request({
url,
data,
method: "GET"
});
};
const _post = (url, data) => {
return request({
url,
data,
method: "POST"
});
};
const _put = (url, data) => {
return request({
url,
data,
method: "PUT"
});
};
const _delete = (url, data) => {
return request({
url,
data,
method: "DELETE"
});
};
module.exports = {
baseURL,
_get,
_post,
_put,
_delete,
_upload
};