im登录根据机构id来

This commit is contained in:
huxuejian 2026-04-14 16:17:40 +08:00
parent acfd0fddbd
commit f7a0e9454c
4 changed files with 71 additions and 54 deletions

View File

@ -8,7 +8,7 @@ export default {
console.log("App Launch: ");
// openId IM
await this.initIMOnLaunch();
// await this.initIMOnLaunch();
},
onShow: function () {
const db = dbStore();

View File

@ -45,7 +45,7 @@ import pageLoading from "./loading.vue";
// const { useLoad, useShow } = useGuard();
const { account } = storeToRefs(useAccount());
const { login } = useAccount();
const { login, getTeams } = useAccount();
const team = ref(null);
const teams = ref([]);
@ -77,10 +77,9 @@ async function changeTeam({ teamId, corpId, corpName }) {
}
}
async function getTeams(inviteTeamId = '') {
async function getMatchTeams(inviteTeamId = '') {
loading.value = true;
const res = await api('getWxappRelateTeams', { openid: account.value.openid });
teams.value = res && Array.isArray(res.data) ? res.data : [];
teams.value = await getTeams();
const matchTeamId = inviteTeamId || (team.value ? team.value.teamId : '');
const validTeam = teams.value.find(i => i.teamId && i.teamId === matchTeamId);
const firstTeam = teams.value[0]
@ -110,7 +109,7 @@ onShow(async () => {
corpUserIds.value[inviteTeam.teamId] = inviteTeam.corpUserId;
}
if (account.value && account.value.openid) {
getTeams(inviteTeam && inviteTeam.teamId ? inviteTeam.teamId : '');
getMatchTeams(inviteTeam && inviteTeam.teamId ? inviteTeam.teamId : '');
} else {
teams.value = [];
}
@ -124,11 +123,13 @@ onShow(async () => {
watch(account, (n, o) => {
if (n && !o) {
getTeams();
getMatchTeams();
} else if (!n && o) {
teams.value = [];
}
})
</script>
<style scoped>
.home-container {

View File

@ -2,34 +2,17 @@
<view class="message-page">
<!-- 消息列表 -->
<scroll-view
class="message-list"
scroll-y="true"
refresher-enabled
:refresher-triggered="refreshing"
@refresherrefresh="handleRefresh"
@scrolltolower="handleLoadMore"
>
<scroll-view class="message-list" scroll-y="true" refresher-enabled :refresher-triggered="refreshing"
@refresherrefresh="handleRefresh" @scrolltolower="handleLoadMore">
<!-- 加载状态 -->
<view
v-if="loading && conversationList.length === 0"
class="loading-container"
>
<view v-if="loading && conversationList.length === 0" class="loading-container">
<text class="loading-text">加载中...</text>
</view>
<!-- 消息列表项 -->
<view
v-for="conversation in conversationList"
:key="conversation.groupID || conversation.conversationID"
class="message-item"
@click="handleClickConversation(conversation)"
>
<view v-for="conversation in conversationList" :key="conversation.groupID || conversation.conversationID"
class="message-item" @click="handleClickConversation(conversation)">
<view class="avatar-container">
<GroupAvatar
:avatarList="getAvatarList(conversation.groupID)"
:size="96"
classType="square"
/>
<GroupAvatar :avatarList="getAvatarList(conversation.groupID)" :size="96" classType="square" />
<view v-if="conversation.unreadCount > 0" class="unread-badge">
<text class="unread-text">{{
conversation.unreadCount > 99 ? "99+" : conversation.unreadCount
@ -57,10 +40,7 @@
</view>
<!-- 空状态 -->
<view
v-if="!loading && conversationList.length === 0"
class="empty-container"
>
<view v-if="!loading && conversationList.length === 0" class="empty-container">
<image class="empty-image" src="/static/empty.svg" mode="aspectFit" />
<text class="empty-text">暂无消息</text>
</view>
@ -76,7 +56,7 @@
</template>
<script setup>
import { ref, computed, onUnmounted } from "vue";
import { ref, computed, onUnmounted, watch } from "vue";
import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
import { storeToRefs } from "pinia";
import useAccountStore from "@/store/account.js";
@ -87,7 +67,7 @@ import useGroupAvatars from "./hooks/use-group-avatars.js";
import GroupAvatar from "@/components/group-avatar.vue";
//
const { account, openid, isIMInitialized } = storeToRefs(useAccountStore());
const { account, openid, isIMInitialized, hasImCorpId } = storeToRefs(useAccountStore());
const { initIMAfterLogin } = useAccountStore();
//
@ -255,7 +235,7 @@ let updateTimer = null;
//
const handleConversationListUpdate = async (eventData) => {
console.log("【消息列表页】会话列表更新事件:", eventData);
//
if (eventData && !Array.isArray(eventData) && eventData.conversationID) {
const conversationID = eventData.conversationID;
@ -374,9 +354,9 @@ const handleMessageReceived = (message) => {
//
const setupConversationListener = () => {
if (!globalTimChatManager) return;
console.log("【消息列表页】设置会话监听器");
//
//
if (globalUnreadListenerManager.isInitialized) {
@ -436,7 +416,7 @@ const formatMessageTime = (timestamp) => {
//
const handleClickConversation = async (conversation) => {
console.log("点击会话:", conversation);
//
const conversationIndex = conversationList.value.findIndex(
(conv) => conv.conversationID === conversation.conversationID
@ -452,7 +432,7 @@ const handleClickConversation = async (conversation) => {
conversationID: conversation.conversationID,
});
console.log("✓ 已标记会话为已读:", conversation.conversationID);
// tabBar
// await globalUnreadListenerManager.refreshBadge();
} catch (error) {
@ -479,6 +459,7 @@ const handleLoadMore = () => {
//
const handleRefresh = async () => {
if (!hasImCorpId.value) return;
refreshing.value = true;
try {
@ -489,7 +470,7 @@ const handleRefresh = async () => {
};
//
onLoad( async() => {
async function init() {
try {
// IM
const imReady = await initIM();
@ -503,7 +484,7 @@ onLoad( async() => {
} catch (error) {
console.log("页面初始化异常:", error.message);
}
});
};
//
const cleanMessageText = (text) => {
@ -522,28 +503,33 @@ onShow(async () => {
//
onHide(() => {
console.log("【消息列表页】页面隐藏");
//
if (updateTimer) {
clearTimeout(updateTimer);
updateTimer = null;
}
// globalTimChatManager
//
//
});
watch(hasImCorpId, (n, o) => {
if (n && !o) {
init();
}
}, { immediate: true })
//
onUnmounted(() => {
console.log("【消息列表页】页面卸载,清理回调");
//
if (updateTimer) {
clearTimeout(updateTimer);
updateTimer = null;
}
// //
// if (globalUnreadListenerManager.isInitialized) {
// globalUnreadListenerManager.removeCallback("onConversationListUpdated", handleConversationListUpdate);

View File

@ -1,4 +1,4 @@
import { ref, watch } from "vue";
import { ref, watch, computed } from "vue";
import { defineStore } from "pinia";
import api from '@/utils/api';
import { toast } from '@/utils/widget';
@ -13,6 +13,9 @@ export default defineStore("accountStore", () => {
const isIMInitialized = ref(false);
const openid = ref("");
const externalUserId = ref('');
const teams = ref([]);
const hasImCorpId = computed(() => teams.value.some(i => i.corpId === 'wpLgjyawAA8N0gWmXgyJq8wpjGcOT7fg'));
const teamsPromise = ref(null);
async function login(phoneCode = '') {
if (loading.value) return;
@ -38,7 +41,7 @@ export default defineStore("accountStore", () => {
uni.setStorageSync('account', res.data);
uni.setStorageSync('openid', res.data.openid);
initIMAfterLogin(openid.value)
// initIMAfterLogin(openid.value)
return res.data
}
}
@ -77,10 +80,10 @@ export default defineStore("accountStore", () => {
isIMInitialized.value = true;
console.log('IM 初始化成功');
// IM 初始化成功后,设置全局未读消息监听
// globalUnreadListenerManager.setup();
return true;
} catch (error) {
console.log('IM初始化异常跳过 IM 初始化:', error.message);
@ -96,7 +99,7 @@ export default defineStore("accountStore", () => {
await globalTimChatManager.destroy();
console.log('腾讯IM退出成功');
}
// 清除全局未读监听
if (globalUnreadListenerManager.isInitialized) {
// globalUnreadListenerManager.destroy();
@ -113,13 +116,28 @@ export default defineStore("accountStore", () => {
// 清除本地存储
uni.removeStorageSync('account');
uni.removeStorageSync('openid');
// 清除 tabBar 徽章
uni.removeTabBarBadge({
index: 1
});
}
async function searchTeams() {
const res = await api('getWxappRelateTeams', { openid: account.value.openid });
teams.value = res && Array.isArray(res.data) ? res.data : [];
return teams.value;
}
async function getTeams() {
if (!teamsPromise.value) {
teamsPromise.value = searchTeams();
}
await teamsPromise.value;
teamsPromise.value = null;
return teams.value;
}
async function getExternalUserId(corpId) {
const unionid = account.value?.unionid;
const openid = account.value?.openid;
@ -130,5 +148,17 @@ export default defineStore("accountStore", () => {
}
}
return { account, login, initIMAfterLogin, logout, openid, isIMInitialized, externalUserId, getExternalUserId }
watch(hasImCorpId, n => {
if (n) {
initIMAfterLogin();
}
}, { immediate: true })
watch(openid, (n, o) => {
if (n && !o) {
getTeams();
}
}, { immediate: true })
return { account, teams, hasImCorpId, login, initIMAfterLogin, logout, openid, isIMInitialized, externalUserId, getExternalUserId, getTeams }
})