utils.ts 1.7 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 56 57

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
}

export function parseRedirectInfo(): RedirectInfo {
  const weexPlus = weex.requireModule('plus')
fxy060608's avatar
fxy060608 已提交
58
  const { path, query, extraData, userAction, fromAppid } =
59 60
    weexPlus.getRedirectInfo() || {}
  const referrerInfo: UniApp.UniConfig['referrerInfo'] = {
fxy060608's avatar
fxy060608 已提交
61
    appId: fromAppid,
62 63 64 65 66 67 68 69 70 71 72 73
    extraData: {},
  }
  if (extraData) {
    referrerInfo.extraData = extraData
  }
  return {
    path,
    query: query ? '?' + query : '',
    referrerInfo,
    userAction,
  }
}