uniapp全局拦截封装(一)

复制代码
const baseUrl="http://localhost:3000"

function showToast(msg, icon = 'error') {
  uni.showToast({
    title: msg,
    icon,
  });
}


// 添加拦截器
const httpInterceptor = {
  // 拦截前触发
  invoke(options) {
    // 1. 非 http 开头需拼接地址
    if (!options.url.startsWith('http')) {
      options.url = baseUrl + options.url;

      console.log("url--",options)
    }
    // 2. 请求超时, 默认 60s
    options.timeout = 60 * 1000;

    options.header={...options}
    // 3. 初始化header
     if (isBlank(options.header)) {
       options.header = {};
     }

    // 4. 添加 token 请求头标识
    const token =uni.getStorageSync("token")
 // 获取本地存储的token
    if (token) {
      options.header.Authorization = `Bearer ${token}`;
    }else{
       uni.redirectTo({
         url:"/pages/login/login"
       })
    }
    
  },
};

uni.addInterceptor('request', httpInterceptor);

export const http=(options)=> {
  return new Promise((resolve,reject)=>{
    if(options.loading) uni.showLoading({title: '加载中'});
    uni.request({
      ...options,
      success(res){
          // resolve(res)                
                  const rawData = res.data;
                  if (res.statusCode >= 200 && res.statusCode < 300) {
                    // 判断跟后端约定好的成功标识
                    if (rawData.code !== 0) {
                      showToast(rawData.message, 'none');
                    }
                    resolve(rawData);
                  } else if (res.statusCode === 401) {
                    // token 失效处理
                    reject(res);
                  } else {
                    switch (res.statusCode) {
                      case 404:
                        showToast('请求资源不存在');
                        break;
                      case 500:
                        showToast('服务器错误');
                        break;
                      case 502:
                        showToast('找不到对应服务器');
                        break;
                      default:
                        showToast(rawData?.message || '请求异常');
                        break;
                    }
                    reject(res);
                  }
      },
      complete(){
        if(options.loading) uni.hideLoading();
      },
      fail(err){
          reject(err)
      }
    })
  })
}


二:在api.jsw文件中
import {http} from "../utils/http"
import "../utils/http"

export const getList=()=>{
  return  http({
        method:"GET",
        url:"/api/list",
        loading:true
    })
}
相关推荐
Rysxt_20 小时前
uni-app 使用教程:从入门到发布
uni-app
homelook2 天前
uniapp蓝牙demo
uni-app
2501_915909063 天前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
郑州光合科技余经理3 天前
技术视角:海外版一站式同城生活服务平台源码解析
java·开发语言·uni-app·php·排序算法·objective-c·生活
wangdaoyin20103 天前
UniApp中使用LivePlayer进行视频或在流媒体播放
uni-app·liveplayer·h5播放视频
2501_915106323 天前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
网络协议·http·ios·小程序·https·uni-app·iphone
游戏开发爱好者83 天前
苹果App Store应用程序上架方式全面指南
android·小程序·https·uni-app·iphone·webview
2501_916008893 天前
深入理解 iPhone 文件管理,从沙盒结构到开发调试的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
一室易安3 天前
解决使用 UniApp 搭配 Vue3 小程序开始 使用uview-plus 的返回顶部up-back-top中onPageScroll 不触发的问题
小程序·uni-app
yilan_n3 天前
鸿蒙应用上传
vue.js·华为·uni-app