FEAT => 更新设备绑定引导页逻辑,优化设备数量获取方式,使用用户状态中的设备总数替代API调用,提升性能和用户体验。

This commit is contained in:
超级老白兔
2025-07-30 14:57:03 +08:00
parent 0693f5de74
commit c78134f05c

View File

@@ -8,7 +8,6 @@ import {
QrcodeOutlined,
} from "@ant-design/icons";
import Layout from "@/components/Layout/Layout";
import { getDashboard } from "@/pages/mobile/home/api";
import { fetchDeviceQRCode, addDeviceByImei } from "./api";
import { useUserStore } from "@/store/module/user";
import styles from "./index.module.scss";
@@ -16,8 +15,8 @@ import styles from "./index.module.scss";
const Guide: React.FC = () => {
const navigate = useNavigate();
const { user } = useUserStore();
const [loading, setLoading] = useState(true);
const [deviceCount, setDeviceCount] = useState(0);
const [loading, setLoading] = useState(false);
const [deviceCount, setDeviceCount] = useState(user?.deviceTotal || 0);
// 添加设备弹窗状态
const [addVisible, setAddVisible] = useState(false);
@@ -31,15 +30,14 @@ const Guide: React.FC = () => {
// 轮询监听相关
const [isPolling, setIsPolling] = useState(false);
const pollingRef = useRef<NodeJS.Timeout | null>(null);
const initialDeviceCountRef = useRef(0);
const initialDeviceCountRef = useRef(deviceCount);
// 检查设备绑定状态
const checkDeviceStatus = useCallback(async () => {
try {
setLoading(true);
const dashboardData = await getDashboard();
const deviceNum = dashboardData?.deviceNum || 0;
// 使用store中的设备数量
const deviceNum = user?.deviceTotal || 0;
setDeviceCount(deviceNum);
// 如果已有设备,直接跳转到首页
@@ -56,7 +54,7 @@ const Guide: React.FC = () => {
} finally {
setLoading(false);
}
}, []);
}, [user?.deviceTotal, navigate]);
useEffect(() => {
checkDeviceStatus();
@@ -71,8 +69,10 @@ const Guide: React.FC = () => {
const pollDeviceStatus = async () => {
try {
const dashboardData = await getDashboard();
const currentDeviceCount = dashboardData?.deviceNum || 0;
// 这里可以调用一个简单的设备数量接口来检查是否有新设备
// 或者使用其他方式检测设备状态变化
// 暂时使用store中的数量实际项目中可能需要调用专门的接口
const currentDeviceCount = user?.deviceTotal || 0;
// 如果设备数量增加了,说明有新设备添加成功
if (currentDeviceCount > initialDeviceCountRef.current) {
@@ -95,7 +95,7 @@ const Guide: React.FC = () => {
// 每3秒检查一次设备状态
pollingRef.current = setInterval(pollDeviceStatus, 3000);
}, [isPolling, deviceCount]);
}, [isPolling, deviceCount, user?.deviceTotal]);
// 停止轮询
const stopPolling = useCallback(() => {
@@ -120,7 +120,7 @@ const Guide: React.FC = () => {
setQrLoading(true);
setQrCode(null);
try {
const accountId = user.s2_accountId;
const accountId = user?.s2_accountId;
if (!accountId) throw new Error("未获取到用户信息");
const res = await fetchDeviceQRCode(accountId);
setQrCode(res.qrCode);