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