@heybox/hb-sdk 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -68,6 +68,20 @@ const STORAGE_SET_STORAGE_METHOD = 'storage.setStorage';
68
68
  * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
69
69
  */
70
70
  const NETWORK_REQUEST_METHOD = 'network.request';
71
+ /** 展示 toast 能力方法名。 */
72
+ const UI_SHOW_TOAST_METHOD = 'ui.showToast';
73
+ /** 展示全局 loading 能力方法名。 */
74
+ const UI_SHOW_LOADING_METHOD = 'ui.showLoading';
75
+ /** 隐藏全局 loading 能力方法名。 */
76
+ const UI_HIDE_LOADING_METHOD = 'ui.hideLoading';
77
+ /** 震动反馈能力方法名。 */
78
+ const DEVICE_VIBRATE_METHOD = 'device.vibrate';
79
+ /** 写入文本剪贴板能力方法名。 */
80
+ const DEVICE_SET_CLIPBOARD_METHOD = 'device.setClipboard';
81
+ /** 关闭当前小程序容器能力方法名。 */
82
+ const NAVIGATION_CLOSE_METHOD = 'navigation.close';
83
+ /** 重载当前小程序容器能力方法名。 */
84
+ const NAVIGATION_RELOAD_METHOD = 'navigation.reload';
71
85
  /**
72
86
  * 小程序开放能力目录。
73
87
  *
@@ -138,6 +152,55 @@ const MINI_PROGRAM_PROTOCOL_CAPABILITIES = [
138
152
  permission: 'network.request',
139
153
  risk: 'high',
140
154
  },
155
+ {
156
+ method: UI_SHOW_TOAST_METHOD,
157
+ module: 'ui',
158
+ capability: UI_SHOW_TOAST_METHOD,
159
+ permission: 'ui.toast',
160
+ risk: 'low',
161
+ },
162
+ {
163
+ method: UI_SHOW_LOADING_METHOD,
164
+ module: 'ui',
165
+ capability: UI_SHOW_LOADING_METHOD,
166
+ permission: 'ui.loading',
167
+ risk: 'low',
168
+ },
169
+ {
170
+ method: UI_HIDE_LOADING_METHOD,
171
+ module: 'ui',
172
+ capability: UI_HIDE_LOADING_METHOD,
173
+ permission: 'ui.loading',
174
+ risk: 'low',
175
+ },
176
+ {
177
+ method: DEVICE_VIBRATE_METHOD,
178
+ module: 'device',
179
+ capability: DEVICE_VIBRATE_METHOD,
180
+ permission: 'device.vibrate',
181
+ risk: 'low',
182
+ },
183
+ {
184
+ method: DEVICE_SET_CLIPBOARD_METHOD,
185
+ module: 'device',
186
+ capability: DEVICE_SET_CLIPBOARD_METHOD,
187
+ permission: 'device.clipboard.write',
188
+ risk: 'medium',
189
+ },
190
+ {
191
+ method: NAVIGATION_CLOSE_METHOD,
192
+ module: 'navigation',
193
+ capability: NAVIGATION_CLOSE_METHOD,
194
+ permission: 'navigation.close',
195
+ risk: 'medium',
196
+ },
197
+ {
198
+ method: NAVIGATION_RELOAD_METHOD,
199
+ module: 'navigation',
200
+ capability: NAVIGATION_RELOAD_METHOD,
201
+ permission: 'navigation.reload',
202
+ risk: 'medium',
203
+ },
141
204
  ];
142
205
 
143
206
  var browser = {};
@@ -3773,19 +3836,48 @@ const MINI_PROGRAM_MOCK_RUNTIME_METHOD_HANDLERS = {
3773
3836
  [STORAGE_GET_STORAGE_METHOD]: (runtime, payload) => runtime.getStorage(payload),
3774
3837
  [STORAGE_SET_STORAGE_METHOD]: (runtime, payload) => runtime.setStorage(payload),
3775
3838
  [NETWORK_REQUEST_METHOD]: (runtime, payload) => runtime.requestNetwork(payload),
3839
+ [UI_SHOW_TOAST_METHOD]: (runtime, payload) => runtime.showToast(payload),
3840
+ [UI_SHOW_LOADING_METHOD]: (runtime, payload) => runtime.showLoading(payload),
3841
+ [UI_HIDE_LOADING_METHOD]: (runtime, payload) => runtime.hideLoading(payload),
3842
+ [DEVICE_VIBRATE_METHOD]: (runtime, payload) => runtime.vibrate(payload),
3843
+ [DEVICE_SET_CLIPBOARD_METHOD]: (runtime, payload) => runtime.setClipboard(payload),
3844
+ [NAVIGATION_CLOSE_METHOD]: (runtime, payload) => runtime.close(payload),
3845
+ [NAVIGATION_RELOAD_METHOD]: (runtime, payload) => runtime.reload(payload),
3776
3846
  };
3777
3847
  class MiniProgramMockRuntime {
3778
3848
  constructor(adapter, options = {}) {
3779
3849
  this.adapter = adapter;
3780
3850
  this.options = options;
3851
+ this.clipboardText = null;
3852
+ this.closed = false;
3853
+ this.loading = {
3854
+ dismissible: false,
3855
+ visible: false,
3856
+ };
3857
+ this.records = [];
3858
+ this.toasts = [];
3859
+ this.vibrates = [];
3781
3860
  }
3782
3861
  async runMethod(method, payload) {
3783
3862
  if (!isMiniProgramMockRuntimeMethod(method)) {
3784
3863
  throw createMockBridgeError('METHOD_NOT_FOUND', `未开放小程序能力: ${method}`);
3785
3864
  }
3865
+ if (this.closed && method !== NAVIGATION_CLOSE_METHOD) {
3866
+ throw createMockBridgeError('INVALID_STATE', '当前小程序容器已关闭');
3867
+ }
3786
3868
  const handler = MINI_PROGRAM_MOCK_RUNTIME_METHOD_HANDLERS[method];
3787
3869
  return handler(this, payload);
3788
3870
  }
3871
+ getState() {
3872
+ return {
3873
+ clipboardText: this.clipboardText,
3874
+ closed: this.closed,
3875
+ loading: { ...this.loading },
3876
+ records: [...this.records],
3877
+ toasts: [...this.toasts],
3878
+ vibrates: [...this.vibrates],
3879
+ };
3880
+ }
3789
3881
  async login() {
3790
3882
  const result = await loginAndGetMiniProgramRuntimeUserInfo(this.adapter);
3791
3883
  this.options.onAuthChange?.(result);
@@ -3815,6 +3907,80 @@ class MiniProgramMockRuntime {
3815
3907
  requestNetwork(payload) {
3816
3908
  return requestMiniProgramRuntimeNetwork(payload, this.adapter);
3817
3909
  }
3910
+ async showToast(payload) {
3911
+ const toast = readShowToastPayload(payload);
3912
+ this.toasts.unshift(toast);
3913
+ this.toasts.splice(10);
3914
+ this.record(UI_SHOW_TOAST_METHOD, toast);
3915
+ renderMockToast(toast);
3916
+ await this.options.onToast?.(toast);
3917
+ }
3918
+ async showLoading(payload) {
3919
+ this.loading = readShowLoadingPayload(payload);
3920
+ this.record(UI_SHOW_LOADING_METHOD, this.loading);
3921
+ renderMockLoading(this.loading);
3922
+ await this.options.onLoadingChange?.({ ...this.loading });
3923
+ }
3924
+ async hideLoading(payload) {
3925
+ assertEmptyPayload(payload, 'ui.hideLoading');
3926
+ this.loading = {
3927
+ dismissible: false,
3928
+ visible: false,
3929
+ };
3930
+ this.record(UI_HIDE_LOADING_METHOD, this.loading);
3931
+ renderMockLoading(this.loading);
3932
+ await this.options.onLoadingChange?.({ ...this.loading });
3933
+ }
3934
+ async vibrate(payload) {
3935
+ const action = readVibratePayload(payload);
3936
+ this.vibrates.unshift(action);
3937
+ this.vibrates.splice(10);
3938
+ this.record(DEVICE_VIBRATE_METHOD, action);
3939
+ if (typeof navigator !== 'undefined' && typeof navigator.vibrate === 'function') {
3940
+ navigator.vibrate(action.pattern);
3941
+ }
3942
+ await this.options.onVibrate?.(action);
3943
+ }
3944
+ async setClipboard(payload) {
3945
+ const text = readSetClipboardPayload(payload);
3946
+ this.record(DEVICE_SET_CLIPBOARD_METHOD, { text });
3947
+ try {
3948
+ if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
3949
+ await navigator.clipboard.writeText(text);
3950
+ }
3951
+ }
3952
+ catch {
3953
+ // Browser clipboard permissions are intentionally non-blocking in the mock host.
3954
+ }
3955
+ this.clipboardText = text;
3956
+ await this.options.onClipboardText?.(text);
3957
+ }
3958
+ async close(payload) {
3959
+ assertEmptyPayload(payload, 'navigation.close');
3960
+ this.record(NAVIGATION_CLOSE_METHOD, { closed: this.closed });
3961
+ if (this.closed) {
3962
+ return;
3963
+ }
3964
+ this.closed = true;
3965
+ closeMockIframe();
3966
+ await this.options.onClose?.();
3967
+ }
3968
+ async reload(payload) {
3969
+ assertEmptyPayload(payload, 'navigation.reload');
3970
+ this.record(NAVIGATION_RELOAD_METHOD, {});
3971
+ await this.options.onReload?.();
3972
+ if (!this.options.onReload) {
3973
+ reloadMockIframe();
3974
+ }
3975
+ }
3976
+ record(method, payload) {
3977
+ this.records.unshift({
3978
+ method,
3979
+ payload,
3980
+ timestamp: Date.now(),
3981
+ });
3982
+ this.records.splice(30);
3983
+ }
3818
3984
  }
3819
3985
  function isMiniProgramMockRuntimeMethod(method) {
3820
3986
  return MINI_PROGRAM_MOCK_RUNTIME_METHODS.includes(method);
@@ -3844,6 +4010,215 @@ function toMockBridgeError(error) {
3844
4010
  function isObjectLike(value) {
3845
4011
  return (typeof value === 'object' || typeof value === 'function') && value !== null;
3846
4012
  }
4013
+ function readShowToastPayload(payload) {
4014
+ if (!isPlainRecord(payload)) {
4015
+ throw createMockBridgeError('INVALID_PARAMS', 'ui.showToast options 必须是对象');
4016
+ }
4017
+ assertOnlyAllowedKeys(payload, new Set(['message', 'status']), 'ui.showToast');
4018
+ if (typeof payload.message !== 'string') {
4019
+ throw createMockBridgeError('INVALID_PARAMS', 'ui.showToast message 必须是字符串');
4020
+ }
4021
+ const message = payload.message.trim();
4022
+ if (!message) {
4023
+ throw createMockBridgeError('INVALID_PARAMS', 'ui.showToast message 不能为空');
4024
+ }
4025
+ if (message.length > 120) {
4026
+ throw createMockBridgeError('INVALID_PARAMS', 'ui.showToast message 不能超过 120 个字符');
4027
+ }
4028
+ if (payload.status !== undefined && payload.status !== 'success' && payload.status !== 'error') {
4029
+ throw createMockBridgeError('INVALID_PARAMS', 'ui.showToast status 必须是 success 或 error');
4030
+ }
4031
+ return {
4032
+ message,
4033
+ status: payload.status,
4034
+ timestamp: Date.now(),
4035
+ };
4036
+ }
4037
+ function readShowLoadingPayload(payload) {
4038
+ if (payload === undefined) {
4039
+ return {
4040
+ dismissible: false,
4041
+ visible: true,
4042
+ };
4043
+ }
4044
+ if (!isPlainRecord(payload)) {
4045
+ throw createMockBridgeError('INVALID_PARAMS', 'ui.showLoading options 必须是对象');
4046
+ }
4047
+ assertOnlyAllowedKeys(payload, new Set(['dismissible']), 'ui.showLoading');
4048
+ if (payload.dismissible !== undefined && typeof payload.dismissible !== 'boolean') {
4049
+ throw createMockBridgeError('INVALID_PARAMS', 'ui.showLoading dismissible 必须是布尔值');
4050
+ }
4051
+ return {
4052
+ dismissible: payload.dismissible === true,
4053
+ visible: true,
4054
+ };
4055
+ }
4056
+ function readVibratePayload(payload) {
4057
+ if (payload !== undefined && !isPlainRecord(payload)) {
4058
+ throw createMockBridgeError('INVALID_PARAMS', 'device.vibrate options 必须是对象');
4059
+ }
4060
+ if (payload !== undefined) {
4061
+ assertOnlyAllowedKeys(payload, new Set(['intensity', 'delay']), 'device.vibrate');
4062
+ }
4063
+ const intensity = payload?.intensity ?? 'light';
4064
+ if (intensity !== 'light' && intensity !== 'medium' && intensity !== 'heavy') {
4065
+ throw createMockBridgeError('INVALID_PARAMS', 'device.vibrate intensity 必须是 light、medium 或 heavy');
4066
+ }
4067
+ const delay = payload?.delay ?? 0;
4068
+ if (!Number.isInteger(delay) || delay < 0 || delay > 5000) {
4069
+ throw createMockBridgeError('INVALID_PARAMS', 'device.vibrate delay 必须是 0..5000 的整数毫秒');
4070
+ }
4071
+ return {
4072
+ delay,
4073
+ intensity,
4074
+ pattern: createVibratePattern(intensity),
4075
+ timestamp: Date.now(),
4076
+ };
4077
+ }
4078
+ function readSetClipboardPayload(payload) {
4079
+ if (!isPlainRecord(payload)) {
4080
+ throw createMockBridgeError('INVALID_PARAMS', 'device.setClipboard options 必须是对象');
4081
+ }
4082
+ const extraKeys = Object.keys(payload).filter(key => key !== 'text');
4083
+ if (extraKeys.length > 0) {
4084
+ throw createMockBridgeError('INVALID_PARAMS', `device.setClipboard 不支持字段: ${extraKeys.join(', ')}`);
4085
+ }
4086
+ if (typeof payload.text !== 'string') {
4087
+ throw createMockBridgeError('INVALID_PARAMS', 'device.setClipboard text 必须是字符串');
4088
+ }
4089
+ if (!payload.text.trim()) {
4090
+ throw createMockBridgeError('INVALID_PARAMS', 'device.setClipboard text 不能为空');
4091
+ }
4092
+ if (payload.text.length > 10000) {
4093
+ throw createMockBridgeError('INVALID_PARAMS', 'device.setClipboard text 不能超过 10000 个字符');
4094
+ }
4095
+ return payload.text;
4096
+ }
4097
+ function assertEmptyPayload(payload, methodName) {
4098
+ if (payload === undefined) {
4099
+ return;
4100
+ }
4101
+ if (!isPlainRecord(payload) || Object.keys(payload).length > 0) {
4102
+ throw createMockBridgeError('INVALID_PARAMS', `${methodName} 不支持参数`);
4103
+ }
4104
+ }
4105
+ function assertOnlyAllowedKeys(payload, allowedKeys, methodName) {
4106
+ const invalidKeys = Object.keys(payload).filter(key => !allowedKeys.has(key));
4107
+ if (invalidKeys.length > 0) {
4108
+ throw createMockBridgeError('INVALID_PARAMS', `${methodName} 不支持字段: ${invalidKeys.join(', ')}`);
4109
+ }
4110
+ }
4111
+ function createVibratePattern(intensity) {
4112
+ switch (intensity) {
4113
+ case 'heavy':
4114
+ return 40;
4115
+ case 'medium':
4116
+ return 25;
4117
+ case 'light':
4118
+ default:
4119
+ return 10;
4120
+ }
4121
+ }
4122
+ function renderMockToast(toast) {
4123
+ const document = readDocument();
4124
+ if (!document) {
4125
+ return;
4126
+ }
4127
+ const element = findOrCreateMockElement(document, 'hb-sdk-mock-runtime-toast');
4128
+ element.textContent = toast.message;
4129
+ element.dataset.status = toast.status ?? 'default';
4130
+ element.style.cssText = [
4131
+ 'position:fixed',
4132
+ 'left:50%',
4133
+ 'bottom:48px',
4134
+ 'transform:translateX(-50%)',
4135
+ 'z-index:2147483647',
4136
+ 'max-width:80%',
4137
+ 'padding:8px 12px',
4138
+ 'border-radius:6px',
4139
+ 'background:rgba(17,24,39,.92)',
4140
+ 'color:#fff',
4141
+ 'font:13px/1.4 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif',
4142
+ 'box-shadow:0 8px 24px rgba(0,0,0,.18)',
4143
+ 'pointer-events:none',
4144
+ 'white-space:pre-wrap',
4145
+ ].join(';');
4146
+ }
4147
+ function renderMockLoading(state) {
4148
+ const document = readDocument();
4149
+ if (!document) {
4150
+ return;
4151
+ }
4152
+ const element = findOrCreateMockElement(document, 'hb-sdk-mock-runtime-loading');
4153
+ element.hidden = !state.visible;
4154
+ element.dataset.dismissible = String(state.dismissible);
4155
+ element.textContent = '加载中...';
4156
+ element.style.cssText = [
4157
+ 'position:fixed',
4158
+ 'inset:0',
4159
+ 'z-index:2147483646',
4160
+ 'display:flex',
4161
+ 'align-items:center',
4162
+ 'justify-content:center',
4163
+ 'background:rgba(15,23,42,.16)',
4164
+ 'color:#fff',
4165
+ 'font:14px/1.4 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif',
4166
+ 'pointer-events:none',
4167
+ ].join(';');
4168
+ }
4169
+ function reloadMockIframe() {
4170
+ const iframe = readMockIframe();
4171
+ if (!iframe) {
4172
+ return;
4173
+ }
4174
+ setTimeout(() => {
4175
+ iframe.src = iframe.src;
4176
+ }, 0);
4177
+ }
4178
+ function closeMockIframe() {
4179
+ const iframe = readMockIframe();
4180
+ if (!iframe) {
4181
+ return;
4182
+ }
4183
+ iframe.hidden = true;
4184
+ iframe.dataset.hbSdkMockClosed = 'true';
4185
+ const document = readDocument();
4186
+ const parent = iframe.parentElement;
4187
+ if (!document || !parent) {
4188
+ return;
4189
+ }
4190
+ const element = findOrCreateMockElement(document, 'hb-sdk-mock-runtime-closed');
4191
+ element.textContent = '小程序已关闭';
4192
+ element.style.cssText = [
4193
+ 'display:flex',
4194
+ 'align-items:center',
4195
+ 'justify-content:center',
4196
+ 'min-height:240px',
4197
+ 'color:#64748b',
4198
+ 'font:14px/1.4 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif',
4199
+ ].join(';');
4200
+ parent.appendChild(element);
4201
+ }
4202
+ function readMockIframe() {
4203
+ const document = readDocument();
4204
+ return document?.querySelector('iframe') ?? null;
4205
+ }
4206
+ function findOrCreateMockElement(document, id) {
4207
+ const current = document.getElementById(id);
4208
+ if (current) {
4209
+ return current;
4210
+ }
4211
+ const element = document.createElement('div');
4212
+ element.id = id;
4213
+ document.body.appendChild(element);
4214
+ return element;
4215
+ }
4216
+ function readDocument() {
4217
+ return typeof document === 'undefined' ? null : document;
4218
+ }
4219
+ function isPlainRecord(value) {
4220
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
4221
+ }
3847
4222
 
3848
4223
  const MINI_PROGRAM_URL_QUERY_PARAM = 'mini_url';
3849
4224
  const MINI_PROGRAM_DEV_SHELL_URL = 'heybox-mini-dev://sandbox';
package/dist/index.cjs.js CHANGED
@@ -473,6 +473,20 @@ const STORAGE_SET_STORAGE_METHOD = 'storage.setStorage';
473
473
  * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
474
474
  */
