美文网首页
uni-app中使用flyio

uni-app中使用flyio

作者: _undefined | 来源:发表于2019-11-06 10:12 被阅读0次

Flyio帮助文档

import Fly from 'flyio/dist/npm/wx';

const fly = new Fly();

// 请求拦截
fly.interceptors.request.use(request => {
    let token = getApp().globalData.app.$store.state.token;
    if (token) {
        request.headers['Authorization'] = `bearer ${token}`;
    }
    return request;
});

// 响应拦截
fly.interceptors.response.use(
    response => {
        return response.data;
    },
    err => {
        let errRes = undefined;
        switch (err.status) {
            case 0:
                uni.showModal({
                    title: '请求超时',
                    content: '请稍后重试!',
                    showCancel: false
                });
                break;
            case 401:
                uni.showModal({
                    title: '登陆失效',
                    content: '请从新登陆!',
                    showCancel: false
                }).then(res => {
                    if (res[1].confirm) {
                        getApp().globalData.app.$store.commit('user/sync_loginData', {});
                        uni.reLaunch({
                            url: '/pages/login/index'
                        });
                    }
                });
                break;
            default:
                errRes = err.response && err.response.data && err.response.data.errors;
                if (errRes && errRes.length) {
                    uni.showModal({
                        title: 'Error',
                        content: `${errRes[0].message}`,
                        showCancel: false
                    });
                }
                console.log(err);
        }
    }
);

Reflect.getPrototypeOf(fly).fetch = options => {
    let opts = Object.assign(
        {
            url: '',
            method: 'POST',
            param: {},
            toast: {
                loading: true
            }
        },
        options || {}
    );

    fly.config = {
        method: opts.method,
        baseURL: 'https://xxx.com',
        timeout: 0
    };

    if (opts.toast.loading) {
        uni.showLoading({
            mask: true,
            title: '正在加载'
        });
    }

    return fly.request(opts.url, opts.param).finally(() => {
        uni.hideLoading();
    });
};

export default fly;

相关文章

  • uni-app中使用flyio

    Flyio帮助文档

  • 【js】Flyio库 使用总结

    flyio使用过程中,请求头请求参数可以全局配置好,也可以在全局配置过的情况下使用时做个性化单独配置,网址flyi...

  • 在uni-app框架中使用rem(h5和微信小程序)

    将在学习uni-app过程中遇到的问题记录一下: 1.rem根元素字号适配 问题uni-app中可以使用rem。r...

  • uni-app 创建

    创建 uni-app 有两种方式 快速搭建 uni-app 使用编译器HbuilderX 创建 使用 vue-cl...

  • uni-app写一个阅读类app

    这个程序主要是学习uni-app和egg.js,介绍:前端开发使用uni-app,后端使用egg.js,数据库使用...

  • uni-app 介绍

    uni-app 介绍 1、什么是uni-app? uni-app是一个使用 Vue.js 开发所有前端应用的框架,...

  • 使用flyio进行网络请求

    https://blog.csdn.net/Sakura_Codeboy/article/details/1037...

  • uni-app入门详解

    1 uni-app学习 1.1 什么是uni-app uni-app是一个使用Vue.js语法来开发所有前端应用的...

  • mpvue系列(二)

    封装mpvue请求文件 一、flyio是什么? flyio.js 是一个基于 promise 的,轻量且强大的Ja...

  • uQRCode 二维码生成插件

    在使用uni-app过程中需要生成二维码功能,主要是需在小程序里进行熏染。网上找了各种插件,最后去uni-app插...

网友评论

      本文标题:uni-app中使用flyio

      本文链接:https://www.haomeiwen.com/subject/xznybctx.html