标签: flyio

  • uni-app中flyio的使用

    在 src 目录下创建 utils 目录,并添加 request.js

    import FlyIO from 'flyio/dist/npm/wx'
    
    // 创建新的 FlyIO 实例
    const service = new FlyIO()
    
    // 设置超时
    service.config.timeout = 5000;
    
    // 设置请求基地址
    service.config.baseURL = 'https://www.xxx.xxx'
    
    // 请求拦截器
    service.interceptors.request.use((request) => {
        uni.showLoading({
            title: "加载中...",
            mask: true
        })
        return request
    })
    
    // 响应拦截器
    service.interceptors.response.use((response) => {
        // 隐藏加载框
        uni.hideLoading()
    
        // 只取返回的数据字段
        return response.data
    }, (err) => {
        uni.hideLoading()
        return Promise.reject(err)
    })
    
    // 导出
    export default service

    在 main.js 中将 request.js 模块导出的 flyio 实例,挂载到 Vue.prototype

    // 导入 request.js 模块
    import service from '@/utils/request'
    
    // 挂载到 Vue.prototype
    Vue.prototype.$service = service