Toggle navigation
Toggle navigation
This project
Loading...
Sign in
developOne
/
harmonyPool
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
zhangbo1_wd
2024-01-26 11:00:04 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3fca87f5241507dbacd1065dacd552d1159d5c88
3fca87f5
1 parent
3411aac4
对接comp接口
Show whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
331 additions
and
52 deletions
PeopleDaily_Harmony/entry/src/main/ets/entryability/EntryAbility.ts
PeopleDaily_Harmony/wdComponent/src/main/ets/components/BottomNavigationComponent.ets
PeopleDaily_Harmony/wdComponent/src/main/ets/components/MinePageComponent.ets
PeopleDaily_Harmony/wdComponent/src/main/ets/components/PageComponent.ets
PeopleDaily_Harmony/wdComponent/src/main/ets/components/TopNavigationComponent.ets
PeopleDaily_Harmony/wdComponent/src/main/ets/network/HttpUrlUtils.ets
PeopleDaily_Harmony/wdComponent/src/main/ets/repository/PageRepository.ets
PeopleDaily_Harmony/wdComponent/src/main/ets/repository/UIUtils.ets
PeopleDaily_Harmony/wdComponent/src/main/ets/repository/bean/BottomNavDTO.ets
PeopleDaily_Harmony/wdComponent/src/main/ets/repository/bean/TopNavDTO.ets
PeopleDaily_Harmony/wdComponent/src/main/ets/viewmodel/BaseViewModel.ets
PeopleDaily_Harmony/wdComponent/src/main/ets/viewmodel/PageViewModel.ets
PeopleDaily_Harmony/wdConstant/src/main/ets/constants/ConfigConstants.ets
PeopleDaily_Harmony/wdKit/src/main/ets/Index.ets
PeopleDaily_Harmony/wdKit/src/main/ets/utils/AppContext.ets
PeopleDaily_Harmony/wdKit/src/main/ets/utils/DateTimeUtils.ts
PeopleDaily_Harmony/wdKit/src/main/ets/utils/ResourcesUtils.ets
PeopleDaily_Harmony/wdNetwork/src/main/ets/http/HttpRequest.ts
PeopleDaily_Harmony/entry/src/main/ets/entryability/EntryAbility.ts
View file @
3fca87f
import
UIAbility
from
'@ohos.app.ability.UIAbility'
;
import
hilog
from
'@ohos.hilog'
;
import
window
from
'@ohos.window'
;
import
{
AppContext
}
from
'wdKit'
;
export
default
class
EntryAbility
extends
UIAbility
{
onCreate
(
want
,
launchParam
)
{
hilog
.
info
(
0x0000
,
'testTag'
,
'%{public}s'
,
'Ability onCreate'
);
AppContext
.
init
(
this
.
context
)
}
onDestroy
()
{
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/components/BottomNavigationComponent.ets
View file @
3fca87f
...
...
@@ -4,6 +4,8 @@ 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';
const TAG = 'BottomNavigationComponent';
...
...
@@ -28,10 +30,9 @@ export struct BottomNavigationComponent {
*/
readonly SIXTY_OPACITY: number = 0.6;
async aboutToAppear() {
Logger.info(TAG, `aboutToAppear currentNavIndex: ${this.currentNavIndex}`);
let bottomNav = await PageViewModel.getBottomNavData(
getContext(this)
)
let bottomNav = await PageViewModel.getBottomNavData()
if (bottomNav) {
Logger.info(TAG, `aboutToAppear, bottomNav.length: ${bottomNav.length}`);
this.bottomNavList = bottomNav
...
...
@@ -47,8 +48,9 @@ export struct BottomNavigationComponent {
ForEach(this.bottomNavList, (navItem: BottomNavDTO, index: number) => {
TabContent() {
Column() {
if (navItem.topNavChannelList && navItem.topNavChannelList.length == 0 && navItem.name == '我的') {
PageComponent({ compList: new LazyDataSource() }) // todo:我的页面组件数据列表
if (UIUtils.isMine(navItem)) {
// 我的页面组件数据列表
MinePageComponent()
} else {
TopNavigationComponent({ topNavList: navItem.topNavChannelList })
}
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/components/MinePageComponent.ets
0 → 100644
View file @
3fca87f
const TAG = 'PageComponent';
/**
* 我的页面
*/
@Component
export struct MinePageComponent {
// TODO 待完善
build() {
Text('我的页面')
}
}
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/components/PageComponent.ets
View file @
3fca87f
import { CommonConstants, CompStyle, ViewType } from 'wdConstant';
import { LazyDataSource, Logger } from 'wdKit';
import { CompDTO } from '../repository/bean/CompDTO';
import
{ PageViewModel }
from '../viewmodel/PageViewModel';
import
PageViewModel
from '../viewmodel/PageViewModel';
import { EmptyComponent } from './EmptyComponent';
import { ErrorComponent } from './ErrorComponent';
import { LabelComponent } from './LabelComponent';
...
...
@@ -17,6 +17,10 @@ export struct PageComponent {
// Group数据及子组件数据
@State compList: LazyDataSource<CompDTO> = new LazyDataSource();
@State currentTopNavSelectedIndex: number = 0;
@State pageId: string = "";
@State channelId: string = "";
// @Link @Watch('onChange') paIndex:number
build() {
if (this.viewType == ViewType.LOADING) {
...
...
@@ -59,18 +63,27 @@ export struct PageComponent {
async aboutToAppear() {
Logger.info(TAG, `aboutToAppear, this.pageId: ${this.viewType} this.currentTopNavSelectedIndex: ${this.currentTopNavSelectedIndex}`);
if (this.currentTopNavSelectedIndex === 1) { // 顶导tab的第0个item是【热点】,第1个item是【推荐】
this.compList.replaceAll()
let pageDto = await PageViewModel.getPageData2(getContext(this))
if (pageDto && pageDto.compList) {
this.compList.push(...pageDto.compList)
}
} else {
let pageDto = await PageViewModel.getPageData1(getContext(this))
// if (this.currentTopNavSelectedIndex === 1) { // 顶导tab的第0个item是【热点】,第1个item是【推荐】
// this.compList.replaceAll()
// let pageDto = await PageViewModel.getPageData2(getContext(this))
// if (pageDto && pageDto.compList) {
// this.compList.push(...pageDto.compList)
// }
// } else {
// let pageDto = await PageViewModel.getPageData1(getContext(this))
// if (pageDto && pageDto.compList) {
// this.compList.push(...pageDto.compList)
// }
// }
// if (this.currentTopNavSelectedIndex != 1) { // 顶导tab的第0个item是【热点】,第1个item是【推荐】
// return
// }
Logger.debug(TAG,'lllllalalal: ' + this.pageId+' , ' + this.channelId);
let pageDto = await PageViewModel.getPageData(this.pageId, this.pageId, this.channelId)
if (pageDto && pageDto.compList) {
this.compList.push(...pageDto.compList)
}
}
}
}
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/components/TopNavigationComponent.ets
View file @
3fca87f
...
...
@@ -21,7 +21,7 @@ export struct TopNavigationComponent {
Tabs() {
ForEach(this.topNavList, (navItem: TopNavDTO, index: number) => {
TabContent() {
PageComponent({ currentTopNavSelectedIndex: index })
PageComponent({ currentTopNavSelectedIndex: index
, pageId: navItem.pageId, channelId: navItem.channelId
})
}
.tabBar(this.tabBarBuilder(navItem, index))
}, (navItem: TopNavDTO) => JSON.stringify(navItem));
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/network/HttpUrlUtils.ets
0 → 100644
View file @
3fca87f
import HashMap from '@ohos.util.HashMap'
import { ConfigConstants } from 'wdConstant/src/main/ets/constants/ConfigConstants'
import { DateTimeUtils, Logger } from 'wdKit'
/**
* 网络请求业务侧工具类
*/
export class HttpUrlUtils {
/**
* 现网地址
*/
static readonly HOST: string = "https://pd-apis-uat.pdnews.cn";
/**
* 展现comp接口
*/
static readonly COMP_PATH: string = "/api/rmrb-bff-display-zh/display/zh/c/compInfo";
static getCommonHeaders(): HashMap<string, string> {
let headers: HashMap<string, string> = new HashMap<string, string>()
headers.set('User-Agent', 'Dalvik/2.1.0 (Linux; U; Android 13; 22101317C Build/TKQ1.221013.002)')
headers.set('channel', this.getChannel())
headers.set('appCode', ConfigConstants.appCode)
headers.set('plat', this.getPlat())
headers.set('Authorization', 'APPCODE 83092caa603a421aa0222308b3f6b27a')
headers.set('Content-Type', 'application/json; charset=utf-8')
headers.set('timestamp', this.getTimestamp())
headers.set('RMRB-X-TOKEN', this.getXToken())
headers.set('device_id', this.getDeviceId())
headers.set('cookie', 'RMRB-X-TOKEN=eyJhbGciOiJIUzI1NiIsImtpZCI6ImQ4WkI2QkhxSEZrdjJ2U25BNlRwZEdKRjBHcjItVzBvS2FaYzdLOUUycmcifQ.eyJpc3MiOiJwZW9wbGVzLWRhaWx5LWZvdXJhIiwic3ViIjoicGVvcGxlcy1kYWlseS1mb3VyYSIsImV4cCI6MTcwMzY0OTYwNiwidXNlcklkIjo0NTk3NzYyOTc0NzQ5NDksInVzZXJWZXJzaW9uIjoiNDU5Nzc2Mjk3NDc0OTQ5XzIiLCJ1c2VyTmFtZSI6IkJ1bGlraWtpMTgxIiwidXNlclR5cGUiOjIsImNyZWF0b3JJZCI6NDI2NTM5MH0.jhQ9kylcm3FxWf0-lBMZuLkdtIQ6XpFnAi0AFZJNwfc')
headers.set('build_version', this.getVersion())
headers.set('adcode', this.getAdCode())
headers.set('os_version', this.getOsVersion())
headers.set('X-Ca-Stage', 'PRE')
headers.set('versionCode', this.getVersionCode())
headers.set('system', this.getTerminalId())
headers.set('version_name', 'debug')
headers.set('EagleEye-TraceID', '0B6DE03D2997435BA875FFBE05425ED2')
headers.set('imei', this.getImei())
headers.set('Accept-Language', 'zh')
headers.set('city', this.getCity())
headers.set('city_dode', this.getCityCode())
// TODO 判断是否登录
headers.set('userId', this.getUserId())
headers.set('userType', this.getUserType())
Logger.debug("TAG", 'getCommonHeaders headers: ' + headers);
return headers;
}
static getCompInfoUrl(pageId: string, groupId: string, channelId: string) {
let url = this.HOST + this.COMP_PATH;
// TODO 暂定只请求第一页,后续对接分页加载,参数再调整
url = url + "?channelStrategy=2&pageSize=20&pageNum=1&loadStrategy=first_load"
+ "&districtCode=" + this.getDistrictCode()
+ "&provinceCode=" + this.getProvinceCode()
+ "&cityCode=" + this.getCityCode()
// + "&refreshTime=" + DateTimeUtils.getCurrentTimeMillis()
+ "&refreshTime=" + "1703472405653"
+ "&pageId=" + pageId
+ "&groupId=" + groupId
+ "&channelId=" + channelId;
return url;
}
private static getCity() {
// TODO 对接定位
return '%E5%90%88%E8%82%A5%E5%B8%82';
}
private static getChannel() {
// TODO 对接配置
return 'rmrb_china_0000';
}
private static getPlat() {
return 'Phone';
}
private static getTimestamp() {
// return DateTimeUtils.getCurrentTime() + '';
return '155203523';
}
private static getXToken() {
return 'eyJhbGciOiJIUzI1NiIsImtpZCI6ImQ4WkI2QkhxSEZrdjJ2U25BNlRwZEdKRjBHcjItVzBvS2FaYzdLOUUycmcifQ.eyJpc3MiOiJwZW9wbGVzLWRhaWx5LWZvdXJhIiwic3ViIjoicGVvcGxlcy1kYWlseS1mb3VyYSIsImV4cCI6MTcwMzY0OTYwNiwidXNlcklkIjo0NTk3NzYyOTc0NzQ5NDksInVzZXJWZXJzaW9uIjoiNDU5Nzc2Mjk3NDc0OTQ5XzIiLCJ1c2VyTmFtZSI6IkJ1bGlraWtpMTgxIiwidXNlclR5cGUiOjIsImNyZWF0b3JJZCI6NDI2NTM5MH0.jhQ9kylcm3FxWf0-lBMZuLkdtIQ6XpFnAi0AFZJNwfc';
}
private static getDeviceId() {
// TODO
return 'b5cf725d-193d-3215-8c77-e76fe15ce64d';
}
private static getVersion() {
// TODO
return '202312251034';
}
private static getVersionCode() {
// TODO
return '7301';
}
private static getAdCode() {
return '340000';
}
private static getOsVersion() {
// TODO
return '13';
}
private static getCityCode() {
// TODO
return '340100';
}
private static getProvinceCode() {
// TODO
return '340000';
}
/**
* 地区code
*/
private static getDistrictCode() {
// TODO
return '340103';
}
private static getTerminalId() {
return 'Android';
}
private static getImei() {
// TODO
return 'b5cf725d-193d-3215-8c77-e76fe15ce64d';
}
private static getUserId() {
// TODO 对接登录
return '459776297474949';
}
private static getUserType() {
return '2';
}
}
\ No newline at end of file
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/repository/PageRepository.ets
View file @
3fca87f
import { ResponseDTO, WDHttp } from 'wdNetwork';
import { NavigationBodyDTO } from './bean/NavigationBodyDTO';
import { PageDTO } from './bean/PageDTO';
import HashMap from '@ohos.util.HashMap';
import { HttpUrlUtils } from '../network/HttpUrlUtils';
export class PageRepository {
static fetchNavigationDataApi(url: string) {
return WDHttp.get<ResponseDTO<NavigationBodyDTO>>(url)
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
return WDHttp.get<ResponseDTO<NavigationBodyDTO>>(url, headers)
};
static fetchPageData(url: string) {
return WDHttp.get<ResponseDTO<PageDTO>>(url)
static fetchPageData(pageId: string, groupId: string, channelId: string) {
let url = HttpUrlUtils.getCompInfoUrl(pageId, groupId, channelId)
let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
return WDHttp.get<ResponseDTO<PageDTO>>(url, headers)
};
}
\ No newline at end of file
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/repository/UIUtils.ets
0 → 100644
View file @
3fca87f
/**
* 网络请求业务侧工具类
*/
import { BottomNavDTO } from './bean/BottomNavDTO';
export class UIUtils {
/**
* 判断是否为我的页面
*/
static isMine(navItem: BottomNavDTO): boolean {
if (navItem == null) {
return false;
}
return navItem.type == '2';
}
}
\ No newline at end of file
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/repository/bean/BottomNavDTO.ets
View file @
3fca87f
...
...
@@ -36,5 +36,6 @@ export interface BottomNavDTO {
statusBarColor: number;
topNavChannelList: TopNavDTO[];
topStyle: string;
// 1-普通页面,2-我的
type: string;
}
\ No newline at end of file
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/repository/bean/TopNavDTO.ets
View file @
3fca87f
...
...
@@ -2,7 +2,7 @@
* 顶导
*/
export interface TopNavDTO {
channelId:
number
;
channelId:
string
;
channelStyle: number;
channelType: number;
defaultPermitted: number;
...
...
@@ -21,7 +21,7 @@ export interface TopNavDTO {
myChannel: string;
name: string;
num: number;
pageId:
number
;
pageId:
string
;
pageType: string;
underlineCColor: string;
}
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/viewmodel/BaseViewModel.ets
0 → 100644
View file @
3fca87f
import { Logger } from 'wdKit';
import { ResponseDTO } from 'wdNetwork';
/**
* 处理返回后的数据
*/
export abstract class BaseViewModel {
abstract getLogTag(): string;
public isRespondsInvalid(resDTO: ResponseDTO<any>, tag?: string): boolean {
if (resDTO == null) {
Logger.error(this.getLogTag(), `${tag == null ? '' : tag + ', '}resDTO is empty`);
return true;
}
// code = 0, success
if (resDTO.code != 0) {
Logger.error(this.getLogTag(), `${tag == null ? '' : tag + ', '}code error, code:${resDTO.code}, message:${resDTO.message}`);
return true;
}
if (!resDTO.data) {
Logger.error(this.getLogTag(), `${tag == null ? '' : tag + ', '}resDTO.data is null`);
return true;
}
return false;
}
}
\ No newline at end of file
...
...
PeopleDaily_Harmony/wdComponent/src/main/ets/viewmodel/PageViewModel.ets
View file @
3fca87f
import { Logger, ResourcesUtils } from 'wdKit';
import {
AppContext,
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';
const TAG = 'PageViewModel';
/**
* mock数据开关,默认关。
* mock数据是本地json数据,可自行修改内容(‘entry\src\main\resources\rawfile\’目录)
*/
const mock_switch = false;
/**
* 处理返回后的数据
*/
export class PageViewModel {
export class PageViewModel extends BaseViewModel {
getLogTag() {
return TAG;
}
/**
* get Nav Data from Resource .
*
* @return BottomNavBean[] Nav Data List
*/
static async getBottomNavData(
context: Context
): Promise<BottomNavDTO[]> {
static async getBottomNavData(): Promise<BottomNavDTO[]> {
Logger.info(TAG, `getBottomNavData start`);
let compRes: ResponseDTO<NavigationBodyDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<NavigationBodyDTO>>(
context,
'bottom_nav.json');
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 []
...
...
@@ -62,8 +72,8 @@ export class PageViewModel {
*
* @return {GroupDTO} compRes.data
*/
static async getPageData1(context: Context): Promise<PageDTO> {
let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>(context, 'comp_list0.json');
static 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`);
return {} as PageDTO
...
...
@@ -77,8 +87,8 @@ export class PageViewModel {
*
* @return {GroupDTO} compRes.data
*/
static async getPageData2(context: Context): Promise<PageDTO> {
let compRes: ResponseDTO<PageDTO> | null = await ResourcesUtils.getResourcesJson<ResponseDTO<PageDTO>>(context, 'comp_list2.json');
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
...
...
@@ -87,18 +97,12 @@ export class PageViewModel {
return compRes.data
}
static getPageData(url: string): Promise<PageDTO> {
async getPageData(pageId: string, groupId: string, channelId: string): Promise<PageDTO> {
Logger.error(TAG, 'getPageData pageId: ' + pageId);
return new Promise<PageDTO>((success, error) => {
PageRepository.fetchPageData(url).then((resDTO: ResponseDTO<PageDTO>) => {
if (!resDTO) {
Logger.error(TAG, 'getPageData then resDTO is empty');
error("page data is empty");
return
}
if (resDTO.code != http.ResponseCode.OK || !resDTO.data) {
Logger.error(TAG, `getPageData then code:${resDTO.code}, message:${resDTO.message}`);
error(`get page data error code:${resDTO.code}, message:${resDTO.message}`);
PageRepository.fetchPageData(pageId, groupId, channelId).then((resDTO: ResponseDTO<PageDTO>) => {
if (this.isRespondsInvalid(resDTO, 'getPageData')) {
error("page data invalid");
return
}
Logger.info(TAG, "getPageData then,resDTO.timeStamp:" + resDTO.timestamp);
...
...
@@ -112,7 +116,6 @@ export class PageViewModel {
}
let pageViewModel = new PageViewModel();
export default pageViewModel as PageViewModel;
\ No newline at end of file
...
...
PeopleDaily_Harmony/wdConstant/src/main/ets/constants/ConfigConstants.ets
View file @
3fca87f
...
...
@@ -28,7 +28,7 @@ export class ConfigConstants {
/**
* 产品渠道应用对照关系:
*/
static readonly appCode: string = "";
static readonly appCode: string = "
83092caa603a421aa0222308b3f6b27a
";
/**
* 基线代码和客户端应用版本号规范
*/
...
...
PeopleDaily_Harmony/wdKit/src/main/ets/Index.ets
View file @
3fca87f
...
...
@@ -10,3 +10,7 @@ export { BasicDataSource } from './utils/BasicDataSource';
export { LazyDataSource } from './utils/LazyDataSource'
export { DateTimeUtils } from './utils/DateTimeUtils'
export { AppContext } from './utils/AppContext'
...
...
PeopleDaily_Harmony/wdKit/src/main/ets/utils/AppContext.ets
0 → 100644
View file @
3fca87f
/**
* 应用ability上下文管理类
*/
export class AppContext {
private static sContext: Context;
public static init(abilityContext: Context) {
this.sContext = abilityContext;
}
public static getAbilityContext() {
return this.sContext;
}
}
\ No newline at end of file
...
...
PeopleDaily_Harmony/wdKit/src/main/ets/utils/DateTimeUtils.ts
View file @
3fca87f
...
...
@@ -316,6 +316,21 @@ export class DateTimeUtils {
static
isTomorrow
(
date
:
number
)
:
boolean
{
return
this
.
isSameDay
(
date
,
this
.
addDays
(
Date
.
now
(),
1
).
getTime
());
}
/**
* 获取当前时间戳,秒
* */
public
static
getCurrentTime
()
:
number
{
let
time
:
number
=
new
Date
().
getTime
();
return
Math
.
floor
(
time
/
1000
);
}
/**
* 获取当前时间戳,毫秒
* */
public
static
getCurrentTimeMillis
()
:
number
{
return
new
Date
().
getTime
();
}
}
// const dateTimeUtils = new DateTimeUtils()
\ No newline at end of file
...
...
PeopleDaily_Harmony/wdKit/src/main/ets/utils/ResourcesUtils.ets
View file @
3fca87f
import buffer from '@ohos.buffer';
import { AppContext } from './AppContext';
import { Logger } from './Logger';
const TAG = "ResourcesUtils";
...
...
@@ -13,7 +14,10 @@ export class ResourcesUtils {
* @param filename 仅文件名(不包含路径,文件在src\main\resources\rawfile下),如:"agconnect-services.json",test.xml,
* @returns text 字符串
*/
static getResourcesText(context: Context, filename: string): Promise<string> {
static getResourcesText(filename: string, context?: Context): Promise<string> {
if (context == null) {
context = AppContext.getAbilityContext();
}
return new Promise((success, error) => {
Logger.info(TAG, "ResourcesUtils getResourcesText filename:" + filename);
context.resourceManager.getRawFileContent(filename).then((content: Uint8Array) => {
...
...
@@ -53,14 +57,17 @@ export class ResourcesUtils {
* 调用方式:
* let compRes: MGHttp.ResponseDTO<BodyComponent> = await ResourcesUtils.getResourcesJson<MGHttp.ResponseDTO<BodyComponent>>(context, 'model/componentList.json');
*/
static getResourcesJson<R>(
context:Context, filename: string
): Promise<R> {
static getResourcesJson<R>(
filename: string, context?: Context
): Promise<R> {
Logger.info(TAG, `getResourcesJson filename: ${filename}`);
if (context == null) {
context = AppContext.getAbilityContext();
}
return new Promise<R>((resolve, reject) => {
ResourcesUtils.getResourcesText(
context, filename
)
ResourcesUtils.getResourcesText(
filename, context
)
.then((text: string) => {
try {
// let config: R = JSON.parse(text)
let config:R = JSON.parse(text) as R;
let config:
R = JSON.parse(text) as R;
if (config) {
Logger.info(TAG, "getResourcesJson parse JSON file success.");
// Logger.info(TAG, `getResourcesJson config : ${JSON.stringify(config)}`);
...
...
@@ -74,7 +81,7 @@ export class ResourcesUtils {
reject(err)
}
})
.catch((error:Error) => {
.catch((error:
Error) => {
reject(error)
})
})
...
...
PeopleDaily_Harmony/wdNetwork/src/main/ets/http/HttpRequest.ts
View file @
3fca87f
import
type
{
AxiosError
,
AxiosRequestConfig
}
from
'@ohos/axios'
;
import
{
AxiosError
,
AxiosHeaders
,
AxiosRequestConfig
,
RawAxiosRequestHeaders
}
from
'@ohos/axios'
;
import
HashMap
from
'@ohos.util.HashMap'
;
import
service
from
'./AxiosRequest'
;
export
class
HttpRequest
{
...
...
@@ -6,17 +7,28 @@ export class HttpRequest {
static
request
<
T
=
any
>
(
config
:
AxiosRequestConfig
)
:
Promise
<
T
>
{
return
new
Promise
<
T
>
((
resolve
,
reject
)
=>
{
service
.
request
<
any
,
T
>
(
config
)
.
then
((
res
:
T
)
=>
{
.
then
((
res
:
T
)
=>
{
resolve
(
res
)
})
.
catch
((
err
:
AxiosError
)
=>
{
.
catch
((
err
:
AxiosError
)
=>
{
reject
(
err
)
})
})
// return service.request<any, T>(config)
}
static
get
<
T
=
any
>
(
url
:
string
,
config
?:
AxiosRequestConfig
)
:
Promise
<
T
>
{
private
static
get0
<
T
=
any
>
(
url
:
string
,
config
?:
AxiosRequestConfig
)
:
Promise
<
T
>
{
return
service
.
get
(
url
,
config
)
}
static
get
<
T
=
any
>
(
url
:
string
,
headers
?:
HashMap
<
string
,
string
>
)
:
Promise
<
T
>
{
let
requestHeaders
:
AxiosHeaders
=
new
AxiosHeaders
()
headers
.
forEach
((
v
,
k
)
=>
{
requestHeaders
.
set
(
k
,
v
);
});
let
config
:
AxiosRequestConfig
=
{
headers
:
requestHeaders
as
RawAxiosRequestHeaders
}
return
service
.
get
(
url
,
config
)
}
...
...
Please
register
or
login
to post a comment