From ff9159288dd38c2288fff776f20dfa0861820b93 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 22 Jun 2022 14:00:44 +0800 Subject: [PATCH] feat(push): add enabled event --- .../packages/uni-push/dist/uni-push.es.js | 4 ++ .../uni-push/dist/uni-push.plus.es.js | 4 ++ src/core/service/api/plugin/push.js | 46 +++++++++++-------- .../service/api/plugin/get-provider.js | 10 ++-- 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.es.js b/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.es.js index 6b8ec3148..e0f491cb0 100644 --- a/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.es.js +++ b/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.es.js @@ -103,6 +103,10 @@ var GtPush = /*@__PURE__*/getDefaultExportFromCjs(gtpushMin); // if (process.env.UNI_PUSH_DEBUG) { // GtPush.setDebugMode(true) // } +// @ts-expect-error +uni.invokePushCallback({ + type: 'enabled', +}); const appid = process.env.UNI_APP_ID; if (!appid) { Promise.resolve().then(() => { diff --git a/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.plus.es.js b/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.plus.es.js index c321f73f9..bcdc4a988 100644 --- a/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.plus.es.js +++ b/packages/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.plus.es.js @@ -1,3 +1,7 @@ +// @ts-expect-error +uni.invokePushCallback({ + type: 'enabled', +}); Promise.resolve().then(() => { plus.push.setAutoNotification && plus.push.setAutoNotification(false); const info = plus.push.getClientInfo(); diff --git a/src/core/service/api/plugin/push.js b/src/core/service/api/plugin/push.js index 7d5aa5d4d..133ad49e4 100644 --- a/src/core/service/api/plugin/push.js +++ b/src/core/service/api/plugin/push.js @@ -8,6 +8,7 @@ import { let cid let cidErrMsg +let enabled function normalizePushMessage (message) { try { @@ -19,7 +20,9 @@ function normalizePushMessage (message) { export function invokePushCallback ( args ) { - if (args.type === 'clientId') { + if (args.type === 'enabled') { + enabled = true + } else if (args.type === 'clientId') { cid = args.cid cidErrMsg = args.errMsg invokeGetPushCidCallbacks(cid, args.errMsg) @@ -61,25 +64,32 @@ export function getPushClientId (args) { const hasSuccess = isFn(success) const hasFail = isFn(fail) const hasComplete = isFn(complete) - getPushCidCallbacks.push((cid, errMsg) => { - let res - if (cid) { - res = { - errMsg: 'getPushClientId:ok', - cid - } - hasSuccess && success(res) - } else { - res = { - errMsg: 'getPushClientId:fail' + (errMsg ? ' ' + errMsg : '') + 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) } - hasFail && fail(res) + hasComplete && complete(res) + }) + if (typeof cid !== 'undefined') { + invokeGetPushCidCallbacks(cid, cidErrMsg) } - hasComplete && complete(res) }) - if (typeof cid !== 'undefined') { - Promise.resolve().then(() => invokeGetPushCidCallbacks(cid, cidErrMsg)) - } } const onPushMessageCallbacks = [] @@ -99,4 +109,4 @@ export const offPushMessage = (fn) => { onPushMessageCallbacks.splice(index, 1) } } -} +} diff --git a/src/platforms/app-plus/service/api/plugin/get-provider.js b/src/platforms/app-plus/service/api/plugin/get-provider.js index 991c46c32..8c09b7dd1 100644 --- a/src/platforms/app-plus/service/api/plugin/get-provider.js +++ b/src/platforms/app-plus/service/api/plugin/get-provider.js @@ -4,7 +4,7 @@ import { import { isFn, isPlainObject } from 'uni-shared' const providers = { - oauth(callback) { + oauth (callback) { plus.oauth.getServices(services => { const provider = [] services.forEach(({ @@ -17,7 +17,7 @@ const providers = { callback(err) }) }, - share(callback) { + share (callback) { plus.share.getServices(services => { const provider = [] services.forEach(({ @@ -30,7 +30,7 @@ const providers = { callback(err) }) }, - payment(callback) { + payment (callback) { plus.payment.getChannels(services => { const provider = [] services.forEach(({ @@ -43,7 +43,7 @@ const providers = { callback(err) }) }, - push(callback) { + push (callback) { if (typeof weex !== 'undefined' || typeof plus !== 'undefined') { const clientInfo = plus.push.getClientInfo() callback(null, [clientInfo.id], [clientInfo]) @@ -53,7 +53,7 @@ const providers = { } } -export function getProvider({ +export function getProvider ({ service }, callbackId) { if (providers[service]) { -- GitLab