gaoluyang
2026-03-09 6c497f71f5441633099fc7a7896da8e8c7835f05
src/pages/login.vue
@@ -7,7 +7,7 @@
         <view class="input-item flex align-center">
            <up-input prefixIcon="account" placeholder="请输入账号" border="bottom"
                           @blur="getUserLoginFacotryList"
                           maxlength="30" v-model="loginForm.username" clearable></up-input>
                           maxlength="30" v-model="loginForm.userName" clearable></up-input>
         </view>
         <view class="input-item flex align-center">
            <up-input prefixIcon="lock" placeholder="请输入密码" border="bottom" maxlength="20" v-model="loginForm.password" clearable type="password"></up-input>
@@ -42,8 +42,19 @@
</template>
<script setup>
import modal from '@/plugins/modal'
import { userLoginFacotryList} from '@/api/login'
import {modal} from "@/plugins";
const showToast = (message) => {
   uni.showToast({
      title: message,
      icon: 'none'
   })
}
import {
   userLoginFacotryList,
   updateClientId,
   getNoticeCount,
} from "@/api/login";
import { ref, onMounted } from "vue";
import useUserStore from '@/store/modules/user'
import { getWxCode } from '@/utils/geek';
@@ -54,7 +65,7 @@
const useWxLogin = ref(false); // 是否使用微信登录
const rememberPassword = ref(false); // 记住密码
const loginForm = ref({
   username: "",
   userName: "",
   password: "",
   factoryId: "",
   currentFatoryName: "",
@@ -64,7 +75,7 @@
// 保存密码到本地存储
function savePassword() {
   if (rememberPassword.value) {
      uni.setStorageSync('remembered_username', loginForm.value.username);
      uni.setStorageSync('remembered_username', loginForm.value.userName);
      uni.setStorageSync('remembered_password', loginForm.value.password);
      uni.setStorageSync('remember_password', true);
   } else {
@@ -82,7 +93,7 @@
      const savedUsername = uni.getStorageSync('remembered_username');
      const savedPassword = uni.getStorageSync('remembered_password');
      if (savedUsername) {
         loginForm.value.username = savedUsername;
         loginForm.value.userName = savedUsername;
      }
      if (savedPassword) {
         loginForm.value.password = savedPassword;
@@ -103,8 +114,9 @@
}
function getUserLoginFacotryList() {
   if(loginForm.value.username){
      userLoginFacotryList({userName:loginForm.value.username}).then(res => {
   if(loginForm.value.userName){
      userLoginFacotryList({userName:loginForm.value.userName}).then(res => {
         console.log('res',res)
         // 检查res.data是否为数组
         if (res.data && Array.isArray(res.data)) {
            // 重新组装数据格式:deptId变成id,deptName变成name
@@ -117,7 +129,7 @@
            factoryList.value = []
         }
      }).catch(error => {
         modal.msgError('获取公司列表失败:', error)
         showToast('获取公司列表失败:', error)
         factoryList.value = []
      })
   }else {
@@ -126,14 +138,14 @@
}
async function handleLogin() {
   if (loginForm.value.username === "") {
      modal.msgError("请输入您的账号")
   if (loginForm.value.userName === "") {
      showToast("请输入您的账号")
   } else if (loginForm.value.password === "") {
      modal.msgError("请输入您的密码")
      showToast("请输入您的密码")
   } else if (loginForm.value.factoryId === "") {
      modal.msgError("请选择公司")
      showToast("请选择公司")
   } else {
      modal.loading("登录中,请耐心等待...")
      showToast("登录中,请耐心等待...")
      pwdLogin()
   }
};
@@ -150,6 +162,13 @@
};
function loginSuccess(result) {
   userStore.getInfo().then(res => {
      const userId = res.user.userId;
      // 登录成功后,将客户端推送标识发送到服务器
      sendClientIdToServer();
      // 启动定时获取未读消息数量的定时器
      startNoticeCountTimer(userId);
   })
   // 设置用户信息
   userStore.getInfo().then(res => {
      uni.switchTab({
@@ -157,6 +176,65 @@
      });
   })
}
// 启动定时获取未读消息数量的定时器
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() {
   // 获取本地存储的客户端标识
   const clientId = uni.getStorageSync("clientid");
   if (clientId) {
      console.log("登录成功,准备发送客户端标识到服务器:", clientId);
      // 这里调用后端接口发送客户端标识
      updateClientId({ cid: clientId })
         .then(res => {
            console.log("服务器响应:", res);
            if (res.code === 200) {
               console.log("客户端标识已成功发送到服务器");
            } else {
               console.log("服务器返回错误:", res.msg);
            }
         })
         .catch(error => {
            console.log("发送客户端标识到服务器失败:", error);
         });
      // 示例:api.updateClientId({ clientId: clientId });
      // 由于没有具体的接口,这里只打印日志
      console.log("客户端标识已发送到服务器");
   } else {
      console.log("未获取到客户端推送标识");
   }
}
// 页面加载时检查是否有保存的密码
onMounted(() => {
   loadPassword();