yyb
2026-05-22 552ec6b7d8ccc56c379da195fc6c9c74312b1070
src/pages/login.vue
@@ -22,6 +22,21 @@
                  clearable
                  type="password"></up-input>
      </view>
      <!-- <view class="input-item flex align-center"
            v-if="factoryList.length > 0">
        <up-input prefixIcon="home"
                  placeholder="请选择工厂"
                  border="bottom"
                  readonly
                  @click="showFactorySelect = true"
                  v-model="selectedFactoryName"
                  clearable></up-input>
        <up-action-sheet :show="showFactorySelect"
                         :actions="factoryList"
                         title="请选择工厂"
                         @close="showFactorySelect = false"
                         @select="handleFactorySelect"></up-action-sheet>
      </view> -->
      <view>
        <button @click="handleLogin"
                class="login-btn cu-btn block bg-blue lg round">登录</button>
@@ -37,6 +52,9 @@
      </up-checkbox>
    </view>
  </view>
  <view class="version-wrap">
    <text class="version-text">版本号:{{ versionName }}</text>
  </view>
</template>
<script setup>
@@ -48,22 +66,36 @@
      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";
  import { wxLogin } from "@/api/oauth";
  import { setToken } from "@/utils/auth";
  import View from "@/pages/procurementManagement/procurementLedger/view.vue";
  import config from "@/config";
  const userStore = useUserStore();
  const useWxLogin = ref(false); // 是否使用微信登录
  const rememberPassword = ref(false); // 记住密码
  const versionName = ref(config?.appInfo?.version || "1.0.0");
  const loginForm = ref({
    userName: "",
    password: "",
    currentFatoryName: "",
    factoryId: "",
  });
  const factoryList = ref([]); // 公司列表
  const showFactorySelect = ref(false);
  const selectedFactoryName = ref("");
  const handleFactorySelect = e => {
    loginForm.value.factoryId = e.id;
    selectedFactoryName.value = e.name;
    showFactorySelect.value = false;
  };
  // 保存密码到本地存储
  function savePassword() {
@@ -118,17 +150,28 @@
              id: item.deptId,
              name: item.deptName,
            }));
            // 如果只有一个工厂,默认选中
            if (factoryList.value.length === 1) {
              loginForm.value.factoryId = factoryList.value[0].id;
              selectedFactoryName.value = factoryList.value[0].name;
            }
          } else {
            // 如果res.data不是数组,设置为空数组
            factoryList.value = [];
            loginForm.value.factoryId = "";
            selectedFactoryName.value = "";
          }
        })
        .catch(error => {
          showToast("获取公司列表失败:", error);
          factoryList.value = [];
          loginForm.value.factoryId = "";
          selectedFactoryName.value = "";
        });
    } else {
      factoryList.value = [];
      loginForm.value.factoryId = "";
      selectedFactoryName.value = "";
    }
  }
@@ -137,6 +180,8 @@
      showToast("请输入您的账号");
    } else if (loginForm.value.password === "") {
      showToast("请输入您的密码");
    } else if (factoryList.value.length > 0 && loginForm.value.factoryId === "") {
      showToast("请选择工厂");
    } else {
      showToast("登录中,请耐心等待...");
      pwdLogin();
@@ -160,18 +205,55 @@
  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: 2, // 消息标签页的索引
            text: count.toString(),
          });
        } else {
          uni.removeTabBarBadge({
            index: 2,
          });
        }
      })
      .catch(error => {
        console.error("获取未读消息数量失败:", error);
      });
  }
  // 将客户端推送标识发送到服务器
@@ -202,6 +284,62 @@
  }
  // 页面加载时检查是否有保存的密码
  onMounted(() => {
    console.log("[login-version] 开始获取登录页版本,默认值:", versionName.value);
    // #ifdef MP-WEIXIN
    try {
      const accountInfo = uni.getAccountInfoSync();
      if (accountInfo?.miniProgram?.version) {
        versionName.value = accountInfo.miniProgram.version;
        console.log(
          "[login-version] 当前环境=MP-WEIXIN,版本=",
          versionName.value
        );
      }
    } catch (e) {
      // 获取失败时使用默认值
      console.log("[login-version] MP-WEIXIN 获取版本失败:", e);
    }
    // #endif
    // #ifdef APP-PLUS
    try {
      // #ifdef APP-PLUS
      // @ts-ignore
      if (plus?.runtime?.getProperty) {
        // @ts-ignore
        const appid = plus.runtime.appid;
        // @ts-ignore
        plus.runtime.getProperty(appid, info => {
          const v = info?.version || info?.versionName || "";
          if (v) {
            versionName.value = String(v);
            console.log(
              "[login-version] 当前环境=APP-PLUS,版本=",
              versionName.value
            );
          } else {
            console.log(
              "[login-version] APP-PLUS 获取到的版本字段为空,使用默认值:",
              versionName.value
            );
          }
          console.log("[login-version] 最终版本号:", versionName.value);
        });
      } else {
        console.log(
          "[login-version] APP-PLUS 环境下缺少 getProperty,使用默认值:",
          versionName.value
        );
        console.log("[login-version] 最终版本号:", versionName.value);
      }
      // #endif
    } catch (e) {
      // 获取失败时使用默认值
      console.log("[login-version] APP-PLUS 获取版本失败:", e);
      console.log("[login-version] 最终版本号:", versionName.value);
    }
    // #endif
    loadPassword();
    getUserLoginFacotryList();
  });
@@ -305,4 +443,15 @@
      }
    }
  }
  .version-wrap {
    width: 100%;
    text-align: center;
    margin-top: 20px;
  }
  .version-text {
    font-size: 12px;
    color: #999;
  }
</style>