All Projects → mushan0x0 → wxapp-api-interceptors

mushan0x0 / wxapp-api-interceptors

Licence: other
微信小程序api拦截器

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to wxapp-api-interceptors

xdk-cli
微信小程序cli脚手架,目前提供的功能有:快速创建启动模版功能【页面 / 组件】, 自动发布体验版功能,设置版本号/版本描述功能,自定义指令功能
Stars: ✭ 43 (-56.57%)
Mutual labels:  weapp, wxapp
Vue Loadable
⏳ Improve your loading state control with pretty simple methods and helpers.
Stars: ✭ 23 (-76.77%)
Mutual labels:  promise, interceptor
Weapp
🐧 微信小程序组件和功能封装,基于微信Component自定义组件开发
Stars: ✭ 235 (+137.37%)
Mutual labels:  weapp, wxapp
Leshare Shop Weapp
基于微信小程序的电商平台,采用原生框架开发
Stars: ✭ 183 (+84.85%)
Mutual labels:  weapp, wxapp
wxapp-boilerplate
微信小程序开发脚手架 (ES6, Redux, Immutable-js, Async/await, Promise, Reselect, Babel, ESLint, Stylelint, Gulp ... )
Stars: ✭ 35 (-64.65%)
Mutual labels:  weapp, wxapp
Wxapp Webpack Plugin
📦 微信小程序 webpack 插件
Stars: ✭ 185 (+86.87%)
Mutual labels:  weapp, wxapp
Axios Auth Refresh
Library that helps you implement automatic refresh of authorization via axios interceptors. You can easily intercept the original request when it fails, refresh the authorization and continue with the original request, without user even noticing.
Stars: ✭ 502 (+407.07%)
Mutual labels:  promise, interceptor
Weapp Qrcode Base64
微信小程序生成二维码的插件,基于base64编码输出二维码,不依赖canvas
Stars: ✭ 100 (+1.01%)
Mutual labels:  weapp, wxapp
mobx-wxapp
在小程序中使用mobx
Stars: ✭ 54 (-45.45%)
Mutual labels:  weapp, wxapp
weapp-OpenRadio
A base music weapp named OpenRadio for wechat. Can use on weapp getting started.
Stars: ✭ 14 (-85.86%)
Mutual labels:  weapp, wxapp
Dva Wxapp
微信小程序的dva集成
Stars: ✭ 183 (+84.85%)
Mutual labels:  weapp, wxapp
wxapp-computed
在微信小程序中使计算值(computed)
Stars: ✭ 20 (-79.8%)
Mutual labels:  weapp, wxapp
We Cropper
微信小程序图片裁剪工具
Stars: ✭ 1,972 (+1891.92%)
Mutual labels:  weapp, wxapp
Wxa Plugin Canvas
小程序海报组件-生成朋友圈分享海报并生成图片
Stars: ✭ 2,692 (+2619.19%)
Mutual labels:  weapp, wxapp
Wx Book
仿追书神器的小说阅读器小程序
Stars: ✭ 122 (+23.23%)
Mutual labels:  weapp, wxapp
Wechat web devtools
微信开发者工具(微信小程序)linux完美支持
Stars: ✭ 2,664 (+2590.91%)
Mutual labels:  weapp, wxapp
Tina
💃 一款轻巧的渐进式微信小程序框架
Stars: ✭ 1,153 (+1064.65%)
Mutual labels:  weapp, wxapp
Wechat Weapp Movie
🎬电影推荐 - 微信小程序
Stars: ✭ 1,355 (+1268.69%)
Mutual labels:  weapp, wxapp
Sieppari
Small, fast, and complete interceptor library for Clojure/Script
Stars: ✭ 133 (+34.34%)
Mutual labels:  promise, interceptor
weapp.request
为微信小程序提供的网络请求组件,是 wx.request 的扩展,基于 Promise API,添加缓存控制
Stars: ✭ 29 (-70.71%)
Mutual labels:  weapp, wxapp

