push.js 2.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10
import {
  isFn,
  isPlainObject
} from 'uni-shared'
import {
  getApiCallbacks
} from 'uni-helpers/utils'

let cid
let cidErrMsg
fxy060608's avatar
fxy060608 已提交
11
let enabled
fxy060608's avatar
fxy060608 已提交
12

13
function normalizePushMessage (message) {
fxy060608's avatar
fxy060608 已提交
14
  try {
15
    return JSON.parse(message)
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21 22
  } catch (e) {}
  return message
}

export function invokePushCallback (
  args
) {
fxy060608's avatar
fxy060608 已提交
23 24 25
  if (args.type === 'enabled') {
    enabled = true
  } else if (args.type === 'clientId') {
fxy060608's avatar
fxy060608 已提交
26 27 28 29 30 31 32
    cid = args.cid
    cidErrMsg = args.errMsg
    invokeGetPushCidCallbacks(cid, args.errMsg)
  } else if (args.type === 'pushMsg') {
    onPushMessageCallbacks.forEach((callback) => {
      callback({
        type: 'receive',
33
        data: normalizePushMessage(args.message)
fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39
      })
    })
  } else if (args.type === 'click') {
    onPushMessageCallbacks.forEach((callback) => {
      callback({
        type: 'click',
40
        data: normalizePushMessage(args.message)
fxy060608's avatar
fxy060608 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54
      })
    })
  }
}

const getPushCidCallbacks = []

function invokeGetPushCidCallbacks (cid, errMsg) {
  getPushCidCallbacks.forEach((callback) => {
    callback(cid, errMsg)
  })
  getPushCidCallbacks.length = 0
}

55
export function getPushClientId (args) {
fxy060608's avatar
fxy060608 已提交
56 57 58 59 60 61 62 63 64 65 66
  if (!isPlainObject(args)) {
    args = {}
  }
  const {
    success,
    fail,
    complete
  } = getApiCallbacks(args)
  const hasSuccess = isFn(success)
  const hasFail = isFn(fail)
  const hasComplete = isFn(complete)
fxy060608's avatar
fxy060608 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  Promise.resolve().then(() => {
    if (typeof enabled === 'undefined') {
      enabled = false
      cid = ''
      cidErrMsg = 'unipush is not enabled'
    }
    getPushCidCallbacks.push((cid, errMsg) => {
      let res
      if (cid) {
        res = {
          errMsg: 'getPushClientId:ok',
          cid
        }
        hasSuccess && success(res)
      } else {
        res = {
          errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '')
        }
        hasFail && fail(res)
fxy060608's avatar
fxy060608 已提交
86
      }
fxy060608's avatar
fxy060608 已提交
87 88 89 90
      hasComplete && complete(res)
    })
    if (typeof cid !== 'undefined') {
      invokeGetPushCidCallbacks(cid, cidErrMsg)
fxy060608's avatar
fxy060608 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    }
  })
}

const onPushMessageCallbacks = []
// 不使用 defineOnApi 实现,是因为 defineOnApi 依赖 UniServiceJSBridge ,该对象目前在小程序上未提供,故简单实现
export const onPushMessage = (fn) => {
  if (onPushMessageCallbacks.indexOf(fn) === -1) {
    onPushMessageCallbacks.push(fn)
  }
}

export const offPushMessage = (fn) => {
  if (!fn) {
    onPushMessageCallbacks.length = 0
  } else {
    const index = onPushMessageCallbacks.indexOf(fn)
    if (index > -1) {
      onPushMessageCallbacks.splice(index, 1)
    }
  }
fxy060608's avatar
fxy060608 已提交
112
}