【小程序】封装通用的请求函数

封装通用的请求函数(uniapp + vue3 + ts + pinia)

  1. 实现请求时统一携带 token 等请求头参数
  2. 对请求成功的数据进行解耦
  3. 对请求失败的数据进行处理
import { useMemberStore } from '@/stores/modules/member'

// 请求基地址
const baseURL = '请求基地址'

// 拦截器
const httpInterceptor = {
  invoke(options: UniApp.RequestOptions) {
    // 拼接请求地址 => 这里判断是为了防止请求时写了全地址
    // 如果 options.url 不是以 http 开头 则进行拼接
    if (!options.url.startsWith('http')) {
      options.url = baseURL + options.url
    }

    // 请求超时
    options.timeout = 10000

    // 携带请求头
    options.header = {
      ...options.header,
      'source-client': 'miniapp',
    }

    // 携带token
    const memberStore = useMemberStore()
    const token = memberStore.profile?.token
    if (token) {
      options.header.Authorization = token
    }
  },
}

// request 和 uploadFile 都需要进行拦截处理
uni.addInterceptor('request', httpInterceptor)
uni.addInterceptor('uploadFile', httpInterceptor)

// 请求函数
// 返回数据类型
interface Data<T> {
  code: string
  msg: string
  result: T
}

// 封装请求函数
export const http = <T>(options: UniApp.RequestOptions) => {
  return new Promise<Data<T>>((resolve, reject) => {
    uni.request({
      ...options,

      // 响应成功
      success(res) {
        // 成功的状态码 => 具体状态码根据实际情况进行修改
        if (res.statusCode >= 200 && res.statusCode < 300) {
          resolve(res.data as Data<T>)
        } else if (res.statusCode === 401) {
          // 401 错误
          const memberStore = useMemberStore()
          memberStore.clearProfile()
          uni.navigateTo({ url: '/pages/login/login' })
          reject(res)
        } else {
          // 其他错误
          uni.showToast({
            icon: 'none',
            title: (res.data as Data<T>).msg || '请求错误',
          })
        }
      },

      // 响应失败
      fail(err) {
        uni.showToast({
          icon: 'none',
          title: '网络错误 请稍后重试',
        })
        reject(err)
      },
    })
  })
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值