|
@@ -1,37 +1,126 @@
|
|
|
// src/pages/check.tsx
|
|
|
-import { useEffect, useState } from 'react';
|
|
|
+import { useEffect, useState, useRef } from 'react';
|
|
|
import Taro from '@tarojs/taro';
|
|
|
-import { View } from '@tarojs/components';
|
|
|
+import { View, Button } from '@tarojs/components';
|
|
|
import { getQuota } from '@/API/security/quotaActions';
|
|
|
|
|
|
export default function Check() {
|
|
|
const [overdueStatus, setoverdueStatus] = useState<boolean | null>(null);
|
|
|
- useEffect(() => {
|
|
|
- const checkoverdue = async () => {
|
|
|
- try {
|
|
|
- const response = await getQuota();
|
|
|
- setoverdueStatus(response.data.overdue);
|
|
|
- console.error(`overdue response: ${JSON.stringify(response)}`);
|
|
|
- if (!response.data.overdue) {
|
|
|
- //overdue 为 false 说明没有过期
|
|
|
- Taro.redirectTo({ url: '/pages/index/index' });
|
|
|
+ const [retryCount, setRetryCount] = useState(0);
|
|
|
+ const [isRetrying, setIsRetrying] = useState(false);
|
|
|
+ const maxRetries = 10;
|
|
|
+ const retryTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
+
|
|
|
+ const checkoverdue = async (currentRetry = 0) => {
|
|
|
+ setIsRetrying(true);
|
|
|
+ setRetryCount(currentRetry);
|
|
|
+
|
|
|
+ try {
|
|
|
+ const response = await getQuota();
|
|
|
+ setoverdueStatus(response.data.overdue);
|
|
|
+ console.info(`overdue response: ${JSON.stringify(response)}`);
|
|
|
+
|
|
|
+ if (!response.data.overdue) {
|
|
|
+ // overdue 为 false 说明没有过期
|
|
|
+ setIsRetrying(false);
|
|
|
+ Taro.redirectTo({ url: '/pages/index/index' });
|
|
|
+ } else {
|
|
|
+ // 过期了,需要重试
|
|
|
+ if (currentRetry < maxRetries) {
|
|
|
+ console.info(`Retrying... (${currentRetry + 1}/${maxRetries})`);
|
|
|
+ retryTimeoutRef.current = setTimeout(() => {
|
|
|
+ checkoverdue(currentRetry + 1);
|
|
|
+ }, 1000);
|
|
|
+ } else {
|
|
|
+ // 达到最大重试次数
|
|
|
+ setIsRetrying(false);
|
|
|
+ console.warn('Max retries reached');
|
|
|
}
|
|
|
- } catch (error) {
|
|
|
- setoverdueStatus(true); // 出错时假设为过期,要求重新同步
|
|
|
- console.error('Error fetching overdue:', error);
|
|
|
}
|
|
|
- };
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error fetching overdue:', error);
|
|
|
+ setoverdueStatus(true); // 出错时假设为过期
|
|
|
+
|
|
|
+ // 出错也需要重试
|
|
|
+ if (currentRetry < maxRetries) {
|
|
|
+ console.info(
|
|
|
+ `Retrying after error... (${currentRetry + 1}/${maxRetries})`
|
|
|
+ );
|
|
|
+ retryTimeoutRef.current = setTimeout(() => {
|
|
|
+ checkoverdue(currentRetry + 1);
|
|
|
+ }, 1000);
|
|
|
+ } else {
|
|
|
+ // 达到最大重试次数
|
|
|
+ setIsRetrying(false);
|
|
|
+ console.warn('Max retries reached after errors');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleManualRetry = () => {
|
|
|
+ // 清除可能存在的自动重试定时器
|
|
|
+ if (retryTimeoutRef.current) {
|
|
|
+ clearTimeout(retryTimeoutRef.current);
|
|
|
+ retryTimeoutRef.current = null;
|
|
|
+ }
|
|
|
+ // 重置状态并重新开始检查
|
|
|
+ setoverdueStatus(null);
|
|
|
+ setRetryCount(0);
|
|
|
+ checkoverdue(0);
|
|
|
+ };
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
checkoverdue();
|
|
|
+
|
|
|
+ // 清理函数:组件卸载时清除定时器
|
|
|
+ return () => {
|
|
|
+ if (retryTimeoutRef.current) {
|
|
|
+ clearTimeout(retryTimeoutRef.current);
|
|
|
+ }
|
|
|
+ };
|
|
|
}, []);
|
|
|
|
|
|
return (
|
|
|
<View style={{ paddingTop: '40vh', textAlign: 'center' }}>
|
|
|
{overdueStatus === null ? (
|
|
|
- <div>checking...</div>
|
|
|
+ <View>
|
|
|
+ <div>正在检查授权状态...</div>
|
|
|
+ {isRetrying && retryCount > 0 && (
|
|
|
+ <div style={{ marginTop: '10px', fontSize: '14px', color: '#666' }}>
|
|
|
+ 重试中... ({retryCount}/{maxRetries})
|
|
|
+ </div>
|
|
|
+ )}
|
|
|
+ </View>
|
|
|
) : overdueStatus === true ? (
|
|
|
- <div>Please sync with the cloud to continue.</div>
|
|
|
+ <View>
|
|
|
+ <div>请与云端同步以继续使用</div>
|
|
|
+ {isRetrying ? (
|
|
|
+ <div style={{ marginTop: '10px', fontSize: '14px', color: '#666' }}>
|
|
|
+ 自动重试中... ({retryCount}/{maxRetries})
|
|
|
+ </div>
|
|
|
+ ) : (
|
|
|
+ <div style={{ marginTop: '10px', fontSize: '14px', color: '#999' }}>
|
|
|
+ 已重试 {retryCount} 次
|
|
|
+ </div>
|
|
|
+ )}
|
|
|
+ <Button
|
|
|
+ onClick={handleManualRetry}
|
|
|
+ style={{
|
|
|
+ marginTop: '20px',
|
|
|
+ padding: '10px 30px',
|
|
|
+ backgroundColor: '#1890ff',
|
|
|
+ color: '#fff',
|
|
|
+ border: 'none',
|
|
|
+ borderRadius: '4px',
|
|
|
+ cursor: 'pointer',
|
|
|
+ }}
|
|
|
+ disabled={isRetrying}
|
|
|
+ >
|
|
|
+ {isRetrying ? '重试中...' : '手动重试'}
|
|
|
+ </Button>
|
|
|
+ </View>
|
|
|
) : (
|
|
|
- <div>overdue valid, redirecting...</div>
|
|
|
+ <div>授权有效,正在跳转...</div>
|
|
|
)}
|
|
|
</View>
|
|
|
);
|