utils.ts 1.8 KB
Newer Older
1 2 3 4
import { extend } from '@vue/shared'
import { parseQuery } from '@dcloudio/uni-shared'
import { createLaunchOptions, LaunchOptions } from '@dcloudio/uni-core'

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

fxy060608's avatar
fxy060608 已提交
7
export function backbuttonListener() {
fxy060608's avatar
fxy060608 已提交
8 9 10 11
  uni.navigateBack({
    from: 'backbutton',
    success() {}, // 传入空方法,避免返回Promise,因为onBackPress可能导致fail
  } as UniApp.NavigateBackOptions)
fxy060608's avatar
fxy060608 已提交
12
}
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

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 || {},
  })
}

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 已提交
56
export function parseRedirectInfo(): RedirectInfo | void {
57
  const weexPlus = weex.requireModule('plus')
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  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,
    }
74 75
  }
}