1.0.4 • Published 1 year ago
http-wxmini v1.0.4
http-wxmini 是什么?
http-wxmini 是一款为微信小程序定制的请求库,用法上同 axios 类似。
功能简介
- 基本请求
 - 并发请求
 - 拦截器
 - 取消请求
 - 本地资源上传
 - 其它……
 
安装
npm install http-wxmini实例
创建实例
使用自定义配置新建一个实例
import WxRequest from 'http-wxmini'
// 对 WxRequest 进行实例化
const instance = new WxRequest({
  baseURL: 'https://a-domain.com/api', // 使用时请换成真实接口
  timeout: 1000, // 超时时长
  isLoading: false // 是否使用默认的 loading 效果
})实例方法
以下是可用的实例方法。指定的配置将与实例的配置合并。
📌config 对象属性值和 wx.request() 方法调用时传递的参数保持一致
instance.request(url, config?)
instance.get(url, params?, config?)
instance.delete(url, params?, config?)
instance.post(url, data?, config?)
instance.put(url, data?, config?)
instance.all(...promise) // 发起并发请求
instance.upload(url, filePath, name?, config?) // 将本地资源上传到服务器方法讲解
实例.request(configOrUrl, config?)
// request 实例方法
// 方法1
instance.request({
  url: '/path',
  method: 'POST'
})
// 方法2
instance.request('/path', {
  method: 'POST'
})实例.get(url, params?, config?)
// get 实例方法
// 不需要请求参数,也不需要自定义请求配置
instance.get('/path')
// 不需要自定义请求配置
instance.get('/path', { id: 123 })
// 需要请求参数,也需要自定义请求配置
instance.get('/path', { id: 123 }, { timeout: 15000 })
// 不需要请求参数,但需要自定义请求配置
instance.get('/path', null, { timeout: 15000 })delete、post、put 方法同理
实例.all(...promise)
// 使用示例
await instance.all(promise1, promise2, promise3)本地资源上传
将本地资源上传到服务器.
/**
 * @description upload 实例方法,用来对 wx.uploadFile 进行封装
 * @param {*} url 文件的上传地址、接口地址
 * @param {*} filePath 要上传的文件资源路径
 * @param {String} name 文件对应的 key
 * @param {*} config 其他配置项
 */
upload(url, filePath, name = 'file', config = {}) {
  return this.request(
    Object.assign({ url, filePath, name, method: 'UPLOAD' }, config)
  )
}
// 使用示例
await instance.upload('/path', filePath, name, { ...config })
                                   配置
请求配置
这些是创建请求时可以用的配置选项。如果没有指定 method,请求将默认使用 GET 方法。
{
  baseURL: '', // 请求基准路径,方便进行接口的统一管理
  url: '', // 是用于请求的服务器 URL
  method: 'GET', // 创建请求时使用的方法,默认值: GET
  header: {}, // 请求头
  data: {}, // 请求参数
  timeout: 60000, // 默认的超时时长,小程序默认的超时时长是 1 分钟
  isLoading: true // 控制是否使用默认的 loading,默认值是 true 表示使用默认的 loading
    
  // ... 📌 其他属性值和 wx.request() 方法调用时传递的参数保持一致
}默认配置
您可以指定默认配置,它将作用于每个请求。
全局默认值
instance.defaults.baseURL = 'https://a-domain.com/api/'
instance.defaults.header['token'] = token
instance.defaults.isLoading = fasle自定义实例默认值
// 创建实例时配置默认值
const instance = new WxRequest({
  baseURL: 'https://a-domain.com/api/'
})
// 创建实例后修改默认值
instance.defaults.header['token'] = token配置的优先级
实例方法配置(2) > 创建实例时配置项(1) > 默认配置(0)
// 默认配置,优先级 0
{
  timeout: 60000 // 默认的超时时长,小程序默认的超时时长是 1 分钟
}
// 创建实例时配置项,优先级 1
const instance = new WxRequest({
  timeout = 2500
})
// 调用实例方法,优先级 2
instance.get('/path', null, {
  timeout: 5000
})拦截器
在请求之前新增、修改参数,在响应以后进行逻辑判断、处理
// 添加请求拦截器
instance.interceptors.request = (config) => {
  // 在发送请求之前做些什么
  return config
}
// 添加响应拦截器
instance.interceptors.response = (response) => {
  // response.isSuccess = true,代码执行了 wx.request 的 success 回调函数
  // response.isSuccess = false,代码执行了 wx.request 的 fail 回调函数
    
  // response.statusCode // http 响应状态码
    
  // response.config // 网络请求请求参数
    
  // response.data 服务器响应的真正数据
    
  // 对响应数据做点什么
  return response
}取消请求
完成中...
完整示例
导入 http-wxmini ,进行网络请求统一配置:
import WxRequest from 'http-wxmini'
// 对 WxRequest 进行实例化
const instance = new WxRequest({
  baseURL: 'https://a-domain.com/api/',
  timeout: 15000,
  isLoading: false // 不使用默认 loading
})
// 添加请求拦截器
instance.interceptors.request = (config) => {
  // 在请求发送之前做点什么……
  // 新增请求头
  const token = wx.getStorageSync('token')
  
  if (token) {
    config.headers['token'] = token
  }
  return config
}
// 添加响应拦截器
instance.interceptors.response = (response) => {
  // 对响应数据做点什么
  // 从 response 中解构 isSuccess
  const { isSuccess, data } = response
  // 如果 isSuccess 为 false,说明执行了 fail 回调函数
  // 这时候就说明网络异常,需要给用户提示网络异常
  if (!isSuccess) {
    wx.showToast({
      title: '网络异常,请稍后重试 !'
    })
    return response
  }
    
  // 对业务状态码进行
  // ...
  // 将服务器响应的数据返回
  return data
}
export default instance导入实例,使用实例提供的方法:
// 导入封装的 网络请求模块实例
import instance from '../utils/instance'
/**
 * @description 用来获取首页轮播图数据
 */
export const reqSwiperData = () => instance.get('/index/findBanner')