fix: refine mini program nickname collection flow
This commit is contained in:
@@ -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: '',
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
<view wx:elif="{{nicknameRequired}}" class="setup-screen">
|
||||
<view class="setup-card">
|
||||
<view class="setup-title">登录</view>
|
||||
<view class="setup-title">完善昵称</view>
|
||||
<view wx:if="{{errorMessage}}" class="setup-text setup-text--danger">
|
||||
{{errorMessage}}
|
||||
</view>
|
||||
@@ -29,7 +29,7 @@
|
||||
disabled="{{loggingIn}}"
|
||||
bindtap="handleStartLogin"
|
||||
>
|
||||
{{loggingIn ? '正在登录' : '微信快捷登录'}}
|
||||
{{loggingIn ? '正在提交' : '确认昵称'}}
|
||||
</button>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
Reference in New Issue
Block a user