分享工具
分享工具
点击查看代码
import { systemShare } from '@kit.ShareKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { Logger } from '@hzw/logger';
import { UserInfo } from '@features/mine';
import { BackEndApi } from '@common/basic';
import { image } from '@kit.ImageKit';
import { fileUri } from '@kit.CoreFileKit';
const TAG = 'VideoShared';
/**
* @Author:
* @Date: 2025.11.01
* @Description: 分享工具
*/
export class VideoScenarioForURL {
/**
* @Author:
* @Date:
* @Description: 分享链接
*/
public shareURL(ctx: UIContext, contentID: string,videoName:string) {
const uiContext: UIContext = ctx;
const contextFaker: Context = uiContext.getHostContext() as Context;
//获取个人所在区域是否为中国
let AreaInfo: string = AppStorage.get<UserInfo>("userInfo")?.area_code as string;
Logger.i(TAG, '当前分享的视频地域为:' + AreaInfo);
const videoPath: string = `${AreaInfo == "CN" ? BackEndApi.SHARE_URL : BackEndApi.SHARE_URL_O}${contentID}`;
Logger.i(TAG, '当前分享的视频地址为:' + videoPath);
// 构造分享链接数据
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
title: videoName, // 链接名字
content: videoPath// 分享链接
});
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
const context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
controller.show(context, {
selectionMode: systemShare.SelectionMode.SINGLE,
previewMode: systemShare.SharePreviewMode.DETAIL,
}).then(() => {
Logger.i(TAG, 'ShareController show success.');
}).catch((error: BusinessError) => {
Logger.e(TAG, `ShareController show error. code: ${error?.code}, message: ${error?.message}`);
});
}
/**
* @Author:
* @Date: 2025.11.01
* @Description: 分享视频
* @param filePath:沙箱路径
*/
public async shareVideo(ctx: UIContext, filePath: string) {
const uiContext: UIContext = ctx;
//构造分享数据
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.VIDEO,
uri: fileUri.getUriFromPath(filePath),
title: 'Video title', // 分享标题
description: 'Video Description', // 分享描述
});
//拉起面板进行分享
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
const context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
controller.show(context, {
selectionMode: systemShare.SelectionMode.SINGLE,
previewMode: systemShare.SharePreviewMode.DETAIL,
}).then(() => {
Logger.i('ShareController show success.');
}).catch((error: BusinessError) => {
Logger.i(`ShareController show error. code: ${error?.code}, message: ${error?.message}`);
});
}
}
浙公网安备 33010602011771号