plus.ts 2.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { extend } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2
import { formatLog } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
3
import { getRouteOptions } from '@dcloudio/uni-core'
Q
qiang 已提交
4 5 6 7 8 9
interface PlusResult extends Record<string, any> {
  code?: number
  message?: string
}

type PlusError = PlusResult
Q
qiang 已提交
10 11
type Resolve = (res: any) => void
type Reject = (errMsg: string, errRes?: any) => void
Q
qiang 已提交
12 13

export function warpPlusSuccessCallback(
Q
qiang 已提交
14
  resolve: Resolve,
Q
qiang 已提交
15 16 17 18 19 20 21 22 23 24 25 26
  after?: (res: any) => any
) {
  return function successCallback(data: PlusResult) {
    delete data.code
    delete data.message
    if (typeof after === 'function') {
      data = after(data)
    }
    resolve(data)
  }
}

Q
qiang 已提交
27
export function warpPlusErrorCallback(reject: Reject, errMsg?: string) {
28
  return function errorCallback(error?: PlusError) {
Q
qiang 已提交
29 30 31 32
    error = error || {}
    // 一键登录errorCallback新增 appid、metadata、uid 参数返回
    errMsg = error.message || errMsg || ''
    delete error.message
fxy060608's avatar
fxy060608 已提交
33
    reject(errMsg, extend({ code: 0 }, error))
Q
qiang 已提交
34 35
  }
}
Q
qiang 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

export function warpPlusEvent(plusObject: () => any, event: string) {
  return function () {
    const object = plusObject()
    object(function (data?: Record<string, any>) {
      if (data) {
        delete data.code
        delete data.message
      }
      UniServiceJSBridge.invokeOnCallback(event, data)
    })
  }
}

export function warpPlusMethod(
  plusObject: () => any,
  before?: (options: any) => any,
  after?: (res: any) => any
) {
  return function (
    options: any,
    { resolve, reject }: { resolve: Resolve; reject: Reject }
  ) {
    const object = plusObject()
    object(
      extend({}, typeof before === 'function' ? before(options) : options, {
        success: warpPlusSuccessCallback(resolve, after),
        fail: warpPlusErrorCallback(reject),
      })
    )
  }
}
D
DCloud_LXH 已提交
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
export function isTabBarPage(path = '') {
  if (!(__uniConfig.tabBar && Array.isArray(__uniConfig.tabBar.list))) {
    return false
  }
  try {
    if (!path) {
      const pages = getCurrentPages()
      if (!pages.length) {
        return false
      }
      const page = pages[pages.length - 1]
      if (!page) {
        return false
      }
      return page.$page.meta.isTabBar
    }
    if (!/^\//.test(path)) {
      path = '/' + path
    }
fxy060608's avatar
fxy060608 已提交
88
    const route = getRouteOptions(path)
89 90
    return route && route.meta.isTabBar
  } catch (e) {
fxy060608's avatar
fxy060608 已提交
91
    if (__DEV__) {
fxy060608's avatar
fxy060608 已提交
92
      console.error(formatLog('isTabBarPage', e))
93 94 95 96
    }
  }
  return false
}