Uniapp-封装API拦截器

1.创建文件夹utils/http.js

//基地址
const baseURL = 'https://xxxxxxxxxxx'
创建请求前拦截器
const httpInterceptor = {
  // 拦截前触发
  invoke(options) {
    // 1.非http开头的需要拼接基地址
    if (!options.url.startsWith('http')) {
      options.url = baseURL + options.url
    }
    // 2.设置请求延迟时间
    options.timeout = 10000
    // 3.设置请求小程序端请求标识
    options.header = {
      'source-client': 'miniapp'
    }
    // 4.添加token标识符
    // const token=this.$store.userinfo.token
    // if(token){
    //   options.header.Authorization=token
    // }
  }
}

//request请求
uni.addInterceptor('request', httpInterceptor)
//文件上传
uni.addInterceptor('uploadFile', httpInterceptor)
//返回Promise对象
export const axios = (options) => {
  return new Promise((resove, reject) => {
    uni.request({
      ...options,
      // 1. 成功请求处理
      success(res) {
      // 1.2 成功状态码接收数据
        if(res.statusCode>=200&&res.statusCode<300){
        resove(res.data)
        }else if(res.statusCode===401){
          // 1.2身份认证失败
          // 1.2.1显示提示框
          // 1.2.2点击确定,清空用户信息,跳转登录页
          reject(res)
        }else{
          // 1.3其他错误
         uni.$showMsg('其他错误', 1500)
          reject(res)
        }
      },
      // 2.失败处理
      fail(err){
        uni.$showMsg('网络错误!',1500)
        reject(res)
      }
    })
  })
}

1.2 封装API

创建根目录API下创建home.js

import { axios } from "../utils/http";
// 获取轮播图数据
export const getswiperdataAPI = () => {
  return axios({
    method: 'GET',
    url: '/api/xxxxx',
  })
}

1.3 页面中使用

<script>
//导入api
  import {getswiperdataAPI} from '../../API/home.js'
  export default {
    data() {
      return {
        list: []
      };
    },
//一进页面就渲染
    onLoad() {
      this.getnum()
    },
    methods: {
//请求函数
      async getnum() {
        const res = await getswiperdataAPI()
        console.log(res);
        this.list = res.message
      },
    }
  }
</script>

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp 中,可以通过封装拦截器来实现全局的请求拦截、响应拦截等功能。下面是一个简单的拦截器封装示例: 1. 在项目的 common 目录下新建一个 interceptor.js 文件,用于存放拦截器的代码。 2. 在 interceptor.js 中定义一个拦截器对象,包含请求拦截和响应拦截两个函数: ``` const interceptor = { // 请求拦截 request: function(config) { // 在请求被发送之前做些什么 // 可以在这里添加全局的请求参数、请求头等配置 return config; }, // 响应拦截 response: function(response) { // 在这里对响应数据做些什么 // 可以在这里添加全局的响应处理逻辑 return response; } }; export default interceptor; ``` 3. 在 main.js 中引入 interceptor.js,并将其挂载到 Vue.prototype 上,使其成为全局可用的插件: ``` import interceptor from './common/interceptor'; Vue.prototype.$interceptor = interceptor; ``` 4. 在需要使用拦截器的地方,如 http.js 文件中,通过以下方式使用拦截器: ``` import Vue from 'vue'; import axios from 'axios'; Vue.prototype.$http = axios.create({ baseURL: 'http://api.example.com', timeout: 5000 }); // 请求拦截 Vue.prototype.$http.interceptors.request.use(config => { // 在请求被发送之前做些什么 // 可以在这里添加全局的请求参数、请求头等配置 return config; }, error => { // 对请求错误做些什么 return Promise.reject(error); }); // 响应拦截 Vue.prototype.$http.interceptors.response.use(response => { // 在这里对响应数据做些什么 // 可以在这里添加全局的响应处理逻辑 return response; }, error => { // 对响应错误做些什么 return Promise.reject(error); }); ``` 这样就完成了一个简单的拦截器封装。在实际使用中,可以根据项目需求添加更多的拦截器功能,如:错误处理、权限验证等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值