92 lines
2.5 KiB
JavaScript
92 lines
2.5 KiB
JavaScript
|
|
import api from "@/utils/api";
|
||
|
|
import { set } from "@/utils/cache";
|
||
|
|
import { normalizeCorpId } from "@/utils/api-base-config";
|
||
|
|
|
||
|
|
function buildTeamLoginUrl(redirectUrl) {
|
||
|
|
const query = [
|
||
|
|
"source=teamInvite",
|
||
|
|
`redirectUrl=${encodeURIComponent(redirectUrl || "")}`,
|
||
|
|
].join("&");
|
||
|
|
return `/pages/login/login?${query}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useTeamAccess() {
|
||
|
|
async function getTeamInfo({ corpId, teamId, fallbackCorpName = "" }) {
|
||
|
|
if (!corpId || !teamId) return null;
|
||
|
|
try {
|
||
|
|
const res = await api(
|
||
|
|
"getTeamData",
|
||
|
|
{ corpId, teamId, withCorpName: true },
|
||
|
|
false
|
||
|
|
);
|
||
|
|
const team = res?.data || null;
|
||
|
|
if (!team) return null;
|
||
|
|
return {
|
||
|
|
...team,
|
||
|
|
corpName: team.corpName || team.corp_name || fallbackCorpName,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
console.warn("getTeamData failed", error);
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function openTeamLogin({
|
||
|
|
corpId,
|
||
|
|
teamId,
|
||
|
|
redirectUrl,
|
||
|
|
fallbackCorpName = "",
|
||
|
|
corpUserId = "",
|
||
|
|
externalUserId = "",
|
||
|
|
}) {
|
||
|
|
if (!corpId || !teamId) {
|
||
|
|
return { success: false, message: "团队信息缺失,暂无法授权" };
|
||
|
|
}
|
||
|
|
|
||
|
|
const team = await getTeamInfo({ corpId, teamId, fallbackCorpName });
|
||
|
|
set("invite-team-info", {
|
||
|
|
corpId,
|
||
|
|
teamId,
|
||
|
|
corpUserId,
|
||
|
|
externalUserId,
|
||
|
|
corpName: team?.corpName || fallbackCorpName,
|
||
|
|
teamName: team?.name || team?.teamName || "",
|
||
|
|
avatars: Array.isArray(team?.memberList)
|
||
|
|
? team.memberList.map((member) => member?.avatar || "").filter(Boolean)
|
||
|
|
: [],
|
||
|
|
});
|
||
|
|
uni.navigateTo({ url: buildTeamLoginUrl(redirectUrl) });
|
||
|
|
return { success: true, team };
|
||
|
|
}
|
||
|
|
|
||
|
|
async function ensureTeamAdded({ appid, account, getTeams, corpId, teamId }) {
|
||
|
|
if (!appid || !account?.openid || !corpId || !teamId) {
|
||
|
|
return { success: false, message: "团队信息缺失,暂无法绑定" };
|
||
|
|
}
|
||
|
|
const teams = await getTeams();
|
||
|
|
const linked = (teams || []).some(
|
||
|
|
(team) =>
|
||
|
|
normalizeCorpId(team.corpId) === normalizeCorpId(corpId) &&
|
||
|
|
String(team.teamId) === String(teamId)
|
||
|
|
);
|
||
|
|
if (linked) return { success: true, alreadyLinked: true };
|
||
|
|
|
||
|
|
const res = await api(
|
||
|
|
"bindWxappWithTeam",
|
||
|
|
{ appid, corpId, teamId, openid: account.openid },
|
||
|
|
false
|
||
|
|
);
|
||
|
|
if (!res?.success) {
|
||
|
|
return { success: false, message: res?.message || "添加团队失败" };
|
||
|
|
}
|
||
|
|
await getTeams();
|
||
|
|
return { success: true, alreadyLinked: false };
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
getTeamInfo,
|
||
|
|
openTeamLogin,
|
||
|
|
ensureTeamAdded,
|
||
|
|
};
|
||
|
|
}
|