loginChat.ts 2.6 KB

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