loginChat.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. debugger;
  41. const conversationMap = {
  42. job: `C2CA${userTypeNum > 0 ? recruitUserId : companyUserId}`,
  43. house: `C2CC${userTypeNum > 0 ? shopUserId : userId}`,
  44. homemaking: `C2CE${userTypeNum > 0 ? shopUserId : userId}`,
  45. };
  46. console.log("conversationMap", conversationMap);
  47. const conversationId = conversationMap[type];
  48. if (conversationId) {
  49. TUIConversationService.switchConversation(conversationId);
  50. }
  51. // 页面跳转逻辑
  52. if (link) {
  53. if (type === "job") {
  54. uni.navigateTo({
  55. url: `/TUIKit/components/TUIChat/index?companyUserId=${companyUserId}&recruitUserId=${recruitUserId}&postId=${postId}`,
  56. });
  57. } else {
  58. uni.navigateTo({
  59. url: `/TUIKit-${capitalize(type)}/components/TUIChat/index`,
  60. });
  61. }
  62. } else {
  63. handleRedirectBasedOnIdentity(Identity, loginInfo.userID);
  64. }
  65. }
  66. return res;
  67. })
  68. .catch((error) => {
  69. console.error("登录聊天失败", error);
  70. uni.showToast({ title: "登录失败,请重试", icon: "none" });
  71. throw error; // 抛出错误供上层处理
  72. });
  73. };
  74. // 封装跳转逻辑
  75. const handleRedirectBasedOnIdentity = (identity, userID) => {
  76. const isJob = identity === "job";
  77. const isMerchant = /[CE]/.test(userID);
  78. const basePrefix = isJob ? "/TUIKit" : `/TUIKit-${capitalize(identity)}`;
  79. const suffixPath = isMerchant
  80. ? "/components/TUIConversation/merChantSideIndex"
  81. : "/components/TUIConversation/index";
  82. uni.reLaunch({
  83. url: `${basePrefix}${suffixPath}`,
  84. success: () => {
  85. uni.$emit("uikitLogin");
  86. },
  87. });
  88. };
  89. export const loginFromStorage = () => {
  90. uni?.getStorage({
  91. key: "userInfo",
  92. success: function (res) {
  93. if (res.data) {
  94. const loginInfo = {
  95. ...JSON.parse(res.data),
  96. };
  97. if (uni?.$TIMPush) {
  98. loginInfo.TIMPush = uni?.$TIMPush;
  99. loginInfo.pushConfig = {
  100. androidConfig: uni?.$TIMPushConfigs, // Android 推送配置,如不需要可传空。
  101. iOSConfig: {
  102. iOSBusinessID: "29064", // iOS 推送配置,如不需要可传空。
  103. },
  104. };
  105. }
  106. loginChat(loginInfo).catch(() => {
  107. uni?.removeStorage({
  108. key: "userInfo",
  109. });
  110. });
  111. }
  112. },
  113. });
  114. };
  115. export declare interface IEnterChatConfig {
  116. isLoginChat: boolean;
  117. conversationID: string;
  118. }
  119. export const openChat = (enterChatConfig: IEnterChatConfig) => {
  120. const { isLoginChat = false, conversationID = "" } = enterChatConfig || {};
  121. const chatPath = "/TUIKit/components/TUIChat/index";
  122. const currentConversationID = TUIStore.getData(
  123. StoreName.CONV,
  124. "currentConversationID"
  125. );
  126. if (!isLoginChat || !conversationID) {
  127. return;
  128. }
  129. if (!currentConversationID) {
  130. TUIConversationService.switchConversation(conversationID);
  131. uni?.navigateTo({
  132. url: chatPath,
  133. });
  134. } else if (currentConversationID !== conversationID) {
  135. uni.navigateBack({
  136. delta: 1,
  137. success: () => {
  138. TUIConversationService.switchConversation(conversationID);
  139. uni?.navigateTo({
  140. url: chatPath,
  141. });
  142. },
  143. });
  144. }
  145. };