zhenghy
import { abilityAccessCtrl, common, Permissions } from '@kit.AbilityKit';
import { http } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { promptAction } from '@kit.ArkUI';
import { image } from '@kit.ImageKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import fs from '@ohos.file.fs';
const PERMISSIONS: Array<Permissions> = [
'ohos.permission.READ_MEDIA',
'ohos.permission.WRITE_MEDIA'
];
@Component
export struct ImageDownloadComponent {
@State image: PixelMap | undefined = undefined;
@State photoAccessHelper: photoAccessHelper.PhotoAccessHelper | undefined = undefined; // 相册模块管理实例
@State imageBuffer: ArrayBuffer | undefined = undefined; // 图片ArrayBuffer
url: string = ''
build() {
Column() {
Image($r('app.media.icon_arrow_left_white'))
.width(24)
.height(24)
.aspectRatio(1)
.interpolation(ImageInterpolation.High)
.rotate({ angle: -90 })
.onClick(async () => {
console.info(`cj2024 onClick ${this.imageBuffer}`)
if (this.imageBuffer !== undefined) {
await this.saveImage(this.imageBuffer);
promptAction.showToast({
message: $r('app.string.image_request_success'),
duration: 2000
})
}
})
}
}
async aboutToAppear(): Promise<void> {
console.info(`cj2024 图片下载 ${this.url}`)
const context = getContext(this) as common.UIAbilityContext;
const atManager = abilityAccessCtrl.createAtManager();
await atManager.requestPermissionsFromUser(context, PERMISSIONS);
this.getPicture();
}
/**
* 通过http的request方法从网络下载图片资源
*/
async getPicture() {
console.info(`cj2024 getPicture`)
http.createHttp()
.request(this.url,
(error: BusinessError, data: http.HttpResponse) => {
if (error) {
// 下载失败时弹窗提示检查网络,不执行后续逻辑
promptAction.showToast({
message: $r('app.string.image_request_fail'),
duration: 2000
})
return;
}
this.transcodePixelMap(data);
// 判断网络获取到的资源是否为ArrayBuffer类型
console.info(`cj2024 getPicture ${data.result}`)
if (data.result instanceof ArrayBuffer) {
console.info(`cj2024 getPicture 222`)
this.imageBuffer = data.result as ArrayBuffer;
}
}
)
}
/**
* 使用createPixelMap将ArrayBuffer类型的图片装换为PixelMap类型
* @param data:网络获取到的资源
*/
transcodePixelMap(data: http.HttpResponse) {
console.info(`cj2024 transcodePixelMap ${data.responseCode}`)
if (http.ResponseCode.OK === data.responseCode) {
const imageData: ArrayBuffer = data.result as ArrayBuffer;
// 通过ArrayBuffer创建图片源实例。
const imageSource: image.ImageSource = image.createImageSource(imageData);
const options: image.InitializationOptions = {
'alphaType': 0, // 透明度
'editable': false, // 是否可编辑
'pixelFormat': 3, // 像素格式
'scaleMode': 1, // 缩略值
'size': { height: 100, width: 100 }
}; // 创建图片大小
// 通过属性创建PixelMap
imageSource.createPixelMap(options).then((pixelMap: PixelMap) => {
this.image = pixelMap;
});
}
}
/**
* 保存ArrayBuffer到图库
* @param buffer:图片ArrayBuffer
* @returns
*/
async saveImage(buffer: ArrayBuffer | string): Promise<void> {
console.info(`cj2024 saveImage buffer ${buffer}`)
const context = getContext(this) as common.UIAbilityContext; // 获取getPhotoAccessHelper需要的context
const helper = photoAccessHelper.getPhotoAccessHelper(context); // 获取相册管理模块的实例
const uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg'); // 指定待创建的文件类型、后缀和创建选项,创建图片或视频资源
console.info(`cj2024 saveImage uri ${uri}`)
const file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
await fs.write(file.fd, buffer);
await fs.close(file.fd);
}
}
\ No newline at end of file
... ...
import { PhotoListBean } from 'wdBean/Index';
import { Logger } from 'wdKit/Index';
import { MultiPictureDetailItemComponent } from './MultiPictureDetailItemComponent';
import { display } from '@kit.ArkUI';
import { display, router } from '@kit.ArkUI';
import { ImageDownloadComponent } from './ImageDownloadComponent';
const TAG = 'ImageSwiperComponent';
... ... @@ -35,6 +36,20 @@ export struct ImageSwiperComponent {
build() {
RelativeContainer() {
Image($r('app.media.icon_arrow_left_white'))
.width(24)
.height(24)
.aspectRatio(1)
.interpolation(ImageInterpolation.High)
.alignRules({
top: { anchor: "__container__", align: VerticalAlign.Top },
left: { anchor: "__container__", align: HorizontalAlign.Start }
})
.onClick(() => {
router.back();
})
.id("backImg")
if (this.photoList && this.photoList?.length > 0) {
Swiper(this.swiperController) {
ForEach(this.photoList, (item: PhotoListBean) => {
... ... @@ -96,6 +111,19 @@ export struct ImageSwiperComponent {
middle: { anchor: "__container__", align: HorizontalAlign.Center }
})
}
ImageDownloadComponent({ url: this.photoList[this.swiperIndex].picPath })
.alignRules({
bottom: { anchor: "__container__", align: VerticalAlign.Bottom },
right: { anchor: "__container__", align: HorizontalAlign.End }
})
.margin({
top: 8,
left: 18,
bottom: 24,
right: 18
})
.id("downloadImg")
}
.width('100%')
.height('100%')
... ...
import { CommonConstants } from 'wdConstant';
import { Logger } from 'wdKit';
const TAG = 'EmptyComponent';
/**
* WDViewDefaultType 缺省页
*/
export const enum WDViewDefaultType {
///无网
WDViewDefaultType_NoNetwork,
///网络失败 请稍后重试-倒计时
WDViewDefaultType_NetworkFailed,
///内容获取失败
WDViewDefaultType_ContentFailed,
}
/**
* 空数据/无数据
*/
@Preview
@Component
export struct MultiPictureDetailEmptyComponent {
// private emptySize: SizeOptions = {};
@State emptyWidth: string | number = CommonConstants.FULL_PARENT;
@State emptyHeight: string | number = CommonConstants.FULL_PARENT;
@State emptyType: number = WDViewDefaultType.WDViewDefaultType_ContentFailed
/**
* The empty image width percentage setting.
*/
readonly EMPTY_IMAGE_WIDTH: string = '15%';
/**
* The empty image height percentage setting.
*/
readonly EMPTY_IMAGE_HEIGHT: string = '15%';
/**
* The empty data text component margin top.
*/
readonly EMPTY_TIP_TEXT_MARGIN_TOP: string = '10';
/**
* The empty data text opacity.
*/
readonly TEXT_OPACITY: number = 1;
build() {
this.noProgrammeData();
}
/**
* 无数据,空白view组件
*/
@Builder
noProgrammeData() {
Column() {
Image(this.buildNoDataTipImage())
.width('this.EMPTY_IMAGE_WIDTH')
.height(this.EMPTY_IMAGE_HEIGHT)
.objectFit(ImageFit.Contain)
// .border({ width: 1, color: Color.Red, radius: 6 })
Text(this.buildNoDataTip())
.fontSize($r('app.float.font_size_14'))
.fontColor('#999999')
.fontWeight(FontWeight.Normal)
.opacity(this.TEXT_OPACITY)
.margin({ top: this.EMPTY_TIP_TEXT_MARGIN_TOP })
.onClick((event: ClickEvent) => {
Logger.info(TAG, `noProgrammeData onClick event?.source: ${event.source}`);
})
Button('点击重试', { type: ButtonType.Normal, stateEffect: true })
.borderRadius(4)
.margin(16)
.height(28)
.fontSize(12)
.fontColor('#CCCCCC')
.fontFamily('PingFang SC-Medium')
.border({ width: 1, color: '#545454' })
.backgroundColor(Color.Black)
}
.justifyContent(FlexAlign.Center)
.width(this.emptyWidth)
.height(this.emptyHeight)
.backgroundColor(Color.Black)
}
buildNoDataTip(): string {
Logger.info(TAG, "buildNoDataTip");
let contentString: string = '获取内容失败请重试'
if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoNetwork) {
contentString = '网络出小差了,请检查网络后重试'
} else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_ContentFailed) {
contentString = '获取内容失败请重试'
}
return contentString
}
buildNoDataTipImage(): Resource | string {
Logger.info(TAG, "buildNoDataTip");
let imageString: Resource | string = $r('app.media.icon_no_content')
if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoNetwork) {
imageString = $r('app.media.icon_no_net')
} else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_ContentFailed) {
imageString = $r('app.media.icon_no_content')
} else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NetworkFailed) {
imageString = $r('app.media.icon_no_net')
}
return imageString
}
}
\ No newline at end of file
... ...
import { Logger } from 'wdKit';
import { ResponseDTO } from 'wdNetwork';
import {
ContentDetailDTO,
PhotoListBean,
... ... @@ -11,6 +12,7 @@ import display from '@ohos.display';
import font from '@ohos.font';
import { OperRowListView } from './view/OperRowListView';
import { MultiPictureDetailItemComponent } from './MultiPictureDetailItemComponent';
import { MultiPictureDetailEmptyComponent } from './MultiPictureDetailEmptyComponent';
import { DateTimeUtils } from 'wdKit/Index';
import { HttpUrlUtils } from 'wdNetwork/Index';
import { WDRouterPage, WDRouterRule } from 'wdRouter/Index';
... ... @@ -34,6 +36,7 @@ export struct MultiPictureDetailPageComponent {
@State swiperIndex: number = 0;
@Provide followStatus: string = '0' // 关注状态
private scroller: Scroller = new Scroller()
@State netStatus: number = 0 // 存储网络状态用来展示缺省图
//watch监听页码回调
onCurrentPageNumUpdated(): void {
... ... @@ -56,10 +59,6 @@ export struct MultiPictureDetailPageComponent {
familySrc: $rawfile('font/BebasNeue_Regular.otf')
})
this.getContentDetailData()
if (HttpUrlUtils.getUserId()) {
this.getInteractBrowsOperate()
this.getBatchAttentionStatus()
}
}
aboutToDisappear() {
... ... @@ -238,6 +237,16 @@ export struct MultiPictureDetailPageComponent {
})
.height(px2vp(this.titleHeight) + 64)
} else {
if (this.netStatus === 1) {
MultiPictureDetailEmptyComponent({ emptyType: 2})
.id('e_empty_content')
.alignRules({
center: { anchor: "__container__", align: VerticalAlign.Center },
middle: { anchor: "__container__", align: HorizontalAlign.Center }
})
}
}
OperRowListView({
contentDetailData: this.contentDetailData,
})
... ... @@ -256,7 +265,6 @@ export struct MultiPictureDetailPageComponent {
.border({ width: { top: 0.5 }, color: '#FFFFFF' })
.id('e_oper_row')
}
}
.width('100%')
.height('100%')
.backgroundColor(Color.Black)
... ... @@ -266,11 +274,41 @@ export struct MultiPictureDetailPageComponent {
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
}
private async getContentDetailData() {
private getContentDetailData() {
try {
let data = await MultiPictureDetailViewModel.getDetailData(this.relId, this.contentId, this.relType)
this.contentDetailData = data?.[0];
PageRepository.fetchDetailData(this.relId, this.contentId, this.relType).then((resDTO: ResponseDTO<ContentDetailDTO[]>) => {
// Logger.info(TAG, `fetchDetailData then,navResDTO.timestamp ${resDTO.timestamp}`);
// Logger.info(TAG, `fetchDetailData then,navResDTO: ${JSON.stringify(resDTO)}`);
// Logger.info(TAG, `fetchDetailData then,navResDTO.data: ${JSON.stringify(resDTO.data)}`);
if (!resDTO || !resDTO.data) {
Logger.error(TAG, 'fetchDetailData is empty');
return
}
if (resDTO.code != 0) {
Logger.error(TAG, `fetchDetailData then code:${resDTO.code}, message:${resDTO.message}`);
return
}
this.contentDetailData = resDTO.data?.[0];
Logger.info(TAG, `contentDetailData:${JSON.stringify(this.contentDetailData)}`)
if (HttpUrlUtils.getUserId()) {
this.getInteractBrowsOperate()
this.getBatchAttentionStatus()
}
}).catch((err: Error) => {
Logger.info(TAG, `fetchDetailData then,err: ${JSON.stringify(err)}`);
/*// 请求失败处理
if (err.response) {
// 请求已发出,但服务器响应的状态码不在2xx范围内
console.error('请求失败,状态码:', err.response.status);
console.error('响应数据:', err.response.data);
} else if (err.request) {
// 请求已发出,但无响应(例如:网络故障)
console.error('请求已发出,但无响应:', err.request);
} else {
// 发生了其他类型的错误(如配置错误或拒绝权限等)
console.error('请求发生错误:', err.message);
}*/
})
} catch (exception) {
}
... ...
... ... @@ -42,6 +42,8 @@ export struct BottomNavigationComponent {
let bottomNav = await PageViewModel.getBottomNavData(getContext(this))
if (bottomNav && bottomNav.bottomNavList != null) {
Logger.info(TAG, `aboutToAppear, bottomNav.length: ${bottomNav.bottomNavList.length}`);
// 使用filter方法移除name为'服务'的项
bottomNav.bottomNavList = bottomNav.bottomNavList.filter(item => item.name !== '服务');
this.bottomNavList = bottomNav.bottomNavList
}
}
... ...