diff --git a/src/main/ets/http/HttpHelper.ts b/src/main/ets/http/HttpHelper.ts index dc9ac7f..52aa5bb 100644 --- a/src/main/ets/http/HttpHelper.ts +++ b/src/main/ets/http/HttpHelper.ts @@ -17,7 +17,7 @@ type HttpParamsPost = { } type HttpParamsForm = { url: string - data?: Record | Object + data?: Record | Object|undefined query?: Record | Object headers?: Record } @@ -185,7 +185,7 @@ export class HttpHelper { connectTimeout: 20000, readTimeout: 20000, header: header, - extraData: encodeURI(data) + extraData: data?encodeURI(data):undefined }) .then((data: http.HttpResponse) => { console.info(`=====>Result:${data.result as string}(${apiNo})`); @@ -324,7 +324,10 @@ export class HttpHelper { return u } - private getContent(data: Record | Object) { + private getContent(data?: Record | Object) { + if (!data) { + return undefined + } let u = '' let q = data if (typeof data === 'object') { diff --git a/src/main/ets/utils/ToolsHelper.ets b/src/main/ets/utils/ToolsHelper.ets index 211351b..c262352 100644 --- a/src/main/ets/utils/ToolsHelper.ets +++ b/src/main/ets/utils/ToolsHelper.ets @@ -12,8 +12,8 @@ export interface Btn { } export interface AlertOptions { - title?: string - msg?: string + title?: string, + msg?: string, action?: Btn } @@ -37,7 +37,7 @@ interface ListItem { } @Builder -function customDialogBuilder(option: ListOptions, dialogId: number) { +function customDialogBuilder(option: ListOptions, dialogTag: string) { Column() { Text(option.title) @@ -58,9 +58,9 @@ function customDialogBuilder(option: ListOptions, dialogId: number) { .fontSize(16) .textAlign(TextAlign.Center) .onClick(() => { - if (ToolsHelper.mapDialog.get(dialogId)) { - promptAction.closeCustomDialog(ToolsHelper.mapDialog.get(dialogId)) - ToolsHelper.mapDialog.remove(dialogId) + if (ToolsHelper.mapDialog.get(dialogTag)) { + promptAction.closeCustomDialog(ToolsHelper.mapDialog.get(dialogTag)) + ToolsHelper.mapDialog.remove(dialogTag) } option.onSelected(index, item) }) @@ -149,22 +149,19 @@ export class ToolsHelper { * 弹出Alert弹窗 * @param options */ - static showAlertDialog( - title?: string, - msg?: string, - action?: Btn) { + static showAlertDialog(options: AlertOptions) { try { promptAction.showDialog({ - alignment: 1, - title: title, - message: msg, + alignment: DialogAlignment.Center, + title: options.title, + message: options.msg, buttons: [{ - text: action?.text ?? "确定", - color: action?.color ?? "#000000", + text: options.action?.text ?? "确定", + color: options.action?.color ?? "#000000", }] }) .then(() => { - action?.onClick && action?.onClick() + options.action?.onClick && options.action?.onClick() }) .catch((err: Error) => { ToolsHelper.showMessage(err.message) @@ -209,7 +206,7 @@ export class ToolsHelper { } } - public static mapDialog = new HashMap() + public static mapDialog = new HashMap() /** * 弹出List弹窗 @@ -228,7 +225,7 @@ export class ToolsHelper { if (isSuccess.length > 0) { options.onError && options.onError(`第(${isSuccess.join("、")})个数据中,没有content字段。`) } else { - const dialogTag = new Date().getTime() + const dialogTag = ToolsHelper.getUuid() promptAction.openCustomDialog({ alignment: 1, builder: customDialogBuilder.bind(p, options, dialogTag) @@ -242,8 +239,7 @@ export class ToolsHelper { * 弹出自定义弹窗 * @param alignment 弹窗在竖直方向上的对齐方式 */ - static showCustomDialog(b: CustomBuilder, alignment?: DialogAlignment) { - const dialogTag = new Date().getTime() + static showCustomDialog(dialogTag: string, b: CustomBuilder, alignment?: DialogAlignment) { promptAction.openCustomDialog({ alignment: alignment ?? DialogAlignment.Center, builder: b @@ -254,6 +250,17 @@ export class ToolsHelper { }) } + /** + * 关闭自定义弹窗 + * @param dialogTag 开启时的tag + */ + static closeCustomDialog(dialogTag: string) { + if (ToolsHelper.mapDialog.get(dialogTag)) { + promptAction.closeCustomDialog(ToolsHelper.mapDialog.get(dialogTag)) + ToolsHelper.mapDialog.remove(dialogTag) + } + } + /** * 获取调用栈第一个类 */ @@ -314,7 +321,7 @@ export class ToolsHelper { private static setTimeOutMap: Map = new Map() private static uniqueIdMap = new WeakMap(); - private static getUuid() { + public static getUuid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { let r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8; return v.toString(16);