zhangbo1_wd

对接底导接口;添加mock开关

import { BottomNavi, CommonConstants } from 'wdConstant';
import { LazyDataSource, Logger } from 'wdKit';
import { Logger } from 'wdKit';
import { TopNavigationComponent } from './TopNavigationComponent';
import { PageComponent } from './PageComponent';
import { BottomNavDTO } from '../repository/bean/BottomNavDTO';
import { PageViewModel } from '../Index';
import { UIUtils } from '../repository/UIUtils';
import { MinePageComponent } from './MinePageComponent';
import PageViewModel from '../viewmodel/PageViewModel';
const TAG = 'BottomNavigationComponent';
... ... @@ -33,9 +32,9 @@ export struct BottomNavigationComponent {
async aboutToAppear() {
Logger.info(TAG, `aboutToAppear currentNavIndex: ${this.currentNavIndex}`);
let bottomNav = await PageViewModel.getBottomNavData()
if (bottomNav) {
Logger.info(TAG, `aboutToAppear, bottomNav.length: ${bottomNav.length}`);
this.bottomNavList = bottomNav
if (bottomNav && bottomNav.bottomNavList != null) {
Logger.info(TAG, `aboutToAppear, bottomNav.length: ${bottomNav.bottomNavList.length}`);
this.bottomNavList = bottomNav.bottomNavList
}
}
... ...
... ... @@ -11,6 +11,10 @@ export class HttpUrlUtils {
*/
static readonly HOST: string = "https://pd-apis-uat.pdnews.cn";
/**
* 启动接口(底导接口)
*/
static readonly BOTTOM_NAV_PATH: string = "/api/rmrb-bff-display-zh/display/zh/c/bottomNavGroup";
/**
* 展现comp接口
*/
static readonly COMP_PATH: string = "/api/rmrb-bff-display-zh/display/zh/c/compInfo";
... ... @@ -46,6 +50,11 @@ export class HttpUrlUtils {
return headers;
}
static getBottomNavGroupUrl() {
// https: //pd-apis-uat.pdnews.cn/api/rmrb-bff-display-zh/display/zh/c/bottomNavGroup
return this.HOST + this.BOTTOM_NAV_PATH;
}
static getCompInfoUrl(pageId: string, groupId: string, channelId: string) {
let url = this.HOST + this.COMP_PATH;
// TODO 暂定只请求第一页,后续对接分页加载,参数再调整
... ...
... ... @@ -5,7 +5,8 @@ import HashMap from '@ohos.util.HashMap';
import { HttpUrlUtils } from '../network/HttpUrlUtils';
export class PageRepository {
static fetchNavigationDataApi(url: string) {
static fetchNavigationDataApi() {
let url = HttpUrlUtils.getBottomNavGroupUrl();
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
return WDHttp.get<ResponseDTO<NavigationBodyDTO>>(url, headers)
};
... ...
... ... @@ -6,13 +6,15 @@ 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';
/**
* mock数据开关,默认关。
* mock数据是本地json数据,可自行修改内容(‘entry\src\main\resources\rawfile\’目录)
*/
const mock_switch = false;
const mock_switch = true;
/**
* 处理返回后的数据
... ... @@ -27,36 +29,33 @@ export class PageViewModel extends BaseViewModel {
*
* @return BottomNavBean[] Nav Data List
*/
static async getBottomNavData(): Promise<BottomNavDTO[]> {
async getBottomNavData(): Promise<NavigationBodyDTO> {
Logger.info(TAG, `getBottomNavData start`);
if (mock_switch) {
return this.getBottomNavDataMock();
}
return this.getNavData();
}
async getBottomNavDataMock(): Promise<NavigationBodyDTO> {
Logger.info(TAG, `getBottomNavDataMock start`);
let compRes: ResponseDTO<NavigationBodyDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<NavigationBodyDTO>>('bottom_nav.json');
if (!compRes || !compRes.data || !compRes.data.bottomNavList) {
Logger.info(TAG, `getBottomNavData compRes bottomNavList is empty`);
return []
if (!compRes || !compRes.data) {
Logger.info(TAG, `getBottomNavDataMock compRes bottomNavList is empty`);
return null
}
Logger.info(TAG, `getBottomNavData getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
return compRes.data.bottomNavList
Logger.info(TAG, `getBottomNavDataMock getResourcesJsonSync compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
static getNavData(url: string): Promise<NavigationBodyDTO> {
private getNavData(): Promise<NavigationBodyDTO> {
return new Promise<NavigationBodyDTO>((success, error) => {
Logger.info(TAG, `getNavData start`);
PageRepository.fetchNavigationDataApi(url).then((navResDTO: ResponseDTO<NavigationBodyDTO>) => {
if (!navResDTO) {
Logger.error(TAG, 'getNavData then navResDTO is empty');
error('navResDTO is empty');
return
}
if (navResDTO.code != http.ResponseCode.OK) {
Logger.error(TAG, `getNavData then code:${navResDTO.code}, message:${navResDTO.message}`);
error('navResDTO Response Code is failure');
return
}
if (!navResDTO.data?.bottomNavList) {
error('navResDTO list is empty');
PageRepository.fetchNavigationDataApi().then((navResDTO: ResponseDTO<NavigationBodyDTO>) => {
if (this.isRespondsInvalid(navResDTO, 'getNavData')) {
error("page data invalid");
return
}
// let navResStr = JSON.stringify(navResDTO);
Logger.info(TAG, "getNavData then,navResDTO.timeStamp:" + navResDTO.timestamp);
let navigationBean = navResDTO.data
success(navigationBean);
... ... @@ -72,7 +71,7 @@ export class PageViewModel extends BaseViewModel {
*
* @return {GroupDTO} compRes.data
*/
static async getPageData1(): Promise<PageDTO> {
private async getPageData1(): Promise<PageDTO> {
let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>('comp_list0.json');
if (!compRes || !compRes.data) {
Logger.info(TAG, `getCompList compRes is empty`);
... ... @@ -86,19 +85,24 @@ export class PageViewModel extends BaseViewModel {
* Get Group data.
*
* @return {GroupDTO} compRes.data
* @deprecated
*/
async getPageData2(): Promise<PageDTO> {
private async getPageData2(): 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`);
return {} as PageDTO
}
// router.push('')
Logger.info(TAG, `getCompList getResourcesJson compRes : ${JSON.stringify(compRes)}`);
return compRes.data
}
async getPageData(pageId: string, groupId: string, channelId: string): Promise<PageDTO> {
Logger.error(TAG, 'getPageData pageId: ' + pageId);
if (mock_switch) {
return this.getPageData1();
}
return new Promise<PageDTO>((success, error) => {
PageRepository.fetchPageData(pageId, groupId, channelId).then((resDTO: ResponseDTO<PageDTO>) => {
if (this.isRespondsInvalid(resDTO, 'getPageData')) {
... ...
... ... @@ -23,7 +23,7 @@ export class HttpRequest {
static get<T = any>(url: string, headers?: HashMap<string, string>): Promise<T> {
let requestHeaders: AxiosHeaders = new AxiosHeaders()
headers.forEach((v, k) => {
headers?.forEach((v, k) => {
requestHeaders.set(k, v);
});
let config: AxiosRequestConfig = {
... ...