跳转优化

This commit is contained in:
zhanchao 2026-08-17 18:34:36 +08:00
parent 56c08eb255
commit 94a515924b
3 changed files with 129 additions and 85 deletions

91
hooks/use-team-access.js Normal file
View File

@ -0,0 +1,91 @@
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,
};
}

View File

@ -32,15 +32,16 @@ import { storeToRefs } from "pinia";
import { onLoad, onShow } from "@dcloudio/uni-app"; import { onLoad, onShow } from "@dcloudio/uni-app";
import useAccount from "@/store/account"; import useAccount from "@/store/account";
import api from "@/utils/api"; import api from "@/utils/api";
import { set } from "@/utils/cache";
import { hideLoading, loading, toast } from "@/utils/widget"; import { hideLoading, loading, toast } from "@/utils/widget";
import { normalizeCorpId } from "@/utils/api-base-config"; import { normalizeCorpId } from "@/utils/api-base-config";
import { useTeamAccess } from "@/hooks/use-team-access";
import FullPage from "@/components/full-page.vue"; import FullPage from "@/components/full-page.vue";
const env = __VITE_ENV__; const env = __VITE_ENV__;
const appid = env.MP_WX_APP_ID; const appid = env.MP_WX_APP_ID;
const { account } = storeToRefs(useAccount()); const { account } = storeToRefs(useAccount());
const { getTeams, login } = useAccount(); const { getTeams, login } = useAccount();
const { openTeamLogin: openTeamInviteLogin, ensureTeamAdded: ensureJoinedTeam } = useTeamAccess();
const corpId = ref(""); const corpId = ref("");
const issueId = ref(""); const issueId = ref("");
@ -54,7 +55,6 @@ const posterVisible = ref(false);
const archiveBindPending = ref(false); const archiveBindPending = ref(false);
const bindingTeamId = ref(""); const bindingTeamId = ref("");
const bindCorpName = ref(""); const bindCorpName = ref("");
const bindingTeam = ref(null);
const projectNameText = computed(() => { const projectNameText = computed(() => {
const projects = Array.isArray(issue.value?.projectSnapshot) ? issue.value.projectSnapshot : []; const projects = Array.isArray(issue.value?.projectSnapshot) ? issue.value.projectSnapshot : [];
@ -133,50 +133,22 @@ function buildClaimUrl() {
return `/pages/experience-coupon/claim?${params}`; return `/pages/experience-coupon/claim?${params}`;
} }
function buildTeamLoginUrl() {
const query = [
"source=teamInvite",
`redirectUrl=${encodeURIComponent(buildClaimUrl())}`,
].join("&");
return `/pages/login/login?${query}`;
}
async function getBindingTeam() {
if (!corpId.value || !bindingTeamId.value) return null;
if (bindingTeam.value) return bindingTeam.value;
try {
const res = await api(
"getTeamData",
{ corpId: corpId.value, teamId: bindingTeamId.value, withCorpName: true },
false
);
const data = res?.data || null;
bindingTeam.value = data;
bindCorpName.value = data?.corpName || data?.corp_name || bindCorpName.value;
return data;
} catch (e) {
console.warn("getTeamData failed", e);
return null;
}
}
async function openTeamLogin() { async function openTeamLogin() {
const customer = await getIssueCustomer(); const customer = await getIssueCustomer();
if (!customer || !bindingTeamId.value) { if (!customer || !bindingTeamId.value) {
tip.value = "体验券发放档案缺少所属团队,暂无法领取"; tip.value = "体验券发放档案缺少所属团队,暂无法领取";
return false; return false;
} }
const team = await getBindingTeam(); const result = await openTeamInviteLogin({
set("invite-team-info", {
corpId: corpId.value, corpId: corpId.value,
teamId: bindingTeamId.value, teamId: bindingTeamId.value,
corpName: bindCorpName.value || issue.value?.corpName || "", redirectUrl: buildClaimUrl(),
teamName: team?.name || team?.teamName || "", fallbackCorpName: bindCorpName.value || issue.value?.corpName || "",
avatars: Array.isArray(team?.memberList)
? team.memberList.map((member) => member?.avatar || "").filter(Boolean)
: [],
}); });
uni.navigateTo({ url: buildTeamLoginUrl() }); if (!result.success) {
tip.value = result.message;
return false;
}
return false; return false;
} }
@ -194,32 +166,20 @@ async function ensureTeamAdded() {
return false; return false;
} }
const teams = await getTeams();
const linked = (teams || []).some(
(team) =>
normalizeCorpId(team.corpId) === corpId.value &&
String(team.teamId) === String(bindingTeamId.value)
);
if (linked) return true;
loading("添加团队中..."); loading("添加团队中...");
try { try {
const res = await api( const res = await ensureJoinedTeam({
"bindWxappWithTeam", appid,
{ account: account.value,
appid, getTeams,
corpId: corpId.value, corpId: corpId.value,
teamId: bindingTeamId.value, teamId: bindingTeamId.value,
openid: account.value?.openid || "", });
}, if (!res.success) {
false tip.value = res.message;
);
if (!res?.success) {
tip.value = res?.message || "添加团队失败";
toast(tip.value); toast(tip.value);
return false; return false;
} }
await getTeams();
return true; return true;
} finally { } finally {
hideLoading(); hideLoading();

View File

@ -65,7 +65,7 @@ import { storeToRefs } from "pinia";
import useAccount from "@/store/account"; import useAccount from "@/store/account";
import api from "@/utils/api"; import api from "@/utils/api";
import { normalizeCorpId } from "@/utils/api-base-config"; import { normalizeCorpId } from "@/utils/api-base-config";
import { set } from "@/utils/cache"; import { useTeamAccess } from "@/hooks/use-team-access";
import { hideLoading, loading as showLoading, toast } from "@/utils/widget"; import { hideLoading, loading as showLoading, toast } from "@/utils/widget";
import FullPage from "@/components/full-page.vue"; import FullPage from "@/components/full-page.vue";
@ -73,6 +73,7 @@ const env = __VITE_ENV__;
const appid = env.MP_WX_APP_ID; const appid = env.MP_WX_APP_ID;
const { account, externalUserId } = storeToRefs(useAccount()); const { account, externalUserId } = storeToRefs(useAccount());
const { getTeams, getExternalUserId, login } = useAccount(); const { getTeams, getExternalUserId, login } = useAccount();
const { openTeamLogin: openTeamInviteLogin, ensureTeamAdded: ensureJoinedTeam } = useTeamAccess();
const corpId = ref(""); const corpId = ref("");
const teamId = ref(""); const teamId = ref("");
@ -112,18 +113,11 @@ function buildCurrentUrl() {
return `/pages/record/appointment-record?${params}`; return `/pages/record/appointment-record?${params}`;
} }
function buildTeamLoginUrl() {
const query = [
"source=teamInvite",
`redirectUrl=${encodeURIComponent(buildCurrentUrl())}`,
].join("&");
return `/pages/login/login?${query}`;
}
async function ensureLogin() { async function ensureLogin() {
if (!account.value) await login(); if (!account.value) await login();
if (!account.value) { if (!account.value) {
uni.navigateTo({ url: "/pages/login/login" }); await ensureCustomerContext();
await openTeamLogin();
return false; return false;
} }
return true; return true;
@ -132,20 +126,23 @@ async function ensureLogin() {
async function ensurePhoneAuthorized() { async function ensurePhoneAuthorized() {
if (account.value?.mobile) return true; if (account.value?.mobile) return true;
await ensureCustomerContext(); await ensureCustomerContext();
return openTeamLogin();
}
async function openTeamLogin() {
if (!corpId.value || !teamId.value) { if (!corpId.value || !teamId.value) {
toast("预约团队信息缺失,暂无法查看预约记录"); toast("预约团队信息缺失,暂无法查看预约记录");
return false; return false;
} }
set("invite-team-info", { const res = await openTeamInviteLogin({
corpId: corpId.value, corpId: corpId.value,
teamId: teamId.value, teamId: teamId.value,
corpUserId: corpUserId.value || "", corpUserId: corpUserId.value || "",
externalUserId: routeExternalUserId.value || externalUserId.value || "", externalUserId: routeExternalUserId.value || externalUserId.value || "",
corpName: corpName.value || "", fallbackCorpName: corpName.value || "",
teamName: "", redirectUrl: buildCurrentUrl(),
avatars: [],
}); });
uni.navigateTo({ url: buildTeamLoginUrl() }); if (!res.success) toast(res.message);
return false; return false;
} }
@ -166,24 +163,19 @@ async function ensureTeamAdded() {
toast("预约团队信息缺失,暂无法查看预约记录"); toast("预约团队信息缺失,暂无法查看预约记录");
return false; return false;
} }
const teams = await getTeams();
const linked = (teams || []).some(
(team) => normalizeCorpId(team.corpId) === corpId.value && String(team.teamId) === String(teamId.value)
);
if (linked) return true;
showLoading("添加团队中..."); showLoading("添加团队中...");
try { try {
const res = await api("bindWxappWithTeam", { const res = await ensureJoinedTeam({
appid, appid,
account: account.value,
getTeams,
corpId: corpId.value, corpId: corpId.value,
teamId: teamId.value, teamId: teamId.value,
openid: account.value?.openid || "", });
}, false); if (!res.success) {
if (!res?.success) { toast(res.message);
toast(res?.message || "添加团队失败");
return false; return false;
} }
await getTeams();
return true; return true;
} finally { } finally {
hideLoading(); hideLoading();
@ -203,6 +195,7 @@ async function ensureArchiveBound() {
} }
async function ensureAccessReady() { async function ensureAccessReady() {
await ensureCustomerContext();
const okLogin = await ensureLogin(); const okLogin = await ensureLogin();
if (!okLogin) return false; if (!okLogin) return false;
const okPhone = await ensurePhoneAuthorized(); const okPhone = await ensurePhoneAuthorized();
@ -542,4 +535,4 @@ onShow(async () => {
font-size: 28rpx; font-size: 28rpx;
color: #999; color: #999;
} }
</style> </style>