微信小程序api拦截器

  • 完美兼容原生小程序项目
  • 完美兼用小程序api的原本调用方式,无痛迁移
  • 小程序api全Promise化
  • 和axios一样的请求方式
  • 小程序api自定义拦截调用参数和返回结果
  • 强大的async拦截

快速开始

安装

npm install wxapp-api-interceptors --save

使用

mpvue等项目
import wxApiInterceptors from 'wxapp-api-interceptors';

wxApiInterceptors(); // 必须在调用小程序api之前调用
原生小程序和taro项目

下载该项目,解压移动文件夹distwxApiInterceptors.jsruntime.js文件到你自己的项目,详见示例

const wxApiInterceptors = require('./wxApiInterceptors');

wxApiInterceptors(); // 必须在调用小程序api之前调用

小程序api调用

不必传success、complete和fail参数。

函数式异步调用方式:
wx.showLoading({title: '登录中...'})
    .then(wx.login)
    .then(data => wx.request.post('/login', {data}))
    .then(() => wx.showToast({title: '登录成功'}))
    .catch(() => wx.showToast({title: '登录失败'}))
    .finally(wx.hideLoading);
也兼容原生的调用方式(不便维护):
wx.showLoading({
    title: '登录中...',
    success: () => {
        wx.login({
            success: (data) => {
                wx.request({
                    url: '/login',
                    data,
                    success: () => wx.showToast({title: '登录成功'}),
                    fail: () => wx.showToast({title: '登录失败'}),
                    complete: wx.hideLoading,
                });
            },
        });
    },
});

自定义拦截器

使用方法及参数:wxApiInterceptors({[api]: {[request](params):params, [response](res):res}}, [isReturn]: boolean)

比如拦截wx.showModal的confirmColor默认值为red,调用成功后并拦截调用成功返回的结果。

wxApiInterceptors({
    showModal: {
        request(params) {
            if (params.confirmColor === undefined) {
                params.confirmColor = 'red';
            }
            return params;
        },
        response(res) {
            res = '调用成功';
            return res;
        },
    }
});

wx.showModal({title: '测试'})
    .then(console.log);
// 控制的输出:调用成功

默认拦截了request api,封装成了和axios差不多的使用方式

调用wx.request[method](url, [config])发送axios化的请求。

默认GET请求
wx.request('https://test.com/banner')
    .then(({data}) => {
        console.log(data);
    })
其他请求方式
wx.request.post('https://test.com', {data: {userName: 'test'}})
    .then(({data}) => {
        console.log(data);
    })

当然也可以再继续拦截request api

比如设置request api默认的host:

wxApiInterceptors({
    request: {
        request(params) {
            const host = 'https://test.com'
            if (!/^(http|\/\/)/.test(params.url)) {
                params.url = host + params.url;
            }
            return params;
        },
    },
});

甚至可以拦截自己的业务状态码:

wxApiInterceptors({
    request: {
        response(res) {
            const {data: {code}} = res;
            // 如果data里的code等于-1就响应为失败
            if (code === -1) {
                return Promise.reject(res);
            }
            return res;
        },
    },
});

强大的async拦截器

比如调用request api的时候都在header里带上本地储存的token,没有的话从服务器获取:

wxApiInterceptors({
    request: {
        async request(params) {
            if (params.header === undefined) {
                params.header = {};
            }
            let token = wx.getStorageSync('token');
            if (!token) {
                ({data: token} = await wx.request('/getToken'));
                wx.setStorageSync('token', token);
            }
            params.header.token = token;
            return params;
        },
    },
});

原生小程序项目使用async需要特殊处理,请看示例

小程序基础版本2.0.8后的小程序,在后台配置了插件不能直接调用全局wx对象issues,需要在调用wxApiInterceptors({}, true)时传入第二个参数为true,会返回一个代理后的对象以供调用,请看示例

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].