diff --git a/src/main/ets/ContextConfig.ets b/src/main/ets/ContextConfig.ets index 552d3a9..7425757 100644 --- a/src/main/ets/ContextConfig.ets +++ b/src/main/ets/ContextConfig.ets @@ -28,7 +28,10 @@ export class GlobalContext { static popUIContext(): void { globalThis._uiUIContext.splice(globalThis._uiUIContext.length-1,1) } - static getUiContext(): UIContext { + static getUiContext(): UIContext|undefined { + if (globalThis._uiUIContext === undefined) { + return undefined; + } return globalThis._uiUIContext[globalThis._uiUIContext.length-1]; } } diff --git a/src/main/ets/utils/ToolsHelper.ets b/src/main/ets/utils/ToolsHelper.ets index 552abe8..e3f85ef 100644 --- a/src/main/ets/utils/ToolsHelper.ets +++ b/src/main/ets/utils/ToolsHelper.ets @@ -295,7 +295,7 @@ export class ToolsHelper { static closeAlertDialog(dialogTag: string) { if (ToolsHelper.mapAlertDialog.get(dialogTag)) { try { - GlobalContext.getUiContext().getPromptAction().closeCustomDialog(ToolsHelper.mapAlertDialog.get(dialogTag)) + GlobalContext.getUiContext()?.getPromptAction().closeCustomDialog(ToolsHelper.mapAlertDialog.get(dialogTag)) } catch (err) { } ToolsHelper.mapAlertDialog.remove(dialogTag) @@ -309,34 +309,45 @@ export class ToolsHelper { static showAlertDialog(options: AlertOptions) { const dialogTag = ToolsHelper.getUuid() const ui = GlobalContext.getUiContext() - const c = new ComponentContent(ui, wrapBuilder(alertDialogBuilder), - new AlertBean({ title: options.title, msg: options.msg, confirm: options.action }, dialogTag)) - ui.getPromptAction().openCustomDialog(c, { - autoCancel: false - }).then(() => { - ToolsHelper.mapAlertDialog.set(dialogTag, c) - }).catch(() => { - try { - promptAction.showDialog({ - alignment: DialogAlignment.Center, - title: options.title, - message: options.msg, - buttons: [{ - text: options.action?.text ?? "确定", - color: options.action?.color ?? "#000000", - }] + if (ui) { + const c = new ComponentContent(ui, wrapBuilder(alertDialogBuilder), + new AlertBean({ title: options.title, msg: options.msg, confirm: options.action }, dialogTag)) + ui.getPromptAction().openCustomDialog(c, { + autoCancel: false + }).then(() => { + ToolsHelper.mapAlertDialog.set(dialogTag, c) + }).catch(() => { + ToolsHelper.showCommonAlert(options) + }) + } else { + ToolsHelper.showCommonAlert(options) + } + } + + private static showCommonAlert(options: AlertOptions) { + try { + promptAction.showDialog({ + alignment: DialogAlignment.Center, + title: options.title, + message: options.msg, + buttons: [{ + text: options.action?.text ?? "确定", + color: options.action?.color ?? "#000000", + }] + }) + .then(() => { + options.action?.onClick && options.action?.onClick() }) - .then(() => { - options.action?.onClick && options.action?.onClick() - }) - .catch((err: Error) => { + .catch((err: Error) => { + if (err.message === 'cancel') { + } else { ToolsHelper.showMessage(err.message) - }) - } catch (error) { - let message = (error as BusinessError).message - ToolsHelper.showMessage(message) - } - }) + } + }) + } catch (error) { + let message = (error as BusinessError).message + ToolsHelper.showMessage(message) + } } /** @@ -347,49 +358,61 @@ export class ToolsHelper { const dialogTag = ToolsHelper.getUuid() const ui = GlobalContext.getUiContext() - const c = new ComponentContent(ui, wrapBuilder(alertDialogBuilder), - new AlertBean({ - title: options.title, - msg: options.msg, - cancel: options.cancel ?? {}, - confirm: options.confirm - }, dialogTag)) - ui.getPromptAction().openCustomDialog(c, { - autoCancel: false - }).then(() => { - ToolsHelper.mapAlertDialog.set(dialogTag, c) - }).catch(() => { - try { - promptAction.showDialog({ - alignment: 1, + if (ui) { + const c = new ComponentContent(ui, wrapBuilder(alertDialogBuilder), + new AlertBean({ title: options.title, - message: options.msg, - buttons: [{ - text: options.cancel?.text ?? "取消", - color: options.cancel?.color ?? "#666666", - }, { - text: options.confirm?.text ?? "确定", - color: options.confirm?.color ?? "#000000", - },] - }) - .then((data) => { - if (data.index === 0) { - options.cancel?.onClick && options.cancel.onClick() - } else { - options.confirm?.onClick && options.confirm.onClick() - } - }) - .catch((err: Error) => { - ToolsHelper.showMessage(err.message) - }) - } catch (error) { - let message = (error as BusinessError).message - ToolsHelper.showMessage(message) - } - }) + msg: options.msg, + cancel: options.cancel ?? {}, + confirm: options.confirm + }, dialogTag)) + ui.getPromptAction().openCustomDialog(c, { + autoCancel: false + }).then(() => { + ToolsHelper.mapAlertDialog.set(dialogTag, c) + }).catch(() => { + ToolsHelper.showCommonConfirm(options) + }) + } else { + ToolsHelper.showCommonConfirm(options) + } } + static showCommonConfirm(options: ConfirmOptions) { + try { + promptAction.showDialog({ + alignment: 1, + title: options.title, + message: options.msg, + buttons: [{ + text: options.cancel?.text ?? "取消", + color: options.cancel?.color ?? "#666666", + }, { + text: options.confirm?.text ?? "确定", + color: options.confirm?.color ?? "#000000", + },] + }) + .then((data) => { + if (data.index === 0) { + options.cancel?.onClick && options.cancel.onClick() + } else { + options.confirm?.onClick && options.confirm.onClick() + } + }) + .catch((err: Error) => { + if (err.message === 'cancel') { + options.cancel?.onClick && options.cancel.onClick() + } else { + ToolsHelper.showMessage(err.message) + } + }) + } catch (error) { + let message = (error as BusinessError).message + ToolsHelper.showMessage(message) + } + } + public static mapDialog = new HashMap() public static mapAlertDialog = new HashMap>() diff --git a/src/main/ets/view/SafeView.ets b/src/main/ets/view/SafeView.ets index 80d2c70..2596e40 100644 --- a/src/main/ets/view/SafeView.ets +++ b/src/main/ets/view/SafeView.ets @@ -30,7 +30,7 @@ export struct SafeView { @Prop hideBack: boolean // 是否显示浅色返回按钮 @Prop lightBack: boolean - // 是否显示返回按钮 + // 是否显示导航栏 @Prop hideNavBar: boolean /** * 是否显示左侧角标,存在onClickLeft时生效 @@ -42,7 +42,7 @@ export struct SafeView { @Prop showBadgeRight: boolean; // 设置返回按钮事件,如果hideBack为true,该事件无效 onBackEvent?: () => void - // 设置返回按钮事件 + // 设置导航栏点击事件 @Prop onClickLeft: TitleBarBtn @Prop onClickRight: TitleBarBtn // 设置导航栏底下标题