请求和上传文件拦截器

拦截器:request请求,uploadFile上传文件

 基础地址-》超时时间-》添加请求头标识-》添加token

接口文档: https://apifox.com/apidoc/shared-0e6ee326-d646-41bd-9214-29dbf47648fa/

 新建一个ts文件

 下面就要完成这些

复制代码
import { useMemberStore } from '@/stores'

/*
添加拦截器:
拦截request请求
拦截uploadFile文件上传
TODO:
1.非http开头需要拼接地址
2.请求超时
3.添加小程序端请求头标识
4.添加token文革求头标识
*/
const baseURL = 'https://pcapi-xiaotuxian-front-devtest.itheima.net'

//添加拦截器
const httpInterceptor = {
  //拦截前出发
  invoke(options: UniApp.RequestOptions) {
    //1.非http开头需要拼接地址
    if (!options.url.startsWith('http')) {
      options.url = baseURL + options.url
    }
    //2.请求超时,默认60s
    options.timeout = 10000
    console.log(options)
    //3.添加小程序端请求头标识
    options.header = {
      //如果有header先保留下来再添加标识
      ...options.header,
      'source-client': 'miniapp',
    }
    //4.添加token文革求头标识
    const memberStroe = useMemberStore()
    const token = memberStroe.profile?.token
    if (token) {
      options.header.Authorization = token
    }
  },
}
uni.addInterceptor('request', httpInterceptor)
uni.addInterceptor('uploadFile', httpInterceptor)
复制代码

按微信开发者工具->点击测试请求,在

 记得填写完成后按一下微信开发者工具->我的保存用户信息

 

 

posted @   艾鑫4646  阅读(77)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示