|
@@ -11,28 +11,47 @@ function capitalize(str: string): string {
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
|
}
|
|
|
|
|
|
-export const loginChat = (loginInfo) => {
|
|
|
- return TUILogin.login(loginInfo).then((res: any) => {
|
|
|
- let link = uni.getStorageSync("link");
|
|
|
- let companyUserId = uni.getStorageSync("companyUserId");
|
|
|
- let recruitUserId = uni.getStorageSync("recruitUserId");
|
|
|
- let Identity = uni.getStorageSync("Identity");
|
|
|
- let postId = uni.getStorageSync("postId");
|
|
|
- let type = uni.getStorageSync("type");
|
|
|
- let shopUserId = uni.getStorageSync("shopUserId");
|
|
|
+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;
|
|
|
+ }
|
|
|
|
|
|
- if (type == "job" && recruitUserId) {
|
|
|
- TUIUserService.switchUserStatus({ displayOnlineStatus: true });
|
|
|
- TUIConversationService.switchConversation(`C2C${recruitUserId}`);
|
|
|
- } else if (type == "house") {
|
|
|
- TUIUserService.switchUserStatus({ displayOnlineStatus: true });
|
|
|
- TUIConversationService.switchConversation(`C2C${shopUserId}`);
|
|
|
- } else if (type == "homemaking") {
|
|
|
- TUIUserService.switchUserStatus({ displayOnlineStatus: true });
|
|
|
- TUIConversationService.switchConversation(`C2C${shopUserId}`);
|
|
|
- }
|
|
|
+ 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));
|
|
|
|
|
|
- uni?.setStorage({
|
|
|
+ // 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,
|
|
@@ -40,9 +59,51 @@ export const loginChat = (loginInfo) => {
|
|
|
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,跳过切换");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3.4 页面跳转(确保IM初始化完成后再跳转)
|
|
|
if (link) {
|
|
|
- if (type == "job") {
|
|
|
+ console.log("跳转到单聊页面");
|
|
|
+ if (type === "job") {
|
|
|
uni.navigateTo({
|
|
|
url: `/TUIKit/components/TUIChat/index?companyUserId=${companyUserId}&recruitUserId=${recruitUserId}&postId=${postId}`,
|
|
|
});
|
|
@@ -51,51 +112,34 @@ export const loginChat = (loginInfo) => {
|
|
|
url: `/TUIKit-${capitalize(type)}/components/TUIChat/index`,
|
|
|
});
|
|
|
}
|
|
|
- }
|
|
|
- //跳转列表
|
|
|
- else {
|
|
|
- if (Identity == "job") {
|
|
|
- if (loginInfo.userID.includes("A")) {
|
|
|
- //求职
|
|
|
- uni?.reLaunch({
|
|
|
- url: "/TUIKit/components/TUIConversation/merChantSideIndex",
|
|
|
- success: () => {
|
|
|
- uni?.$emit("uikitLogin", res);
|
|
|
- },
|
|
|
- });
|
|
|
- } else {
|
|
|
- uni?.reLaunch({
|
|
|
- url: "/TUIKit/components/TUIConversation/index",
|
|
|
- success: () => {
|
|
|
- uni?.$emit("uikitLogin", res);
|
|
|
- },
|
|
|
- });
|
|
|
- }
|
|
|
- } else {
|
|
|
- const hasLetter = /[CE]/.test(loginInfo.userID);
|
|
|
- if (hasLetter) {
|
|
|
- uni?.reLaunch({
|
|
|
- url: `/TUIKit-${capitalize(
|
|
|
- Identity
|
|
|
- )}/components/TUIConversation/merChantSideIndex`,
|
|
|
- success: () => {
|
|
|
- uni?.$emit("uikitLogin", res);
|
|
|
- },
|
|
|
- });
|
|
|
- } else {
|
|
|
- uni?.reLaunch({
|
|
|
- url: `/TUIKit-${capitalize(
|
|
|
- Identity
|
|
|
- )}/components/TUIConversation/index`,
|
|
|
- success: () => {
|
|
|
- uni?.$emit("uikitLogin", res);
|
|
|
- },
|
|
|
- });
|
|
|
- }
|
|
|
- }
|
|
|
+ } else {
|
|
|
+ console.log("跳转到会话列表");
|
|
|
+ handleRedirectBasedOnIdentity(Identity, loginInfo.userID);
|
|
|
}
|
|
|
|
|
|
return res;
|
|
|
+ } catch (error) {
|
|
|
+ // 集中捕获所有阶段的错误
|
|
|
+ console.error("IM登录初始化失败", error);
|
|
|
+ uni.showToast({ title: "IM初始化失败,请重试", icon: "none" });
|
|
|
+ throw error; // 允许上层处理
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// 封装跳转逻辑(保持不变)
|
|
|
+const handleRedirectBasedOnIdentity = (identity, userID) => {
|
|
|
+ const isJob = identity === "job";
|
|
|
+ const isMerchant = /[CE]/.test(userID);
|
|
|
+ const basePrefix = isJob ? "/TUIKit" : `/TUIKit-${capitalize(identity)}`;
|
|
|
+ const suffixPath = isMerchant
|
|
|
+ ? "/components/TUIConversation/merChantSideIndex"
|
|
|
+ : "/components/TUIConversation/index";
|
|
|
+
|
|
|
+ uni.reLaunch({
|
|
|
+ url: `${basePrefix}${suffixPath}`,
|
|
|
+ success: () => {
|
|
|
+ uni.$emit("uikitLogin");
|
|
|
+ },
|
|
|
});
|
|
|
};
|
|
|
|