当前位置: 首页 > news >正文

uniapp的请求封装,如何避免重复提交请求

1、如何封装uniapp,并且如何使用uniapp的封装查看👉uniapp请求封装_uni-app-x 请求封装-CSDN博客​​​​​​​
2、声明一个请求记录的缓存,代码如下

// 存储请求记录
let requestRecords = {};
// 重复请求拦截时间(毫秒)
const INTERCEPT_DURATION = 2000;
const request = (url, data = {}, method = "GET", ContentType = "application/json") => {const requestObj = {data,url,time: Date.now(),};if (method !== "GET") {if (Object.keys(requestRecords).length == 0) {requestRecords = requestObj;} else {const s_url = requestRecords.url; // 请求地址const s_data = requestRecords.data; // 请求数据const s_time = requestRecords.time; // 请求时间if (s_data === requestObj.data &&requestObj.time - s_time < INTERCEPT_DURATION &&s_url === requestObj.url) {uni.showToast({title: "数据正在处理,请勿重复提交",icon: "none",duration: 2000,});return;}requestRecords = requestObj;}}return new Promise(function (resolve, reject) {let header = {};if (uni.getStorageSync("token")) {header = {"Content-Type": ContentType,Authorization: uni.getStorageSync("token"),};} else {header = {"Content-Type": ContentType,};}if (Object.keys(data).length && !data.showLoading) {uni.showLoading({title: "加载中",mask: true,});}console.log("请求参数", data, url);uni.request({url: BASE_URL + url,data,method,header,success: function (res) {console.log("res", res);if (res.data.code == 200) {resolve(res.data);} else if (res.data.code == 401) {uni.navigateTo({url: "/pages/login/login",});} else {if (Object.keys(res.data).length && !data.showLoading) {uni.showToast({title: res.data.msg,icon: "none",duration: 2000,});}reject(res);}},fail: function (err) {console.log("err", err);uni.getNetworkType({success: function (res) {console.log("当前网络状态:", res.networkType);if (res.networkType === "none") {console.log("当前无网络");uni.showToast({title: "当前网络不可用,请检查网络连接",icon: "none",});return;} else {uni.showToast({title: "加载失败,请稍后重试!",icon: "none",duration: 2000,});}},});reject(err);},complete: function () {console.log("结束");if (!data.showLoading) {uni.hideLoading();}},});});
};


 

http://www.xdnf.cn/news/964621.html

相关文章:

  • mysql-innoDB存储引擎事务的原理
  • ​​​​​​​未来已来:深度解读 BLE 6.0 的革命性特性与实战应用
  • SkyReels-V1:开启多模态视频生成的新纪元
  • SpringDoc集成到Springboot
  • 第1章信息化知识归纳总结补充内容
  • day52 ResNet18 CBAM
  • Canfestival的移植思想
  • EndNote 21完整安装指南:从零开始的详细步骤(附EndNote下载安装包)
  • HTML 文本省略号
  • HTML 标签 综合案例
  • 在鸿蒙HarmonyOS 5中HarmonyOS应用开发实现QQ音乐风格的播放功能
  • CppCon 2015 学习:Improving the future<T> with monads
  • MinHook 对.NET底层的 SendMessage 拦截真实案例反思
  • PHP和Node.js哪个更爽?
  • 【论文阅读】多任务学习起源类论文《Multi-Task Feature Learning》
  • MyBatis注解开发的劣势与不足
  • LeetCode--27.移除元素
  • Leetcode 3578. Count Partitions With Max-Min Difference at Most K
  • HTML 列表、表格、表单
  • Docker-containerd-CRI-CRI-O-OCI-runc
  • 【kafka】Golang实现分布式Masscan任务调度系统
  • Python 自动化临时邮箱工具,轻松接收验证码,支持调用和交互模式(支持谷歌gmail/googlemail)
  • 【C++】26. 哈希扩展1—— 位图
  • 【PhysUnits】17.5 实现常量除法(div.rs)
  • Linux上并行打包压缩工具
  • Cryosparc: Local Motion Correction注意输出颗粒尺寸
  • 基于大模型的输尿管下段结石诊疗全流程预测与方案研究
  • 多场景 OkHttpClient 管理器 - Android 网络通信解决方案
  • 【AI study】ESMFold安装
  • Ribbon负载均衡实战指南:7种策略选择与生产避坑