评论

教你怎么监听小程序的返回键

本文主要讲怎么监听拦截小程序的返回键

更新:2020年7月28日08:51:11
基础库2.12.0起,可以调用wx.enableAlertBeforeUnload监听原生右上角返回、物理返回以及wx.navigateBack时弹框提示
AIP详情请看:
https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.enableAlertBeforeUnload.html
//========================================

怎么监听小程序的返回键?

应该有很多人想要监听用户的这个动作吧,但是很遗憾,小程序不会给你这个API的,那是不是就没辙了?

幸好我们还可以自定义导航栏,这样一来我们就可以监听用户的这一动作了。

什么?这你已经知道啦?

那好咱们就不说自定义导航栏的返回监听了,说一下物理返回和左滑?右滑?(不管了,反正是滑)返回上一页怎么监听。

监听物理返回

首先说一下这个监听方法的缺点,虽说是监听,但是还是无法真正意义上的监听并拦截来阻止页面跳转,页面还是会返回上一页,而后重新载入刚刚的页面,如果这不是你想要的,那可以不用往下看了

其次说一下用到什么东西: wx.onAppRoute、wx.showModal

最后是一些主要代码:

重写wx.showModal,主要是加个confirmStay参数和使wx.showModal Promise化

const {
  showModal
} = wx;
Object.defineProperty(wx, 'showModal', {
  configurable: false, // 是否可以配置
  enumerable: false, // 是否可迭代
  writable: false, // 是否可重写
  value(...param) {
    return new Promise(function (rs, rj) {
      let { success, fail, complete, confirmStay } = param[0]
      param[0].success = (res) => {
        res.navBack = (res.confirm && !confirmStay) || (res.cancel && confirmStay)
        wx.setStorageSync('showBackModal', !res.navBack)
        success && success(res)
        rs(res)
      }
      param[0].fail = (res) => {
        fail && fail(res)
        rj(res)
      }
      param[0].complete = (res) => {
        complete && complete(res)
        (res.confirm || res.cancel) ? rs(res) : rj(res)
      }
      return showModal.apply(this, param); // 原样移交函数参数和this
    }.bind(this))
  }
});

使用wx.onAppRoute实现返回原来的页面

wx.onAppRoute(function (res) {
  var a = getApp(), ps = getCurrentPages(), t = ps[ps.length - 1],
    b = a && a.globalData && a.globalData.pageBeforeBacks || {},
    c = a && a.globalData && a.globalData.lastPage || {}
  if (res.openType == 'navigateBack') {
    var showBackModal = wx.getStorageSync('showBackModal')
    if (c.route && showBackModal && typeof b[c.route] == 'function') {
      wx.navigateTo({
        url: '/' + c.route + '?useCache=1',
      })
      b[c.route]().then(res => {
        if (res.navBack){
          a.globalData.pageBeforeBacks = {}
          wx.navigateBack({ delta: 1 })
        }
      })
    }
  } else if (res.openType == 'navigateTo' || res.openType == 'redirectTo') {

    if (!a.hasOwnProperty('globalData')) a.globalData = {}
    if (!a.globalData.hasOwnProperty('lastPage')) a.globalData.lastPage = {}
    if (!a.globalData.hasOwnProperty('pageBeforeBacks')) a.globalData.pageBeforeBacks = {}

    if (ps.length >= 2 && t.onBeforeBack && typeof t.onBeforeBack == 'function') {
      let { onUnload } = t
      wx.setStorageSync('showBackModal', !0)
      t.onUnload = function () {
        a.globalData.lastPage = {
          route: t.route,
          data: t.data
        }
        onUnload()
      }

    }

    t.onBeforeBack && typeof t.onBeforeBack == 'function' && (a.globalData.pageBeforeBacks[t.route] = t.onBeforeBack)
  }

})

改造Page

const myPage = Page
Page = function(e){
  let { onLoad, onShow, onUnload } = e
  e.onLoad = (() => {
    return function (res) {
      this.app = getApp()
      this.app.globalData = this.app.globalData || {}
      let reinit = () => {
        if (this.app.globalData.lastPage && this.app.globalData.lastPage.route == this.route) {
          this.app.globalData.lastPage.data && this.setData(this.app.globalData.lastPage.data)
          Object.assign(this, this.app.globalData.lastPage.syncProps || {})
        }
      }
      this.useCache = res.useCache

      res.useCache ? reinit() : (onLoad && onLoad.call(this, res))
    }
  })()
  e.onShow = (() => {
    return function (res) {
      !this.useCache && onShow && onShow.call(this, res)
    }
  })()
  e.onUnload = (() => {
    return function (res) {

      this.app.globalData = Object.assign(this.app.globalData || {}, {
        lastPage: this
      })
      onUnload && onUnload.call(this, res)
    }
  })()
  return myPage.call(this, e)
}

在需要监听的页面加个onBeforeBack方法,方法返回Promise化的wx.showModal

onBeforeBack: function () {
  return wx.showModal({
    title: '提示',
    content: '信息尚未保存,确定要返回吗?',
    confirmStay: !1 //结合content意思,点击确定按钮,是否留在原来页面,confirmStay默认false
  })
}

运行测试,Oj8K

