diff --git a/lib/apis.js b/lib/apis.js index ca14f0fed08ac471e10e7b49a113e444a43faac0..0ac63a6a105010cbcc1b4028f80a89dee218b68d 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -119,7 +119,8 @@ const device = [ 'stopBeaconDiscovery', 'checkIsSupportSoterAuthentication', 'checkIsSoterEnrolledInDevice', - 'startSoterAuthentication', + 'startSoterAuthentication', + 'onThemeChange', 'onUIStyleChange' ] diff --git a/lib/modules.json b/lib/modules.json index 740b9fa775089ba5551c12032b677a7b97de29c4..b9457b674dade7d3f135f85e2455d112856eb821 100644 --- a/lib/modules.json +++ b/lib/modules.json @@ -121,6 +121,7 @@ "uni.getBeacons": true, "uni.startBeaconDiscovery": true, "uni.stopBeaconDiscovery": true, + "uni.onThemeChange": true, "uni.onUIStyleChange": true } }, { diff --git a/src/core/service/api/device/theme.js b/src/core/service/api/device/theme.js index 2c1adb6e9716731ee9b032409bcddff5abe49100..575d854fb1d23ae381df4f6d8a3c9236f65505eb 100644 --- a/src/core/service/api/device/theme.js +++ b/src/core/service/api/device/theme.js @@ -8,6 +8,17 @@ import { const callbacks = [] +onMethod('onThemeChange', function (res) { + callbacks.forEach(callbackId => { + invoke(callbackId, res) + }) +}) + +export function onThemeChange (callbackId) { + callbacks.push(callbackId) +} + +// 旧版本 API,后期文档更新后考虑移除 onMethod('onUIStyleChange', function (res) { callbacks.forEach(callbackId => { invoke(callbackId, res) diff --git a/src/platforms/app-plus/service/framework/app.js b/src/platforms/app-plus/service/framework/app.js index 9a59f2f91ddffd12c9f9bac99da1ce384c546831..755aa1e18a35d4d5502dcd233b9bb20e9e2d6e71 100644 --- a/src/platforms/app-plus/service/framework/app.js +++ b/src/platforms/app-plus/service/framework/app.js @@ -95,9 +95,17 @@ function initGlobalListeners () { }) }) - globalEvent.addEventListener('uistylechange', function (event) { - publish('onUIStyleChange', { - style: event.uistyle + globalEvent.addEventListener('uistylechange', function (event) { + const args = { + theme: event.uistyle + } + + callAppHook(appCtx, 'onThemeChange', args) + publish('onThemeChange', args) + + // 兼容旧版本 API + publish('onUIStyleChange', { + style: event.uistyle }) })