123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import { TUILogin } from "@tencentcloud/tui-core";
- import {
- TUIUserService,
- TUIConversationService,
- TUIStore,
- StoreName,
- } from "@tencentcloud/chat-uikit-engine";
- import { TUIGlobal } from "@tencentcloud/universal-api";
- function capitalize(str: string): string {
- if (!str) return "";
- return str.charAt(0).toUpperCase() + str.slice(1);
- }
- export const loginChat = (loginInfo) => {
- return TUILogin.login(loginInfo)
- .then((res: any) => {
- // 合并读取 storage 数据
- const link = uni.getStorageSync("link");
- const companyUserId = uni.getStorageSync("companyUserId");
- const recruitUserId = uni.getStorageSync("recruitUserId");
- const Identity = uni.getStorageSync("Identity");
- const postId = uni.getStorageSync("postId");
- const type = uni.getStorageSync("type");
- const shopUserId = uni.getStorageSync("shopUserId");
- const userLoginType = uni.getStorageSync("userLoginType");
- const userId = uni.getStorageSync("userId"); // 修复此处错误
- // 异步保存用户信息
- uni.setStorage({
- key: "userInfo",
- data: JSON.stringify({
- ...loginInfo,
- TIMPush: undefined,
- pushConfig: {},
- }),
- });
- // 设置在线状态
- TUIUserService.switchUserStatus({ displayOnlineStatus: true });
- if (res.data) {
- const userTypeNum = parseInt(userLoginType);
- // 构建 conversationId
- // userTypeNum>0为商家向用户发 商家的IMID默认接收到的就是带ABC的ID
- // userTypeNum<=0时为用户像商家发 用户向商家发 需要进入商家的会话,商家的会话时商家的imID需要带ABCD
- const conversationMap = {
- job: `C2C${userTypeNum > 0 ? recruitUserId : "A" + companyUserId}`,
- house: `C2C${userTypeNum > 0 ? shopUserId : "C" + userId}`,
- homemaking: `C2C${userTypeNum > 0 ? shopUserId : "E" + userId}`,
- };
- console.log("conversationMap", conversationMap);
- const conversationId = conversationMap[type];
- if (conversationId) {
- TUIConversationService.switchConversation(conversationId);
- }
- // 页面跳转逻辑
- if (link) {
- if (type === "job") {
- uni.navigateTo({
- url: `/TUIKit/components/TUIChat/index?companyUserId=${companyUserId}&recruitUserId=${recruitUserId}&postId=${postId}`,
- });
- } else {
- uni.navigateTo({
- url: `/TUIKit-${capitalize(type)}/components/TUIChat/index`,
- });
- }
- } else {
- handleRedirectBasedOnIdentity(Identity, loginInfo.userID);
- }
- }
- return res;
- })
- .catch((error) => {
- console.error("登录聊天失败", error);
- uni.showToast({ title: "登录失败,请重试", icon: "none" });
- throw error; // 抛出错误供上层处理
- });
- };
- // 封装跳转逻辑
- const handleRedirectBasedOnIdentity = (identity, userID) => {
- const isJob = identity === "job";
- const isMerchant = /[CE]/.test(userID);
- const basePrefix = isJob ? "/TUIKit" : `/TUIKit-${capitalize(identity)}`;
- const suffixPath = isMerchant
- ? "/components/TUIConversation/merChantSideIndex"
- : "/components/TUIConversation/index";
- uni.reLaunch({
- url: `${basePrefix}${suffixPath}`,
- success: () => {
- uni.$emit("uikitLogin");
- },
- });
- };
- export const loginFromStorage = () => {
- uni?.getStorage({
- key: "userInfo",
- success: function (res) {
- if (res.data) {
- const loginInfo = {
- ...JSON.parse(res.data),
- };
- if (uni?.$TIMPush) {
- loginInfo.TIMPush = uni?.$TIMPush;
- loginInfo.pushConfig = {
- androidConfig: uni?.$TIMPushConfigs, // Android 推送配置,如不需要可传空。
- iOSConfig: {
- iOSBusinessID: "29064", // iOS 推送配置,如不需要可传空。
- },
- };
- }
- loginChat(loginInfo).catch(() => {
- uni?.removeStorage({
- key: "userInfo",
- });
- });
- }
- },
- });
- };
- export declare interface IEnterChatConfig {
- isLoginChat: boolean;
- conversationID: string;
- }
- export const openChat = (enterChatConfig: IEnterChatConfig) => {
- const { isLoginChat = false, conversationID = "" } = enterChatConfig || {};
- const chatPath = "/TUIKit/components/TUIChat/index";
- const currentConversationID = TUIStore.getData(
- StoreName.CONV,
- "currentConversationID"
- );
- if (!isLoginChat || !conversationID) {
- return;
- }
- if (!currentConversationID) {
- TUIConversationService.switchConversation(conversationID);
- uni?.navigateTo({
- url: chatPath,
- });
- } else if (currentConversationID !== conversationID) {
- uni.navigateBack({
- delta: 1,
- success: () => {
- TUIConversationService.switchConversation(conversationID);
- uni?.navigateTo({
- url: chatPath,
- });
- },
- });
- }
- };
|