-
Notifications
You must be signed in to change notification settings - Fork 478
/
Copy pathShareAppMessage.cs
94 lines (88 loc) · 2.6 KB
/
ShareAppMessage.cs
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
using UnityEngine;
using WeChatWASM;
public class ShareAppMessage : Details
{
private string localImagePath;
protected override void TestAPI(string[] args)
{
if (GetOptionString(1, "") == "本地图片文件路径")
{
DownloadFileImage();
}
else
{
shareAppMessage();
}
}
private void ShowLoading()
{
WX.ShowLoading(new ShowLoadingOption()
{
title = "正在下载图片...",
mask = true
});
}
private void HideLoading()
{
WX.HideLoading(new HideLoadingOption());
}
private void DownloadFileImage()
{
ShowLoading();
WX.DownloadFile(new DownloadFileOption()
{
url = "https://picsum.photos/400/400?random=1",
success = (res) =>
{
Debug.Log("WX.DownloadFile success");
if (res.statusCode == 200)
{
Debug.Log(res.tempFilePath);
var fs = WX.GetFileSystemManager();
// 将临时文件保存为本地缓存文件
localImagePath = fs.SaveFileSync(res.tempFilePath, WX.env.USER_DATA_PATH + "/shareImage.jpg");
Debug.Log($"本地缓存文件保存路径: {localImagePath}");
shareAppMessage();
}
},
fail = (res) =>
{
Debug.Log("WX.DownloadFile fail");
},
complete = (res) =>
{
Debug.Log("WX.DownloadFile complete");
HideLoading();
}
});
}
private void shareAppMessage()
{
string title = GetOptionString(0, "");
string imageUrl = GetOptionString(1, "");
string imageUrlId = GetOptionString(2, "");
bool toCurrentGroupValue = GetOptionBool(3, true);
// 如果选择了本地图片文件路径,使用下载保存到本地的文件路径
if (imageUrl == "本地图片文件路径")
{
imageUrl = localImagePath;
}
WX.ShareAppMessage(new ShareAppMessageOption()
{
title = title,
imageUrl = imageUrl,
imageUrlId = imageUrlId,
toCurrentGroup = toCurrentGroupValue
});
}
private void OnDestroy()
{
// 清理文件
if (!string.IsNullOrEmpty(localImagePath))
{
var fs = WX.GetFileSystemManager();
fs.UnlinkSync(localImagePath);
Debug.Log("清理本地图片成功");
}
}
}