im登录根据机构id来
This commit is contained in:
parent
acfd0fddbd
commit
f7a0e9454c
2
App.vue
2
App.vue
@ -8,7 +8,7 @@ export default {
|
||||
console.log("App Launch: ");
|
||||
|
||||
// 获取 openId 并初始化 IM
|
||||
await this.initIMOnLaunch();
|
||||
// await this.initIMOnLaunch();
|
||||
},
|
||||
onShow: function () {
|
||||
const db = dbStore();
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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 }
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user