这里为各人介绍微信小程序api阻挡器,,,,,,微信小程序api阻挡器完善兼容原生小程序项目完善兼用小程序api的原本挪用方式。。。

npm install wxapp-api-interceptors --save 使用 mpvue等项目
import wxApiInterceptors from 'wxapp-api-interceptors'; wxApiInterceptors(); // 必需在挪用小程序api之前挪用 原生小程序项目
下载该项目,,,,,,解压移动文件夹dist里wxApiInterceptors.js和runtime.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}})
好比阻挡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;
},
},
});
KESION pp电子软件
KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,,,,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。。。KESION 一直通过手艺立异,,,,,,提供产品和服务,,,,,,助力企业向数字化转型,,,,,,通过科技驱动商业刷新,,,,,,让商业变得更智慧!
小程序最新更新新增可跳转到第三方app,,,,,,那么在app上怎样分享内容到小程序呢。。。...
小程序购物车抛物线动画的实现,,,,,,要怎样开发,,,,,,下面为各人介绍这篇文章。。。...