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
    })
}
相关推荐
小徐_23331 天前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts
iOS阿玮2 天前
永远不要站在用户的对立面,挑战大众的公知。
uni-app·app·apple
xw52 天前
uni-app中v-if使用”异常”
前端·uni-app
!win !2 天前
uni-app中v-if使用”异常”
前端·uni-app
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张2 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
00后程序员张2 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬2 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app
2501_915106322 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
xkxnq2 天前
Uniapp崩溃监控体系构建:内存泄漏三维定位法(堆栈/资源/线程)
uni-app