Prechádzať zdrojové kódy

添加更新日志弹窗

chendeben 1 rok pred
rodič
commit
f0846a3ce6

+ 0 - 21
entry/src/main/ets/common/constants/UpdateConstants.ets

@@ -7,23 +7,7 @@ export class UpdateConstants {
    * 可以根据需要修改为你的更新日志地址
    */
   static readonly UPDATE_LOG_URL: string = 'https://static.ss5.xyz/files/update_log.html';
-  /**
-   * 备用更新日志URL(当主URL无法访问时使用)
-   */
-  static readonly BACKUP_UPDATE_LOG_URL: string = 'https://gitee.com/your-repo/raw/master/CHANGELOG.md';
-  /**
-   * 本地更新日志文件路径(可选,用于离线显示)
-   */
-  static readonly LOCAL_CHANGELOG_PATH: string = 'rawfile/changelog.html';
-  /**
-   * 更新日志缓存时间(毫秒)
-   */
-  static readonly CACHE_DURATION: number = 24 * 60 * 60 * 1000; // 24小时
 
-  /**
-   * 网络请求超时时间(毫秒)
-   */
-  static readonly REQUEST_TIMEOUT: number = 10000; // 10秒
 
   /**
    * 是否启用更新日志功能
@@ -37,11 +21,6 @@ export class UpdateConstants {
    * 更新日志弹窗显示延迟时间(毫秒)
    */
   static readonly DIALOG_SHOW_DELAY: number = 1500;
-  /**
-   * 应用版本号(手动维护,每次更新时修改此值)
-   * 这是一个简单可靠的版本管理方案
-   */
-  static readonly APP_VERSION: string = '2.1.0';
   /**
    * 构建时间戳(用于区分不同的构建版本)
    */

+ 7 - 46
entry/src/main/ets/common/util/UpdateLogManager.ets

@@ -2,13 +2,13 @@
  * 更新日志管理器
  * 负责管理更新日志的显示逻辑、版本检查等功能
  */
-import { PreferencesUtil } from '@pura/harmony-utils';
+import { AppUtil, PreferencesUtil } from '@pura/harmony-utils';
 import { UpdateConstants } from '../constants/UpdateConstants';
 import Logger from './Logger';
 
 export class UpdateLogManager {
   private static readonly TAG = 'UpdateLogManager';
-  
+
   /**
    * 检查并显示更新日志
    * @returns Promise<boolean> 是否显示了更新日志
@@ -42,20 +42,20 @@ export class UpdateLogManager {
     try {
       const currentVersion = UpdateLogManager.getCurrentVersion();
       const lastShownVersion = PreferencesUtil.getStringSync('last_shown_update_version', '');
-      
+
       // 如果配置为每次启动都检查
       if (UpdateConstants.CHECK_ON_EVERY_LAUNCH) {
         Logger.info(UpdateLogManager.TAG, '配置为每次启动都显示更新日志');
         return true;
       }
-      
+
       // 检查版本是否更新
       const isVersionUpdated = lastShownVersion !== currentVersion;
-      
+
       if (isVersionUpdated) {
         Logger.info(UpdateLogManager.TAG, `检测到版本更新:${lastShownVersion} -> ${currentVersion}`);
       }
-      
+
       return isVersionUpdated;
     } catch (error) {
       Logger.error(UpdateLogManager.TAG, '检查更新日志显示条件失败:' + error);
@@ -68,8 +68,7 @@ export class UpdateLogManager {
    */
   private static getCurrentVersion(): string {
     try {
-      // 使用配置中的版本号,这是最可靠的方案
-      return UpdateConstants.APP_VERSION;
+      return AppUtil.getVersionName();
     } catch (error) {
       Logger.error(UpdateLogManager.TAG, '获取应用版本号失败:' + error);
       return '1.0.0';
@@ -101,43 +100,5 @@ export class UpdateLogManager {
     }
   }
 
-  /**
-   * 获取更新日志URL
-   */
-  public static getUpdateLogUrl(): string {
-    const currentVersion = UpdateLogManager.getCurrentVersion();
-    
-    // 使用配置的URL
-    if (UpdateConstants.UPDATE_LOG_URL) {
-      Logger.info(UpdateLogManager.TAG, `使用配置的URL: ${UpdateConstants.UPDATE_LOG_URL}`);
-      return `${UpdateConstants.UPDATE_LOG_URL}?version=${currentVersion}&t=${Date.now()}`;
-    }
-
-    // 使用备用URL
-    if (UpdateConstants.BACKUP_UPDATE_LOG_URL) {
-      Logger.info(UpdateLogManager.TAG, `使用备用URL: ${UpdateConstants.BACKUP_UPDATE_LOG_URL}`);
-      return UpdateConstants.BACKUP_UPDATE_LOG_URL;
-    }
 
-    // 最后使用本地HTML文件
-    const localUrl = `resource://rawfile/simple-changelog.html`;
-    Logger.info(UpdateLogManager.TAG, `使用本地HTML文件: ${localUrl}`);
-    return localUrl;
-  }
-
-  /**
-   * 获取备用URL(网络失败时使用)
-   */
-  public static getBackupUrl(): string {
-    return `resource://rawfile/simple-changelog.html`;
-  }
-
-  /**
-   * 检查网络连接状态
-   */
-  private static async checkNetworkConnection(): Promise<boolean> {
-    // 这里可以添加网络连接检查逻辑
-    // 暂时返回true,实际项目中可以根据需要实现
-    return true;
-  }
 }

+ 231 - 46
entry/src/main/ets/dialog/OnlineUpdateLogDialog.ets

@@ -6,12 +6,15 @@ import { webview } from '@kit.ArkWeb';
 import { UpdateConstants } from '../common/constants/UpdateConstants';
 import { UpdateLogManager } from '../common/util/UpdateLogManager';
 import { ConfigurationConstant } from '@kit.AbilityKit';
-import { AppUtil } from '@pura/harmony-utils';
+import { AppUtil, PreferencesUtil } from '@pura/harmony-utils';
+import { CommonConstants } from '../common/constants/CommonConstants';
 
 @CustomDialog
 export default struct OnlineUpdateLogDialog {
   /** 弹窗控制器 */
-  controller: CustomDialogController;
+  controller?: CustomDialogController;
+  /** 关闭回调 */
+  onClose?: () => void;
   /** 更新日志URL */
   @State updateLogUrl: string = '';
   /** 当前应用版本 */
@@ -29,17 +32,31 @@ export default struct OnlineUpdateLogDialog {
   /** 深色模式 */
   @State isDarkMode: boolean = false;
   @StorageProp('currentColorMode') currentMode: number = ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT;
+  @StorageProp('themeColor') themeColor: string = CommonConstants.DEFAULT_THEME_COLOR;
 
   aboutToAppear() {
-    // 获取当前应用版本
-    this.getCurrentVersion();
-    // 设置更新日志URL - 直接使用在线URL
-    this.updateLogUrl = UpdateConstants.UPDATE_LOG_URL;
-    // 检查深色模式
-    this.isDarkMode = this.currentMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK;
-    
-    console.info('OnlineUpdateLogDialog URL:', this.updateLogUrl);
-    console.info('OnlineUpdateLogDialog 开始加载在线更新日志');
+    try {
+      // 获取当前应用版本
+      this.getCurrentVersion();
+      // 设置更新日志URL - 直接使用在线URL
+      this.updateLogUrl = UpdateConstants.UPDATE_LOG_URL;
+      // 检查深色模式
+      this.isDarkMode = this.currentMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK;
+      let themeColor = PreferencesUtil.getStringSync('THEME_COLOR', CommonConstants.DEFAULT_THEME_COLOR);
+      AppStorage.setOrCreate('themeColor', themeColor);
+      this.themeColor = themeColor
+      console.info('OnlineUpdateLogDialog URL:', this.updateLogUrl);
+      console.info('OnlineUpdateLogDialog 开始加载在线更新日志');
+      
+      // 确保WebView控制器已初始化
+      if (!this.webViewController) {
+        this.webViewController = new webview.WebviewController();
+      }
+    } catch (error) {
+      console.error('OnlineUpdateLogDialog aboutToAppear error:', error);
+      this.errorMessage = '初始化失败';
+      this.isLoading = false;
+    }
   }
 
   /**
@@ -57,7 +74,57 @@ export default struct OnlineUpdateLogDialog {
    * 记录已显示的版本,避免重复显示
    */
   private markVersionShown() {
-    UpdateLogManager.markCurrentVersionShown();
+    try {
+      UpdateLogManager.markCurrentVersionShown();
+    } catch (error) {
+      console.error('OnlineUpdateLogDialog markVersionShown error:', error);
+    }
+  }
+
+  /**
+   * 颜色变亮工具方法
+   */
+  private lightenColor(color: string, amount: number): string {
+    try {
+      // 移除 # 号
+      const hex = color.replace('#', '');
+      
+      // 转换为 RGB
+      const r = parseInt(hex.substring(0, 2), 16);
+      const g = parseInt(hex.substring(2, 4), 16);
+      const b = parseInt(hex.substring(4, 6), 16);
+      
+      // 增加亮度
+      const newR = Math.min(255, Math.floor(r + (255 - r) * amount));
+      const newG = Math.min(255, Math.floor(g + (255 - g) * amount));
+      const newB = Math.min(255, Math.floor(b + (255 - b) * amount));
+      
+      // 转换回十六进制
+      const newHex = '#' + 
+        newR.toString(16).padStart(2, '0') +
+        newG.toString(16).padStart(2, '0') +
+        newB.toString(16).padStart(2, '0');
+      
+      return newHex;
+    } catch (error) {
+      console.error('lightenColor error:', error);
+      return color; // 出错时返回原色
+    }
+  }
+
+  /**
+   * 组件销毁时清理资源
+   */
+  aboutToDisappear() {
+    try {
+      console.info('OnlineUpdateLogDialog aboutToDisappear');
+      // 清理WebView控制器
+      if (this.webViewController) {
+        // 这里可以添加WebView的清理逻辑,如果需要的话
+      }
+    } catch (error) {
+      console.error('OnlineUpdateLogDialog aboutToDisappear error:', error);
+    }
   }
 
   build() {
@@ -77,66 +144,142 @@ export default struct OnlineUpdateLogDialog {
           .borderRadius(4)
           .backgroundColor('rgba(255,255,255,0.2)')
           .onClick(() => {
-            this.markVersionShown();
-            this.controller.close();
+            try {
+              this.markVersionShown();
+              if (this.onClose) {
+                this.onClose();
+              } else {
+                this.controller?.close();
+              }
+            } catch (error) {
+              console.error('OnlineUpdateLogDialog 关闭弹窗失败:', error);
+            }
           })
       }
       .width('100%')
       .height(48)
       .padding({ left: 20, right: 20 })
-      .backgroundColor('#2196F3')
+      .linearGradient(this.isDarkMode ? {
+        direction: GradientDirection.Right,
+        colors: [
+          ['#404040', 0.0],
+          ['#4A4A4A', 1.0]
+        ]
+      } : {
+        direction: GradientDirection.Right,
+        colors: [
+          [this.themeColor, 0.0],
+          [this.lightenColor(this.themeColor, 0.1), 1.0]
+        ]
+      })
       .justifyContent(FlexAlign.SpaceBetween)
       .alignItems(VerticalAlign.Center)
+      .borderRadius({
+        topLeft: 12,
+        topRight: 12,
+        bottomLeft: 0,
+        bottomRight: 0
+      })
 
       // 版本信息和进度条
       Column() {
-        Text(`当前版本:v${this.currentVersion}`)
-          .fontSize(12)
-          .fontColor(Color.Gray)
-          .alignSelf(ItemAlign.Start)
-          .margin({ bottom: 8 })
+        Row() {
+          Text('当前版本:')
+            .fontSize(12)
+            .fontColor(this.isDarkMode ? '#E0E0E0' : Color.Gray)
+          
+          Text(`v${this.currentVersion}`)
+            .fontSize(12)
+            .fontColor(this.themeColor)
+            .fontWeight(FontWeight.Medium)
+        }
+        .alignSelf(ItemAlign.Start)
+        .margin({ bottom: 8 })
 
-        // 进度条 - 参考WebIndex.ets的实现
+        // 进度条 - 使用主题色
         if (this.progressVisible && this.progressValue < 100) {
-          Progress({ value: this.progressValue, total: 100, type: ProgressType.Linear })
-            .width('100%')
-            .height(3)
-            .color('#2196F3')
-            .backgroundColor('#E0E0E0')
+          Column() {
+            Progress({ value: this.progressValue, total: 100, type: ProgressType.Linear })
+              .width('100%')
+              .height(4)
+              .color(this.themeColor)
+              .backgroundColor(this.isDarkMode ? '#4A4A4A' : '#E0E0E0')
+              .borderRadius(2)
+            
+            Text(`加载中... ${this.progressValue}%`)
+              .fontSize(10)
+              .fontColor(this.isDarkMode ? '#E0E0E0' : Color.Gray)
+              .margin({ top: 4 })
+              .alignSelf(ItemAlign.End)
+          }
+          .width('100%')
         }
       }
       .width('100%')
       .padding({ left: 20, right: 20, top: 12, bottom: 8 })
+      .backgroundColor(this.isDarkMode ? '#3A3A3A' : '#F8F9FA')
+      .borderRadius({
+        topLeft: 0,
+        topRight: 0,
+        bottomLeft: 8,
+        bottomRight: 8
+      })
 
       // WebView内容区域 - 完全参考WebIndex.ets的实现
       if (this.errorMessage) {
         // 错误状态
         Column() {
-          Text('❌')
-            .fontSize(48)
+          // 错误图标背景
+          Column() {
+            Text('❌')
+              .fontSize(32)
+          }
+          .width(64)
+          .height(64)
+          .backgroundColor(this.isDarkMode ? '#4A4A4A' : '#FFF5F5')
+          .borderRadius(32)
+          .justifyContent(FlexAlign.Center)
+          .alignItems(HorizontalAlign.Center)
+          .border({
+            width: 1,
+            color: this.isDarkMode ? '#666666' : '#FED7D7'
+          })
           
           Text('加载失败')
             .fontSize(16)
-            .fontColor(Color.Black)
-            .margin({ top: 12 })
+            .fontColor(this.isDarkMode ? Color.White : Color.Black)
+            .fontWeight(FontWeight.Medium)
+            .margin({ top: 16 })
           
           Text(this.errorMessage)
             .fontSize(12)
-            .fontColor(Color.Gray)
-            .margin({ top: 4 })
+            .fontColor(this.isDarkMode ? '#E0E0E0' : Color.Gray)
+            .margin({ top: 8, left: 20, right: 20 })
             .textAlign(TextAlign.Center)
+            .maxLines(3)
           
           Button('重试')
             .fontSize(14)
-            .backgroundColor('#2196F3')
-            .margin({ top: 16 })
+            .backgroundColor(this.themeColor)
+            .fontColor(Color.White)
+            .borderRadius(8)
+            .width(120)
+            .height(40)
+            .margin({ top: 20 })
             .onClick(() => {
-              this.errorMessage = '';
-              this.isLoading = true;
-              this.progressValue = 0;
-              this.progressVisible = true;
-              // 重新加载
-              this.webViewController.refresh();
+              try {
+                this.errorMessage = '';
+                this.isLoading = true;
+                this.progressValue = 0;
+                this.progressVisible = true;
+                // 重新加载
+                if (this.webViewController) {
+                  this.webViewController.refresh();
+                }
+              } catch (error) {
+                console.error('OnlineUpdateLogDialog 重试失败:', error);
+                this.errorMessage = '重试失败,请稍后再试';
+              }
             })
         }
         .width('100%')
@@ -144,6 +287,7 @@ export default struct OnlineUpdateLogDialog {
         .justifyContent(FlexAlign.Center)
         .alignItems(HorizontalAlign.Center)
         .padding(20)
+        .backgroundColor(this.isDarkMode ? '#2C2C2C' : Color.White)
       } else {
         // WebView内容 - 完全按照WebIndex.ets的方式实现
         Web({
@@ -152,6 +296,13 @@ export default struct OnlineUpdateLogDialog {
         })
           .width('100%')
           .height(350)
+          .borderRadius(8)
+          .backgroundColor(this.isDarkMode ? '#2C2C2C' : Color.White)
+          // .border({
+          //   width: 1,
+          //   color: this.isDarkMode ? '#333333' : '#E0E0E0'
+          // })
+          .margin({ left: 12, right: 12, bottom: 8 })
           .darkMode(this.isDarkMode ? WebDarkMode.On : WebDarkMode.Off)
           .forceDarkAccess(this.isDarkMode)
           .onProgressChange((event) => {
@@ -195,31 +346,65 @@ export default struct OnlineUpdateLogDialog {
       Row() {
         Button('稍后查看')
           .fontSize(14)
-          .backgroundColor('#9E9E9E')
+          .backgroundColor(this.isDarkMode ? '#666666' : '#9E9E9E')
           .fontColor(Color.White)
+          .borderRadius(8)
           .layoutWeight(1)
+          .height(44)
           .onClick(() => {
-            this.controller.close();
+            try {
+              if (this.onClose) {
+                this.onClose();
+              } else {
+                this.controller?.close();
+              }
+            } catch (error) {
+              console.error('OnlineUpdateLogDialog 稍后查看按钮失败:', error);
+            }
           })
         
         Blank().width(12)
         
         Button('知道了')
           .fontSize(14)
-          .backgroundColor('#2196F3')
+          .backgroundColor(this.themeColor)
           .fontColor(Color.White)
+          .borderRadius(8)
           .layoutWeight(1)
+          .height(44)
           .onClick(() => {
-            this.markVersionShown();
-            this.controller.close();
+            try {
+              this.markVersionShown();
+              if (this.onClose) {
+                this.onClose();
+              } else {
+                this.controller?.close();
+              }
+            } catch (error) {
+              console.error('OnlineUpdateLogDialog 知道了按钮失败:', error);
+            }
           })
       }
+      .visibility(Visibility.Hidden)
       .width('100%')
       .padding({ left: 20, right: 20, top: 16, bottom: 16 })
+      .backgroundColor(this.isDarkMode ? '#3A3A3A' : '#F8F9FA')
+      .borderRadius({
+        topLeft: 0,
+        topRight: 0,
+        bottomLeft: 12,
+        bottomRight: 12
+      })
     }
-    .backgroundColor(Color.White)
+    .backgroundColor(this.isDarkMode ? '#2C2C2C' : Color.White)
     .borderRadius(12)
     .width('92%')
     .constraintSize({ maxHeight: '85%' })
+    .shadow({
+      radius: 16,
+      color: this.isDarkMode ? 'rgba(0,0,0,0.8)' : 'rgba(0,0,0,0.15)',
+      offsetX: 0,
+      offsetY: 4
+    })
   }
 }

+ 32 - 4
entry/src/main/ets/pages/NewIndex.ets

@@ -260,19 +260,41 @@ struct NewIndex {
     }
   }
 
+  /** 更新日志弹窗控制器 */
+  private updateLogDialogController?: CustomDialogController;
+
   /**
    * 显示更新日志弹窗
    */
   private showUpdateLogDialog() {
     try {
-      const dialogController = new CustomDialogController({
-        builder: OnlineUpdateLogDialog({}),
+      // 如果已有弹窗,先关闭
+      if (this.updateLogDialogController) {
+        this.updateLogDialogController.close();
+        this.updateLogDialogController = undefined;
+      }
+
+      // 创建弹窗控制器
+      this.updateLogDialogController = new CustomDialogController({
+        builder: OnlineUpdateLogDialog({
+          onClose: () => {
+            // 弹窗关闭回调
+            if (this.updateLogDialogController) {
+              this.updateLogDialogController.close();
+              this.updateLogDialogController = undefined;
+            }
+          }
+        }),
         autoCancel: false,
         alignment: DialogAlignment.Center,
-        customStyle: true
+        customStyle: true,
+        cancel: () => {
+          console.info('NewIndex 更新日志弹窗被取消');
+          this.updateLogDialogController = undefined;
+        }
       });
 
-      dialogController.open();
+      this.updateLogDialogController.open();
       console.info('NewIndex 更新日志弹窗已显示');
     } catch (error) {
       console.error('NewIndex 显示更新日志弹窗失败:', error);
@@ -288,6 +310,12 @@ struct NewIndex {
     this.breakpointSystem.unregister();
     emitter.off(888);
     emitter.off(1001);
+    
+    // 清理更新日志弹窗
+    if (this.updateLogDialogController) {
+      this.updateLogDialogController.close();
+      this.updateLogDialogController = undefined;
+    }
   }
 
   build() {