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.
79 lines
2.5 KiB
79 lines
2.5 KiB
var global = require('./config.js');
|
|
const request = function (url, options) {
|
|
let token = uni.getStorageSync('token');
|
|
if (token == undefined || token == null) {
|
|
token = '';
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: `${global.BASEURL()}${url}`,
|
|
method: options.method,
|
|
data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
|
|
header: {
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
Authorization: token
|
|
},
|
|
success(response) {
|
|
// console.log(response);
|
|
if (response.statusCode === 200) {
|
|
if (response.data.code === 0) {
|
|
resolve(response.data);
|
|
} else if (response.data.code === 10007 || response.data.code == 1024 || response.data.code == 2003) {
|
|
uni.reLaunch({
|
|
url: '/pages/login/login'
|
|
});
|
|
} else {
|
|
let errmsg = response.data.msg;
|
|
if (errmsg == undefined || errmsg == 'undefined') {
|
|
errmsg = '未返回错误信息';
|
|
}
|
|
uni.showToast({
|
|
title: errmsg,
|
|
icon: 'none',
|
|
duration: 3000
|
|
});
|
|
reject(response.data);
|
|
}
|
|
} else {
|
|
uni.showToast({
|
|
title: '网络问题,请稍后再试。',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
reject(false);
|
|
}
|
|
},
|
|
fail(error) {
|
|
uni.showToast({
|
|
title: '网络问题,请稍后再试。',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
reject(error.data);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
function get(url, options = {}) {
|
|
return request(url, {
|
|
method: 'GET',
|
|
data: options
|
|
});
|
|
}
|
|
function post(url, options = {}) {
|
|
return request(url, {
|
|
method: 'POST',
|
|
data: options
|
|
});
|
|
}
|
|
function put(url, options = {}) {
|
|
return request(url, {
|
|
method: 'PUT',
|
|
data: options
|
|
});
|
|
}
|
|
module.exports = {
|
|
get: get,
|
|
post: post,
|
|
put: put
|
|
};
|
|
|