Skip to content

Commit 4b9b63c

Browse files
committed
update Share示例修改和优化
1 parent be8621a commit 4b9b63c

25 files changed

+789
-188
lines changed

Demo/API_V2/Assets/API/GUI/Menu/MenuSO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ MonoBehaviour:
2828
- buttonText: "\u83B7\u53D6\u83DC\u5355\u6309\u94AE\u5E03\u5C40\u4F4D\u7F6E\u4FE1\u606F"
2929
- buttonText: "\u8BBE\u7F6E\u72B6\u6001\u680F\u6837\u5F0F"
3030
initialResultList: []
31+
entryOrder: 0
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,153 @@
11
using WeChatWASM;
22
using System;
3+
using UnityEngine;
34

45
public class Favorites : Details
56
{
67
private bool _isListeningAddToFavorites = false;
7-
private readonly Action<Action<OnAddToFavoritesListenerResult>> _onAddToFavorites = (
8-
callback
9-
) =>
10-
{
11-
callback(
12-
new OnAddToFavoritesListenerResult
8+
private Action<Action<OnAddToFavoritesListenerResult>> _onAddToFavorites;
9+
private string localImagePath;
10+
11+
protected override void TestAPI(string[] args)
12+
{
13+
// 如果已经在监听中,只执行切换监听的操作,避免重复下载
14+
if (_isListeningAddToFavorites)
1315
{
14-
title = "收藏标题",
15-
imageUrl = "xx",
16-
query = "key1=val1&key2=val2",
17-
disableForward = false
16+
onAddToFavorites();
17+
return;
1818
}
19-
);
20-
};
21-
protected override void TestAPI(string[] args)
19+
20+
// 根据对应参数,执行DownloadFileImage()下载图片
21+
if (GetOptionString(1, "") == "本地图片文件路径")
22+
{
23+
DownloadFileImage();
24+
}
25+
else
26+
{
27+
InitializeFavoritesCallback();
28+
onAddToFavorites();
29+
}
30+
}
31+
32+
private void ShowLoading()
2233
{
23-
onAddToFavorites();
34+
WX.ShowLoading(new ShowLoadingOption()
35+
{
36+
title = "正在下载图片...",
37+
mask = true
38+
});
2439
}
25-
private void Start()
40+
41+
private void HideLoading()
2642
{
27-
//GameManager.Instance.detailsController.BindExtraButtonAction(0, onAddToFavorites);
43+
WX.HideLoading(new HideLoadingOption());
2844
}
45+
46+
private void DownloadFileImage()
47+
{
48+
ShowLoading();
49+
WX.DownloadFile(new DownloadFileOption()
50+
{
51+
url = "https://picsum.photos/400/400?random=1",
52+
success = (res) =>
53+
{
54+
Debug.Log("WX.DownloadFile success");
55+
if (res.statusCode == 200)
56+
{
57+
Debug.Log(res.tempFilePath);
58+
var fs = WX.GetFileSystemManager();
59+
// 将临时文件保存为本地缓存文件
60+
localImagePath = fs.SaveFileSync(res.tempFilePath, WX.env.USER_DATA_PATH + "/favoriteImage.jpg");
61+
Debug.Log($"本地缓存文件保存路径: {localImagePath}");
62+
InitializeFavoritesCallback();
63+
onAddToFavorites();
64+
}
65+
},
66+
fail = (res) =>
67+
{
68+
Debug.Log("WX.DownloadFile fail");
69+
},
70+
complete = (res) =>
71+
{
72+
Debug.Log("WX.DownloadFile complete");
73+
HideLoading();
74+
}
75+
});
76+
}
77+
78+
//设置收藏回调函数
79+
private void InitializeFavoritesCallback()
80+
{
81+
string title = GetOptionString(0, "");
82+
string imageUrl = GetOptionString(1, "");
83+
bool disableForward = !GetOptionBool(2, false);
84+
85+
if (imageUrl == "本地图片文件路径")
86+
{
87+
imageUrl = localImagePath;
88+
}
89+
90+
_onAddToFavorites = (callback) =>
91+
{
92+
callback(
93+
new OnAddToFavoritesListenerResult
94+
{
95+
title = title,
96+
imageUrl = imageUrl,
97+
disableForward = disableForward
98+
}
99+
);
100+
Debug.Log($"收藏回调参数 - 标题: {title}, 图片URL: {imageUrl}, 禁止转发: {disableForward}");
101+
};
102+
}
103+
104+
//切换收藏监听状态
29105
public void onAddToFavorites()
30106
{
31107
if (!_isListeningAddToFavorites)
32108
{
33109
WX.OnAddToFavorites(_onAddToFavorites);
110+
Debug.Log("开始监听收藏");
111+
// 添加开始监听时的提示
112+
WX.ShowToast(new ShowToastOption()
113+
{
114+
title = "已开启收藏监听",
115+
icon = "none",
116+
duration = 1500
117+
});
34118
}
35119
else
36120
{
37121
WX.OffAddToFavorites(_onAddToFavorites);
122+
Debug.Log("取消监听收藏");
123+
// 添加取消监听时的提示
124+
WX.ShowToast(new ShowToastOption()
125+
{
126+
title = "已取消收藏监听",
127+
icon = "none",
128+
duration = 1500
129+
});
38130
}
39131
_isListeningAddToFavorites = !_isListeningAddToFavorites;
40132
GameManager.Instance.detailsController.ChangeInitialButtonText(
41133
_isListeningAddToFavorites ? "取消监听收藏" : "开始监听收藏"
42134
);
43135
}
136+
44137
private void OnDestroy()
45138
{
46-
WX.OffAddToFavorites(_onAddToFavorites);
139+
if (_isListeningAddToFavorites)
140+
{
141+
WX.OffAddToFavorites(_onAddToFavorites);
142+
Debug.Log("清理收藏监听");
143+
}
144+
145+
// 清理本地文件
146+
if (!string.IsNullOrEmpty(localImagePath))
147+
{
148+
var fs = WX.GetFileSystemManager();
149+
fs.UnlinkSync(localImagePath);
150+
Debug.Log("清理本地图片成功");
151+
}
47152
}
48-
}
153+
}

