潘超林 2 dni temu
rodzic
commit
68dbb7895c
2 zmienionych plików z 62 dodań i 112 usunięć
  1. 60 111
      loginChat.ts
  2. 2 1
      pages/views/login.vue

+ 60 - 111
loginChat.ts

@@ -11,122 +11,71 @@ function capitalize(str: string): string {
   return str.charAt(0).toUpperCase() + str.slice(1);
 }
 
-export const loginChat = async (loginInfo) => {
-  // 前置校验:确保必填参数存在
-  if (!loginInfo?.userID || !loginInfo?.userSig) {
-    const error = new Error("缺少userID或userSig");
-    console.error("登录参数错误", error);
-    uni.showToast({ title: "登录信息不完整", icon: "none" });
-    throw error;
-  }
-
-  try {
-    // 1. 先读取所有存储数据(同步操作前置)
-    const [
-      link,
-      companyUserId,
-      recruitUserId,
-      Identity,
-      postId,
-      type,
-      shopUserId,
-      userLoginType,
-      userId,
-    ] = [
-      "link",
-      "companyUserId",
-      "recruitUserId",
-      "Identity",
-      "postId",
-      "type",
-      "shopUserId",
-      "userLoginType",
-      "userId",
-    ].map((key) => uni.getStorageSync(key));
-
-    // 2. 执行登录(核心步骤,await确保完成)
-    console.log("开始IM登录", loginInfo.userID);
-    const res = await TUILogin.login(loginInfo);
-    console.log("IM登录成功", res);
-
-    // 3. 登录成功后再执行后续操作(确保登录状态就绪)
-    // 3.1 保存用户信息(异步不阻塞主流程)
-    uni.setStorage({
-      key: "userInfo",
-      data: JSON.stringify({
-        ...loginInfo,
-        TIMPush: undefined,
-        pushConfig: {},
-      }),
-    });
-
-    // 3.2 设置在线状态(确保服务已初始化)
-    await TUIUserService.switchUserStatus({ displayOnlineStatus: true });
-    console.log("用户在线状态设置完成");
-
-    // 3.3 处理会话切换(类型校验+容错)
-    if (type && ["job", "house", "homemaking"].includes(type)) {
-      const userTypeNum = parseInt(userLoginType) || 0;
-      // 会话ID构建(避免空值拼接)
-      let conversationId;
-      switch (type) {
-        case "job":
-          const jobTargetId = userTypeNum > 0 ? companyUserId : recruitUserId;
-          conversationId = jobTargetId
-            ? `C2C${userTypeNum > 0 ? "A" + jobTargetId : jobTargetId}`
-            : "";
-          break;
-        case "house":
-          const houseTargetId = userTypeNum > 0 ? shopUserId : userId;
-          conversationId = houseTargetId
-            ? `C2C${userTypeNum > 0 ? "C" + houseTargetId : houseTargetId}`
-            : "";
-          break;
-        case "homemaking":
-          const homeTargetId = userTypeNum > 0 ? shopUserId : userId;
-          conversationId = homeTargetId
-            ? `C2C${userTypeNum > 0 ? "E" + homeTargetId : homeTargetId}`
-            : "";
-          break;
-      }
-
-      // 切换会话(确保ID有效再执行)
-      if (conversationId) {
-        console.log("切换会话", conversationId);
-        await TUIConversationService.switchConversation(conversationId);
-        console.log("会话切换完成");
-      } else {
-        console.warn("未找到有效会话ID,跳过切换");
-      }
-    }
+export const loginChat = (loginInfo) => {
+  return TUILogin.login(loginInfo)
+    .then((res: any) => {
+      // 合并读取 storage 数据
+      const link = uni.getStorageSync("link");
+      const companyUserId = uni.getStorageSync("companyUserId");
+      const recruitUserId = uni.getStorageSync("recruitUserId");
+      const Identity = uni.getStorageSync("Identity");
+      const postId = uni.getStorageSync("postId");
+      const type = uni.getStorageSync("type");
+      const shopUserId = uni.getStorageSync("shopUserId");
+      const userLoginType = uni.getStorageSync("userLoginType");
+      const userId = uni.getStorageSync("userId"); // 修复此处错误
+      // 异步保存用户信息
+      uni.setStorage({
+        key: "userInfo",
+        data: JSON.stringify({
+          ...loginInfo,
+          TIMPush: undefined,
+          pushConfig: {},
+        }),
+      });
+      // 设置在线状态
+      TUIUserService.switchUserStatus({ displayOnlineStatus: true });
+      if (res.data) {
+        const userTypeNum = parseInt(userLoginType);
+        // 构建 conversationId
+        debugger;
+        const conversationMap = {
+          job: `C2CA${userTypeNum > 0 ? recruitUserId : companyUserId}`,
+          house: `C2CC${userTypeNum > 0 ? shopUserId : userId}`,
+          homemaking: `C2CE${userTypeNum > 0 ? shopUserId : userId}`,
+        };
+        console.log("conversationMap", conversationMap);
 
-    // 3.4 页面跳转(确保IM初始化完成后再跳转)
-    if (link) {
-      console.log("跳转到单聊页面");
-      if (type === "job") {
-        uni.navigateTo({
-          url: `/TUIKit/components/TUIChat/index?companyUserId=${companyUserId}&recruitUserId=${recruitUserId}&postId=${postId}`,
-        });
-      } else {
-        uni.navigateTo({
-          url: `/TUIKit-${capitalize(type)}/components/TUIChat/index`,
-        });
+        const conversationId = conversationMap[type];
+        if (conversationId) {
+          TUIConversationService.switchConversation(conversationId);
+        }
+        // 页面跳转逻辑
+        if (link) {
+          if (type === "job") {
+            uni.navigateTo({
+              url: `/TUIKit/components/TUIChat/index?companyUserId=${companyUserId}&recruitUserId=${recruitUserId}&postId=${postId}`,
+            });
+          } else {
+            uni.navigateTo({
+              url: `/TUIKit-${capitalize(type)}/components/TUIChat/index`,
+            });
+          }
+        } else {
+          handleRedirectBasedOnIdentity(Identity, loginInfo.userID);
+        }
       }
-    } else {
-      console.log("跳转到会话列表");
-      handleRedirectBasedOnIdentity(Identity, loginInfo.userID);
-    }
 
-    return res;
-  } catch (error) {
-    // 集中捕获所有阶段的错误
-    console.error("IM登录初始化失败", error);
-    uni.showToast({ title: "IM初始化失败,请重试", icon: "none" });
-    throw error; // 允许上层处理
-  }
+      return res;
+    })
+    .catch((error) => {
+      console.error("登录聊天失败", error);
+      uni.showToast({ title: "登录失败,请重试", icon: "none" });
+      throw error; // 抛出错误供上层处理
+    });
 };
 
-// 封装跳转逻辑(保持不变)
+// 封装跳转逻辑
 const handleRedirectBasedOnIdentity = (identity, userID) => {
   const isJob = identity === "job";
   const isMerchant = /[CE]/.test(userID);

+ 2 - 1
pages/views/login.vue

@@ -23,7 +23,8 @@ onLoad(() => {
     "shopUserId",
     "houseId",
     "refId",
-    "demandId"
+    "demandId",
+    "userLoginType",
   ];
   // 批量获取参数并存储到storage
   const params: any = {};