zhangbo1_wd

context上下文获取问题,先加传参。后续优化全局上下文的获取

@@ -31,7 +31,7 @@ export struct BottomNavigationComponent { @@ -31,7 +31,7 @@ export struct BottomNavigationComponent {
31 31
32 async aboutToAppear() { 32 async aboutToAppear() {
33 Logger.info(TAG, `aboutToAppear currentNavIndex: ${this.currentNavIndex}`); 33 Logger.info(TAG, `aboutToAppear currentNavIndex: ${this.currentNavIndex}`);
34 - let bottomNav = await PageViewModel.getBottomNavData() 34 + let bottomNav = await PageViewModel.getBottomNavData(getContext(this))
35 if (bottomNav && bottomNav.bottomNavList != null) { 35 if (bottomNav && bottomNav.bottomNavList != null) {
36 Logger.info(TAG, `aboutToAppear, bottomNav.length: ${bottomNav.bottomNavList.length}`); 36 Logger.info(TAG, `aboutToAppear, bottomNav.length: ${bottomNav.bottomNavList.length}`);
37 this.bottomNavList = bottomNav.bottomNavList 37 this.bottomNavList = bottomNav.bottomNavList
@@ -80,7 +80,7 @@ export struct PageComponent { @@ -80,7 +80,7 @@ export struct PageComponent {
80 // return 80 // return
81 // } 81 // }
82 Logger.debug(TAG,'lllllalalal: ' + this.pageId+' , ' + this.channelId); 82 Logger.debug(TAG,'lllllalalal: ' + this.pageId+' , ' + this.channelId);
83 - let pageDto = await PageViewModel.getPageData(this.pageId, this.pageId, this.channelId) 83 + let pageDto = await PageViewModel.getPageData(this.pageId, this.pageId, this.channelId, getContext(this))
84 if (pageDto && pageDto.compList) { 84 if (pageDto && pageDto.compList) {
85 this.compList.push(...pageDto.compList) 85 this.compList.push(...pageDto.compList)
86 } 86 }
1 -import { AppContext, Logger, ResourcesUtils } from 'wdKit'; 1 +import { Logger, ResourcesUtils } from 'wdKit';
2 import { ResponseDTO, } from 'wdNetwork'; 2 import { ResponseDTO, } from 'wdNetwork';
3 import { PageRepository } from '../repository/PageRepository'; 3 import { PageRepository } from '../repository/PageRepository';
4 -import http from '@ohos.net.http';  
5 import { NavigationBodyDTO } from '../repository/bean/NavigationBodyDTO'; 4 import { NavigationBodyDTO } from '../repository/bean/NavigationBodyDTO';
6 -import { BottomNavDTO } from '../repository/bean/BottomNavDTO';  
7 import { PageDTO } from '../repository/bean/PageDTO'; 5 import { PageDTO } from '../repository/bean/PageDTO';
8 import { BaseViewModel } from './BaseViewModel'; 6 import { BaseViewModel } from './BaseViewModel';
9 -import Router from '@system.router';  
10 -import router from '@ohos.router';  
11 7
12 const TAG = 'PageViewModel'; 8 const TAG = 'PageViewModel';
13 /** 9 /**
@@ -29,17 +25,17 @@ export class PageViewModel extends BaseViewModel { @@ -29,17 +25,17 @@ export class PageViewModel extends BaseViewModel {
29 * 25 *
30 * @return BottomNavBean[] Nav Data List 26 * @return BottomNavBean[] Nav Data List
31 */ 27 */
32 - async getBottomNavData(): Promise<NavigationBodyDTO> { 28 + async getBottomNavData(context?: Context): Promise<NavigationBodyDTO> {
33 Logger.info(TAG, `getBottomNavData start`); 29 Logger.info(TAG, `getBottomNavData start`);
34 if (mock_switch) { 30 if (mock_switch) {
35 - return this.getBottomNavDataMock(); 31 + return this.getBottomNavDataMock(context);
36 } 32 }
37 return this.getNavData(); 33 return this.getNavData();
38 } 34 }
39 35
40 - async getBottomNavDataMock(): Promise<NavigationBodyDTO> { 36 + async getBottomNavDataMock(context?: Context): Promise<NavigationBodyDTO> {
41 Logger.info(TAG, `getBottomNavDataMock start`); 37 Logger.info(TAG, `getBottomNavDataMock start`);
42 - let compRes: ResponseDTO<NavigationBodyDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<NavigationBodyDTO>>('bottom_nav.json'); 38 + let compRes: ResponseDTO<NavigationBodyDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<NavigationBodyDTO>>('bottom_nav.json', context);
43 if (!compRes || !compRes.data) { 39 if (!compRes || !compRes.data) {
44 Logger.info(TAG, `getBottomNavDataMock compRes bottomNavList is empty`); 40 Logger.info(TAG, `getBottomNavDataMock compRes bottomNavList is empty`);
45 return null 41 return null
@@ -71,8 +67,8 @@ export class PageViewModel extends BaseViewModel { @@ -71,8 +67,8 @@ export class PageViewModel extends BaseViewModel {
71 * 67 *
72 * @return {GroupDTO} compRes.data 68 * @return {GroupDTO} compRes.data
73 */ 69 */
74 - private async getPageData1(): Promise<PageDTO> {  
75 - let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>('comp_list0.json'); 70 + private async getPageData1(context?: Context): Promise<PageDTO> {
  71 + let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>('comp_list0.json', context);
76 if (!compRes || !compRes.data) { 72 if (!compRes || !compRes.data) {
77 Logger.info(TAG, `getCompList compRes is empty`); 73 Logger.info(TAG, `getCompList compRes is empty`);
78 return {} as PageDTO 74 return {} as PageDTO
@@ -87,7 +83,7 @@ export class PageViewModel extends BaseViewModel { @@ -87,7 +83,7 @@ export class PageViewModel extends BaseViewModel {
87 * @return {GroupDTO} compRes.data 83 * @return {GroupDTO} compRes.data
88 * @deprecated 84 * @deprecated
89 */ 85 */
90 - private async getPageData2(): Promise<PageDTO> { 86 + private async getPageData2(context?: Context): Promise<PageDTO> {
91 let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>('comp_list2.json'); 87 let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>('comp_list2.json');
92 if (!compRes || !compRes.data) { 88 if (!compRes || !compRes.data) {
93 Logger.info(TAG, `getCompList compRes is empty`); 89 Logger.info(TAG, `getCompList compRes is empty`);
@@ -98,10 +94,10 @@ export class PageViewModel extends BaseViewModel { @@ -98,10 +94,10 @@ export class PageViewModel extends BaseViewModel {
98 return compRes.data 94 return compRes.data
99 } 95 }
100 96
101 - async getPageData(pageId: string, groupId: string, channelId: string): Promise<PageDTO> { 97 + async getPageData(pageId: string, groupId: string, channelId: string, context?: Context): Promise<PageDTO> {
102 Logger.error(TAG, 'getPageData pageId: ' + pageId); 98 Logger.error(TAG, 'getPageData pageId: ' + pageId);
103 if (mock_switch) { 99 if (mock_switch) {
104 - return this.getPageData1(); 100 + return this.getPageData1(context);
105 } 101 }
106 return new Promise<PageDTO>((success, error) => { 102 return new Promise<PageDTO>((success, error) => {
107 PageRepository.fetchPageData(pageId, groupId, channelId).then((resDTO: ResponseDTO<PageDTO>) => { 103 PageRepository.fetchPageData(pageId, groupId, channelId).then((resDTO: ResponseDTO<PageDTO>) => {
@@ -20,7 +20,7 @@ export class ResourcesUtils { @@ -20,7 +20,7 @@ export class ResourcesUtils {
20 } 20 }
21 return new Promise((success, error) => { 21 return new Promise((success, error) => {
22 Logger.info(TAG, "ResourcesUtils getResourcesText filename:" + filename); 22 Logger.info(TAG, "ResourcesUtils getResourcesText filename:" + filename);
23 - context.resourceManager.getRawFileContent(filename).then((content: Uint8Array) => { 23 + context?.resourceManager.getRawFileContent(filename).then((content: Uint8Array) => {
24 if (!content) { 24 if (!content) {
25 Logger.warn(TAG, 'getResourcesText then content is empty'); 25 Logger.warn(TAG, 'getResourcesText then content is empty');
26 error("file is empty"); 26 error("file is empty");