微信小程序
2020-12-24 本文已影响0人
wppeng
1. request数据请求封装
import http from '../libs/request/index.js'
export const httpUrl="https://xx.xxx.xx";
//设置请求配置
http.setConfig({
baseUrl: httpUrl
})
export const wxLogin=data=>http.get('/api/Public/FlowerLoginWeixin',data);
- 调用
import deepMerge from "../function/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;
console.log(options.header);
return new Promise((resolve, reject) => {
options.complete = (response) => {
if (response.statusCode == 200) {
//返回请求数据
resolve(response.data);
}else if(response.statusCode == 401){
console.log("清楚缓存,重新授权");
wx.clearStorage({
success(){
wx.redirectTo({
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.url);
wx.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;
- deepMerge文件
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;
- deepClone文件
// 对象深度克隆
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;