OnlineUpdateLog.ets 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * 在线更新日志弹窗组件
  3. * 参考WebIndex.ets的成功实现,确保WebView正常工作
  4. */
  5. import { webview } from '@kit.ArkWeb';
  6. import { UpdateConstants } from '../common/constants/UpdateConstants';
  7. import { UpdateLogManager } from '../common/util/UpdateLogManager';
  8. import { ConfigurationConstant } from '@kit.AbilityKit';
  9. import { AppUtil, PreferencesUtil } from '@pura/harmony-utils';
  10. import { CommonConstants } from '../common/constants/CommonConstants';
  11. @Component
  12. export default struct OnlineUpdateLog {
  13. /** 弹窗控制器 */
  14. controller?: CustomDialogController;
  15. /** 关闭回调 */
  16. onClose?: () => void;
  17. /** 更新日志URL */
  18. @State updateLogUrl: string = '';
  19. /** 当前应用版本 */
  20. @State currentVersion: string = '';
  21. /** WebView控制器 */
  22. private webViewController: webview.WebviewController = new webview.WebviewController();
  23. /** 是否加载完成 */
  24. @State isLoading: boolean = true;
  25. /** 加载错误信息 */
  26. @State errorMessage: string = '';
  27. /** 加载进度 */
  28. @State progressValue: number = 0;
  29. /** 进度条是否可见 */
  30. @State progressVisible: boolean = true;
  31. /** 深色模式 */
  32. @State isDarkMode: boolean = false;
  33. @StorageProp('currentColorMode') currentMode: number = ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT;
  34. @StorageProp('themeColor') themeColor: string = CommonConstants.DEFAULT_THEME_COLOR;
  35. aboutToAppear() {
  36. try {
  37. // 获取当前应用版本
  38. this.getCurrentVersion();
  39. // 设置更新日志URL - 直接使用在线URL
  40. this.updateLogUrl = UpdateConstants.UPDATE_LOG_URL;
  41. // 检查深色模式
  42. this.isDarkMode = this.currentMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK;
  43. let themeColor = PreferencesUtil.getStringSync('THEME_COLOR', CommonConstants.DEFAULT_THEME_COLOR);
  44. AppStorage.setOrCreate('themeColor', themeColor);
  45. this.themeColor = themeColor
  46. console.info('OnlineUpdateLogDialog URL:', this.updateLogUrl);
  47. console.info('OnlineUpdateLogDialog 开始加载在线更新日志');
  48. // 确保WebView控制器已初始化
  49. if (!this.webViewController) {
  50. this.webViewController = new webview.WebviewController();
  51. }
  52. } catch (error) {
  53. console.error('OnlineUpdateLogDialog aboutToAppear error:', error);
  54. this.errorMessage = '初始化失败';
  55. this.isLoading = false;
  56. }
  57. }
  58. /**
  59. * 获取当前应用版本号
  60. */
  61. private getCurrentVersion() {
  62. try {
  63. this.currentVersion = AppUtil.getVersionName()//UpdateConstants.APP_VERSION;
  64. } catch (error) {
  65. this.currentVersion = '1.0.0';
  66. }
  67. }
  68. /**
  69. * 记录已显示的版本,避免重复显示
  70. */
  71. private markVersionShown() {
  72. try {
  73. UpdateLogManager.markCurrentVersionShown();
  74. } catch (error) {
  75. console.error('OnlineUpdateLogDialog markVersionShown error:', error);
  76. }
  77. }
  78. /**
  79. * 组件销毁时清理资源
  80. */
  81. aboutToDisappear() {
  82. try {
  83. console.info('OnlineUpdateLogDialog aboutToDisappear');
  84. // 清理WebView控制器
  85. if (this.webViewController) {
  86. // 这里可以添加WebView的清理逻辑,如果需要的话
  87. }
  88. } catch (error) {
  89. console.error('OnlineUpdateLogDialog aboutToDisappear error:', error);
  90. }
  91. }
  92. build() {
  93. Column() {
  94. // 版本信息和进度条
  95. Column() {
  96. // 进度条 - 使用主题色
  97. if (this.progressVisible && this.progressValue < 100) {
  98. Column() {
  99. Progress({ value: this.progressValue, total: 100, type: ProgressType.Linear })
  100. .width('100%')
  101. .height(4)
  102. .color(this.themeColor)
  103. .backgroundColor(this.isDarkMode ? '#4A4A4A' : '#E0E0E0')
  104. .borderRadius(2)
  105. Text(`加载中... ${this.progressValue}%`)
  106. .fontSize(10)
  107. .fontColor(this.isDarkMode ? '#E0E0E0' : Color.Gray)
  108. .margin({ top: 4 })
  109. .alignSelf(ItemAlign.End)
  110. }
  111. .width('100%')
  112. }
  113. }
  114. .width('100%')
  115. .padding({ left: 20, right: 20, top: 12, bottom: 8 })
  116. .backgroundColor(this.isDarkMode ? '#3A3A3A' : '#F8F9FA')
  117. .borderRadius({
  118. topLeft: 0,
  119. topRight: 0,
  120. bottomLeft: 8,
  121. bottomRight: 8
  122. })
  123. // WebView内容区域 - 完全参考WebIndex.ets的实现
  124. if (this.errorMessage) {
  125. // 错误状态
  126. Column() {
  127. // 错误图标背景
  128. Column() {
  129. Text('❌')
  130. .fontSize(32)
  131. }
  132. .width(64)
  133. .height(64)
  134. .backgroundColor(this.isDarkMode ? '#4A4A4A' : '#FFF5F5')
  135. .borderRadius(32)
  136. .justifyContent(FlexAlign.Center)
  137. .alignItems(HorizontalAlign.Center)
  138. .border({
  139. width: 1,
  140. color: this.isDarkMode ? '#666666' : '#FED7D7'
  141. })
  142. Text('加载失败')
  143. .fontSize(16)
  144. .fontColor(this.isDarkMode ? Color.White : Color.Black)
  145. .fontWeight(FontWeight.Medium)
  146. .margin({ top: 16 })
  147. Text(this.errorMessage)
  148. .fontSize(12)
  149. .fontColor(this.isDarkMode ? '#E0E0E0' : Color.Gray)
  150. .margin({ top: 8, left: 20, right: 20 })
  151. .textAlign(TextAlign.Center)
  152. .maxLines(3)
  153. Button('重试')
  154. .fontSize(14)
  155. .backgroundColor(this.themeColor)
  156. .fontColor(Color.White)
  157. .borderRadius(8)
  158. .width(120)
  159. .height(40)
  160. .margin({ top: 20 })
  161. .onClick(() => {
  162. try {
  163. this.errorMessage = '';
  164. this.isLoading = true;
  165. this.progressValue = 0;
  166. this.progressVisible = true;
  167. // 重新加载
  168. if (this.webViewController) {
  169. this.webViewController.refresh();
  170. }
  171. } catch (error) {
  172. console.error('OnlineUpdateLogDialog 重试失败:', error);
  173. this.errorMessage = '重试失败,请稍后再试';
  174. }
  175. })
  176. }
  177. .width('100%')
  178. .height(350)
  179. .justifyContent(FlexAlign.Center)
  180. .alignItems(HorizontalAlign.Center)
  181. .padding(20)
  182. .backgroundColor(this.isDarkMode ? '#2C2C2C' : Color.White)
  183. } else {
  184. // WebView内容 - 完全按照WebIndex.ets的方式实现
  185. Web({
  186. src: this.updateLogUrl,
  187. controller: this.webViewController
  188. })
  189. .width('100%')
  190. .height(350)
  191. .borderRadius(12)
  192. .layoutWeight(1)
  193. .backgroundColor(this.isDarkMode ? '#2C2C2C' : Color.White)
  194. // .border({
  195. // width: 1,
  196. // color: this.isDarkMode ? '#333333' : '#E0E0E0'
  197. // })
  198. .margin({ left: 12, right: 12, bottom: 8 })
  199. .darkMode(this.isDarkMode ? WebDarkMode.On : WebDarkMode.Off)
  200. .forceDarkAccess(this.isDarkMode)
  201. .onProgressChange((event) => {
  202. if (event) {
  203. console.info('OnlineUpdateLogDialog WebView进度:', event.newProgress);
  204. this.progressValue = event.newProgress;
  205. // 进度完成时隐藏进度条
  206. if (event.newProgress >= 100) {
  207. setTimeout(() => {
  208. this.progressVisible = false;
  209. this.isLoading = false;
  210. }, 500);
  211. }
  212. }
  213. })
  214. .onPageBegin(() => {
  215. console.info('OnlineUpdateLogDialog WebView开始加载:', this.updateLogUrl);
  216. this.isLoading = true;
  217. this.errorMessage = '';
  218. this.progressValue = 0;
  219. this.progressVisible = true;
  220. })
  221. .onPageEnd(() => {
  222. console.info('OnlineUpdateLogDialog WebView加载完成');
  223. this.isLoading = false;
  224. setTimeout(() => {
  225. this.progressVisible = false;
  226. }, 1000);
  227. })
  228. .onErrorReceive((event) => {
  229. const errorInfo = event?.error?.getErrorInfo() || '网络错误';
  230. console.error('OnlineUpdateLogDialog WebView加载错误:', errorInfo);
  231. this.isLoading = false;
  232. this.progressVisible = false;
  233. this.errorMessage = `加载失败:${errorInfo}`;
  234. })
  235. }
  236. }
  237. .backgroundColor(this.isDarkMode ? '#2C2C2C' : Color.White)
  238. .borderRadius(12)
  239. .width('92%')
  240. .constraintSize({ maxHeight: '85%' })
  241. .shadow({
  242. radius: 16,
  243. color: this.isDarkMode ? 'rgba(0,0,0,0.8)' : 'rgba(0,0,0,0.15)',
  244. offsetX: 0,
  245. offsetY: 4
  246. })
  247. }
  248. }