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
    })
}
相关推荐
anyup1 天前
支持鸿蒙!开源三个月,uView Pro 开源库近期更新全面大盘点,及未来计划
前端·vue.js·uni-app
阿斌_bingyu7091 天前
uniapp实现android/IOS消息推送
android·ios·uni-app
茶憶2 天前
UniApp RenderJS中集成 Leaflet地图,突破APP跨端开发限制
javascript·vue.js·uni-app
2501_915918412 天前
移动端 HTTPS 抓包实战,多工具组合分析与高效排查指南
数据库·网络协议·ios·小程序·https·uni-app·iphone
雪芽蓝域zzs2 天前
uni-app 将 base64 图片编码转为 Blob 本地文件路径
网络协议·udp·uni-app
星光一影2 天前
陪诊陪检系统源码,陪诊小程序,陪诊APP,陪诊服务,家政上门系统,居家护理陪护源码
mysql·小程序·uni-app·php
Qlittleboy2 天前
uniapp里 rich-text 里的img图片如何控制最大宽度
uni-app
阿奇__2 天前
uniapp h5 app 小程序获取当前定位
小程序·uni-app
*小雪2 天前
uniapp写H5授权登录及分享,返回到目标页面
开发语言·javascript·uni-app
一只程序熊2 天前
uniapp uniim ios配置消息推送
uni-app