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

let cid
let cidErrMsg

fxy060608's avatar
fxy060608 已提交
12
function normalizePushMessage (type, message) {
fxy060608's avatar
fxy060608 已提交
13
  try {
fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    const res = JSON.parse(message)
    if (type === 'receive') {
      if (res.payload) {
        if (res.aps) {
          res.payload.aps = res.aps
        }
        return res.payload
      }
    } else if (type === 'click') {
      delete res.type
      delete res.__UUID__
      delete res.appid
      if (res.aps && res.aps.alert) {
        res.title = res.aps.alert.title
      }
    }
fxy060608's avatar
fxy060608 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  } catch (e) {}
  return message
}

export function invokePushCallback (
  args
) {
  if (args.type === 'clientId') {
    cid = args.cid
    cidErrMsg = args.errMsg
    invokeGetPushCidCallbacks(cid, args.errMsg)
  } else if (args.type === 'pushMsg') {
    onPushMessageCallbacks.forEach((callback) => {
      callback({
        type: 'receive',
fxy060608's avatar
fxy060608 已提交
45
        data: normalizePushMessage('receive', args.message)
fxy060608's avatar
fxy060608 已提交
46 47 48 49 50 51
      })
    })
  } else if (args.type === 'click') {
    onPushMessageCallbacks.forEach((callback) => {
      callback({
        type: 'click',
fxy060608's avatar
fxy060608 已提交
52
        data: normalizePushMessage('click', args.message)
fxy060608's avatar
fxy060608 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
      })
    })
  }
}

const getPushCidCallbacks = []

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

export function getPushCid (args) {
  if (!isPlainObject(args)) {
    args = {}
  }
  const {
    success,
    fail,
    complete
  } = getApiCallbacks(args)
  const hasSuccess = isFn(success)
  const hasFail = isFn(fail)
  const hasComplete = isFn(complete)
  getPushCidCallbacks.push((cid, errMsg) => {
    let res
    if (cid) {
      res = {
        errMsg: 'getPushCid:ok',
        cid
      }
      hasSuccess && success(res)
    } else {
      res = {
        errMsg: 'getPushCid:fail' + (errMsg ? ' ' + errMsg : '')
      }
      hasFail && fail(res)
    }
    hasComplete && complete(res)
  })
  if (typeof cid !== 'undefined') {
    Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg))
  }
}

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