475
475
  const NETWORK_REQUEST_METHOD = 'network.request';
476
+ /** 展示 toast 能力方法名。 */
477
+ const UI_SHOW_TOAST_METHOD = 'ui.showToast';
478
+ /** 展示全局 loading 能力方法名。 */
479
+ const UI_SHOW_LOADING_METHOD = 'ui.showLoading';
480
+ /** 隐藏全局 loading 能力方法名。 */
481
+ const UI_HIDE_LOADING_METHOD = 'ui.hideLoading';
482
+ /** 震动反馈能力方法名。 */
483
+ const DEVICE_VIBRATE_METHOD = 'device.vibrate';
484
+ /** 写入文本剪贴板能力方法名。 */
485
+ const DEVICE_SET_CLIPBOARD_METHOD = 'device.setClipboard';
486
+ /** 关闭当前小程序容器能力方法名。 */
487
+ const NAVIGATION_CLOSE_METHOD = 'navigation.close';
488
+ /** 重载当前小程序容器能力方法名。 */
489
+ const NAVIGATION_RELOAD_METHOD = 'navigation.reload';
476
490
 
477
491
  /**
478
492
  * 唤起登录授权,并在流程返回后刷新用户公开基础资料。
@@ -728,6 +742,59 @@ function createUserModule(requester) {
728
742
  };
729
743
  }
730
744
 
745
+ /** 展示 toast。 */
746
+ function showToast(requester, options) {
747
+ return requester.request(UI_SHOW_TOAST_METHOD, options);
748
+ }
749
+ /** 展示全局 loading。 */
750
+ function showLoading(requester, options) {
751
+ return requester.request(UI_SHOW_LOADING_METHOD, options ?? {});
752
+ }
753
+ /** 隐藏全局 loading。 */
754
+ function hideLoading(requester) {
755
+ return requester.request(UI_HIDE_LOADING_METHOD);
756
+ }
757
+ /** 创建 UI 模块。 */
758
+ function createUiModule(requester) {
759
+ return {
760
+ showToast: options => showToast(requester, options),
761
+ showLoading: options => showLoading(requester, options),
762
+ hideLoading: () => hideLoading(requester),
763
+ };
764
+ }
765
+
766
+ /** 触发宿主震动反馈。 */
767
+ function vibrate(requester, options) {
768
+ return requester.request(DEVICE_VIBRATE_METHOD, options ?? {});
769
+ }
770
+ /** 写入系统文本剪贴板。 */
771
+ function setClipboard(requester, options) {
772
+ return requester.request(DEVICE_SET_CLIPBOARD_METHOD, options);
773
+ }
774
+ /** 创建设备能力模块。 */
775
+ function createDeviceModule(requester) {
776
+ return {
777
+ vibrate: options => vibrate(requester, options),
778
+ setClipboard: options => setClipboard(requester, options),
779
+ };
780
+ }
781
+
782
+ /** 请求宿主关闭当前小程序容器。 */
783
+ function close(requester) {
784
+ return requester.request(NAVIGATION_CLOSE_METHOD);
785
+ }
786
+ /** 请求宿主重载当前小程序容器。 */
787
+ function reload(requester) {
788
+ return requester.request(NAVIGATION_RELOAD_METHOD);
789
+ }
790
+ /** 创建导航能力模块。 */
791
+ function createNavigationModule(requester) {
792
+ return {
793
+ close: () => close(requester),
794
+ reload: () => reload(requester),
795
+ };
796
+ }
797
+
731
798
  /**
732
799
  * 外部小程序 SDK 实例。
733
800
  *
@@ -763,6 +830,12 @@ class MiniProgramSDK {
763
830
  storage;
764
831
  /** 网络请求相关开放能力。 */
