小程序如何自主实现拦截器的示例代码

在一些框架中发现会提供一个很实用的功能:拦截器(interceptor)。例如要实现这个需求:小程序每次获取到定位后都存到 globalData 里: wx.getLocation({ // .. success(res) { getApp()....

在一些框架中发现会提供一个很实用的功能:拦截器(interceptor)。例如要实现这个需求:小程序每次获取到定位后都存到 globalData 里:

wx.getLocation({
 // ..
 success(res) {
  getApp().globalData.location = res
  // ...
 }
})

如果每一处使用 wx.getLocation 的地方都这么写也没啥大问题,但总显得不够“智能”,一方面是多了重复代码,另一方面如果需求变动,获取到定位后存到别的地方,那要改很多次。

优雅的拦截器

有了拦截器,可以更优雅的实现它:

intercept('getLocation', {
 success(res) {
  getApp().globalData.location = res
 }
})

只要在一处定义如上的拦截器,其他地方直接用 wx.getLocation 即可。那么,如何实现上面的方式呢?

实现 intercept 方法

// utils/intercept.js
// 存储拦截器定义
var interceptors = {}
function intercept(key, config) {
 intercept[key] = config
}
export {
 intercept,
 interceptors
}

很简单,暴露出 intercept 方法,定义一个存储器也一并暴露出去。

代理 wx

要实现使用 wx.getLocation 自动应用拦截器,就必须基于原有方法重新定义它。

import { interceptors } from './intercept'

// 备份原有微信方法
var wxBackup = {}
[
 'getLocation'
 // 还可以有很多其他方法 ...
].forEach((key) => {
 wxBackup[key] = wx[key]
 wx[key] = (config) => {
  if (interceptors[key]) {
   // 备份业务代码传入的回调方法
   var backup = {}
   var interceptor = interceptors[key]
   [
    'success',
    'fail',
    'complete'
   ].forEach((k) => {
    backup[k] = config[k]
    config[k] = (res) => {
     interceptor[k](res)
     backup[k](res)
    }
   })
  }
  wxBackup[key](config)
 }
})

当然,上述代码用数组列出了所有可能被定义拦截器的微信函数,也可以使用 Object.keys(wx) 通用处理。

更多使用场景

上面的场景比较简单,拦截器的应用还有更多场景。比如每次请求传参带上公参经纬度,接口返回的数据都会约定包裹在 object 中,请求回来需要取一遍。数据异常时还要针对错误码做特定处理,就可以很方便的用拦截器处理:

intercept('request', {
 data(data) {
  var location = getApp().globalData.location
  data.location = location.latitude + ',' + location.longitude
  return data
 },
 success(res) {
  if (res.code == 200) {
   return res.object
  } else {
   if (res.code == 'xxx') {
    // 登录失效,重新登录
    // ....
   }
  }
 }
})

注意,拦截器函数里多了返回值,具体实现方法就不多写,基于上述实现完善代码即可。

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
admin
admin

651 篇文章

作家榜 »

  1. admin 651 文章
  2. 粪斗 185 文章
  3. 王凯 92 文章
  4. 廖雪 78 文章
  5. 牟雪峰 12 文章
  6. 李沁雪 9 文章
  7. 全易 2 文章
  8. Stevengring 0 文章