routerBefore.js 1.12 KB
class RouterBefore {
  constructor(options) {
    // console.log('---------------- 路由初始化 ----------------')
    this.initPageProxy()
  }
  /**监听页面 */
  pageLeaveProxy(thisPage) {
    let onLoad = thisPage.onLoad,
      onShow = thisPage.onShow
    thisPage.onLoad = function () {
      const event = getApp().globalData.ROUTE_QUERY
      const userInfo = wx.$util.getUserInfo()
      let options = {
        ...event,
        ...arguments[0]
      }
      getApp().globalData.ROUTE_QUERY = {}
      this.setData({
        userInfo
      })
      onLoad && onLoad.apply(this, [options]), (this.options = options)
    }
    thisPage.onShow = function () {
      const userInfo = wx.$util.getUserInfo()
      onShow && onShow.apply(this, arguments), (this.setData({
        userInfo
      }))
    }
  }
  /**全局注册事件 */
  initPageProxy() {
    let thisPage = Page
    let _this = this
    Page = function (el) {
      try {
        el || (el = {}), _this.pageLeaveProxy(el), thisPage.apply(this, arguments)
      } catch (el) {
        thisPage.apply(this, arguments)
      }
    }
  }
}
export default new RouterBefore()