root-back-home.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <view class="root-back-home"></view>
  3. </template>
  4. <script>
  5. // 不带 path 的再次进入(聊天下拉、最近使用等)会停在上次的红包页。
  6. // 页面栈只有这一页时,返回会直接退出小程序。先打开首页再回到当前页,返回就能进首页。
  7. const REOPEN_SCENES = [1001, 1089, 1090, 1103, 1104, 1106]
  8. const HOME_URL = '/pages/sell/index'
  9. let padding = false
  10. function getEntryScene() {
  11. try {
  12. if (typeof uni.getEnterOptionsSync === 'function') {
  13. const scene = uni.getEnterOptionsSync().scene
  14. if (scene) return Number(scene)
  15. }
  16. } catch (e) {}
  17. try {
  18. const app = getApp()
  19. return Number(app && app.globalData && app.globalData.entryScene) || 0
  20. } catch (e) {
  21. return 0
  22. }
  23. }
  24. export default {
  25. methods: {
  26. sync() {
  27. if (padding) return true
  28. const pages = getCurrentPages()
  29. if (pages.length !== 1) return false
  30. if (!REOPEN_SCENES.includes(getEntryScene())) return false
  31. const page = pages[0]
  32. const route = page.route || ''
  33. if (route.indexOf('packet/pages/') !== 0) return false
  34. const query = {}
  35. const pageOptions = page.options || {}
  36. if (pageOptions.bianhao) query.bianhao = pageOptions.bianhao
  37. const bianhao = this.$parent && this.$parent.bianhao
  38. if (bianhao && !query.bianhao) query.bianhao = bianhao
  39. const qs = Object.keys(query)
  40. .map((key) => key + '=' + encodeURIComponent(query[key]))
  41. .join('&')
  42. const url = '/' + route + (qs ? '?' + qs : '')
  43. padding = true
  44. const openPacket = (retry) => {
  45. uni.navigateTo({
  46. url,
  47. success: () => {
  48. padding = false
  49. },
  50. fail: () => {
  51. if (retry) {
  52. setTimeout(() => openPacket(false), 200)
  53. return
  54. }
  55. padding = false
  56. }
  57. })
  58. }
  59. uni.switchTab({
  60. url: HOME_URL,
  61. success: () => {
  62. // 等首页切换完成再打开红包页,避免路由冲突
  63. setTimeout(() => openPacket(true), 50)
  64. },
  65. fail: () => {
  66. padding = false
  67. }
  68. })
  69. return true
  70. }
  71. }
  72. }
  73. </script>
  74. <style lang="scss" scoped>
  75. .root-back-home {
  76. height: 0;
  77. width: 0;
  78. overflow: hidden;
  79. }
  80. </style>