1
1
using WeChatWASM ;
2
2
using System ;
3
+ using UnityEngine ;
3
4
4
5
public class Favorites : Details
5
6
{
6
7
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 )
13
15
{
14
- title = "收藏标题" ,
15
- imageUrl = "xx" ,
16
- query = "key1=val1&key2=val2" ,
17
- disableForward = false
16
+ onAddToFavorites ( ) ;
17
+ return ;
18
18
}
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 ( )
22
33
{
23
- onAddToFavorites ( ) ;
34
+ WX . ShowLoading ( new ShowLoadingOption ( )
35
+ {
36
+ title = "正在下载图片..." ,
37
+ mask = true
38
+ } ) ;
24
39
}
25
- private void Start ( )
40
+
41
+ private void HideLoading ( )
26
42
{
27
- //GameManager.Instance.detailsController.BindExtraButtonAction(0, onAddToFavorites );
43
+ WX . HideLoading ( new HideLoadingOption ( ) ) ;
28
44
}
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
+ //切换收藏监听状态
29
105
public void onAddToFavorites ( )
30
106
{
31
107
if ( ! _isListeningAddToFavorites )
32
108
{
33
109
WX . OnAddToFavorites ( _onAddToFavorites ) ;
110
+ Debug . Log ( "开始监听收藏" ) ;
111
+ // 添加开始监听时的提示
112
+ WX . ShowToast ( new ShowToastOption ( )
113
+ {
114
+ title = "已开启收藏监听" ,
115
+ icon = "none" ,
116
+ duration = 1500
117
+ } ) ;
34
118
}
35
119
else
36
120
{
37
121
WX . OffAddToFavorites ( _onAddToFavorites ) ;
122
+ Debug . Log ( "取消监听收藏" ) ;
123
+ // 添加取消监听时的提示
124
+ WX . ShowToast ( new ShowToastOption ( )
125
+ {
126
+ title = "已取消收藏监听" ,
127
+ icon = "none" ,
128
+ duration = 1500
129
+ } ) ;
38
130
}
39
131
_isListeningAddToFavorites = ! _isListeningAddToFavorites ;
40
132
GameManager . Instance . detailsController . ChangeInitialButtonText (
41
133
_isListeningAddToFavorites ? "取消监听收藏" : "开始监听收藏"
42
134
) ;
43
135
}
136
+
44
137
private void OnDestroy ( )
45
138
{
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
+ }
47
152
}
48
- }
153
+ }
0 commit comments