const BASE_URL = “http://192.168.1.57:8080/api/”;
// 获取储存的子女姓名
const childrenName = uni.getStorageSync(“childrenName”);
// 获取储存的子女手机号码
const childrenPhone = uni.getStorageSync(“childrenPhone”);
const myRequest = (options) => {
// 调接口加载
uni.showLoading({
title: “加载中”,
mask: true,
});
return new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + options.url,
//默认参数
data: options.data || {},
// 非必填
timeout: 60000, //超时时间, 默认60000,单位 ms
dataType: json, //如果设为 json,会尝试对返回的数据做一次 JSON.parse
// 配置请求头参数-例如token
header: {
Authorization: uni.getStorageSync(“token”) ?
uni.getStorageSync(“token”) :
“”,
} || {},
method: options.method || “GET”,
// sslVerify: true,
// 接口请求成功
success: (res) => {
// 关闭加载
uni.hideLoading();
// console.log(‘接口所有参数’, res);
if (res.statusCode !== 200) {
// 不同报错信息的提示和配置
if (res.statusCode == 500) {
return uni.showToast({
title: “服务器重启中…”,
icon: “none”,
mask: true,
});
} else {
return uni.showToast({
title: “获取数据失败”,
icon: “none”,
mask: true,
});
}
}
// 调用成功且有数据 返回数据 组件内通过 .then() 或者async await 接受异步返回数据
//resolve(res.data)
//在接口200 调用成功后 才能进行判断接口内的状态码 code 以此判定作何操作和提示
const {
statusCode,
data
} = res;
let return_code = res.data.code;
let return_message = res.data.message;
switch (return_code) {
case 200:
// 成功的数据data状态码 则直接返回数据
resolve(res.data);
break;
case 401: //表示无权限
uni.clearStorage();
uni.showModal({
title: “提示”,
content: “身份失效,请重新登录!”,
complete: () => {
uni.reLaunch({
url: “/pages/login/index”,
});
},
});
break;
default:
// 其他的如无特定要求 则做提示
// reject(res.data)
return uni.showToast({
title: return_message || “请求失败”,
duration: 2000,
icon: “none”,
});
}
},
// 接口接口失败
fail: (error) => {
// 关闭加载
uni.hideLoading();
console.log(2, error);
uni.showToast({
title: “网络异常”,
icon: “none”,
mask: true,
});
// 失败数据
reject(error);
},
});
});
};
const getRequest = (url, data) => {
var promise = new Promise((resolve, reject) => {
var postData = data;
uni.request({
url: commoneUrl + url,
data: postData,
method: “GET”,
dataType: “json”,
header: {
“content-type”: “application/json”,
},
success: function(res) {
if (res.statusCode === 200) {
resolve(res.data);
} else {
resolve(res.data);
}
},
error: function(e) {
reject(“网络出错”);
},
});
});
return promise;
};
//post请求封装
const postRequest = (url, data) => {
var promise = new Promise((resolve, reject) => {
var postData = data;
uni.request({
url: commoneUrl + url,
data: postData,
method: “POST”,
// withCredentials:true,
header: {
“content-type”: “application/x-www-form-urlencoded”,
// “Authorization”:’22’
},
success: function(res) {
// console.log(res);
if (res.statusCode === 200 && res.data.resultCode == 0) {
resolve(res.data);
} else {
resolve(res.data);
}
},
error: function(e) {
reject(“网络出错”);
},
});
});
return promise;
};
//带Token请求
const httpTokenRequest = (opts, data) => {
uni.onNetworkStatusChange((res) => {
if (!res.isConnected) {
uni.showToast({
title: ‘网络连接不可用!’,
icon: ‘none’
});
}
return false
});
// 调接口加载
let token = uni.getStorageSync(‘token’);
// hadToken()
if (token == ” || token == undefined || token == null) {
uni.showToast({
title: ‘账号已过期,请重新登录’,
icon: ‘none’,
complete: function() {
uni.reLaunch({
url: ‘/pages/login/index’
});
}
});
} else {
let httpDefaultOpts = {
url: baseUrl + opts.url,
data: data,
method: opts.method,
header: opts.method == ‘get’ ? {
‘X-Access-Token’: token,
‘X-Requested-With’: ‘XMLHttpRequest’,
“Accept”: “application/json”,
“Content-Type”: “application/json; charset=UTF-8”
} : {
‘X-Access-Token’: token,
‘X-Requested-With’: ‘XMLHttpRequest’,
‘Content-Type’: ‘application/json; charset=UTF-8’
},
dataType: ‘json’,
}
uni.showLoading({
title: “加载中”,
mask: true,
});
let promise = new Promise(function(resolve, reject) {
uni.request(httpDefaultOpts).then(
(res) => {
// 关闭加载
uni.hideLoading();
if (res[1].data.code == 200) {
resolve(res[1])
} else {
if (res[1].data.code == 5000) {
uni.reLaunch({
url: ‘/pages/login/index’
});
// 清除本地所有缓存
uni.clearStorageSync();
} else {
// 成功回调
resolve(res[1])
}
}
}
).catch(
(response) => {
// 关闭加载
uni.hideLoading();
reject(response)
}
)
})
return promise
}
// let token = uni.getStorageSync(‘token’)
//此token是登录成功后后台返回保存在storage中的
};
// 不带token请求
const httpRequest = (opts, data) => {
uni.onNetworkStatusChange(function(res) {
if (!res.isConnected) {
uni.showToast({
title: ‘网络连接不可用!’,
icon: ‘none’
});
}
return false
});
let httpDefaultOpts = {
url: baseUrl + opts.url,
data: data,
method: opts.method,
header: opts.method == ‘get’ || ‘GET’ ? {
‘X-Requested-With’: ‘XMLHttpRequest’,
“Accept”: “application/json”,
“Content-Type”: “application/json; charset=UTF-8”
} : {
‘X-Requested-With’: ‘XMLHttpRequest’,
‘Content-Type’: ‘application/json; charset=UTF-8’
},
dataType: ‘json’,
}
uni.showLoading({
title: “加载中”,
mask: true,
});
let promise = new Promise(function(resolve, reject) {
uni.request(httpDefaultOpts).then(
(res) => {
// 关闭加载
uni.hideLoading();
resolve(res[1])
}
).catch(
(response) => {
// 关闭加载
uni.hideLoading();
reject(response)
}
)
})
return promise
};
// 判断本地是否存在token
const hadToken = () => {
let token = uni.getStorageSync(‘token’);
if (token == ” || token == undefined || token == null) {
uni.showToast({
title: ‘账号已过期,请重新登录’,
icon: ‘none’,
complete: function() {
uni.reLaunch({
url: ‘/pages/login/index’
});
}
});
return false;
}
return true
}
module.exports = {
myRequest: myRequest, //发起请求 请求头携带token
postRequest: postRequest, //Get请求封装
postRequest: postRequest, //post请求封装
httpTokenRequest: httpTokenRequest, //带Token请求
httpRequest: httpRequest ,//不带Token请求
hadToken:hadToken //判断本地是否存在token
};
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/144797.html