parseAppOptions.ts 1.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6
import { ComponentPublicInstance } from 'vue'

import { MiniProgramAppOptions } from '@dcloudio/uni-mp-core'

function onAliAuthError(
  this: ComponentPublicInstance,
7
  method: ($event: unknown) => void,
fxy060608's avatar
fxy060608 已提交
8 9 10 11
  $event: any
) {
  $event.type = 'getphonenumber'
  $event.detail.errMsg =
12 13
    'getPhoneNumber:fail Error: ' + $event.detail.errorMessage
  method($event)
fxy060608's avatar
fxy060608 已提交
14 15 16 17
}

function onAliGetAuthorize(
  this: ComponentPublicInstance,
18
  method: ($event: unknown) => void,
fxy060608's avatar
fxy060608 已提交
19 20 21
  $event: any
) {
  my.getPhoneNumber({
雪洛's avatar
雪洛 已提交
22
    success: (res) => {
fxy060608's avatar
fxy060608 已提交
23
      $event.type = 'getphonenumber'
雪洛's avatar
雪洛 已提交
24 25 26 27
      const response = JSON.parse(res.response)
      $event.detail.errMsg = 'getPhoneNumber:ok'
      $event.detail.encryptedData = response.response
      $event.detail.sign = response.sign
28
      method($event)
fxy060608's avatar
fxy060608 已提交
29
    },
雪洛's avatar
雪洛 已提交
30
    fail: (res) => {
fxy060608's avatar
fxy060608 已提交
31
      $event.type = 'getphonenumber'
雪洛's avatar
雪洛 已提交
32
      $event.detail.errMsg = 'getPhoneNumber:fail Error: ' + JSON.stringify(res)
33
      method($event)
34
    },
fxy060608's avatar
fxy060608 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  })
}

export function parse(appOptions: MiniProgramAppOptions) {
  const oldOnLaunch = appOptions.onLaunch as Function
  appOptions.onLaunch = function onLaunch(options: App.LaunchShowOption) {
    oldOnLaunch.call(this, options)
    if (!this.$vm) {
      return
    }
    const globalProperties = (this.$vm as any).$app.config.globalProperties
    if (!globalProperties.$onAliAuthError) {
      globalProperties.$onAliAuthError = onAliAuthError
      globalProperties.$onAliGetAuthorize = onAliGetAuthorize
    }
  }
}