WDSystemShare.ets
3.63 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
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';
const TAG = "WDSystemShare"
export class WDSystemShare implements WDShareInterface {
shareContent(scene: ShareScene, content: ShareContent, contentType: ShareContentType): Promise<string> {
return new Promise((resolve, fail) => {
try {
let data = this.getShareData(content, contentType)
let controller: systemShare.ShareController = new systemShare.ShareController(data);
let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext
controller.on('dismiss', () => {
resolve("dismiss")
});
controller.show(context, {
previewMode: systemShare.SharePreviewMode.DEFAULT,
selectionMode: systemShare.SelectionMode.SINGLE
});
console.log("分享控制器调用完成")
} catch (e) {
fail(e)
}
})
}
getShareData(content: ShareContent, contentType: ShareContentType) : systemShare.SharedData {
if (contentType === ShareContentType.PrueText) {
let prueText = content as ShareContentText
console.log("分享纯文本")
return new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: prueText.text
});
}
if (contentType === ShareContentType.ImageAndText) {
let imageAndText = content as ShareContentImageAndText
console.log("分享图片和文本")
let data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: imageAndText.title
});
data.addRecord({
utd: utd.UniformDataType.PNG,
uri: imageAndText.imgURI // 这里必须为本地连接
});
return data
}
console.log("分享链接和文本")
let link = content as ShareContentLink
let data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: link.title
});
let shareLink = this.generateShareLink(link)
console.log("分享 shareLink ==> " + shareLink)
data.addRecord({
utd: utd.UniformDataType.HYPERLINK,
// uri: link.link // SDK 设计 不能传这里
content: link.link
});
return data
}
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
}
}