utils.ts 2.2 KB
Newer Older
1
import { extend } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2
import { addLeadingSlash, parseQuery } from '@dcloudio/uni-shared'
3
import { createLaunchOptions, LaunchOptions } from '@dcloudio/uni-core'
fxy060608's avatar
fxy060608 已提交
4
import { $reLaunch } from '../../api/route/reLaunch'
5

fxy060608's avatar
fxy060608 已提交
6
export const EVENT_BACKBUTTON = 'backbutton'
7

fxy060608's avatar
fxy060608 已提交
8
export function backbuttonListener() {
fxy060608's avatar
fxy060608 已提交
9 10 11 12
  uni.navigateBack({
    from: 'backbutton',
    success() {}, // 传入空方法,避免返回Promise,因为onBackPress可能导致fail
  } as UniApp.NavigateBackOptions)
fxy060608's avatar
fxy060608 已提交
13
}
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

const enterOptions: LaunchOptions = createLaunchOptions()
const launchOptions: LaunchOptions = createLaunchOptions()

export function getLaunchOptions() {
  return launchOptions
}

export function getEnterOptions() {
  return enterOptions
}

export function initEnterOptions({
  path,
  query,
  referrerInfo,
}: Partial<RedirectInfo>) {
  extend(enterOptions, {
    path,
    query: query ? parseQuery(query) : {},
    referrerInfo: referrerInfo || {},
  })
}

fxy060608's avatar
fxy060608 已提交
38 39 40 41 42 43 44 45 46 47 48
export function initEnterReLaunch(info: RedirectInfo) {
  __uniConfig.realEntryPagePath =
    __uniConfig.realEntryPagePath || __uniConfig.entryPagePath
  __uniConfig.entryPagePath = info.path
  __uniConfig.entryPageQuery = info.query
  $reLaunch(
    { url: addLeadingSlash(info.path) + info.query },
    { resolve() {}, reject() {} }
  )
}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
export function initLaunchOptions({
  path,
  query,
  referrerInfo,
}: Partial<RedirectInfo>) {
  extend(launchOptions, {
    path,
    query: query ? parseQuery(query) : {},
    referrerInfo: referrerInfo || {},
  })
  extend(enterOptions, launchOptions)
  return launchOptions
}

interface RedirectInfo extends Omit<LaunchOptions, 'query' | 'scene'> {
  query: string
  userAction: boolean
}

fxy060608's avatar
fxy060608 已提交
68
export function parseRedirectInfo(): RedirectInfo | void {
69
  const weexPlus = weex.requireModule('plus')
fxy060608's avatar
fxy060608 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  if (weexPlus.getRedirectInfo) {
    const { path, query, extraData, userAction, fromAppid } =
      weexPlus.getRedirectInfo() || {}
    const referrerInfo: UniApp.UniConfig['referrerInfo'] = {
      appId: fromAppid,
      extraData: {},
    }
    if (extraData) {
      referrerInfo.extraData = extraData
    }
    return {
      path,
      query: query ? '?' + query : '',
      referrerInfo,
      userAction,
    }
86 87
  }
}