diff --git a/components/LoginPopup/LoginPopup.vue b/components/LoginPopup/LoginPopup.vue index e90e26e..59510a1 100644 --- a/components/LoginPopup/LoginPopup.vue +++ b/components/LoginPopup/LoginPopup.vue @@ -160,6 +160,11 @@ const getFestivalName = () => { }; const open = async () => { + // #ifdef MP-ALIPAY + handleAlipayLogin(); + return; + // #endif + // #ifdef MP-WEIXIN const isAgreed = await privacyRef.value.check(); if (isAgreed) { @@ -167,11 +172,50 @@ const open = async () => { } // #endif - // #ifndef MP-WEIXIN + // #ifndef MP-WEIXIN || MP-ALIPAY popupRef.value.open(); // #endif }; +const handleAlipayLogin = async () => { + try { + uni.showLoading({ title: "登录中...", mask: true }); + const code = await alipayLogin(); + const imageUrl = avatarUrl.value ? await uploadImage(avatarUrl.value) : ""; + + const loginRes = await apiLogin({ + code, + nickname: nickname.value || getFestivalName(), + avatarUrl: imageUrl, + platform: "alipay", + shareToken: props.shareToken, + }); + // 保存用户信息到store + userStore.setUserInfo({ + nickName: loginRes?.user?.nickname || nickname.value || getFestivalName(), + avatarUrl: loginRes?.user?.avatar || imageUrl, + id: loginRes?.user?.id, + isVip: loginRes?.isVip || false, + vipExpireAt: loginRes?.vipExpireAt || null, + }); + + userStore.setToken(loginRes.token); + + uni.hideLoading(); + uni.showToast({ title: "登录成功", icon: "success" }); + emit("logind"); + popupRef.value.close(); + + // 重置临时变量 + avatarUrl.value = ""; + nickname.value = ""; + } catch (err) { + uni.hideLoading(); + uni.showToast({ title: "登录失败", icon: "none" }); + console.error(err); + } +}; + const onPrivacyAgree = () => { popupRef.value.open(); }; @@ -235,36 +279,7 @@ const confirmLogin = async () => { avatarUrl.value = ""; nickname.value = ""; } else if (platform === "mp-alipay") { - const code = await alipayLogin(); - const imageUrl = avatarUrl.value - ? await uploadImage(avatarUrl.value) - : ""; - - const loginRes = await apiLogin({ - code, - nickname: nickname.value || getFestivalName(), - avatarUrl: imageUrl, - platform: "alipay", - shareToken: props.shareToken, - }); - // 保存用户信息到store - userStore.setUserInfo({ - nickName: loginRes?.user?.nickname || nickname.value, - avatarUrl: loginRes?.user?.avatar || imageUrl, - id: loginRes?.user?.id, - isVip: loginRes?.isVip || false, - vipExpireAt: loginRes?.vipExpireAt || null, - }); - - userStore.setToken(loginRes.token); - - uni.showToast({ title: "登录成功", icon: "success" }); - emit("logind"); - popupRef.value.close(); - - // 重置临时变量 - avatarUrl.value = ""; - nickname.value = ""; + await handleAlipayLogin(); } } catch (err) { uni.showToast({ title: "登录失败", icon: "none" }); diff --git a/components/PrivacyPopup/PrivacyPopup.vue b/components/PrivacyPopup/PrivacyPopup.vue index 05566fb..6bd276e 100644 --- a/components/PrivacyPopup/PrivacyPopup.vue +++ b/components/PrivacyPopup/PrivacyPopup.vue @@ -35,8 +35,8 @@ const privacyContractName = ref("《用户隐私保护指引》"); const emit = defineEmits(["agree"]); let resolveCheck = null; -// #ifdef MP-WEIXIN const check = () => { + // #ifdef MP-WEIXIN return new Promise((resolve) => { if (uni.getPrivacySetting) { uni.getPrivacySetting({ @@ -59,8 +59,11 @@ const check = () => { resolve(true); } }); + // #endif + // #ifdef MP-ALIPAY + return Promise.resolve(true); + // #endif }; -// #endif // Only for WeChat Mini Program // #ifdef MP-WEIXIN