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: "); console.log("App Launch: ");
// openId IM // openId IM
await this.initIMOnLaunch(); // await this.initIMOnLaunch();
}, },
onShow: function () { onShow: function () {
const db = dbStore(); const db = dbStore();

View File

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

View File

@ -2,34 +2,17 @@
<view class="message-page"> <view class="message-page">
<!-- 消息列表 --> <!-- 消息列表 -->
<scroll-view <scroll-view class="message-list" scroll-y="true" refresher-enabled :refresher-triggered="refreshing"
class="message-list" @refresherrefresh="handleRefresh" @scrolltolower="handleLoadMore">
scroll-y="true"
refresher-enabled
:refresher-triggered="refreshing"
@refresherrefresh="handleRefresh"
@scrolltolower="handleLoadMore"
>
<!-- 加载状态 --> <!-- 加载状态 -->
<view <view v-if="loading && conversationList.length === 0" class="loading-container">
v-if="loading && conversationList.length === 0"
class="loading-container"
>
<text class="loading-text">加载中...</text> <text class="loading-text">加载中...</text>
</view> </view>
<!-- 消息列表项 --> <!-- 消息列表项 -->
<view <view v-for="conversation in conversationList" :key="conversation.groupID || conversation.conversationID"
v-for="conversation in conversationList" class="message-item" @click="handleClickConversation(conversation)">
:key="conversation.groupID || conversation.conversationID"
class="message-item"
@click="handleClickConversation(conversation)"
>
<view class="avatar-container"> <view class="avatar-container">
<GroupAvatar <GroupAvatar :avatarList="getAvatarList(conversation.groupID)" :size="96" classType="square" />
:avatarList="getAvatarList(conversation.groupID)"
:size="96"
classType="square"
/>
<view v-if="conversation.unreadCount > 0" class="unread-badge"> <view v-if="conversation.unreadCount > 0" class="unread-badge">
<text class="unread-text">{{ <text class="unread-text">{{
conversation.unreadCount > 99 ? "99+" : conversation.unreadCount conversation.unreadCount > 99 ? "99+" : conversation.unreadCount
@ -57,10 +40,7 @@
</view> </view>
<!-- 空状态 --> <!-- 空状态 -->
<view <view v-if="!loading && conversationList.length === 0" class="empty-container">
v-if="!loading && conversationList.length === 0"
class="empty-container"
>
<image class="empty-image" src="/static/empty.svg" mode="aspectFit" /> <image class="empty-image" src="/static/empty.svg" mode="aspectFit" />
<text class="empty-text">暂无消息</text> <text class="empty-text">暂无消息</text>
</view> </view>
@ -76,7 +56,7 @@
</template> </template>
<script setup> <script setup>
import { ref, computed, onUnmounted } from "vue"; import { ref, computed, onUnmounted, watch } from "vue";
import { onLoad, onShow, onHide } from "@dcloudio/uni-app"; import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
import { storeToRefs } from "pinia"; import { storeToRefs } from "pinia";
import useAccountStore from "@/store/account.js"; 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"; import GroupAvatar from "@/components/group-avatar.vue";
// //
const { account, openid, isIMInitialized } = storeToRefs(useAccountStore()); const { account, openid, isIMInitialized, hasImCorpId } = storeToRefs(useAccountStore());
const { initIMAfterLogin } = useAccountStore(); const { initIMAfterLogin } = useAccountStore();
// //
@ -479,6 +459,7 @@ const handleLoadMore = () => {
// //
const handleRefresh = async () => { const handleRefresh = async () => {
if (!hasImCorpId.value) return;
refreshing.value = true; refreshing.value = true;
try { try {
@ -489,7 +470,7 @@ const handleRefresh = async () => {
}; };
// //
onLoad( async() => { async function init() {
try { try {
// IM // IM
const imReady = await initIM(); const imReady = await initIM();
@ -503,7 +484,7 @@ onLoad( async() => {
} catch (error) { } catch (error) {
console.log("页面初始化异常:", error.message); console.log("页面初始化异常:", error.message);
} }
}); };
// //
const cleanMessageText = (text) => { const cleanMessageText = (text) => {
@ -534,6 +515,11 @@ onHide(() => {
// //
}); });
watch(hasImCorpId, (n, o) => {
if (n && !o) {
init();
}
}, { immediate: true })
// //
onUnmounted(() => { onUnmounted(() => {
console.log("【消息列表页】页面卸载,清理回调"); console.log("【消息列表页】页面卸载,清理回调");

View File

@ -1,4 +1,4 @@
import { ref, watch } from "vue"; import { ref, watch, computed } from "vue";
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import api from '@/utils/api'; import api from '@/utils/api';
import { toast } from '@/utils/widget'; import { toast } from '@/utils/widget';
@ -13,6 +13,9 @@ export default defineStore("accountStore", () => {
const isIMInitialized = ref(false); const isIMInitialized = ref(false);
const openid = ref(""); const openid = ref("");
const externalUserId = 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 = '') { async function login(phoneCode = '') {
if (loading.value) return; if (loading.value) return;
@ -38,7 +41,7 @@ export default defineStore("accountStore", () => {
uni.setStorageSync('account', res.data); uni.setStorageSync('account', res.data);
uni.setStorageSync('openid', res.data.openid); uni.setStorageSync('openid', res.data.openid);
initIMAfterLogin(openid.value) // initIMAfterLogin(openid.value)
return res.data return res.data
} }
} }
@ -120,6 +123,21 @@ export default defineStore("accountStore", () => {
}); });
} }
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) { async function getExternalUserId(corpId) {
const unionid = account.value?.unionid; const unionid = account.value?.unionid;
const openid = account.value?.openid; 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 }
}) })