是不是很简单,马上去试试水吧,效果图就不放了,静态图也看不出效果,动态图懒得弄,想看效果的自己运行代码片段吧

代码片段

https://developers.weixin.qq.com/s/hc2tyrmw79hg

最后一次编辑于  2020-07-28  
点赞 66
收藏
评论

84 个评论

  • 阿
布
    阿 布
    2021-04-07

    死翘翘了

    2021-04-07
    赞同 3
    回复 5
    • 啊咧苗儿
      啊咧苗儿
      2021-09-06
      求怎么弄的啊
      2021-09-06
      回复
    • CheaterC
      CheaterC
      2023-02-01回复啊咧苗儿
      y:2s;-webkit-animation-delay:2s;background-color: yellow; color: red; padding: 6px 10px;margin-right: 5px;border-radius: 10%;box-shadow: 0px 0px 9px -2px #000;display: inline-block;">死翘翘了</p>
      2023-02-01
      回复
    • CheaterC
      CheaterC
      2023-02-01
      <p style="animation: slideDown 1s ease-in-out infinite;animation-delay:2s;-webkit-animation-delay:2s;background-color: yellow; color: red; padding: 6px 10px;margin-right: 5px;border-radius: 10%;box-shadow: 0px 0px 9px -2px #000;display: inline-block;">死翘翘了</p>
      2023-02-01
      回复
    • Roger🌌
      Roger🌌
      2023-05-05
      这是xss注入啊,小心给你封号
      2023-05-05
      回复
    • yuyuyu
      yuyuyu
      2023-10-19
      <p style="animation: slideDown 1s ease-in-out infinite;animation-delay:2s;-webkit-animation-delay:2s;background-color: yellow; color: red; padding: 6px 10px;margin-right: 5px;border-radius: 10%;box-shadow: 0px 0px 9px -2px #000;display: inline-block;">死翘翘了</p>
      2023-10-19
      回复
  • Admin ²º²⁴
    Admin ²º²⁴
    2020-01-16

    这个不错,收藏了!~

    2020-01-16
    赞同 3
    回复 5
  • 木木
    木木
    2023-09-29

    🐂🍺

    2023-09-29
    赞同 1
    回复
  • 刘拓飞
    刘拓飞
    2022-12-07

    <p style="animation: weuiLoading 1s ease-in-out infinite;animation-delay:0s;-webkit-animation-delay:0s;background-color: red; color: #fff; padding: 6px 10px;margin-right: 5px;border-radius: 10%;box-shadow: 0px 0px 9px -2px #000;display: inline-block;">有</p><p style="animation: weuiLoading 1s ease-in-out infinite;animation-delay:0s;-webkit-animation-delay:0s;background-color: red; color: #fff; padding: 6px 10px;margin-right: 5px;border-radius: 10%;box-shadow: 0px 0px 9px -2px #000;display: inline-block;">点</p><p style="animation: weuiLoading 1s ease-in-out infinite;animation-delay:0s;-webkit-animation-delay:0s;background-color: red; color: #fff; padding: 6px 10px;margin-right: 5px;border-radius: 10%;box-shadow: 0px 0px 9px -2px #000;display: inline-block;">东</p><p style="animation: weuiLoading 1s ease-in-out infinite;animation-delay:0s;-webkit-animation-delay:0s;background-color: red; color: #fff; padding: 6px 10px;margin-right: 5px;border-radius: 10%;box-shadow: 0px 0px 9px -2px #000;display: inline-block;">西</p>

    2022-12-07
    赞同 1
    回复
  • Bond
    Bond
    2020-01-06

    2020-01-06
    赞同 1
    回复 2
  • o0o有脾气的酸奶
    o0o有脾气的酸奶
    2019-12-09

    2019-12-09
    赞同 1
    回复 13
    查看更多(8)
  • Roses
    Roses
    06-21

    <p style="animation: slideDown 1s ease-in-out infinite;animation-delay:2s;-webkit-animation-delay:2s;background-color: yellow; color: red; padding: 6px 10px;margin-right: 5px;border-radius: 10%;box-shadow: 0px 0px 9px -2px #000;display: inline-block;">死翘翘了</p>

    06-21
    赞同
    回复
  • 汪超超
    汪超超
    05-08

    <p style="animation: slideDown 1s ease-in-out infinite;animation-delay:2s;-webkit-animation-delay:2s;background-color: yellow; color: red; padding: 6px 10px;margin-right: 5px;border-radius: 10%;box-shadow: 0px 0px 9px -2px #000;display: inline-block;">哈哈哈</p>

    05-08
    赞同
    回复
  • 💤
    💤
    04-10

    针不戳

    04-10
    赞同
    回复 1
    • 十一
      十一
      05-23
      帅哥,咋搞的
      05-23
      回复
  • 阿苏
    阿苏
    2023-12-12

    <p style="animation: weuiLoading 1s ease-in-out infinite;animation-delay:0s;-webkit-animation-delay:0s;background-color: red; color: #fff; padding: 6px 10px;margin-right: 5px;border-radius: 10%;box-shadow: 0px 0px 9px -2px #000;display: inline-block;">333</p>

    2023-12-12
    赞同
    回复

正在加载...

登录 后发表内容