微信小程序
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 = https://www.it610.com/article/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 = https://www.it610.com/article/{}, header = {}){
return request({
method:'GET',
url,
header,
data
})
},
// post请求
post(url, data = https://www.it610.com/article/{}, 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;
推荐阅读
- 一个小故事,我的思考。
- 家乡的那条小河
- 一个人的碎碎念
- 野营记-第五章|野营记-第五章 讨伐梦魇兽
- 昨夜小楼听风
- 2021-02-17|2021-02-17 小儿按摩膻中穴-舒缓咳嗽
- 基于微信小程序带后端ssm接口小区物业管理平台设计
- 2019.4.18感恩日记
- 那件我们忽略的小事叫感恩
- 你有婚内虐待行为吗()