46
46
</template >
47
47
48
48
<script setup>
49
- import { ref , computed , onMounted , reactive } from ' vue' ;
49
+ import { ref , computed , onMounted , reactive , watch } from ' vue' ;
50
50
import { useMainStore } from ' @/store' ;
51
51
import { useI18n } from ' vue-i18n' ;
52
52
import { trackEvent } from ' @/utils/use-analytics' ;
@@ -59,6 +59,7 @@ const isDarkMode = computed(() => store.isDarkMode);
59
59
const isMobile = computed (() => store .isMobile );
60
60
const userPreferences = computed (() => store .userPreferences );
61
61
62
+ const alertCrontrol = ref (false );
62
63
const alertToShow = ref (false );
63
64
const alertStyle = ref (" " );
64
65
const alertTitle = ref (" " );
@@ -187,53 +188,69 @@ const checkConnectivityHandler = (test, onTestComplete, isManualRun) => {
187
188
188
189
// 检查所有网络连通性
189
190
const checkAllConnectivity = (isAlertToShow , isRefresh , isManualRun ) => {
191
+ alertToShow .value = isAlertToShow;
192
+ return new Promise ((resolve , reject ) => {
193
+ if (isRefresh) {
194
+ connectivityTests .forEach ((test ) => {
195
+ test .status = t (' connectivity.StatusWait' );
196
+ test .time = 0 ;
197
+ });
198
+ trackEvent (' Section' , ' RefreshClick' , ' Connectivity' );
199
+ }
190
200
191
- if (isRefresh) {
192
- connectivityTests .forEach ((test ) => {
193
- test .status = t (' connectivity.StatusWait' );
194
- test .time = 0 ;
195
- });
196
- trackEvent (' Section' , ' RefreshClick' , ' Connectivity' );
197
- }
198
- let totalTests = connectivityTests .length ;
199
- let successCount = 0 ;
200
- let completedCount = 0 ;
201
+ let totalTests = connectivityTests .length ;
202
+ let successCount = 0 ;
203
+ let completedCount = 0 ;
204
+ let testPromises = [];
201
205
202
- const onTestComplete = (isSuccess ) => {
203
- if (isSuccess) {
204
- successCount++ ;
205
- }
206
- completedCount++ ;
206
+ const onTestComplete = (isSuccess ) => {
207
+ if (isSuccess) {
208
+ successCount++ ;
209
+ }
210
+ completedCount++ ;
211
+ };
207
212
208
- // 只有当所有测试都完成时才做出最终判断
209
- if (completedCount === totalTests) {
210
- alertToShow .value = true ;
213
+ connectivityTests .forEach ((test , index ) => {
214
+ let testPromise = new Promise ((testResolve , testReject ) => {
215
+ setTimeout (() => {
216
+ checkConnectivityHandler (test, (isSuccess ) => {
217
+ if (isSuccess) {
218
+ onTestComplete (true );
219
+ testResolve ();
220
+ } else {
221
+ onTestComplete (false );
222
+ testReject ();
223
+ }
224
+ }, isManualRun);
225
+ }, 50 * index);
226
+ });
227
+ testPromises .push (testPromise);
228
+ });
229
+
230
+ // 无论如何都会 Resolve
231
+ Promise .allSettled (testPromises).then (() => {
211
232
if (successCount === totalTests) {
212
- updateConnectivityAlert (true , " success" );
233
+ updateConnectivityAlert (" success" );
213
234
} else {
214
- updateConnectivityAlert (true , " error" );
235
+ updateConnectivityAlert (" error" );
215
236
}
216
- }
217
- };
237
+ resolve ();
238
+ });
239
+
218
240
219
- connectivityTests .forEach ((test , index ) => {
220
- setTimeout (() => {
221
- checkConnectivityHandler (test, onTestComplete, isManualRun);
222
- }, 50 * index);
241
+ isStarted .value = true ;
223
242
});
243
+ };
224
244
225
- if ((isAlertToShow || ! isStarted .value ) && autoShowAltert .value ) {
226
- setTimeout (() => {
227
- store .setAlert (alertToShow .value , alertStyle .value , alertMessage .value , alertTitle .value );
228
- }, 4000 );
245
+ const sendAlert = () => {
246
+ if ((alertToShow .value || ! isStarted .value ) && autoShowAltert .value ) {
247
+ store .setAlert (alertToShow .value , alertStyle .value , alertMessage .value , alertTitle .value );
229
248
}
230
-
231
- isStarted .value = true ;
232
249
};
233
250
251
+
234
252
// 更新通知气泡
235
- const updateConnectivityAlert = (show , type ) => {
236
- alertToShow .value = show;
253
+ const updateConnectivityAlert = (type ) => {
237
254
if (type === " success" ) {
238
255
alertStyle .value = " text-success" ;
239
256
alertMessage .value = t (' alert.Congrats_Message' );
@@ -245,14 +262,19 @@ const updateConnectivityAlert = (show, type) => {
245
262
}
246
263
};
247
264
248
- const handelCheckStart = () => {
249
- setTimeout (() => {
250
- checkAllConnectivity (true , false , false );
251
- }, 2000 );
265
+ // 主控制函数
266
+ const handelCheckStart = async (fromApp = false ) => {
267
+ if (fromApp) {
268
+ await checkAllConnectivity (false , true , true );
269
+ } else {
270
+ await checkAllConnectivity (true , false , false );
271
+ }
272
+ // 传递完成状态到 store
273
+ store .setLoadingStatus (' connectivity' , true );
252
274
if (autoRefresh .value ) {
253
- intervalId .value = setInterval (() => {
275
+ intervalId .value = setInterval (async () => {
254
276
if (counter .value < maxCounts .value && ! manualRun .value ) {
255
- checkAllConnectivity (false , false , false );
277
+ await checkAllConnectivity (false , false , false );
256
278
counter .value ++ ;
257
279
} else {
258
280
clearInterval (intervalId .value );
@@ -262,7 +284,13 @@ const handelCheckStart = () => {
262
284
};
263
285
264
286
onMounted (() => {
265
- store .setLoadingStatus (' connectivity' , true );
287
+ store .setMountingStatus (' connectivity' , true );
288
+ });
289
+
290
+ watch (() => store .allHasLoaded , (newValue , oldValue ) => {
291
+ if (newValue === true ) {
292
+ sendAlert ();
293
+ }
266
294
});
267
295
268
296
defineExpose ({
0 commit comments