这版是公共号授权版

This commit is contained in:
cao123
2026-08-12 11:47:51 +08:00
parent d0776f9676
commit e47df01af9
18 changed files with 365 additions and 378 deletions
+44 -313
View File
@@ -3,261 +3,13 @@ import { LoginRes } from "./login.d";
import { wxLogin, wxRequest } from "../../utils/wx-api";
const app = getApp<IAppOption>();
const defaultAvatarUrl =
"https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";
type LoginDisplayConfig = {
showLoginImage: boolean;
};
type PartialLoginDisplayConfig = Partial<LoginDisplayConfig>;
const loginConfigKeys = [
"login",
"loginConfig",
"loginPage",
"auth",
"authConfig",
"authPage",
];
const WXROOM_REDIRECT_PATH_KEY = "wxroomRedirectPath";
function isPlainObject(value: unknown): value is Record<string, any> {
return Object.prototype.toString.call(value) === "[object Object]";
}
function hasOwnValue(source: Record<string, any>, keys: string[]) {
return keys.some((key) => source[key] !== undefined && source[key] !== null);
}
function normalizeString(value: unknown) {
return typeof value === "string" ? value.trim() : "";
}
function normalizeBoolean(value: unknown) {
if (typeof value === "boolean") return value;
if (typeof value === "number") return value > 0;
if (typeof value !== "string") return undefined;
const normalized = value.trim().toLowerCase();
if (["1", "true", "yes", "show", "visible", "enabled"].includes(normalized)) {
return true;
}
if (["0", "false", "no", "hide", "hidden", "disabled"].includes(normalized)) {
return false;
}
return undefined;
}
function normalizeSwitchValue(value: unknown) {
const normalized = normalizeBoolean(value);
if (normalized !== undefined) return normalized;
if (typeof value === "string") return value.trim().length > 0;
if (Array.isArray(value)) return value.length > 0;
if (isPlainObject(value)) {
const innerToggle = getBooleanValue(value, ["show", "visible", "enabled", "value"]);
if (innerToggle !== undefined) return innerToggle;
return !!getStringValue(value, [
"url",
"src",
"image",
"imageUrl",
"background",
"backgroundImage",
]);
}
return undefined;
}
function getObjectValue(source: Record<string, any>, keys: string[]) {
for (const key of keys) {
const value = source[key];
if (value !== undefined && value !== null && value !== "") {
return value;
}
}
return undefined;
}
function getStringValue(source: Record<string, any>, keys: string[]):string {
const value = getObjectValue(source, keys);
if (value === undefined) return "";
if (isPlainObject(value)) {
return getStringValue(value, [
"url",
"src",
"image",
"imageUrl",
"background",
"backgroundImage",
]);
}
return normalizeString(value);
}
function getBooleanValue(source: Record<string, any>, keys: string[]) {
const value = getObjectValue(source, keys);
return normalizeBoolean(value);
}
function pushConfigCandidate(list: Record<string, any>[], value: unknown) {
if (isPlainObject(value)) {
list.push(value);
}
}
function collectLoginConfigCandidates(template: unknown) {
const candidates: Record<string, any>[] = [];
if (!isPlainObject(template)) return candidates;
if (
hasOwnValue(template, [
"showLoginImage",
"showLoginBackground",
"showBackgroundImage",
"showImage",
"imageSwitch",
"backgroundSwitch",
"loginImage",
"heroImage",
"loginBg",
"loginBackground",
"backgroundImage",
"backgroundUrl",
"background",
"image",
])
) {
candidates.push(template);
}
loginConfigKeys.forEach((key) => {
pushConfigCandidate(candidates, template[key]);
pushConfigCandidate(candidates, template.pageConfig?.[key]);
pushConfigCandidate(candidates, template.pages?.[key]);
pushConfigCandidate(candidates, template.property?.[key]);
});
if (Array.isArray(template.pages?.login)) {
template.pages.login.forEach((item: any) => {
pushConfigCandidate(candidates, item);
pushConfigCandidate(candidates, item?.props);
});
}
Object.entries(template).forEach(([key, value]) => {
if (!/login|auth/i.test(key)) return;
if (Array.isArray(value)) {
value.forEach((item) => {
if (!isPlainObject(item)) return;
pushConfigCandidate(candidates, item);
pushConfigCandidate(candidates, item.props);
});
return;
}
pushConfigCandidate(candidates, value);
});
return candidates;
}
function readLoginImageSwitch(source: Record<string, any>) {
const imageValue = getObjectValue(source, [
"showLoginImage",
"showLoginBackground",
"showBackgroundImage",
"showImage",
"imageSwitch",
"backgroundSwitch",
"loginImage",
"heroImage",
"loginBackground",
"loginBg",
"backgroundImage",
"backgroundUrl",
"background",
"image",
"imageUrl",
]);
return normalizeSwitchValue(imageValue);
}
function resolveLoginDisplayConfigFromSource(source: unknown): PartialLoginDisplayConfig {
if (!isPlainObject(source)) return {};
const showLoginImage = readLoginImageSwitch(source);
return {
...(showLoginImage !== undefined ? { showLoginImage } : {}),
};
}
function mergeLoginDisplayConfig(
current: PartialLoginDisplayConfig,
next: PartialLoginDisplayConfig
) {
return {
...current,
...(next.showLoginImage !== undefined ? { showLoginImage: next.showLoginImage } : {}),
};
}
Page({
data: {
back: "",
agree: false,
nickFocus: true,
showPopup: false,
safeBottom: app.globalData.safeBottom,
showLoginImage: false,
userInfo: {
avatarUrl: defaultAvatarUrl,
nickName: "微信用户",
},
},
applyLoginDisplayConfig(options: Record<string, any> = {}) {
const template = app.globalData.template as Record<string, any> | null;
const config = collectLoginConfigCandidates(template).reduce(
(result, item) =>
mergeLoginDisplayConfig(result, resolveLoginDisplayConfigFromSource(item)),
{} as PartialLoginDisplayConfig
);
const optionConfig = resolveLoginDisplayConfigFromSource(options);
const merged = mergeLoginDisplayConfig(config, optionConfig);
this.setData({
showLoginImage: merged.showLoginImage ?? false,
});
},
async onChooseAvatar(e: any) {
const { avatarUrl } = e.detail;
wx.uploadFile({
url: `${BASE_URL}/api/operation/sys-media/upload-oss`,
filePath: avatarUrl,
name: "media",
success: (res: any) => {
const { data } = JSON.parse(res.data);
this.setData({
userInfo: {
...this.data.userInfo,
avatarUrl: data,
},
});
},
fail: () => {
wx.showToast({
title: "上传头像失败",
icon: "none",
});
},
});
},
buildQueryString(params: Record<string, any>): string {
@@ -288,56 +40,55 @@ 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;
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`;
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) {
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?code=${code}`;
}
} else {
fullPath = `/pages/live/registration/registration?state=${state}`;
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 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?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();
const currentPage = pages[pages.length - 2];
if (!currentPage) {
// 如果没有上一页,跳转到第一个可用 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;
}
@@ -382,33 +133,24 @@ Page({
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 } : {}),
};
loginResult = await wxRequest<LoginRes>({
url: `${BASE_URL}/api/auth/app-login?appId=${appId}&code=${loginRes.code}`,
method: "POST",
data: reqData,
data: {
state: wx.getStorageSync("state") ?? undefined,
},
});
} 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?appId=${appId}&code=${loginRes.code}`,
method: "POST",
data: reqData,
data: {
state: wx.getStorageSync("code") ?? undefined,
},
});
}
@@ -442,10 +184,6 @@ Page({
});
app.globalData.isLogin = true;
this.triggerEvent("loginSuccess", {
userName: resNickName,
avatarUrl: resAvatarUrl,
});
wx.showToast({
title: "登录成功",
duration: 2000,
@@ -477,12 +215,5 @@ Page({
if (options.back) {
this.setData({ back: options.back });
}
this.applyLoginDisplayConfig(options);
},
onShow() {
setTimeout(() => {
this.setData({ nickFocus: true });
}, 200);
},
});
});