HHSLinkSLink/HSLink-app/util/request.js

123 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-09-16 09:25:13 +08:00
2020-10-10 10:44:32 +08:00
const baseUrl = 'http://106.53.113.158:8048'; //服务器地址
// const baseUrl = 'http://192.168.10.29:8048'; //服务器地址
// const baseUrl = 'http://127.0.0.1:8048'; //服务器地址
2020-09-16 09:25:13 +08:00
const get = (url, data) => {
let httpDefaultOpts = {
url: baseUrl+url,
data: data,
method: 'get',
header: {
'X-Requested-With': 'XMLHttpRequest',
"Accept": "application/json",
"Content-Type": "application/json; charset=UTF-8"
},
dataType: 'json',
}
let promise = new Promise(function(resolve, reject) {
2020-09-17 20:01:50 +08:00
uni.request(httpDefaultOpts).then((res) => {
if (res[1].data.success === true && res[1].data.data !== null) {
resolve(res[1].data)
} else {
uni.showToast({
icon: 'none',
title: '服务器出现异常'
});
}
2020-09-16 09:25:13 +08:00
}
).catch(
(response) => {
2020-09-27 15:39:35 +08:00
reject(response);
uni.showToast({
icon: 'none',
title: '服务器出现异常'
});
2020-09-16 09:25:13 +08:00
}
)
})
return promise
};
const post = (url, data) => {
let httpDefaultOpts = {
url: baseUrl+url,
data: data,
method: 'post',
header: {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json'
},
dataType: 'json',
}
let promise = new Promise(function(resolve, reject) {
2020-09-17 20:01:50 +08:00
uni.request(httpDefaultOpts).then((res) => {
if (res[1].data.success === true && res[1].data.data !== null) {
2020-09-23 18:53:25 +08:00
if (res[1].data.data === 0) {
uni.showToast({
icon: "none",
title: "服务器出小差了,请稍后再试"
2020-09-17 20:01:50 +08:00
})
2020-09-23 18:53:25 +08:00
} else {
resolve(res[1].data)
2020-09-17 20:01:50 +08:00
}
2020-09-23 18:53:25 +08:00
} else {
resolve({
2020-09-27 15:39:35 +08:00
message: "服务器出现异常",
data: res[1].data
});
2020-09-23 18:53:25 +08:00
}
}
).catch(
(response) => {
reject(response)
}
)
})
2020-09-16 09:25:13 +08:00
return promise
};
//带Token请求
const httpTokenRequest = (opts, data) => {
let token = "";
uni.getStorage({
key: 'token',
success: function(ress) {
token = ress.data
}
});
//此token是登录成功后后台返回保存在storage中的
let httpDefaultOpts = {
url: baseUrl+opts.url,
data: data,
method: opts.method,
header: opts.method == 'get' ? {
'Token': token,
'X-Requested-With': 'XMLHttpRequest',
"Accept": "application/json",
"Content-Type": "application/json; charset=UTF-8"
} : {
'Token': token,
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
dataType: 'json',
}
let promise = new Promise(function(resolve, reject) {
uni.request(httpDefaultOpts).then(
(res) => {
resolve(res[1])
}
).catch(
(response) => {
reject(response)
}
)
})
return promise
};
export default {
baseUrl,
get,
post,
httpTokenRequest
}