765
832
  network;
833
+ /** UI 交互相关开放能力。 */
834
+ ui;
835
+ /** 设备相关开放能力。 */
836
+ device;
837
+ /** 导航与容器控制相关开放能力。 */
838
+ navigation;
766
839
  constructor(options) {
767
840
  this.client = new MiniProgramBridgeClient(options);
768
841
  this.auth = createAuthModule(this.client);
@@ -771,6 +844,9 @@ class MiniProgramSDK {
771
844
  this.viewport = createViewportModule(this.client);
772
845
  this.storage = createStorageModule(this.client);
773
846
  this.network = createNetworkModule(this.client);
847
+ this.ui = createUiModule(this.client);
848
+ this.device = createDeviceModule(this.client);
849
+ this.navigation = createNavigationModule(this.client);
774
850
  }
775
851
  /**
776
852
  * 等待 SDK 与父容器完成握手。
@@ -896,6 +972,22 @@ const storage = {
896
972
  const network = {
897
973
  request: config => getDefaultSDK().network.request(config),
898
974
  };
975
+ /** 默认 SDK 实例的 UI 模块。 */
976
+ const ui = {
977
+ showToast: options => getDefaultSDK().ui.showToast(options),
978
+ showLoading: options => getDefaultSDK().ui.showLoading(options),
979
+ hideLoading: () => getDefaultSDK().ui.hideLoading(),
980
+ };
981
+ /** 默认 SDK 实例的 device 模块。 */
982
+ const device = {
983
+ vibrate: options => getDefaultSDK().device.vibrate(options),
984
+ setClipboard: options => getDefaultSDK().device.setClipboard(options),
985
+ };
986
+ /** 默认 SDK 实例的 navigation 模块。 */
987
+ const navigation = {
988
+ close: () => getDefaultSDK().navigation.close(),
989
+ reload: () => getDefaultSDK().navigation.reload(),
990
+ };
899
991
 
900
992
  const hbSDK = {
901
993
  ready,
@@ -907,6 +999,9 @@ const hbSDK = {
907
999
  viewport,
908
1000
  storage,
909
1001
  network,
1002
+ ui,
1003
+ device,
1004
+ navigation,
910
1005
  };
911
1006
 
912
1007
  exports.HbMiniProgramNetworkError = HbMiniProgramNetworkError;
@@ -915,11 +1010,14 @@ exports.MiniProgramSDK = MiniProgramSDK;
915
1010
  exports.auth = auth;
916
1011
  exports.createMiniProgramSDK = createMiniProgramSDK;
917
1012
  exports.default = hbSDK;
1013
+ exports.device = device;
1014
+ exports.navigation = navigation;
918
1015
  exports.network = network;
919
1016
  exports.off = off;
920
1017
  exports.on = on;
921
1018
  exports.ready = ready;
922
1019
  exports.share = share;
923
1020
  exports.storage = storage;
1021
+ exports.ui = ui;
924
1022
  exports.user = user;
925
1023
  exports.viewport = viewport;
package/dist/index.esm.js CHANGED
@@ -469,6 +469,20 @@ const STORAGE_SET_STORAGE_METHOD = 'storage.setStorage';
469
469
  * 供 SDK 与父容器 runtime 共享同一 bridge method 标识。
470
470
  */
471
471
  const NETWORK_REQUEST_METHOD = 'network.request';
472
+ /** 展示 toast 能力方法名。 */
473
+ const UI_SHOW_TOAST_METHOD = 'ui.showToast';
474
+ /** 展示全局 loading 能力方法名。 */
475
+ const UI_SHOW_LOADING_METHOD = 'ui.showLoading';
476
+ /** 隐藏全局 loading 能力方法名。 */
477
+ const UI_HIDE_LOADING_METHOD = 'ui.hideLoading';
478
+ /** 震动反馈能力方法名。 */
479
+ const DEVICE_VIBRATE_METHOD = 'device.vibrate';
480
+ /** 写入文本剪贴板能力方法名。 */
481
+ const DEVICE_SET_CLIPBOARD_METHOD = 'device.setClipboard';
482
+ /** 关闭当前小程序容器能力方法名。 */
483
+ const NAVIGATION_CLOSE_METHOD = 'navigation.close';
484
+ /** 重载当前小程序容器能力方法名。 */
485
+ const NAVIGATION_RELOAD_METHOD = 'navigation.reload';
472
486
 
473
487
  /**
474
488
  * 唤起登录授权,并在流程返回后刷新用户公开基础资料。
@@ -724,6 +738,59 @@ function createUserModule(requester) {
724
738
  };
725
739
  }
726
740
 
741
+ /** 展示 toast。 */
742
+ function showToast(requester, options) {
743
+ return requester.request(UI_SHOW_TOAST_METHOD, options);
744
+ }
745
+ /** 展示全局 loading。 */
746
+ function showLoading(requester, options) {
747
+ return requester.request(UI_SHOW_LOADING_METHOD, options ?? {});
748
+ }
749
+ /** 隐藏全局 loading。 */
750
+ function hideLoading(requester) {
751
+ return requester.request(UI_HIDE_LOADING_METHOD);
752
+ }
753
+ /** 创建 UI 模块。 */
754
+ function createUiModule(requester) {
755
+ return {
756
+ showToast: options => showToast(requester, options),
757
+ showLoading: options => showLoading(requester, options),
758
+ hideLoading: () => hideLoading(requester),
759
+ };
760
+ }
761
+
762
+ /** 触发宿主震动反馈。 */
763
+ function vibrate(requester, options) {
764
+ return requester.request(DEVICE_VIBRATE_METHOD, options ?? {});
765
+ }
766
+ /** 写入系统文本剪贴板。 */
767
+ function setClipboard(requester, options) {
768
+ return requester.request(DEVICE_SET_CLIPBOARD_METHOD, options);
769
+ }
770
+ /** 创建设备能力模块。 */
771
+ function createDeviceModule(requester) {
772
+ return {
773
+ vibrate: options => vibrate(requester, options),
774
+ setClipboard: options => setClipboard(requester, options),
775
+ };
776
+ }
777
+
778
+ /** 请求宿主关闭当前小程序容器。 */
779
+ function close(requester) {
780
+ return requester.request(NAVIGATION_CLOSE_METHOD);
781
+ }
782
+ /** 请求宿主重载当前小程序容器。 */
783
+ function reload(requester) {
784
+ return requester.request(NAVIGATION_RELOAD_METHOD);
785
+ }
786
+ /** 创建导航能力模块。 */
787
+ function createNavigationModule(requester) {
788
+ return {
789
+ close: () => close(requester),
790
+ reload: () => reload(requester),
791
+ };
792
+ }
793
+
727
794
  /**
728
795
  * 外部小程序 SDK 实例。
729
796
  *
@@ -759,6 +826,12 @@ class MiniProgramSDK {
759
826
  storage;
760
827
  /** 网络请求相关开放能力。 */
761
828
  network;
829
+ /** UI 交互相关开放能力。 */
830
+ ui;
831
+ /** 设备相关开放能力。 */
832
+ device;
833
+ /** 导航与容器控制相关开放能力。 */
834
+ navigation;
762
835
  constructor(options) {
763
836
  this.client = new MiniProgramBridgeClient(options);
764
837
  this.auth = createAuthModule(this.client);
@@ -767,6 +840,9 @@ class MiniProgramSDK {
767
840
  this.viewport = createViewportModule(this.client);
768
841
  this.storage = createStorageModule(this.client);
769
842
  this.network = createNetworkModule(this.client);
843
+ this.ui = createUiModule(this.client);
844
+ this.device = createDeviceModule(this.client);
845
+ this.navigation = createNavigationModule(this.client);
770
846
  }
771
847
  /**
772
848
  * 等待 SDK 与父容器完成握手。
@@ -892,6 +968,22 @@ const storage = {
892
968
  const network = {
893
969
  request: config => getDefaultSDK().network.request(config),
894
970
  };
971
+ /** 默认 SDK 实例的 UI 模块。 */
972
+ const ui = {
973
+ showToast: options => getDefaultSDK().ui.showToast(options),
974
+ showLoading: options => getDefaultSDK().ui.showLoading(options),
975
+ hideLoading: () => getDefaultSDK().ui.hideLoading(),
976
+ };
977
+ /** 默认 SDK 实例的 device 模块。 */
978
+ const device = {
979
+ vibrate: options => getDefaultSDK().device.vibrate(options),
980
+ setClipboard: options => getDefaultSDK().device.setClipboard(options),
981
+ };
982
+ /** 默认 SDK 实例的 navigation 模块。 */
983
+ const navigation = {
984
+ close: () => getDefaultSDK().navigation.close(),
985
+ reload: () => getDefaultSDK().navigation.reload(),
986
+ };
895
987
 
896
988
  const hbSDK = {
897
989
  ready,
@@ -903,6 +995,9 @@ const hbSDK = {
903
995
  viewport,
904
996
  storage,
905
997
  network,
998
+ ui,
999
+ device,
1000
+ navigation,
906
1001
  };
907
1002
 
908
- export { HbMiniProgramNetworkError, HbMiniProgramSDKError, MiniProgramSDK, auth, createMiniProgramSDK, hbSDK as default, network, off, on, ready, share, storage, user, viewport };
1003
+ export { HbMiniProgramNetworkError, HbMiniProgramSDKError, MiniProgramSDK, auth, createMiniProgramSDK, hbSDK as default, device, navigation, network, off, on, ready, share, storage, ui, user, viewport };