page.js 3.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
import {
  initWebview,
  createWebview
fxy060608's avatar
init v3  
fxy060608 已提交
4 5 6
} from './webview'

import {
fxy060608's avatar
fxy060608 已提交
7
  navigateFinish
fxy060608's avatar
init v3  
fxy060608 已提交
8 9
} from './navigator'

Q
qiang 已提交
10 11
import tabBar from '../framework/tab-bar'

fxy060608's avatar
fxy060608 已提交
12 13 14 15
import {
  createPage
} from '../../page-factory'

fxy060608's avatar
fxy060608 已提交
16
const pages = []
fxy060608's avatar
fxy060608 已提交
17

fxy060608's avatar
fxy060608 已提交
18 19 20 21
export function getCurrentPages (returnAll) {
  return returnAll ? pages.slice(0) : pages.filter(page => {
    return !page.$page.meta.isTabBar || page.$page.meta.visible
  })
fxy060608's avatar
fxy060608 已提交
22
}
fxy060608's avatar
fxy060608 已提交
23

fxy060608's avatar
fxy060608 已提交
24 25 26
/**
 * 首页需要主动registerPage,二级页面路由跳转时registerPage
 */
fxy060608's avatar
fxy060608 已提交
27
export function registerPage ({
fxy060608's avatar
fxy060608 已提交
28
  url,
fxy060608's avatar
fxy060608 已提交
29
  path,
fxy060608's avatar
fxy060608 已提交
30
  query,
fxy060608's avatar
fxy060608 已提交
31
  openType,
fxy060608's avatar
fxy060608 已提交
32
  webview
fxy060608's avatar
fxy060608 已提交
33 34
}) {
  const routeOptions = JSON.parse(JSON.stringify(__uniRoutes.find(route => route.path === path)))
fxy060608's avatar
fxy060608 已提交
35

fxy060608's avatar
fxy060608 已提交
36 37 38
  if (
    openType === 'reLaunch' ||
    (
fxy060608's avatar
fxy060608 已提交
39
      !__uniConfig.realEntryPagePath &&
fxy060608's avatar
fxy060608 已提交
40
      pages.length === 0
fxy060608's avatar
fxy060608 已提交
41
    )
fxy060608's avatar
fxy060608 已提交
42
  ) {
fxy060608's avatar
fxy060608 已提交
43
    routeOptions.meta.isQuit = true
Q
qiang 已提交
44 45
  } else if (!routeOptions.meta.isTabBar) {
    routeOptions.meta.isQuit = false
fxy060608's avatar
fxy060608 已提交
46 47
  }

fxy060608's avatar
fxy060608 已提交
48
  if (!webview) {
fxy060608's avatar
fxy060608 已提交
49
    webview = createWebview(path, routeOptions, query)
50
  } else {
fxy060608's avatar
fxy060608 已提交
51
    webview = plus.webview.getWebviewById(webview.id)
52
    webview.nvue = routeOptions.meta.isNVue
fxy060608's avatar
fxy060608 已提交
53 54
  }

fxy060608's avatar
fxy060608 已提交
55 56 57 58 59
  if (routeOptions.meta.isTabBar) {
    routeOptions.meta.visible = true
  }

  if (routeOptions.meta.isTabBar && webview.id !== '1') {
Q
qiang 已提交
60
    tabBar.append(webview)
fxy060608's avatar
fxy060608 已提交
61 62
  }

fxy060608's avatar
fxy060608 已提交
63
  if (process.env.NODE_ENV !== 'production') {
fxy060608's avatar
fxy060608 已提交
64
    console.log(`[uni-app] registerPage(${path},${webview.id})`)
fxy060608's avatar
fxy060608 已提交
65
  }
fxy060608's avatar
fxy060608 已提交
66

fxy060608's avatar
fxy060608 已提交
67
  initWebview(webview, routeOptions, path, query)
fxy060608's avatar
fxy060608 已提交
68 69

  const route = path.slice(1)
fxy060608's avatar
fxy060608 已提交
70

fxy060608's avatar
fxy060608 已提交
71
  webview.__uniapp_route = route
fxy060608's avatar
fxy060608 已提交
72

fxy060608's avatar
init v3  
fxy060608 已提交
73
  const pageInstance = {
fxy060608's avatar
fxy060608 已提交
74
    route,
fxy060608's avatar
fxy060608 已提交
75
    options: Object.assign({}, query || {}),
fxy060608's avatar
fxy060608 已提交
76 77
    $getAppWebview () {
      // 重要,不能直接返回 webview 对象,因为 plus 可能会被二次替换,返回的 webview 对象内部的 plus 不正确
fxy060608's avatar
fxy060608 已提交
78 79
      // 导致 webview.getStyle 等逻辑出错(旧的 webview 内部 plus 被释放)
      return plus.webview.getWebviewById(webview.id)
fxy060608's avatar
fxy060608 已提交
80
    },
fxy060608's avatar
fxy060608 已提交
81 82 83 84
    $page: {
      id: parseInt(webview.id),
      meta: routeOptions.meta,
      path,
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89 90 91
      route,
      openType
    },
    $remove () {
      const index = pages.findIndex(page => page === this)
      if (index !== -1) {
        pages.splice(index, 1)
fxy060608's avatar
fxy060608 已提交
92 93 94
        if (!webview.nvue) {
          this.$vm.$destroy()
        }
fxy060608's avatar
fxy060608 已提交
95 96 97 98
        if (process.env.NODE_ENV !== 'production') {
          console.log(`[uni-app] removePage`, path, webview.id)
        }
      }
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103
    },
    // 兼容小程序框架
    selectComponent (selector) {
      return this.$vm.selectComponent(selector)
    },
fxy060608's avatar
fxy060608 已提交
104 105
    selectAllComponents (selector) {
      return this.$vm.selectAllComponents(selector)
fxy060608's avatar
fxy060608 已提交
106
    }
fxy060608's avatar
init v3  
fxy060608 已提交
107 108 109 110 111
  }

  pages.push(pageInstance)

  // 首页是 nvue 时,在 registerPage 时,执行路由堆栈
112
  if (webview.id === '1' && webview.nvue) {
fxy060608's avatar
fxy060608 已提交
113 114
    __uniConfig.onReady(function () {
      navigateFinish(webview)
fxy060608's avatar
init v3  
fxy060608 已提交
115 116 117 118 119
    })
  }

  if (__PLATFORM__ === 'app-plus') {
    if (!webview.nvue) {
fxy060608's avatar
fxy060608 已提交
120
      const pageId = webview.id
fxy060608's avatar
fxy060608 已提交
121 122
      try {
        createPage(route, pageId, query, pageInstance).$mount()
fxy060608's avatar
fxy060608 已提交
123 124 125
      } catch (e) {
        console.error(e)
      }
fxy060608's avatar
init v3  
fxy060608 已提交
126 127
    }
  }
fxy060608's avatar
fxy060608 已提交
128 129

  return webview
fxy060608's avatar
init v3  
fxy060608 已提交
130
}