Demo/API_V2/Assets/API/Share/Favorites/FavoritesSO.asset

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,21 @@ MonoBehaviour:
1616
entryName: "\u6536\u85CF\u76D1\u542C"
1717
entryAPI: wx.onAddToFavorites | wx.offAddToFavorites
1818
entryDescription: "\u76D1\u542C\u7528\u6237\u70B9\u51FB\u83DC\u5355\u300C\u6536\u85CF\u300D\u6309\u94AE\u65F6\u89E6\u53D1\u7684\u4E8B\u4EF6\n\uFF08\u5B89\u53537.0.15\u8D77\u652F\u6301\uFF0CiOS
19-
\u6682\u4E0D\u652F\u6301\uFF09"
20-
optionList: []
19+
\u6682\u4E0D\u652F\u6301\uFF09\n\u6CE8\u610F\uFF1A\u7A7A\u9009\u9879\u5219\u4F7F\u7528\u9ED8\u8BA4\u503C"
20+
optionList:
21+
- optionName: title
22+
availableOptions:
23+
- "\u6536\u85CF\u6807\u9898"
24+
-
25+
- optionName: imageUrl
26+
availableOptions:
27+
- https://mmocgame.qpic.cn/wechatgame/QgsibLsZPBy1uwWJnNNPeRXjBMKm327We8xrXWxGMM4cFKUZaqy0SU8TcudBf72sE/0
28+
- "\u672C\u5730\u56FE\u7247\u6587\u4EF6\u8DEF\u5F84"
29+
-
30+
- optionName: disableForward
31+
availableOptions:
32+
- false
33+
- true
2134
initialButtonText: "\u5F00\u59CB\u76D1\u542C\u6536\u85CF"
2235
extraButtonList: []
2336
initialResultList: []
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,94 @@
1-
using WeChatWASM;
2-
using System;
3-
using LitJson;
41
using UnityEngine;
2+
using WeChatWASM;
53

