page.js 2.8 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 10
} from './navigator'

import {
fxy060608's avatar
fxy060608 已提交
11 12
  PAGE_CREATE
} from '../../constants'
fxy060608's avatar
fxy060608 已提交
13

Q
qiang 已提交
14 15
import tabBar from '../framework/tab-bar'

fxy060608's avatar
fxy060608 已提交
16 17 18 19
import {
  createPage
} from '../../page-factory'

fxy060608's avatar
fxy060608 已提交
20
const pages = []
fxy060608's avatar
fxy060608 已提交
21

fxy060608's avatar
fxy060608 已提交
22 23 24 25
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 已提交
26
}
fxy060608's avatar
fxy060608 已提交
27

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

fxy060608's avatar
fxy060608 已提交
39 40 41
  if (openType === 'reLaunch' || pages.length === 0) {
    // pages.length===0 表示首页触发 redirectTo
    routeOptions.meta.isQuit = true
Q
qiang 已提交
42 43
  } else if (!routeOptions.meta.isTabBar) {
    routeOptions.meta.isQuit = false
fxy060608's avatar
fxy060608 已提交
44 45
  }

fxy060608's avatar
fxy060608 已提交
46
  if (!webview) {
fxy060608's avatar
fxy060608 已提交
47
    webview = createWebview(path, routeOptions)
48 49
  } else {
    webview = plus.webview.getWebviewById(webview.id)
fxy060608's avatar
fxy060608 已提交
50 51
  }

fxy060608's avatar
fxy060608 已提交
52 53 54 55 56
  if (routeOptions.meta.isTabBar) {
    routeOptions.meta.visible = true
  }

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

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

fxy060608's avatar
init v3  
fxy060608 已提交
64
  initWebview(webview, routeOptions)
fxy060608's avatar
fxy060608 已提交
65 66

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

fxy060608's avatar
fxy060608 已提交
68
  webview.__uniapp_route = route
fxy060608's avatar
fxy060608 已提交
69

fxy060608's avatar
init v3  
fxy060608 已提交
70
  const pageInstance = {
fxy060608's avatar
fxy060608 已提交
71
    route,
fxy060608's avatar
fxy060608 已提交
72
    options: Object.assign({}, query || {}),
fxy060608's avatar
fxy060608 已提交
73 74 75
    $getAppWebview () {
      return webview
    },
fxy060608's avatar
fxy060608 已提交
76 77 78 79
    $page: {
      id: parseInt(webview.id),
      meta: routeOptions.meta,
      path,
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84 85 86
      route,
      openType
    },
    $remove () {
      const index = pages.findIndex(page => page === this)
      if (index !== -1) {
        pages.splice(index, 1)
fxy060608's avatar
fxy060608 已提交
87 88 89
        if (!webview.nvue) {
          this.$vm.$destroy()
        }
fxy060608's avatar
fxy060608 已提交
90 91 92 93
        if (process.env.NODE_ENV !== 'production') {
          console.log(`[uni-app] removePage`, path, webview.id)
        }
      }
fxy060608's avatar
fxy060608 已提交
94
    }
fxy060608's avatar
init v3  
fxy060608 已提交
95 96 97 98 99 100 101
  }

  pages.push(pageInstance)

  // 首页是 nvue 时,在 registerPage 时,执行路由堆栈
  if (webview.id === '1' && routeOptions.meta.isNVue) {
    webview.nvue = true
fxy060608's avatar
fxy060608 已提交
102 103
    __uniConfig.onReady(function () {
      navigateFinish(webview)
fxy060608's avatar
init v3  
fxy060608 已提交
104 105 106 107 108
    })
  }

  if (__PLATFORM__ === 'app-plus') {
    if (!webview.nvue) {
fxy060608's avatar
fxy060608 已提交
109 110 111 112 113
      const pageId = webview.id

      // 通知页面已开始创建
      UniServiceJSBridge.publishHandler('vdSync', {
        data: [
fxy060608's avatar
fxy060608 已提交
114
          [PAGE_CREATE, [pageId, route]]
fxy060608's avatar
fxy060608 已提交
115 116 117 118 119 120
        ],
        options: {
          timestamp: Date.now()
        }
      }, [pageId])

fxy060608's avatar
fxy060608 已提交
121
      createPage(route, pageId, query, pageInstance).$mount()
fxy060608's avatar
init v3  
fxy060608 已提交
122 123
    }
  }
fxy060608's avatar
fxy060608 已提交
124 125

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