loginChat.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { TUILogin } from "@tencentcloud/tui-core";
  2. import {
  3. TUIUserService,
  4. TUIConversationService,
  5. TUIStore,
  6. StoreName,
  7. } from "@tencentcloud/chat-uikit-engine";
  8. import { TUIGlobal } from "@tencentcloud/universal-api";
  9. function capitalize(str: string): string {
  10. if (!str) return "";
  11. return str.charAt(0).toUpperCase() + str.slice(1);
  12. }
  13. export const loginChat = (loginInfo) => {
  14. return TUILogin.login(loginInfo)
  15. .then((res: any) => {
  16. // 合并读取 storage 数据
  17. const link = uni.getStorageSync("link");
  18. const companyUserId = uni.getStorageSync("companyUserId");
  19. const recruitUserId = uni.getStorageSync("recruitUserId");
  20. const Identity = uni.getStorageSync("Identity");
  21. const postId = uni.getStorageSync("postId");
  22. const type = uni.getStorageSync("type");
  23. const shopUserId = uni.getStorageSync("shopUserId");
  24. const userLoginType = uni.getStorageSync("userLoginType");
  25. const userId = uni.getStorageSync("userId"); // 修复此处错误
  26. const userType = uni.getStorageSync("userType");
  27. // 异步保存用户信息
  28. uni.setStorage({
  29. key: "userInfo",
  30. data: JSON.stringify({
  31. ...loginInfo,
  32. TIMPush: undefined,
  33. pushConfig: {},
  34. }),
  35. });
  36. // 设置在线状态
  37. TUIUserService.switchUserStatus({ displayOnlineStatus: true });
  38. if (res.data) {
  39. const userTypeNum = parseInt(userLoginType);
  40. // 构建 conversationId
  41. // userTypeNum>0为商家向用户发 商家的IMID默认接收到的就是带ABC的ID
  42. // userTypeNum<=0时为用户像商家发 用户向商家发 需要进入商家的会话,商家的会话时商家的imID需要带ABCD
  43. // usertype:0 此房源为普通用户身份发布, 会话id中不带C,usertype:1 此房源为商家身份发布, 会话id中带C
  44. const conversationMap = {
  45. job: `C2C${userTypeNum > 0 ? recruitUserId : "A" + companyUserId}`,
  46. house: `C2C${userTypeNum > 0 ? shopUserId : userType > 0 ? "C" + shopUserId : shopUserId}`,
  47. homemaking: `C2C${userTypeNum > 0 ? shopUserId : "E" + shopUserId}`,
  48. };
  49. const conversationId = conversationMap[type];
  50. if (conversationId) {
  51. TUIConversationService.switchConversation(conversationId);
  52. }
  53. // 页面跳转逻辑
  54. if (link) {
  55. if (type === "job") {
  56. uni.redirectTo({
  57. url: `/TUIKit/components/TUIChat/index?companyUserId=${companyUserId}&recruitUserId=${recruitUserId}&postId=${postId}`,
  58. });
  59. } else {
  60. uni.redirectTo({
  61. url: `/TUIKit-${capitalize(type)}/components/TUIChat/index`,
  62. });
  63. }
  64. } else {
  65. handleRedirectBasedOnIdentity(Identity, loginInfo.userID);
  66. }
  67. }
  68. return res;
  69. })
  70. .catch((error) => {
  71. console.error("登录聊天失败", error);
  72. uni.showToast({ title: "登录失败,请重试", icon: "none" });
  73. throw error; // 抛出错误供上层处理
  74. });
  75. };
  76. // 封装跳转逻辑
  77. const handleRedirectBasedOnIdentity = (identity, userID) => {
  78. const isJob = identity === "job";
  79. const isMerchant = /[ABCDEFGHIJKLMNO]/.test(userID);
  80. const basePrefix = isJob ? "/TUIKit" : `/TUIKit-${capitalize(identity)}`;
  81. const suffixPath = isMerchant
  82. ? "/components/TUIConversation/merChantSideIndex"
  83. : "/components/TUIConversation/index";
  84. uni.reLaunch({
  85. url: `${basePrefix}${suffixPath}`,
  86. success: () => {
  87. uni.$emit("uikitLogin");
  88. },
  89. });
  90. };
  91. export const loginFromStorage = () => {
  92. uni?.getStorage({
  93. key: "userInfo",
  94. success: function (res) {
  95. if (res.data) {
  96. const loginInfo = {
  97. ...JSON.parse(res.data),
  98. };
  99. if (uni?.$TIMPush) {
  100. loginInfo.TIMPush = uni?.$TIMPush;
  101. loginInfo.pushConfig = {
  102. androidConfig: uni?.$TIMPushConfigs, // Android 推送配置,如不需要可传空。
  103. iOSConfig: {
  104. iOSBusinessID: "29064", // iOS 推送配置,如不需要可传空。
  105. },
  106. };
  107. }
  108. loginChat(loginInfo).catch(() => {
  109. uni?.removeStorage({
  110. key: "userInfo",
  111. });
  112. });
  113. }
  114. },
  115. });
  116. };
  117. export declare interface IEnterChatConfig {
  118. isLoginChat: boolean;
  119. conversationID: string;
  120. }
  121. export const openChat = (enterChatConfig: IEnterChatConfig) => {
  122. const { isLoginChat = false, conversationID = "" } = enterChatConfig || {};
  123. const chatPath = "/TUIKit/components/TUIChat/index";
  124. const currentConversationID = TUIStore.getData(
  125. StoreName.CONV,
  126. "currentConversationID"
  127. );
  128. if (!isLoginChat || !conversationID) {
  129. return;
  130. }
  131. if (!currentConversationID) {
  132. TUIConversationService.switchConversation(conversationID);
  133. uni?.navigateTo({
  134. url: chatPath,
  135. });
  136. } else if (currentConversationID !== conversationID) {
  137. uni.navigateBack({
  138. delta: 1,
  139. success: () => {
  140. TUIConversationService.switchConversation(conversationID);
  141. uni?.navigateTo({
  142. url: chatPath,
  143. });
  144. },
  145. });
  146. }
  147. };