64
public class ShareAppMessage : Details
75
{
6+
private string localImagePath;
87
protected override void TestAPI(string[] args)
98
{
10-
updateShareMenu();
9+
if (GetOptionString(1, "") == "本地图片文件路径")
10+
{
11+
DownloadFileImage();
12+
}
13+
else
14+
{
15+
shareAppMessage();
16+
}
1117
}
12-
private void Start()
18+
private void ShowLoading()
1319
{
14-
GameManager.Instance.detailsController.BindExtraButtonAction(0, ShareAppMessages);
20+
WX.ShowLoading(new ShowLoadingOption()
21+
{
22+
title = "正在下载图片...",
23+
mask = true
24+
});
1525
}
16-
public void updateShareMenu()
26+
private void HideLoading()
1727
{
18-
var parameter = new UpdatableMessageFrontEndParameter[]
19-
{
20-
new UpdatableMessageFrontEndParameter { name = "xxx", value = "yyy" },
21-
new UpdatableMessageFrontEndParameter { name = "zz", value = "kk" }
22-
};
23-
24-
var info = new UpdatableMessageFrontEndTemplateInfo { parameterList = parameter, templateId = "模板id" };
28+
WX.HideLoading(new HideLoadingOption());
29+
}
2530

26-
WX.UpdateShareMenu(
27-
new UpdateShareMenuOption
31+
private void DownloadFileImage()
32+
{
33+
ShowLoading();
34+
WX.DownloadFile(new DownloadFileOption()
35+
{
36+
url = "https://picsum.photos/400/400?random=1",
37+
success = (res) =>
2838
{
29-
isPrivateMessage = true,
30-
isUpdatableMessage = true,
31-
activityId = "xxx",
32-
templateInfo = info,
33-
success = (res) =>
34-
{
35-
WX.ShowToast(new ShowToastOption { title = "设置成功" });
36-
},
37-
fail = (res) =>
39+
Debug.Log("WX.DownloadFile success");
40+
if (res.statusCode == 200)
3841
{
39-
Debug.Log("fail" + res.errMsg);
40-
},
41-
complete = (res) =>
42-
{
43-
Debug.Log("complete");
42+
Debug.Log(res.tempFilePath);
43+
var fs = WX.GetFileSystemManager();
44+
// 将临时文件保存为本地缓存文件
45+
localImagePath = fs.SaveFileSync(res.tempFilePath, WX.env.USER_DATA_PATH + "/shareImage.jpg");
46+
Debug.Log($"本地缓存文件保存路径: {localImagePath}");
47+
shareAppMessage();
4448
}
49+
},
50+
fail = (res) =>
51+
{
52+
Debug.Log("WX.DownloadFile fail");
53+
},
54+
complete = (res) =>
55+
{
56+
Debug.Log("WX.DownloadFile complete");
57+
HideLoading();
4558
}
46-
);
47-
WX.OnShow((res) =>
48-
{
49-
Debug.Log("Scene:" + res.scene);
50-
Debug.Log("shareTicket:" + res.shareTicket);
51-
Debug.Log("chatType:" + res.chatType);
52-
});
59+
});
5360
}
5461

55-
private void ShareAppMessages()
62+
private void shareAppMessage()
5663
{
57-
WX.ShareAppMessage(new ShareAppMessageOption() { title = "小游戏分享" });
64+
string title = GetOptionString(0, "");
65+
string imageUrl = GetOptionString(1, "");
66+
string imageUrlId = GetOptionString(2, "");
67+
bool toCurrentGroupValue = GetOptionBool(3, true);
68+
69+
// 如果选择了本地图片文件路径,使用下载保存到本地的文件路径
70+
if (imageUrl == "本地图片文件路径")
71+
{
72+
imageUrl = localImagePath;
73+
}
74+
75+
WX.ShareAppMessage(new ShareAppMessageOption()
76+
{
77+
title = title,
78+
imageUrl = imageUrl,
79+
imageUrlId = imageUrlId,
80+
toCurrentGroup = toCurrentGroupValue
81+
});
82+
}
83+
84+
private void OnDestroy()
85+
{
86+
// 清理文件
87+
if (!string.IsNullOrEmpty(localImagePath))
88+
{
89+
var fs = WX.GetFileSystemManager();
90+
fs.UnlinkSync(localImagePath);
91+
Debug.Log("清理本地图片成功");
92+
}
5893
}
5994
}

Demo/API_V2/Assets/API/Share/ShareAppMessage/ShareAppMessageSO.asset

+22-6
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,27 @@ MonoBehaviour:
1414
m_EditorClassIdentifier:
1515
entryScriptTypeName: ShareAppMessage
1616
entryName: "\u5206\u4EAB\u8F6C\u53D1"
17-
entryAPI: updateShareMenu | ShareAppMessage
18-
entryDescription: "\u66F4\u65B0\u8F6C\u53D1\u5C5E\u6027\uFF0C\u6DFB\u52A0onShow\u83B7\u53D6Shareticket\u3002\n\u4E3B\u52A8\u62C9\u8D77\u8F6C\u53D1\uFF0C\u8FDB\u5165\u9009\u62E9\u901A\u8BAF\u5F55\u754C\u9762\u3002\n"
19-
optionList: []
20-
initialButtonText: "\u66F4\u65B0\u8F6C\u53D1\u5C5E\u6027"
21-
extraButtonList:
22-
- buttonText: "\u4E3B\u52A8\u8F6C\u53D1\u5206\u4EAB"
17+
entryAPI: ShareAppMessage
18+
entryDescription: "title\uFF1A\u4E0D\u4F20\u5219\u9ED8\u8BA4\u4F7F\u7528\u5F53\u524D\u5C0F\u6E38\u620F\u7684\u6635\u79F0\u3002\n\u4E3B\u52A8\u62C9\u8D77\u8F6C\u53D1\uFF0C\u8FDB\u5165\u9009\u62E9\u901A\u8BAF\u5F55\u754C\u9762\u3002\n\u6CE8\u610F\uFF1A\u7A7A\u9009\u9879\u5219\u4F7F\u7528\u9ED8\u8BA4\u503C"
19+
optionList:
20+
- optionName: title
21+
availableOptions:
22+
- "\u5C0F\u6E38\u620F\u5206\u4EAB"
23+
-
24+
- optionName: imageUrl
25+
availableOptions:
26+
- https://mmocgame.qpic.cn/wechatgame/QgsibLsZPBy1uwWJnNNPeRXjBMKm327We8xrXWxGMM4cFKUZaqy0SU8TcudBf72sE/0
27+
- "\u672C\u5730\u56FE\u7247\u6587\u4EF6\u8DEF\u5F84"
28+
-
29+
- optionName: imageUrlId
30+
availableOptions:
31+
- CoH3v3JtRc2ajuDvZyhHXQ==
32+
-
33+
- optionName: toCurrentGroup
34+
availableOptions:
35+
- false
36+
- true
37+
initialButtonText: "\u4E3B\u52A8\u8F6C\u53D1\u5206\u4EAB"
38+
extraButtonList: []
2339
initialResultList: []
2440
entryOrder: 0

0 commit comments

Comments
 (0)