push.ts 2.7 KB
Newer Older
1
import { defineAsyncApi } from '../../helpers/api'
fxy060608's avatar
fxy060608 已提交
2 3 4 5

interface OnPushCidCallback {
  type: 'clientId'
  cid: string
fxy060608's avatar
fxy060608 已提交
6
  errMsg?: string
fxy060608's avatar
fxy060608 已提交
7 8 9 10 11 12 13 14 15 16 17 18
}

interface OnPushLineStateCallback {
  type: 'lineState'
  online: boolean
}

interface OnPushMsgCallback {
  type: 'pushMsg'
  message: unknown
}

fxy060608's avatar
fxy060608 已提交
19 20 21 22 23 24 25
interface OnPushClickCallback {
  type: 'click'
  message: unknown
}

let cid: string | undefined
let cidErrMsg: string | undefined
fxy060608's avatar
fxy060608 已提交
26
function normalizePushMessage(message: unknown) {
fxy060608's avatar
fxy060608 已提交
27
  try {
fxy060608's avatar
fxy060608 已提交
28
    return JSON.parse(message as string) as Record<string, any>
fxy060608's avatar
fxy060608 已提交
29 30 31
  } catch (e: any) {}
  return message
}
fxy060608's avatar
fxy060608 已提交
32 33 34 35 36
/**
 * @private
 * @param args
 */
export function invokePushCallback(
fxy060608's avatar
fxy060608 已提交
37 38 39 40 41
  args:
    | OnPushCidCallback
    | OnPushLineStateCallback
    | OnPushMsgCallback
    | OnPushClickCallback
fxy060608's avatar
fxy060608 已提交
42 43 44
) {
  if (args.type === 'clientId') {
    cid = args.cid
fxy060608's avatar
fxy060608 已提交
45 46
    cidErrMsg = args.errMsg
    invokeGetPushCidCallbacks(cid, args.errMsg)
fxy060608's avatar
fxy060608 已提交
47 48
  } else if (args.type === 'pushMsg') {
    onPushMessageCallbacks.forEach((callback) => {
fxy060608's avatar
fxy060608 已提交
49 50
      callback({
        type: 'receive',
fxy060608's avatar
fxy060608 已提交
51
        data: normalizePushMessage(args.message),
fxy060608's avatar
fxy060608 已提交
52
      })
fxy060608's avatar
fxy060608 已提交
53 54 55
    })
  } else if (args.type === 'click') {
    onPushMessageCallbacks.forEach((callback) => {
fxy060608's avatar
fxy060608 已提交
56 57
      callback({
        type: 'click',
fxy060608's avatar
fxy060608 已提交
58
        data: normalizePushMessage(args.message),
fxy060608's avatar
fxy060608 已提交
59
      })
fxy060608's avatar
fxy060608 已提交
60 61 62 63
    })
  }
}

fxy060608's avatar
fxy060608 已提交
64
const getPushCidCallbacks: ((cid?: string, errMsg?: string) => void)[] = []
fxy060608's avatar
fxy060608 已提交
65

fxy060608's avatar
fxy060608 已提交
66
function invokeGetPushCidCallbacks(cid?: string, errMsg?: string) {
fxy060608's avatar
fxy060608 已提交
67
  getPushCidCallbacks.forEach((callback) => {
fxy060608's avatar
fxy060608 已提交
68
    callback(cid, errMsg)
fxy060608's avatar
fxy060608 已提交
69 70 71 72
  })
  getPushCidCallbacks.length = 0
}

73 74 75 76 77 78 79 80 81 82 83 84 85
const API_GET_PUSH_CLIENT_ID = 'getPushClientId'
export const getPushClientId = defineAsyncApi(
  API_GET_PUSH_CLIENT_ID,
  (_, { resolve, reject }) => {
    getPushCidCallbacks.push((cid?: string, errMsg?: string) => {
      if (cid) {
        resolve({ cid })
      } else {
        reject(errMsg)
      }
    })
    if (typeof cid !== 'undefined') {
      Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg))
fxy060608's avatar
fxy060608 已提交
86 87
    }
  }
88
)
fxy060608's avatar
fxy060608 已提交
89 90

interface OnPushMessageSuccess {
fxy060608's avatar
fxy060608 已提交
91
  type: 'click' | 'receive'
fxy060608's avatar
fxy060608 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  data: unknown
}

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

export const offPushMessage: (fn?: OnPushMessageCallback) => void = (fn) => {
  if (!fn) {
    onPushMessageCallbacks.length = 0
  } else {
    const index = onPushMessageCallbacks.indexOf(fn)
    if (index > -1) {
      onPushMessageCallbacks.splice(index, 1)
    }
  }
}