ソースを参照

Merge remote-tracking branch 'origin/master'

徐勤民 2 ヶ月 前
コミット
79d69e0350
2 ファイル変更34 行追加24 行削除
  1. 6 3
      src/main/ets/http/HttpHelper.ts
  2. 28 21
      src/main/ets/utils/ToolsHelper.ets

+ 6 - 3
src/main/ets/http/HttpHelper.ts

@@ -17,7 +17,7 @@ type HttpParamsPost = {
 }
 type HttpParamsForm = {
   url: string
-  data?: Record<string, string> | Object
+  data?: Record<string, string> | Object|undefined
   query?: Record<string, string> | Object
   headers?: Record<string, string>
 }
@@ -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<string, string> | Object) {
+  private getContent(data?: Record<string, string> | Object) {
+    if (!data) {
+      return undefined
+    }
     let u = ''
     let q = data
     if (typeof data === 'object') {

+ 28 - 21
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<T>(option: ListOptions<T>, dialogId: number) {
+function customDialogBuilder<T>(option: ListOptions<T>, dialogTag: string) {
 
   Column() {
     Text(option.title)
@@ -58,9 +58,9 @@ function customDialogBuilder<T>(option: ListOptions<T>, 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<number, number>()
+  public static mapDialog = new HashMap<string, number>()
 
   /**
    * 弹出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<string, ThrottleInterface> = new Map()
   private static uniqueIdMap = new WeakMap<Function, string>();
 
-  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);