loginChat.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. export const loginChat = (loginInfo) => {
  10. return TUILogin.login(loginInfo).then((res: any) => {
  11. let personId = uni.getStorageSync("personId");
  12. let type = uni.getStorageSync("type");
  13. if (type == "link") {
  14. TUIGlobal?.reLaunch({
  15. url: "/TUIKit/components/TUIChat/indexlink",
  16. });
  17. TUIConversationService.switchConversation(`C2C${personId}`);
  18. } else {
  19. uni?.reLaunch({
  20. url: "/TUIKit/components/TUIConversation/index",
  21. success: () => {
  22. uni?.$emit("uikitLogin", res);
  23. },
  24. });
  25. TUIUserService.switchUserStatus({ displayOnlineStatus: true });
  26. uni?.setStorage({
  27. key: "userInfo",
  28. data: JSON.stringify({
  29. ...loginInfo,
  30. TIMPush: undefined,
  31. pushConfig: {},
  32. }),
  33. });
  34. }
  35. console.log("1231231", TUILogin.getContext());
  36. return res;
  37. });
  38. };
  39. export const loginFromStorage = () => {
  40. uni?.getStorage({
  41. key: "userInfo",
  42. success: function (res) {
  43. if (res.data) {
  44. const loginInfo = {
  45. ...JSON.parse(res.data),
  46. };
  47. if (uni?.$TIMPush) {
  48. loginInfo.TIMPush = uni?.$TIMPush;
  49. loginInfo.pushConfig = {
  50. androidConfig: uni?.$TIMPushConfigs, // Android 推送配置,如不需要可传空。
  51. iOSConfig: {
  52. iOSBusinessID: "29064", // iOS 推送配置,如不需要可传空。
  53. },
  54. };
  55. }
  56. loginChat(loginInfo).catch(() => {
  57. uni?.removeStorage({
  58. key: "userInfo",
  59. });
  60. });
  61. }
  62. },
  63. });
  64. };
  65. export declare interface IEnterChatConfig {
  66. isLoginChat: boolean;
  67. conversationID: string;
  68. }
  69. export const openChat = (enterChatConfig: IEnterChatConfig) => {
  70. const { isLoginChat = false, conversationID = "" } = enterChatConfig || {};
  71. const chatPath = "/TUIKit/components/TUIChat/index";
  72. const currentConversationID = TUIStore.getData(
  73. StoreName.CONV,
  74. "currentConversationID"
  75. );
  76. if (!isLoginChat || !conversationID) {
  77. return;
  78. }
  79. if (!currentConversationID) {
  80. TUIConversationService.switchConversation(conversationID);
  81. uni?.navigateTo({
  82. url: chatPath,
  83. });
  84. } else if (currentConversationID !== conversationID) {
  85. uni.navigateBack({
  86. delta: 1,
  87. success: () => {
  88. TUIConversationService.switchConversation(conversationID);
  89. uni?.navigateTo({
  90. url: chatPath,
  91. });
  92. },
  93. });
  94. }
  95. };