sadjv3/anmo-user/common/httpRequest.js

381 lines
10 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import configdata from './config'
import cache from './cache'
import * as websocketUtils from 'utils/websocketUtils.js';
//因为项目内没有封装拦截器所以每次请求前都需要获取并设置token因此获取时涉及时间问题async/await
function getTokenData() {
let token = uni.getStorageSync("token");
if (token) return token;
}
module.exports = {
config: websocketUtils.config,
post: function(url, data, header) {
header = header || "application/x-www-form-urlencoded";
url = websocketUtils.getBaseUrl() + url;
let token = uni.getStorageSync("token");
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: "POST",
header: {
"content-type": header,
"token": token
},
success: function(result) {
if (result.data.code == 401) {
// uni.clearStorage();
uni.removeStorageSync("token")
uni.removeStorageSync("userId")
uni.removeStorageSync("phone")
uni.removeStorageSync("openid")
uni.removeStorageSync("userName")
uni.removeStorageSync("relation")
uni.removeStorageSync("relation_id")
uni.removeStorageSync("isInvitation")
uni.removeStorageSync("zhiFuBao")
uni.removeStorageSync("zhiFuBaoName")
uni.removeStorageSync("onlineStatus")
// uni.showToast({
// title: '用户信息失效,请重新登录!',
// icon: 'none'
// })
}
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
},
postT: function(url, data, header) {
header = header || "application/x-www-form-urlencoded";
url = websocketUtils.getBaseUrl() + url;
let token = uni.getStorageSync("token");
if (token) {
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: "POST",
header: {
"content-type": header,
"token": token
},
success: function(result) {
if (result.data.code == 401) {
uni.removeStorageSync("token")
uni.removeStorageSync("userId")
uni.removeStorageSync("phone")
uni.removeStorageSync("openid")
uni.removeStorageSync("userName")
uni.removeStorageSync("relation")
uni.removeStorageSync("relation_id")
uni.removeStorageSync("isInvitation")
uni.removeStorageSync("zhiFuBao")
uni.removeStorageSync("zhiFuBaoName")
uni.removeStorageSync("onlineStatus")
// uni.showToast({
// title: '用户信息失效,请重新登录!',
// icon: 'none'
// })
}
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
} else {
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: "POST",
header: {
"content-type": header,
},
success: function(result) {
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
}
},
postJson: function(url, data, header) {
header = header || "application/json";
url = websocketUtils.getBaseUrl() + url;
let token = uni.getStorageSync("token");
if (token) {
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: "POST",
header: {
"content-type": header,
"token": token
},
success: function(result) {
if (result.data.code == 401) {
uni.removeStorageSync("token")
uni.removeStorageSync("userId")
uni.removeStorageSync("phone")
uni.removeStorageSync("openid")
uni.removeStorageSync("userName")
uni.removeStorageSync("relation")
uni.removeStorageSync("relation_id")
uni.removeStorageSync("isInvitation")
uni.removeStorageSync("zhiFuBao")
uni.removeStorageSync("zhiFuBaoName")
uni.removeStorageSync("onlineStatus")
// uni.showToast({
// title: '用户信息失效,请重新登录!',
// icon: 'none'
// })
}
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
} else {
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: "POST",
header: {
"content-type": header,
},
success: function(result) {
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
}
},
getT: function(url, data, header) {
header = header || "application/x-www-form-urlencoded";
url = websocketUtils.getBaseUrl() + url;
let token = uni.getStorageSync("token");
if (token) {
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: "GET",
header: {
"content-type": header,
"token": token
},
success: function(result) {
if (result.data.code == 401) {
uni.removeStorageSync("token")
uni.removeStorageSync("userId")
uni.removeStorageSync("phone")
uni.removeStorageSync("openid")
uni.removeStorageSync("userName")
uni.removeStorageSync("relation")
uni.removeStorageSync("relation_id")
uni.removeStorageSync("isInvitation")
uni.removeStorageSync("zhiFuBao")
uni.removeStorageSync("zhiFuBaoName")
uni.removeStorageSync("onlineStatus")
// uni.showToast({
// title: '用户信息失效,请重新登录!',
// icon: 'none'
// })
}
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
} else {
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: "GET",
header: {
"content-type": header
},
success: function(result) {
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
}
},
get: function(url, data, header) {
header = header || "application/x-www-form-urlencoded";
url = websocketUtils.getBaseUrl() + url;
let token = uni.getStorageSync("token");
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: "GET",
header: {
"content-type": header,
"token": token
},
success: function(result) {
if (result.data.code == 401) {
uni.removeStorageSync("token")
uni.removeStorageSync("userId")
uni.removeStorageSync("phone")
uni.removeStorageSync("openid")
uni.removeStorageSync("userName")
uni.removeStorageSync("relation")
uni.removeStorageSync("relation_id")
uni.removeStorageSync("isInvitation")
uni.removeStorageSync("zhiFuBao")
uni.removeStorageSync("zhiFuBaoName")
uni.removeStorageSync("onlineStatus")
// uni.showToast({
// title: '用户信息失效,请重新登录!',
// icon: 'none'
// })
}
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
},
getMsg: function(url, data, header) {
header = header || "application/x-www-form-urlencoded";
url = websocketUtils.getBaseUrl() + url;
let token = uni.getStorageSync("token");
return new Promise((succ, error) => {
uni.request({
url: url,
data: data,
method: "GET",
header: {
"content-type": header,
"token": token
},
success: function(result) {
if (result.data.code == 401) {
uni.removeStorageSync("token")
uni.removeStorageSync("userId")
uni.removeStorageSync("phone")
uni.removeStorageSync("openid")
uni.removeStorageSync("userName")
uni.removeStorageSync("relation")
uni.removeStorageSync("relation_id")
uni.removeStorageSync("isInvitation")
uni.removeStorageSync("zhiFuBao")
uni.removeStorageSync("zhiFuBaoName")
uni.removeStorageSync("onlineStatus")
// uni.showToast({
// title: '用户信息失效,请重新登录!',
// icon: 'none'
// })
}
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
},
getXZX: function(url, data, header) {
header = header || "application/json";
url = websocketUtils.getBaseUrl() + url;
return new Promise(async (succ, error) => {
uni.request({
url: url,
data: data,
method: "GET",
header: {
"content-type": header,
"token": await getTokenData(),
},
success: function(result) {
if (result.data.code == 401) {
uni.removeStorageSync("token")
uni.removeStorageSync("userId")
uni.removeStorageSync("phone")
uni.removeStorageSync("openid")
uni.removeStorageSync("userName")
uni.removeStorageSync("relation")
uni.removeStorageSync("relation_id")
uni.removeStorageSync("isInvitation")
uni.removeStorageSync("zhiFuBao")
uni.removeStorageSync("zhiFuBaoName")
uni.removeStorageSync("onlineStatus")
// uni.showToast({
// title: '用户信息失效,请重新登录!',
// icon: 'none'
// })
}
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
},
postXZX: function(url, data, header) {
header = header || "application/x-www-form-urlencoded";
url = websocketUtils.getBaseUrl() + url;
return new Promise(async (succ, error) => {
uni.request({
url: url,
data: data,
method: "POST",
header: {
"content-type": header,
"token": await getTokenData(),
},
success: function(result) {
if (result.data.code == 401) {
uni.removeStorageSync("token")
uni.removeStorageSync("userId")
uni.removeStorageSync("phone")
uni.removeStorageSync("openid")
uni.removeStorageSync("userName")
uni.removeStorageSync("relation")
uni.removeStorageSync("relation_id")
uni.removeStorageSync("isInvitation")
uni.removeStorageSync("zhiFuBao")
uni.removeStorageSync("zhiFuBaoName")
uni.removeStorageSync("onlineStatus")
// uni.showToast({
// title: '用户信息失效,请重新登录!',
// icon: 'none'
// })
}
succ.call(self, result.data)
},
fail: function(e) {
error.call(self, e)
}
})
})
},
}