diff --git a/components/LoginPopup/LoginPopup.vue b/components/LoginPopup/LoginPopup.vue index b07bd69..e90e26e 100644 --- a/components/LoginPopup/LoginPopup.vue +++ b/components/LoginPopup/LoginPopup.vue @@ -93,7 +93,7 @@ import { useUserStore } from "@/stores/user"; import { getPlatformProvider, isSinglePageMode } from "@/utils/system"; import { uploadImage } from "@/utils/common"; import { apiLogin } from "@/api/auth.js"; -import { wxLogin } from "@/utils/login.js"; +import { wxLogin, alipayLogin } from "@/utils/login.js"; import PrivacyPopup from "@/components/PrivacyPopup/PrivacyPopup.vue"; const popupRef = ref(null); @@ -202,6 +202,7 @@ const confirmLogin = async () => { try { const platform = getPlatformProvider(); + if (platform === "mp-weixin") { const code = await wxLogin(); const imageUrl = avatarUrl.value @@ -230,6 +231,37 @@ const confirmLogin = async () => { emit("logind"); popupRef.value.close(); + // 重置临时变量 + 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 = ""; diff --git a/utils/login.js b/utils/login.js index c4981e2..e1cb1c1 100644 --- a/utils/login.js +++ b/utils/login.js @@ -1,30 +1,53 @@ export const wxLogin = () => { return new Promise((resolve, reject) => { wx.login({ - success: res => { + success: (res) => { if (res.code) { - resolve(res.code) + resolve(res.code); } else { - reject('登录失败:code为空') + reject("登录失败:code为空"); } }, - fail: err => { - reject(err) - } - }) - }) -} + fail: (err) => { + reject(err); + }, + }); + }); +}; + +export const alipayLogin = () => { + return new Promise((resolve, reject) => { + // #ifdef MP-ALIPAY + my.getAuthCode({ + scopes: "auth_user", + success: (res) => { + if (res.authCode) { + resolve(res.authCode); + } else { + reject("登录失败:authCode为空"); + } + }, + fail: (err) => { + reject(err); + }, + }); + // #endif + // #ifndef MP-ALIPAY + reject("当前非支付宝环境"); + // #endif + }); +}; export const wxGetUserProfile = () => { return new Promise((resolve, reject) => { wx.getUserProfile({ - desc: '用于完善用户信息', - success: res => { - resolve(res) + desc: "用于完善用户信息", + success: (res) => { + resolve(res); }, - fail: err => { - reject(err) - } - }) - }) -} + fail: (err) => { + reject(err); + }, + }); + }); +}; diff --git a/utils/request.js b/utils/request.js index 113e60b..022bc02 100644 --- a/utils/request.js +++ b/utils/request.js @@ -1,6 +1,6 @@ -const BASE_URL = "https://api.ai-meng.com"; +// const BASE_URL = "https://api.ai-meng.com"; // const BASE_URL = 'http://127.0.0.1:3999' -// const BASE_URL = "http://192.168.1.3:3999"; +const BASE_URL = "http://192.168.1.3:3999"; // const BASE_URL = "http://192.168.31.253:3999"; import { useUserStore } from "@/stores/user";