fix
This commit is contained in:
@@ -79,7 +79,7 @@ function getObjectValue(source: Record<string, any>, keys: string[]) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function getStringValue(source: Record<string, any>, keys: string[]):string {
|
||||
function getStringValue(source: Record<string, any>, keys: string[]): string {
|
||||
const value = getObjectValue(source, keys);
|
||||
if (value === undefined) return "";
|
||||
|
||||
@@ -213,12 +213,10 @@ Page({
|
||||
agree: false,
|
||||
nickFocus: true,
|
||||
showPopup: false,
|
||||
avatarUrl: defaultAvatarUrl,
|
||||
nickname: '',
|
||||
safeBottom: app.globalData.safeBottom,
|
||||
showLoginImage: false,
|
||||
userInfo: {
|
||||
avatarUrl: defaultAvatarUrl,
|
||||
nickName: "微信用户",
|
||||
},
|
||||
},
|
||||
|
||||
applyLoginDisplayConfig(options: Record<string, any> = {}) {
|
||||
@@ -245,10 +243,7 @@ Page({
|
||||
success: (res: any) => {
|
||||
const { data } = JSON.parse(res.data);
|
||||
this.setData({
|
||||
userInfo: {
|
||||
...this.data.userInfo,
|
||||
avatarUrl: data,
|
||||
},
|
||||
avatarUrl: data,
|
||||
});
|
||||
},
|
||||
fail: () => {
|
||||
@@ -275,7 +270,6 @@ Page({
|
||||
const tabPages = [
|
||||
"/pages/tab-bar/medicine-box/index",
|
||||
"/pages/tab-bar/shopping-cart/shopping-cart",
|
||||
"/pages/tab-bar/course/course",
|
||||
"/pages/tab-bar/user/user",
|
||||
"/pages/tab-bar/points-mall/points-mall",
|
||||
"/pages/tab-bar/category/category",
|
||||
@@ -288,24 +282,8 @@ Page({
|
||||
"/package-live/wxroom/index";
|
||||
wx.removeStorageSync(WXROOM_REDIRECT_PATH_KEY);
|
||||
} else {
|
||||
const store = app.globalData.store;
|
||||
const state = wx.getStorageSync("state") ?? undefined;
|
||||
const code = wx.getStorageSync("code") ?? undefined;
|
||||
const groupId = wx.getStorageSync("groupId") ?? undefined;
|
||||
|
||||
if (state && !code) {
|
||||
if (self.data.back === "video") {
|
||||
const storeParams =
|
||||
typeof store === "string" ? store : this.buildQueryString(store);
|
||||
fullPath = `/pages/live/video/video?${storeParams}`;
|
||||
} else if (self.data.back === "red") {
|
||||
fullPath = `/pages/common/red/red`;
|
||||
} else if (self.data.back === "user") {
|
||||
fullPath = `/pages/tab-bar/user/user`;
|
||||
} else {
|
||||
fullPath = `/pages/live/registration/registration?state=${state}`;
|
||||
}
|
||||
} else if (!state && code) {
|
||||
const store = app.globalData.store;
|
||||
const code = wx.getStorageSync("code") ?? undefined;
|
||||
if (self.data.back === "video") {
|
||||
const storeParams =
|
||||
typeof store === "string" ? store : this.buildQueryString(store);
|
||||
@@ -317,15 +295,7 @@ Page({
|
||||
} else {
|
||||
fullPath = `/pages/live/registration/registration?code=${code}`;
|
||||
}
|
||||
} else {
|
||||
if (self.data.back === "room") {
|
||||
fullPath = `/pages/live/room/room?groupId=${groupId}`;
|
||||
} else if (self.data.back === "red") {
|
||||
fullPath = `/pages/common/red/red`;
|
||||
} else if (self.data.back === "user") {
|
||||
fullPath = `/pages/tab-bar/user/user`;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
const pages = getCurrentPages();
|
||||
@@ -335,9 +305,9 @@ Page({
|
||||
// 如果没有上一页,跳转到第一个可用 tab,而不是硬编码的 index
|
||||
const tabbarItems = app.globalData.tabbarItems;
|
||||
if (tabbarItems && tabbarItems.length > 0) {
|
||||
wx.switchTab({ url: tabbarItems[0].pagePath });
|
||||
wx.switchTab({ url: tabbarItems[0].pagePath });
|
||||
} else {
|
||||
wx.switchTab({ url: "/pages/tab-bar/medicine-box/index" });
|
||||
wx.switchTab({ url: "/pages/tab-bar/medicine-box/index" });
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -376,65 +346,70 @@ Page({
|
||||
});
|
||||
if (!this.data.agree) return;
|
||||
}
|
||||
this.setData({
|
||||
showPopup: true
|
||||
})
|
||||
|
||||
},
|
||||
closePopup() {
|
||||
this.setData({
|
||||
showPopup: false
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
// 获取昵称(微信系统填入后失焦触发 bindblur)
|
||||
onNicknameInput(e: any) {
|
||||
const { value } = e.detail;
|
||||
this.setData({
|
||||
nickname: value
|
||||
});
|
||||
},
|
||||
// 提交登录
|
||||
async submitLogin() {
|
||||
const { avatarUrl, nickname } = this.data;
|
||||
if (!avatarUrl) {
|
||||
wx.showToast({
|
||||
title: '请选择头像',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!nickname) {
|
||||
wx.showToast({
|
||||
title: '请选择昵称',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
wx.showLoading({ title: "登录中" });
|
||||
const loginRes = await wxLogin();
|
||||
if (!loginRes.code) throw new Error(loginRes.errMsg);
|
||||
|
||||
const { avatarUrl, nickName } = this.data.userInfo;
|
||||
const state = wx.getStorageSync("state");
|
||||
let loginResult: LoginRes;
|
||||
|
||||
if (state) {
|
||||
const reqData: Record<string, any> = {
|
||||
state: wx.getStorageSync("state") ?? undefined,
|
||||
...(nickName && nickName !== "微信用户" ? { nickName } : {}),
|
||||
...(avatarUrl && avatarUrl !== defaultAvatarUrl ? { avatarUrl } : {}),
|
||||
};
|
||||
const reqData: Record<string, any> = {
|
||||
state: loginRes.code,
|
||||
avatarUrl,
|
||||
nickName: nickname
|
||||
};
|
||||
|
||||
loginResult = await wxRequest<LoginRes>({
|
||||
url: `${BASE_URL}/api/auth/app-login?appId=${appId}&code=${loginRes.code}`,
|
||||
method: "POST",
|
||||
data: reqData,
|
||||
});
|
||||
} else {
|
||||
const reqData: Record<string, any> = {
|
||||
state: wx.getStorageSync("code") ?? undefined,
|
||||
...(nickName && nickName !== "微信用户" ? { nickName } : {}),
|
||||
...(avatarUrl && avatarUrl !== defaultAvatarUrl ? { avatarUrl } : {}),
|
||||
};
|
||||
loginResult = await wxRequest<LoginRes>({
|
||||
url: `${BASE_URL}/api/auth/app-auth-new?appId=${appId}&code=${loginRes.code}`,
|
||||
method: "POST",
|
||||
data: reqData,
|
||||
});
|
||||
|
||||
loginResult = await wxRequest<LoginRes>({
|
||||
url: `${BASE_URL}/api/auth/app-auth?appId=${appId}&code=${loginRes.code}`,
|
||||
method: "POST",
|
||||
data: reqData,
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
loginResult?.statusCode !== 200 ||
|
||||
(loginResult.data.code && loginResult.data.code !== 200)
|
||||
) {
|
||||
if (loginResult?.statusCode !== 200) {
|
||||
throw new Error(loginResult?.data?.msg || "登录失败");
|
||||
}
|
||||
|
||||
const {
|
||||
mpUserId,
|
||||
id,
|
||||
openId,
|
||||
unionId,
|
||||
nickName: resNickName,
|
||||
avatarUrl: resAvatarUrl,
|
||||
} = loginResult.data;
|
||||
|
||||
const storageData = {
|
||||
openId,
|
||||
userId: mpUserId,
|
||||
mbuId: id,
|
||||
unionId,
|
||||
userName: resNickName,
|
||||
avatarUrl: resAvatarUrl,
|
||||
openId: loginResult.data.openId,
|
||||
userId: loginResult.data.mpUserId,
|
||||
userName: loginResult.data.nickName,
|
||||
avatarUrl: loginResult.data.avatarUrl,
|
||||
};
|
||||
|
||||
Object.entries(storageData).forEach(([key, value]) => {
|
||||
@@ -442,18 +417,15 @@ Page({
|
||||
});
|
||||
|
||||
app.globalData.isLogin = true;
|
||||
this.triggerEvent("loginSuccess", {
|
||||
userName: resNickName,
|
||||
avatarUrl: resAvatarUrl,
|
||||
});
|
||||
|
||||
wx.showToast({
|
||||
title: "登录成功",
|
||||
duration: 2000,
|
||||
});
|
||||
// 跳转到看课页面
|
||||
this.redirectUrl();
|
||||
|
||||
} catch (err: any) {
|
||||
console.log("登录失败:", err);
|
||||
app.globalData.isLogin = false;
|
||||
wx.showToast({
|
||||
title: `登录失败:${err.message || err.errMsg}`,
|
||||
@@ -462,8 +434,10 @@ Page({
|
||||
} finally {
|
||||
setTimeout(() => wx.hideToast(), 3000);
|
||||
}
|
||||
},
|
||||
|
||||
// 关闭弹窗
|
||||
this.closePopup();
|
||||
},
|
||||
handleToggleAgree() {
|
||||
this.setData({ agree: !this.data.agree });
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user