小程序promise封装
const requestFetch = (url, method, param) => {
let ajaxTimes = 0;
ajaxTimes++;
wx.showLoading({
title: "加载中",
mask: true
});
let header = {
'Authorization': wx.getStorageSync('token'),
'content-type': 'application/json'
};
return new Promise((resolve, reject) => {
wx.request({
data: param,
header: header,
method: method,
url: baseUrl + url,
success: (result) => {
const errArrCode = [2010, 1008, 2016]
if (result.data.code == 2008) {
Notify({
type: 'danger',
message: '登录失效,返回重新登录'
});
wx.removeStorageSync('token')
setTimeout(() => {
wx.reLaunch({
url: '../login/login'
})
}, 1500)
} else if (result.data.code == 2015) {
Notify({
type: 'danger',
message: result.data.msg
});
wx.removeStorageSync('token')
setTimeout(() => {
wx.reLaunch({
url: '../login/login'
})
}, 1500)
} else if (errArrCode.includes(result.data.code)) {
Notify({
type: 'danger',
message: result.data.msg
});
return
} else if (result.data.code == 200) {
resolve(result.data.data)
} else {
Notify({
type: 'danger',
message: '网络错误'
});
}
},
fail: (err) => {
Notify({
type: 'danger',
message: '网络连接失败'
});
reject(err)
},
complete: () => {
ajaxTimes--;
if (ajaxTimes === 0) {
wx.hideLoading();
}
}
})
})
}
const deepCopy = (obj, cache = []) => {
if (obj === null || typeof obj !== 'object') {
return obj;
}
const hit = cache.filter(c => c.original === obj)[0];
if (hit) {
return hit.copy;
}
const copy = Array.isArray(obj) ? [] : {};
cache.push({
original: obj,
copy,
});
Object.keys(obj).forEach(key => {
copy[key] = deepCopy(obj[key], cache);
});
return copy;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具