WDSystemShare.ets
4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { ShareContent, ShareContentImageAndText, ShareContentLink, ShareContentText,
ShareContentType,
ShareScene } from '../Constant';
import { WDShareInterface } from '../WDShareInterface';
import { AsyncCallback } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import { systemShare } from '@kit.ShareKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { JSON } from '@kit.ArkTS';
import { image } from '@kit.ImageKit';
import { WDShareBase } from '../WDShareBase';
import { Logger } from 'wdKit';
const TAG = "WDSystemShare"
export class WDSystemShare implements WDShareInterface {
shareContent(scene: ShareScene, content: ShareContent, contentType: ShareContentType, context?: common.UIAbilityContext): Promise<string> {
return new Promise(async (resolve, fail) => {
try {
let data = await this.getShareData(content, contentType, context)
let controller: systemShare.ShareController = new systemShare.ShareController(data);
controller.on('dismiss', () => {
resolve("dismiss")
});
controller.show(context, {
previewMode: systemShare.SharePreviewMode.DEFAULT,
selectionMode: systemShare.SelectionMode.SINGLE
});
Logger.debug(TAG, "分享控制器调用完成")
} catch (e) {
Logger.error(TAG, "异常1" + JSON.stringify(e))
fail(e)
}
})
}
getShareData(content: ShareContent, contentType: ShareContentType, context?: common.UIAbilityContext) : Promise<systemShare.SharedData> {
return new Promise((resolve, fail) => {
if (contentType === ShareContentType.PrueText) {
let prueText = content as ShareContentText
Logger.debug(TAG, "分享纯文本")
resolve(new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: prueText.text
}))
return;
}
if (contentType === ShareContentType.ImageAndText) {
let imageAndText = content as ShareContentImageAndText
Logger.debug(TAG, "分享图片和文本")
let data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: imageAndText.title
});
data.addRecord({
utd: utd.UniformDataType.PNG,
uri: imageAndText.imgURI // 这里必须为本地连接
});
resolve(data)
return
}
Logger.debug(TAG, "分享链接和文本")
let link = content as ShareContentLink
let posterImg: Uint8Array | undefined = undefined
let shareLink = this.generateShareLink(link)
Logger.debug("TAG, 分享 shareLink ==> " + shareLink)
if (!context || !link.posterImg) {
let data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.HYPERLINK,
// uri: link.link // SDK 设计 不能传这里
content: link.link,
title: link.title,
description: link.desc,
thumbnail: posterImg
});
resolve(data)
return
}
context.resourceManager.getMediaContent(link.posterImg)
.then((posterImg) => {
let data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.HYPERLINK,
// uri: link.link // SDK 设计 不能传这里
content: link.link,
title: link.title,
description: link.desc,
thumbnail: posterImg
});
resolve(data)
})
.catch((e: Error) => {
Logger.error(TAG, "异常" + JSON.stringify(e))
fail(e)
})
})
}
generateShareLink(shareContent: ShareContentLink) {
let link = "https://peopledailychinahosactivity.drcn.agconnect.link?deeplink=" + encodeURIComponent(shareContent.deeplink)
link += "&harmonyos_deeplink=" + encodeURIComponent(shareContent.deeplink)
link += "&harmonyos_package_name=com.peopledailychina.hosactivity"
link += "&harmonyos_fallback_url=" + encodeURIComponent(shareContent.link)
// link += "&android_deeplink=" + encodeURIComponent(shareContent.deeplink)
// link += "&android_fallback_url=" + encodeURIComponent(shareContent.link)
//
// link += "&ios_link=" + encodeURIComponent(shareContent.deeplink)
// link += "&ios_fallback_url=" + encodeURIComponent(shareContent.link)
link += "&preview_type=1"
link += "&landing_page_type=1"
// link += "&social_desc=" + encodeURIComponent(shareContent.desc || " ")
// link += "&social_image=" + encodeURIComponent(shareContent.icon || " ")
// link += "&social_title=" + encodeURIComponent(shareContent.title)
link += "®ion_id=0"
return link
}
}