fix: refine mini program nickname collection flow

This commit is contained in:
kdletters
2026-06-07 00:42:05 +08:00
parent 79d0d7a305
commit e3ecac85f3
8 changed files with 109 additions and 10 deletions

View File

@@ -111,6 +111,58 @@ function normalizeNicknameInput(value) {
return String(value || '').trim();
}
function normalizeNicknameForMatch(value) {
return normalizeNicknameInput(value).replace(/\s+/gu, '').toLowerCase();
}
function isPhoneLikeDisplayName(value) {
const normalized = normalizeNicknameForMatch(value);
if (!normalized) {
return false;
}
const digits = normalized.replace(/\D/gu, '');
return (
/^(\+?86)?1\d{10}$/u.test(normalized) ||
/^1\d{2}\*{4}\d{4}$/u.test(normalized) ||
(/[*x]/iu.test(normalized) && digits.length >= 7) ||
digits.length >= 11
);
}
function isDefaultDisplayName(value, publicUserCode) {
const normalized = normalizeNicknameForMatch(value);
const normalizedPublicUserCode = normalizeNicknameForMatch(publicUserCode);
if (!normalized) {
return true;
}
return (
normalized === '微信旅人' ||
normalized === '玩家' ||
normalized === normalizedPublicUserCode ||
/^sy-\d{8}$/iu.test(normalized) ||
/^user[_-]/iu.test(normalized) ||
isPhoneLikeDisplayName(normalized)
);
}
function shouldRequestNicknameAfterLogin(authResult) {
const user = authResult && authResult.user ? authResult.user : {};
const wechatDisplayName = normalizeNicknameInput(user.wechatDisplayName);
if (wechatDisplayName && !isDefaultDisplayName(wechatDisplayName, user.publicUserCode)) {
return false;
}
return (
authResult &&
(authResult.created ||
isDefaultDisplayName(user.displayName, user.publicUserCode) ||
(wechatDisplayName &&
isDefaultDisplayName(wechatDisplayName, user.publicUserCode)))
);
}
function normalizeMiniProgramEnv(value) {
const normalized = String(value || '').trim().toLowerCase();
if (normalized === 'release') {
@@ -372,6 +424,8 @@ async function resolveAuthResult(displayName) {
return {
token: response.token,
bindingStatus: response.bindingStatus || 'pending_bind_phone',
user: response.user || null,
created: response.created === true,
};
}
@@ -431,13 +485,14 @@ Page({
authResult: null,
bindingPhone: false,
errorMessage: '',
loggingIn: false,
loading: false,
nicknameRequired: true,
loggingIn: true,
loading: true,
nicknameRequired: false,
phoneBindingRequired: false,
returnToPreviousPage,
webViewUrl: '',
});
await this.startAuthFlow(returnToPreviousPage, '');
},
handleNicknameInput(event) {
@@ -465,6 +520,20 @@ Page({
async startAuthFlow(returnToPreviousPage, displayName) {
try {
const authResult = await resolveAuthResult(displayName);
if (!displayName && shouldRequestNicknameAfterLogin(authResult)) {
this.setData({
authResult,
errorMessage: '',
loggingIn: false,
loading: false,
nicknameRequired: true,
phoneBindingRequired: false,
returnToPreviousPage,
webViewUrl: '',
});
return;
}
if (authResult.bindingStatus === 'pending_bind_phone') {
this.setData({
authResult,
@@ -512,7 +581,7 @@ Page({
error && error.message ? error.message : '微信登录失败,请稍后重试。',
loggingIn: false,
loading: false,
nicknameRequired: true,
nicknameRequired: false,
phoneBindingRequired: false,
returnToPreviousPage,
webViewUrl: '',