无所不能的uniapp拦截器

无所不能的uniapp拦截器-uni.addInterceptor

拦截器能做什么?

uni.addInterceptoruni-app 框架中的一个 API,用于为特定的异步 API 方法添加拦截器。拦截器可以在方法执行前或执行后插入自定义逻辑,比如权限校验、日志记录、数据预处理等。 附上官方文档链接

参数名 类型 必填 默认值 说明 平台差异说明
invoke Function 拦截前触发
returnValue Function 方法调用后触发,处理返回值
success Function 成功回调拦截
fail Function 失败回调拦截
complete Function 完成回调拦截

项目实战

  • uni.request拦截器
  • 登录校验拦截器
  • uni.setStorage数据预处理
  • 隐私权限判断、引导

uni.request拦截器

ts 复制代码
const BASE_URL = 'https://www.example.com/'

const httpInterceptor: UniNamespace.InterceptorOptions = {
  invoke(args) {
    // request 触发前拼接 url
    args.url = BASE_URL + args.url
  },
  success(result) {
    console.log('interceptor-success',result)
    // 请求成功,但接口报错等情况上报
    if(result.data.code === xxx){
        errorReport(result.data.msg)
    }
  },
  fail(err) {
    console.log('interceptor-fail',err)
    // 请求失败等情况上报
    errorReport()
  },
  complete(res) {
    console.log('interceptor-complete',res)
  }
}
// 进行错误上报
function errorReport(errMsg?: string){
    uni.showToast({
      icon: "none",
      title: errMsg || "网络错误,换个网络试试",
    });
    customReport({
        msg: 'xxxx'
    })
}
ts 复制代码
export const requestInterceptor = {
  install() {
    // 拦截 request 请求
    uni.addInterceptor("request", httpInterceptor);
    // 拦截 uploadFile 文件上传
    uni.addInterceptor("uploadFile", httpInterceptor);
  },
};

登录校验拦截器

ts 复制代码
const loginRoute = "/pages/login/index";

let needLoginPages: string[] = [
 "/pages/order/index",
];

/**
* 判断是否登录
*/
export function isLogged(): boolean {
 return false;
}

// 黑名单登录拦截器 - (适用于大部分页面不需要登录,少部分页面需要登录)
const navigateToInterceptor: UniNamespace.InterceptorOptions = {
 // 注意,这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
 invoke({ url }: { url: string }) {
   // console.log(url) 
   const path = url.split("?")[0];
   const isNeedLogin = needLoginPages.includes(path);
   if (!isNeedLogin) {
     return true;
   }
   const hasLogin = isLogged();
   if (hasLogin) {
     return true;
   }
   goLogin(url);
   return false;
 },
};

function goLogin(redirect: string = "") {
 uni.navigateTo({
   url: `${loginRoute}?redirect=${encodeURIComponent(redirect)}`,
 });
}

export const routeInterceptor = {
 install() {
   uni.addInterceptor("navigateTo", navigateToInterceptor);
   uni.addInterceptor("reLaunch", navigateToInterceptor);
   uni.addInterceptor("redirectTo", navigateToInterceptor);
   uni.addInterceptor("switchTab", navigateToInterceptor);
 },
};

uni.setStorage数据预处理

ts 复制代码
// 这是个数据处理函数
function processingData(){
}

const storage: UniNamespace.InterceptorOptions = {
  invoke(args) {
    console.log('存储数据前:', args);
    args.data = processingData(args.data); 
    console.log('处理后的数据:', args.data);
  },
  success() {
    console.log('数据存储成功');
  },
  fail(err) {
    console.error('数据存储失败:', err);
  }
}
ts 复制代码
export const storageInterceptor = {
  install() {
    uni.addInterceptor("setStorage", storage);
  },
};
借助拦截器修复快手小程序平台的一个bug
ts 复制代码
const setStorageInterceptor: UniNamespace.InterceptorOptions = {
  invoke(args: { data: any }) {
    // console.log(args, "========args=======");
    // #ifdef MP-KUAISHOU
    args.data = JSON.parse(JSON.stringify(args.data));
    // #endif
  },
  success(args) {
    // console.log(args, "========success=======");
  },
  fail(err) {
    // console.log(err, "========fail=======");
  },
};
/**
 * 解决快手小程序setStorage不支持proxy对象的问题
 * */
export const KuaiShouSetStorageProxyFixInterceptor = {
  install() {
    // 拦截 setStorage
    uni.addInterceptor("setStorage", setStorageInterceptor);
  },
};

隐私权限判断、引导

开发中......

相关推荐
A_aspectJ1 小时前
【Bootstrap V4系列】学习入门教程之 组件-输入组(Input group)
前端·css·学习·bootstrap·html
兆。1 小时前
电子商城后台管理平台-Flask Vue项目开发
前端·vue.js·后端·python·flask
互联网搬砖老肖1 小时前
Web 架构之负载均衡全解析
前端·架构·负载均衡
sunbyte2 小时前
Tailwind CSS v4 主题化实践入门(自定义 Theme + 主题模式切换)✨
前端·javascript·css·tailwindcss
湛海不过深蓝3 小时前
【css】css统一设置变量
前端·css
DONSEE广东东信智能读卡器3 小时前
蓝牙身份证阅读器使用Uniapp调用二次开发demo
javascript·uni-app·蓝牙·身份证阅读器
程序员的世界你不懂4 小时前
tomcat6性能优化
前端·性能优化·firefox
爱吃巧克力的程序媛4 小时前
QML ProgressBar控件详解
前端
进取星辰4 小时前
21、魔法传送阵——React 19 文件上传优化
前端·react.js·前端框架
wqqqianqian4 小时前
国产linux系统(银河麒麟,统信uos)使用 PageOffice 在线打开Word文件,并用前端对话框实现填空填表
linux·前端·word·pageoffice