zhangbo1_wd

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

... ... @@ -31,7 +31,7 @@ export struct BottomNavigationComponent {
async aboutToAppear() {
Logger.info(TAG, `aboutToAppear currentNavIndex: ${this.currentNavIndex}`);
let bottomNav = await PageViewModel.getBottomNavData()
let bottomNav = await PageViewModel.getBottomNavData(getContext(this))
if (bottomNav && bottomNav.bottomNavList != null) {
Logger.info(TAG, `aboutToAppear, bottomNav.length: ${bottomNav.bottomNavList.length}`);
this.bottomNavList = bottomNav.bottomNavList
... ...
... ... @@ -80,7 +80,7 @@ export struct PageComponent {
// return
// }
Logger.debug(TAG,'lllllalalal: ' + this.pageId+' , ' + this.channelId);
let pageDto = await PageViewModel.getPageData(this.pageId, this.pageId, this.channelId)
let pageDto = await PageViewModel.getPageData(this.pageId, this.pageId, this.channelId, getContext(this))
if (pageDto && pageDto.compList) {
this.compList.push(...pageDto.compList)
}
... ...
import { AppContext, Logger, ResourcesUtils } from 'wdKit';
import { Logger, ResourcesUtils } from 'wdKit';
import { ResponseDTO, } from 'wdNetwork';
import { PageRepository } from '../repository/PageRepository';
import http from '@ohos.net.http';
import { NavigationBodyDTO } from '../repository/bean/NavigationBodyDTO';
import { BottomNavDTO } from '../repository/bean/BottomNavDTO';
import { PageDTO } from '../repository/bean/PageDTO';
import { BaseViewModel } from './BaseViewModel';
import Router from '@system.router';
import router from '@ohos.router';
const TAG = 'PageViewModel';
/**
... ... @@ -29,17 +25,17 @@ export class PageViewModel extends BaseViewModel {
*
* @return BottomNavBean[] Nav Data List
*/
async getBottomNavData(): Promise<NavigationBodyDTO> {
async getBottomNavData(context?: Context): Promise<NavigationBodyDTO> {
Logger.info(TAG, `getBottomNavData start`);
if (mock_switch) {
return this.getBottomNavDataMock();
return this.getBottomNavDataMock(context);
}
return this.getNavData();
}
async getBottomNavDataMock(): Promise<NavigationBodyDTO> {
async getBottomNavDataMock(context?: Context): Promise<NavigationBodyDTO> {
Logger.info(TAG, `getBottomNavDataMock start`);
let compRes: ResponseDTO<NavigationBodyDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<NavigationBodyDTO>>('bottom_nav.json');
let compRes: ResponseDTO<NavigationBodyDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<NavigationBodyDTO>>('bottom_nav.json', context);
if (!compRes || !compRes.data) {
Logger.info(TAG, `getBottomNavDataMock compRes bottomNavList is empty`);
return null
... ... @@ -71,8 +67,8 @@ export class PageViewModel extends BaseViewModel {
*
* @return {GroupDTO} compRes.data
*/
private async getPageData1(): Promise<PageDTO> {
let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>('comp_list0.json');
private async getPageData1(context?: Context): Promise<PageDTO> {
let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>('comp_list0.json', context);
if (!compRes || !compRes.data) {
Logger.info(TAG, `getCompList compRes is empty`);
return {} as PageDTO
... ... @@ -87,7 +83,7 @@ export class PageViewModel extends BaseViewModel {
* @return {GroupDTO} compRes.data
* @deprecated
*/
private async getPageData2(): Promise<PageDTO> {
private async getPageData2(context?: Context): Promise<PageDTO> {
let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>('comp_list2.json');
if (!compRes || !compRes.data) {
Logger.info(TAG, `getCompList compRes is empty`);
... ... @@ -98,10 +94,10 @@ export class PageViewModel extends BaseViewModel {
return compRes.data
}
async getPageData(pageId: string, groupId: string, channelId: string): Promise<PageDTO> {
async getPageData(pageId: string, groupId: string, channelId: string, context?: Context): Promise<PageDTO> {
Logger.error(TAG, 'getPageData pageId: ' + pageId);
if (mock_switch) {
return this.getPageData1();
return this.getPageData1(context);
}
return new Promise<PageDTO>((success, error) => {
PageRepository.fetchPageData(pageId, groupId, channelId).then((resDTO: ResponseDTO<PageDTO>) => {
... ...
... ... @@ -20,7 +20,7 @@ export class ResourcesUtils {
}
return new Promise((success, error) => {
Logger.info(TAG, "ResourcesUtils getResourcesText filename:" + filename);
context.resourceManager.getRawFileContent(filename).then((content: Uint8Array) => {
context?.resourceManager.getRawFileContent(filename).then((content: Uint8Array) => {
if (!content) {
Logger.warn(TAG, 'getResourcesText then content is empty');
error("file is empty");
... ...