zhangwencui
3 天以前 8d4a66c31aa0f6418403ade8a780f27c9fa9d921
src/pages/login.vue
@@ -48,7 +48,11 @@
      icon: "none",
    });
  };
  import { userLoginFacotryList, updateClientId } from "@/api/login";
  import {
    userLoginFacotryList,
    updateClientId,
    getNoticeCount,
  } from "@/api/login";
  import { ref, onMounted } from "vue";
  import useUserStore from "@/store/modules/user";
  import { getWxCode } from "@/utils/geek";
@@ -160,20 +164,57 @@
  function loginSuccess(result) {
    // 设置用户信息
    userStore.getInfo().then(res => {
      const userId = res.user.userId;
      // 获取路由权限
      userStore.getRouters().then(() => {
        console.log("路由权限获取成功");
      }).catch(error => {
        console.error("获取路由权限失败:", error);
      });
      userStore
        .getRouters()
        .then(() => {
          console.log("路由权限获取成功");
        })
        .catch(error => {
          console.error("获取路由权限失败:", error);
        });
      // 登录成功后,将客户端推送标识发送到服务器
      sendClientIdToServer();
      uni.switchTab({
        url: "/pages/index",
      });
      // 启动定时获取未读消息数量的定时器
      startNoticeCountTimer(userId);
      uni.switchTab({ url: "/pages/index" });
    });
  }
  // 启动定时获取未读消息数量的定时器
  function startNoticeCountTimer(userId) {
    // 立即获取一次未读消息数量
    updateNoticeCount(userId);
    // 设置定时器,每30秒获取一次
    setInterval(() => {
      updateNoticeCount(userId);
    }, 30000);
  }
  // 更新未读消息数量
  function updateNoticeCount(userId) {
    getNoticeCount(userId)
      .then(res => {
        const count = res.data || 0;
        console.log("未读消息数量:", count);
        // 更新tabbar的角标
        if (count > 0) {
          uni.setTabBarBadge({
            index: 1, // 消息标签页的索引
            text: count.toString(),
          });
        } else {
          uni.removeTabBarBadge({
            index: 1,
          });
        }
      })
      .catch(error => {
        console.error("获取未读消息数量失败:", error);
      });
  }
  // 将客户端推送标识发送到服务器
  function sendClientIdToServer() {
    // 获取本地存储的客户端标识