1. request数据请求封装
- 调用
export const httpUrl="https://xxx.xxx.xx";
//设置请求配置
http.setConfig({
baseUrl: httpUrl
})
function getToken(){
try {
let tmp = uni.getStorageSync('token');
if (tmp) {
return tmp;
}else{
return "";
}
} catch (e) {
return "";
}
}
//获取数据
export const GetTest=data=>http.get('/api/xx/xx',data,{"Token":getToken()}); //第三个参数为请求头
- 封装的方法
import deepMerge from "./deepMerge";
let config = {
baseUrl: '', // 请求的根域名
// 默认的请求头
header: {
'content-type': 'application/json;charset=UTF-8'
},
method: 'GET',
// 设置为json,返回后uni.request会对数据进行一次JSON.parse
dataType: 'json',
// 此参数无需处理,因为5+和支付宝小程序不支持,默认为text即可
responseType: 'text'
}
// 主要请求部分
async function request(options = {}) {
options.dataType = options.dataType || config.dataType;
options.responseType = options.responseType || config.responseType;
options.url = options.url || '';
options.data = options.data || null;
options.header = Object.assign(config.header, options.header);
options.method = options.method ||config.method;
return new Promise((resolve, reject) => {
options.complete = (response) => {
//console.log(response.statusCode);
if (response.statusCode == 200) {
//返回请求数据
resolve(response.data);
}else if(response.statusCode==401){
//授权失败,跳转登录页,重新登录
uni.showModal({
title: '登录过期',
content: '登录权限已过期,需要重新登录',
showCancel:false,
success: function (res) {
if (res.confirm) {
uni.reLaunch({
url: '/pages/login/login'
});
}
}
});
} else {
console.error(options);
//直接接入catch回调
reject(response)
}
};
//根据请求判断是否需要加上域名
options.url=options.url.indexOf('http') == 0 ?options.url:config.baseUrl+options.url;
console.log(options.method+':'+options.url+'【数据】'+JSON.stringify(options.data))
uni.request(options);
})
}
const http = {
// 设置全局默认配置
setConfig(customConfig) {
// 深度合并对象,否则会造成对象深层属性丢失
config = deepMerge(config, customConfig);
},
ajax(options={}){
return request(options)
},
// get请求
get(url, data = {}, header = {}){
return request({
method: 'GET',
url,
header,
data
})
},
// post请求
post(url, data = {}, header = {}){
return request({
url,
method: 'POST',
header,
data
})
}
}
export default http;
import deepClone from "./deepClone";
// JS对象深度合并
function deepMerge(target = {}, source = {}) {
target = deepClone(target);
if (typeof target !== 'object' || typeof source !== 'object') return false;
for (var prop in source) {
if (!source.hasOwnProperty(prop)) continue;
if (prop in target) {
if (typeof target[prop] !== 'object') {
target[prop] = source[prop];
} else {
if (typeof source[prop] !== 'object') {
target[prop] = source[prop];
} else {
if (target[prop].concat && source[prop].concat) {
target[prop] = target[prop].concat(source[prop]);
} else {
target[prop] = deepMerge(target[prop], source[prop]);
}
}
}
} else {
target[prop] = source[prop];
}
}
return target;
}
export default deepMerge;
// 对象深度克隆
function deepClone(object = {}) {
var o, i, j, k;
if (typeof(object) !== "object" || object === null) return object;
if (object instanceof Array) {
o = [];
i = 0;
j = object.length;
for (; i < j; i++) {
if (typeof(object[i]) === "object" && object[i] != null) {
o[i] = deepClone(object[i]);
} else {
o[i] = object[i];
}
}
} else {
o = {};
for (i in object) {
if (typeof(object[i]) === "object" && object[i] !== null) {
o[i] = deepClone(object[i]);
} else {
o[i] = object[i];
}
}
}
return o;
}
export default deepClone;