diff --git a/pages/login/login.vue b/pages/login/login.vue index 23716f4..caac2c2 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -105,12 +105,33 @@ function toHome() { }); } +async function syncExternalUserRelation() { + const currentTeam = team.value || {}; + const currentAccount = account.value || {}; + if (!(currentTeam.corpId && currentTeam.externalUserId && currentAccount.openid)) return; + try { + const res = await api('syncWxappExternalUserRelation', { + corpId: currentTeam.corpId, + externalUserId: currentTeam.externalUserId, + unionid: currentAccount.unionid || '', + openid: currentAccount.openid, + corpUserId: currentTeam.corpUserId || '', + }, false); + if (!res || !res.success) { + console.warn('关联 externalUserId 失败:', res && res.message); + } + } catch (error) { + console.warn('关联 externalUserId 异常:', error && error.message ? error.message : error); + } +} + async function bindTeam() { const res = await api('bindWxappWithTeam', { appid, corpId: team.value.corpId, teamId: team.value.teamId, openid: account.value.openid }); if (!res || !res.success) { return toast("关联团队失败"); } + await syncExternalUserRelation(); const res1 = await api('getWxAppCustomerCount', { miniAppId: account.value.openid, corpId: team.value.corpId, teamId: team.value.teamId }); if (res1 && res1.data > 0) { toHome(); @@ -126,7 +147,7 @@ async function getPhoneNumber(e) { if (!res) return; } if (team.value) { - bindTeam(account.value) + bindTeam() } else if (redirectUrl.value) { await attempToPage(redirectUrl.value); } else { diff --git a/pages/login/redirect-page.vue b/pages/login/redirect-page.vue index 1c409a5..085e432 100644 --- a/pages/login/redirect-page.vue +++ b/pages/login/redirect-page.vue @@ -29,7 +29,51 @@ function copy() { }) } -async function changeTeam({ teamId, corpId, corpUserId, qrid, referenceCustomerId }) { +function safeDecode(value) { + try { + return decodeURIComponent(value); + } catch (error) { + return value; + } +} + +function parseInviteOptions(options = {}) { + const href = + typeof options.q === "string" ? safeDecode(options.q) : ""; + const [, url = ""] = href.split("?"); + const data = url.split("&").reduce((acc, cur) => { + if (!cur) return acc; + const [key, ...valueParts] = cur.split("="); + if (!key) return acc; + acc[key] = safeDecode(valueParts.join("=") || ""); + return acc; + }, {}); + return { + ...options, + ...data, + }; +} + +async function syncExternalUserRelation({ corpId, externalUserId, corpUserId }) { + const currentAccount = account.value || {}; + if (!(corpId && externalUserId && currentAccount.openid)) return; + try { + const res = await api('syncWxappExternalUserRelation', { + corpId, + externalUserId, + unionid: currentAccount.unionid || '', + openid: currentAccount.openid, + corpUserId: corpUserId || '', + }, false); + if (!res || !res.success) { + console.warn('关联 externalUserId 失败:', res && res.message); + } + } catch (error) { + console.warn('关联 externalUserId 异常:', error && error.message ? error.message : error); + } +} + +async function changeTeam({ teamId, corpId, corpUserId, externalUserId, qrid, referenceCustomerId }) { loading.value = true; const res = await api("getTeamData", { teamId, corpId, withCorpName: true }); loading.value = false; @@ -39,16 +83,17 @@ async function changeTeam({ teamId, corpId, corpUserId, qrid, referenceCustomerI set('home-invite-team-info', { teamId: team.value.teamId, corpUserId: corpUserId || '', - corpUserId, + externalUserId: externalUserId || '', qrid, referenceCustomerId: referenceCustomerId || '' }); await login() if (account.value && account.value.mobile) { - bindTeam(corpUserId) + bindTeam({ corpUserId, externalUserId }) } else { set("invite-team-info", { corpUserId, + externalUserId, qrid, referenceCustomerId, corpId: team.value.corpId, @@ -69,11 +114,16 @@ async function changeTeam({ teamId, corpId, corpUserId, qrid, referenceCustomerI } } -async function bindTeam(corpUserId) { +async function bindTeam({ corpUserId, externalUserId }) { const res = await api('bindWxappWithTeam', { appid, corpId: team.value.corpId, teamId: team.value.teamId, openid: account.value.openid }); if (!res || !res.success) { return toast("关联团队失败"); } + await syncExternalUserRelation({ + corpId: team.value.corpId, + externalUserId, + corpUserId, + }); const res1 = await api('getWxAppCustomerCount', { miniAppId: account.value.openid, corpId: team.value.corpId, teamId: team.value.teamId }); if (res1 && res1.data > 0) { uni.switchTab({ @@ -89,15 +139,7 @@ async function bindTeam(corpUserId) { onLoad((options) => { if (options.q) { opts.value = JSON.stringify(options) - const href = - typeof options.q === "string" ? decodeURIComponent(options.q) : ""; - const [, url = ""] = href.split("?"); - const data = url.split("&").reduce((acc, cur) => { - const [key, value] = cur.split("="); - acc[key] = value; - return acc; - }, {}); - changeTeam(data); + changeTeam(parseInviteOptions(options)); } else if (options.type === 'archive') { changeTeam(options); } @@ -124,4 +166,4 @@ onLoad((options) => { opacity: 1; } } - \ No newline at end of file + diff --git a/utils/api.js b/utils/api.js index aeec03d..a2d56ed 100644 --- a/utils/api.js +++ b/utils/api.js @@ -58,6 +58,7 @@ const urlsConfig = { unbindMiniAppArchive: 'unbindMiniAppArchive', updateMedicalRecord: 'updateMedicalRecord', getUnionidToExternalUserid: 'getUnionidToExternalUserid', + syncWxappExternalUserRelation: 'syncWxappExternalUserRelation', getWxAppCustomerCount: "getWxAppCustomerCount", updateCustomer: 'update', getRefrencePeople: 'getRefrencePeople', @@ -115,4 +116,3 @@ export default async function api(urlId, data = {}, loading = true) { } }, loading) } -