feat: alipay login
This commit is contained in:
@@ -93,7 +93,7 @@ import { useUserStore } from "@/stores/user";
|
|||||||
import { getPlatformProvider, isSinglePageMode } from "@/utils/system";
|
import { getPlatformProvider, isSinglePageMode } from "@/utils/system";
|
||||||
import { uploadImage } from "@/utils/common";
|
import { uploadImage } from "@/utils/common";
|
||||||
import { apiLogin } from "@/api/auth.js";
|
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";
|
import PrivacyPopup from "@/components/PrivacyPopup/PrivacyPopup.vue";
|
||||||
|
|
||||||
const popupRef = ref(null);
|
const popupRef = ref(null);
|
||||||
@@ -202,6 +202,7 @@ const confirmLogin = async () => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const platform = getPlatformProvider();
|
const platform = getPlatformProvider();
|
||||||
|
|
||||||
if (platform === "mp-weixin") {
|
if (platform === "mp-weixin") {
|
||||||
const code = await wxLogin();
|
const code = await wxLogin();
|
||||||
const imageUrl = avatarUrl.value
|
const imageUrl = avatarUrl.value
|
||||||
@@ -230,6 +231,37 @@ const confirmLogin = async () => {
|
|||||||
emit("logind");
|
emit("logind");
|
||||||
popupRef.value.close();
|
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 = "";
|
avatarUrl.value = "";
|
||||||
nickname.value = "";
|
nickname.value = "";
|
||||||
|
|||||||
@@ -1,30 +1,53 @@
|
|||||||
export const wxLogin = () => {
|
export const wxLogin = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
wx.login({
|
wx.login({
|
||||||
success: res => {
|
success: (res) => {
|
||||||
if (res.code) {
|
if (res.code) {
|
||||||
resolve(res.code)
|
resolve(res.code);
|
||||||
} else {
|
} else {
|
||||||
reject('登录失败:code为空')
|
reject("登录失败:code为空");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
fail: err => {
|
fail: (err) => {
|
||||||
reject(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 = () => {
|
export const wxGetUserProfile = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
wx.getUserProfile({
|
wx.getUserProfile({
|
||||||
desc: '用于完善用户信息',
|
desc: "用于完善用户信息",
|
||||||
success: res => {
|
success: (res) => {
|
||||||
resolve(res)
|
resolve(res);
|
||||||
},
|
},
|
||||||
fail: err => {
|
fail: (err) => {
|
||||||
reject(err)
|
reject(err);
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -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://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";
|
// const BASE_URL = "http://192.168.31.253:3999";
|
||||||
import { useUserStore } from "@/stores/user";
|
import { useUserStore } from "@/stores/user";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user