修复:收口小程序键盘遮挡适配

This commit is contained in:
xiaowang
2026-04-22 16:53:32 +08:00
parent 1523e9d8e5
commit d535994614
12 changed files with 897 additions and 168 deletions
+47 -2
View File
@@ -5,11 +5,21 @@ const authStore = require('../../utils/auth')
const actionGuard = require('../../utils/action-guard')
const navigation = require('../../utils/navigation')
const { runWithLoading } = require('../../utils/loading')
const {
createKeyboardSafeData,
attachKeyboardSafe,
detachKeyboardSafe,
handleKeyboardFocus,
handleKeyboardBlur,
resetKeyboardSafe
} = require('../../utils/keyboard-safe')
const phoneRegex = /^1[3-9]\d{9}$/
Page({
data: {
data: Object.assign(createKeyboardSafeData({
cursorSpacing: 108
}), {
redirect: '',
activeTab: 'login',
loginType: 'password',
@@ -59,9 +69,10 @@ Page({
verificationCode: '',
newPassword: ''
}
},
}),
onLoad(options) {
this.ensureKeyboardSafe()
this.setData({
redirect: options && options.redirect ? options.redirect : '',
authHelperText: this.buildAuthHelperText('login', 'password')
@@ -73,11 +84,37 @@ Page({
},
onShow() {
this.ensureKeyboardSafe()
this.consumePendingWechatMiniLogin()
},
onHide() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
},
onUnload() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
},
noop() {},
ensureKeyboardSafe() {
attachKeyboardSafe(this, {
cursorSpacing: 108,
spacerExtraRpx: 56
})
},
handleKeyboardFocus(event) {
handleKeyboardFocus(this, event)
},
handleKeyboardBlur() {
handleKeyboardBlur(this)
},
detectWechatMiniCapability() {
let supported = false
let disabledReason = ''
@@ -158,6 +195,7 @@ Page({
handleTabChange(event) {
const tab = event.currentTarget.dataset.tab
resetKeyboardSafe(this)
this.setData({
activeTab: tab,
authHelperText: this.buildAuthHelperText(tab, this.data.loginType),
@@ -174,6 +212,7 @@ Page({
handleLoginTypeChange(event) {
const type = event.currentTarget.dataset.type
resetKeyboardSafe(this)
this.setData({
loginType: type,
authHelperText: this.buildAuthHelperText(this.data.activeTab, type),
@@ -189,6 +228,7 @@ Page({
},
handleShowTraditionalLogin() {
resetKeyboardSafe(this)
this.setData({
traditionalLoginExpanded: true,
showResetPanel: false
@@ -196,6 +236,7 @@ Page({
},
handleToggleResetPanel() {
resetKeyboardSafe(this)
this.setData({
showResetPanel: !this.data.showResetPanel
})
@@ -299,6 +340,7 @@ Page({
},
async handleAccountBlur() {
this.handleKeyboardBlur()
const account = this.data.form.account.trim()
if (!account) {
return
@@ -320,6 +362,7 @@ Page({
},
async handlePhoneBlur() {
this.handleKeyboardBlur()
const phone = this.data.form.phone.trim()
if (!phone) {
return
@@ -849,6 +892,7 @@ Page({
},
openMiniShopSelector(result) {
resetKeyboardSafe(this)
const shops = Array.isArray(result && result.shops) ? result.shops : []
if (!shops.length) {
wx.showToast({
@@ -887,6 +931,7 @@ Page({
},
async finishLogin(payload, successMessage) {
resetKeyboardSafe(this)
getApp().setUserSession(payload)
this.clearMiniShopSelector()
this.setData({
+276 -134
View File
@@ -1,158 +1,300 @@
<navigation-bar title="账号中心" back="{{false}}" color="#2d2430" background="#fff7f8"></navigation-bar>
<view class="auth-page-root">
<navigation-bar title="账号中心" back="{{false}}" color="#2d2430" background="#fff7f8"></navigation-bar>
<view class="page-shell auth-page">
<view class="page-content auth-content">
<view class="page-card auth-panel">
<view class="tab-row">
<view class="tab-item {{activeTab === 'login' ? 'active' : ''}}" data-tab="login" bindtap="handleTabChange">登录</view>
<view class="tab-item {{activeTab === 'register' ? 'active' : ''}}" data-tab="register" bindtap="handleTabChange">注册</view>
</view>
<block wx:if="{{activeTab === 'login'}}">
<view class="wechat-login-panel">
<view class="wechat-login-copy">
<text class="wechat-login-title">微信手机号一键登录</text>
<text class="wechat-login-desc">更快登录</text>
<scroll-view
class="auth-scroll"
scroll-y
enable-flex
enhanced
show-scrollbar="false"
scroll-with-animation="{{true}}"
scroll-into-view="{{keyboardAnchorId}}"
>
<view class="page-shell auth-page-shell auth-page">
<view class="page-content auth-content {{keyboardVisible ? 'keyboard-active' : ''}}">
<view class="page-card auth-panel">
<view class="tab-row">
<view class="tab-item {{activeTab === 'login' ? 'active' : ''}}" data-tab="login" bindtap="handleTabChange">登录</view>
<view class="tab-item {{activeTab === 'register' ? 'active' : ''}}" data-tab="register" bindtap="handleTabChange">注册</view>
</view>
<button
wx:if="{{wechatMiniSupported}}"
class="primary-btn wechat-login-btn"
open-type="getPhoneNumber"
bindgetphonenumber="handleWechatMiniPhoneLogin"
loading="{{wechatMiniLoading}}"
disabled="{{wechatMiniLoading || loading}}"
>
{{wechatMiniLoading ? '正在获取微信手机号...' : '微信手机号一键登录'}}
</button>
<block wx:if="{{activeTab === 'login'}}">
<view class="wechat-login-panel">
<view class="wechat-login-copy">
<text class="wechat-login-title">微信手机号一键登录</text>
<text class="wechat-login-desc">更快登录</text>
</view>
<button
wx:else
class="secondary-btn wechat-login-btn wechat-login-btn-disabled"
bindtap="handleWechatMiniUnavailableTap"
>
微信手机号一键登录
</button>
<button
wx:if="{{wechatMiniSupported}}"
class="primary-btn wechat-login-btn"
open-type="getPhoneNumber"
bindgetphonenumber="handleWechatMiniPhoneLogin"
loading="{{wechatMiniLoading}}"
disabled="{{wechatMiniLoading || loading}}"
>
{{wechatMiniLoading ? '正在获取微信手机号...' : '微信手机号一键登录'}}
</button>
<text class="wechat-login-hint">{{wechatMiniSupported ? '单门店可直接登录' : wechatMiniDisabledReason}}</text>
</view>
<button
wx:else
class="secondary-btn wechat-login-btn wechat-login-btn-disabled"
bindtap="handleWechatMiniUnavailableTap"
>
微信手机号一键登录
</button>
<view wx:if="{{miniShopTicket && !showMiniShopSelector}}" class="hint-card mini-login-resume">
<text class="mini-login-resume-text">请选择登录门店</text>
<view class="ghost-btn mini-login-resume-btn" bindtap="handleResumeMiniShopSelector">继续选择门店</view>
</view>
<text class="wechat-login-hint">{{wechatMiniSupported ? '单门店可直接登录' : wechatMiniDisabledReason}}</text>
</view>
<view wx:if="{{!traditionalLoginExpanded}}" class="traditional-entry">
<view class="secondary-btn traditional-entry-btn" bindtap="handleShowTraditionalLogin">其他登录方式</view>
</view>
<view wx:if="{{miniShopTicket && !showMiniShopSelector}}" class="hint-card mini-login-resume">
<text class="mini-login-resume-text">请选择登录门店</text>
<view class="ghost-btn mini-login-resume-btn" bindtap="handleResumeMiniShopSelector">继续选择门店</view>
</view>
<view wx:if="{{traditionalLoginExpanded}}" class="traditional-login-panel">
<view class="login-divider">
<view class="divider-line"></view>
<text class="login-divider-text">其他方式</text>
<view class="divider-line"></view>
<view wx:if="{{!traditionalLoginExpanded}}" class="traditional-entry">
<view class="secondary-btn traditional-entry-btn" bindtap="handleShowTraditionalLogin">其他登录方式</view>
</view>
<view wx:if="{{traditionalLoginExpanded}}" class="traditional-login-panel">
<view class="login-divider">
<view class="divider-line"></view>
<text class="login-divider-text">其他方式</text>
<view class="divider-line"></view>
</view>
<view class="toggle-row">
<view class="toggle-item {{loginType === 'password' ? 'active' : ''}}" data-type="password" bindtap="handleLoginTypeChange">账号密码登录</view>
<view class="toggle-item {{loginType === 'phone' ? 'active' : ''}}" data-type="phone" bindtap="handleLoginTypeChange">验证码登录</view>
</view>
</view>
</block>
<view wx:if="{{activeTab === 'register' || traditionalLoginExpanded}}" class="auth-helper">{{authHelperText}}</view>
<block wx:if="{{activeTab === 'register'}}">
<view id="auth-register-nickname-anchor" class="field-block">
<text class="field-label">昵称</text>
<input
class="field-input"
placeholder="请输入昵称"
placeholder-class="field-placeholder"
value="{{form.nickname}}"
data-field="nickname"
data-keyboard-anchor="auth-register-nickname-anchor"
bindinput="handleInputChange"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
</block>
<view id="auth-account-anchor" wx:if="{{activeTab === 'register' || traditionalLoginExpanded}}" class="field-block">
<text class="field-label">账号</text>
<input
class="field-input"
placeholder="请输入账号"
placeholder-class="field-placeholder"
value="{{form.account}}"
data-field="account"
data-keyboard-anchor="auth-account-anchor"
bindinput="handleInputChange"
bindblur="handleAccountBlur"
bindfocus="handleKeyboardFocus"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
<text wx:if="{{activeTab === 'login' && loginAccountStatusText}}" class="status-text {{loginAccountStatusType}}">{{loginAccountStatusText}}</text>
<text wx:if="{{activeTab === 'register' && registerAccountStatusText}}" class="status-text {{registerAccountStatusType}}">{{registerAccountStatusText}}</text>
</view>
<view class="toggle-row">
<view class="toggle-item {{loginType === 'password' ? 'active' : ''}}" data-type="password" bindtap="handleLoginTypeChange">账号密码登录</view>
<view class="toggle-item {{loginType === 'phone' ? 'active' : ''}}" data-type="phone" bindtap="handleLoginTypeChange">验证码登录</view>
<view id="auth-phone-anchor" wx:if="{{activeTab === 'register' || (traditionalLoginExpanded && loginType === 'phone')}}" class="field-block">
<text class="field-label">手机号</text>
<input
class="field-input"
type="number"
placeholder="请输入手机号"
placeholder-class="field-placeholder"
value="{{form.phone}}"
data-field="phone"
data-keyboard-anchor="auth-phone-anchor"
bindinput="handleInputChange"
bindblur="handlePhoneBlur"
bindfocus="handleKeyboardFocus"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
<text wx:if="{{activeTab === 'register' && registerPhoneStatusText}}" class="status-text {{registerPhoneStatusType}}">{{registerPhoneStatusText}}</text>
</view>
<view id="auth-login-password-anchor" wx:if="{{activeTab === 'login' && traditionalLoginExpanded && loginType === 'password'}}" class="field-block">
<text class="field-label">密码</text>
<input
class="field-input"
password
placeholder="请输入密码"
placeholder-class="field-placeholder"
value="{{form.password}}"
data-field="password"
data-keyboard-anchor="auth-login-password-anchor"
bindinput="handleInputChange"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
<view id="auth-code-anchor" wx:if="{{activeTab === 'register' || (traditionalLoginExpanded && loginType === 'phone')}}" class="field-block">
<text class="field-label">验证码</text>
<view class="code-row">
<input
class="field-input code-input"
type="number"
placeholder="请输入验证码"
placeholder-class="field-placeholder"
value="{{form.verificationCode}}"
data-field="verificationCode"
data-keyboard-anchor="auth-code-anchor"
bindinput="handleInputChange"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
<view class="code-btn {{countdown > 0 ? 'disabled' : ''}}" bindtap="handleSendCode">{{countdown > 0 ? countdown + 's' : '发送验证码'}}</view>
</view>
</view>
<view id="auth-register-password-anchor" wx:if="{{activeTab === 'register'}}" class="field-block">
<text class="field-label">密码</text>
<input
class="field-input"
password
placeholder="请输入密码"
placeholder-class="field-placeholder"
value="{{form.password}}"
data-field="password"
data-keyboard-anchor="auth-register-password-anchor"
bindinput="handleInputChange"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
<view id="auth-register-confirm-password-anchor" wx:if="{{activeTab === 'register'}}" class="field-block">
<text class="field-label">确认密码</text>
<input
class="field-input"
password
placeholder="请再次输入密码"
placeholder-class="field-placeholder"
value="{{form.confirmPassword}}"
data-field="confirmPassword"
data-keyboard-anchor="auth-register-confirm-password-anchor"
bindinput="handleInputChange"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
<view wx:if="{{activeTab === 'register'}}" class="field-block">
<text class="field-label">选择门店</text>
<picker mode="selector" range="{{shops}}" range-key="shopName" value="{{registerShopIndex}}" bindchange="handleRegisterShopChange">
<view class="field-picker">{{registerShopDisplayName || '请选择门店'}}</view>
</picker>
</view>
<view wx:if="{{activeTab === 'login' && traditionalLoginExpanded && !allowLoginShopless && loginShops.length}}" class="field-block">
<text class="field-label">选择登录门店</text>
<picker mode="selector" range="{{loginShops}}" range-key="shopName" value="{{loginShopIndex}}" bindchange="handleLoginShopChange">
<view class="field-picker">{{loginShopDisplayName || '请选择登录门店'}}</view>
</picker>
</view>
<view wx:if="{{activeTab === 'login' && traditionalLoginExpanded && loginShopMessage}}" class="hint-card login-shop-tip">{{loginShopMessage}}</view>
<view wx:if="{{activeTab === 'login' && traditionalLoginExpanded}}" class="aux-row">
<text class="aux-link" bindtap="handleToggleResetPanel">忘记密码?</text>
</view>
<view wx:if="{{activeTab === 'register' || traditionalLoginExpanded}}" class="primary-btn submit-btn" bindtap="handleSubmit">{{activeTab === 'login' ? '立即登录' : '完成注册'}}</view>
</view>
</block>
<view wx:if="{{activeTab === 'register' || traditionalLoginExpanded}}" class="auth-helper">{{authHelperText}}</view>
<view wx:if="{{showResetPanel}}" class="page-card reset-card">
<view class="section-title">重置密码</view>
<view class="section-subtitle">短信验证后重置密码</view>
<block wx:if="{{activeTab === 'register'}}">
<view class="field-block">
<text class="field-label">昵称</text>
<input class="field-input" placeholder="请输入昵称" placeholder-class="field-placeholder" value="{{form.nickname}}" data-field="nickname" bindinput="handleInputChange"></input>
</view>
</block>
<view id="auth-reset-phone-anchor" class="field-block">
<text class="field-label">手机号</text>
<input
class="field-input"
type="number"
placeholder="请输入绑定手机号"
placeholder-class="field-placeholder"
value="{{resetForm.phone}}"
data-field="phone"
data-keyboard-anchor="auth-reset-phone-anchor"
bindinput="handleResetInputChange"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
<view wx:if="{{activeTab === 'register' || traditionalLoginExpanded}}" class="field-block">
<text class="field-label">账号</text>
<input class="field-input" placeholder="请输入账号" placeholder-class="field-placeholder" value="{{form.account}}" data-field="account" bindinput="handleInputChange" bindblur="handleAccountBlur"></input>
<text wx:if="{{activeTab === 'login' && loginAccountStatusText}}" class="status-text {{loginAccountStatusType}}">{{loginAccountStatusText}}</text>
<text wx:if="{{activeTab === 'register' && registerAccountStatusText}}" class="status-text {{registerAccountStatusType}}">{{registerAccountStatusText}}</text>
</view>
<view id="auth-reset-code-anchor" class="field-block">
<text class="field-label">验证码</text>
<view class="code-row">
<input
class="field-input code-input"
type="number"
placeholder="请输入验证码"
placeholder-class="field-placeholder"
value="{{resetForm.verificationCode}}"
data-field="verificationCode"
data-keyboard-anchor="auth-reset-code-anchor"
bindinput="handleResetInputChange"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
<view class="code-btn {{forgotCountdown > 0 ? 'disabled' : ''}}" bindtap="handleSendForgotCode">{{forgotCountdown > 0 ? forgotCountdown + 's' : '发送验证码'}}</view>
</view>
</view>
<view wx:if="{{activeTab === 'register' || (traditionalLoginExpanded && loginType === 'phone')}}" class="field-block">
<text class="field-label">手机号</text>
<input class="field-input" type="number" placeholder="请输入手机号" placeholder-class="field-placeholder" value="{{form.phone}}" data-field="phone" bindinput="handleInputChange" bindblur="handlePhoneBlur"></input>
<text wx:if="{{activeTab === 'register' && registerPhoneStatusText}}" class="status-text {{registerPhoneStatusType}}">{{registerPhoneStatusText}}</text>
</view>
<view id="auth-reset-password-anchor" class="field-block">
<text class="field-label">新密码</text>
<input
class="field-input"
password
placeholder="请输入新密码"
placeholder-class="field-placeholder"
value="{{resetForm.newPassword}}"
data-field="newPassword"
data-keyboard-anchor="auth-reset-password-anchor"
bindinput="handleResetInputChange"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
<view wx:if="{{activeTab === 'login' && traditionalLoginExpanded && loginType === 'password'}}" class="field-block">
<text class="field-label">密码</text>
<input class="field-input" password placeholder="请输入密码" placeholder-class="field-placeholder" value="{{form.password}}" data-field="password" bindinput="handleInputChange"></input>
</view>
<view wx:if="{{activeTab === 'register' || (traditionalLoginExpanded && loginType === 'phone')}}" class="field-block">
<text class="field-label">验证码</text>
<view class="code-row">
<input class="field-input code-input" type="number" placeholder="请输入验证码" placeholder-class="field-placeholder" value="{{form.verificationCode}}" data-field="verificationCode" bindinput="handleInputChange"></input>
<view class="code-btn {{countdown > 0 ? 'disabled' : ''}}" bindtap="handleSendCode">{{countdown > 0 ? countdown + 's' : '发送验证码'}}</view>
<view class="primary-btn submit-btn" bindtap="handleResetPassword">确认重置密码</view>
</view>
</view>
<view wx:if="{{activeTab === 'register'}}" class="field-block">
<text class="field-label">密码</text>
<input class="field-input" password placeholder="请输入密码" placeholder-class="field-placeholder" value="{{form.password}}" data-field="password" bindinput="handleInputChange"></input>
</view>
<view wx:if="{{activeTab === 'register'}}" class="field-block">
<text class="field-label">确认密码</text>
<input class="field-input" password placeholder="请再次输入密码" placeholder-class="field-placeholder" value="{{form.confirmPassword}}" data-field="confirmPassword" bindinput="handleInputChange"></input>
</view>
<view wx:if="{{activeTab === 'register'}}" class="field-block">
<text class="field-label">选择门店</text>
<picker mode="selector" range="{{shops}}" range-key="shopName" value="{{registerShopIndex}}" bindchange="handleRegisterShopChange">
<view class="field-picker">{{registerShopDisplayName || '请选择门店'}}</view>
</picker>
</view>
<view wx:if="{{activeTab === 'login' && traditionalLoginExpanded && !allowLoginShopless && loginShops.length}}" class="field-block">
<text class="field-label">选择登录门店</text>
<picker mode="selector" range="{{loginShops}}" range-key="shopName" value="{{loginShopIndex}}" bindchange="handleLoginShopChange">
<view class="field-picker">{{loginShopDisplayName || '请选择登录门店'}}</view>
</picker>
</view>
<view wx:if="{{activeTab === 'login' && traditionalLoginExpanded && loginShopMessage}}" class="hint-card login-shop-tip">{{loginShopMessage}}</view>
<view wx:if="{{activeTab === 'login' && traditionalLoginExpanded}}" class="aux-row">
<text class="aux-link" bindtap="handleToggleResetPanel">忘记密码?</text>
</view>
<view wx:if="{{activeTab === 'register' || traditionalLoginExpanded}}" class="primary-btn submit-btn" bindtap="handleSubmit">{{activeTab === 'login' ? '立即登录' : '完成注册'}}</view>
<view wx:if="{{keyboardVisible}}" class="keyboard-spacer" style="height: {{keyboardInsetRpx}}rpx;"></view>
</view>
<view wx:if="{{showResetPanel}}" class="page-card reset-card">
<view class="section-title">重置密码</view>
<view class="section-subtitle">短信验证后重置密码</view>
<view class="field-block">
<text class="field-label">手机号</text>
<input class="field-input" type="number" placeholder="请输入绑定手机号" placeholder-class="field-placeholder" value="{{resetForm.phone}}" data-field="phone" bindinput="handleResetInputChange"></input>
</view>
<view class="field-block">
<text class="field-label">验证码</text>
<view class="code-row">
<input class="field-input code-input" type="number" placeholder="请输入验证码" placeholder-class="field-placeholder" value="{{resetForm.verificationCode}}" data-field="verificationCode" bindinput="handleResetInputChange"></input>
<view class="code-btn {{forgotCountdown > 0 ? 'disabled' : ''}}" bindtap="handleSendForgotCode">{{forgotCountdown > 0 ? forgotCountdown + 's' : '发送验证码'}}</view>
</view>
</view>
<view class="field-block">
<text class="field-label">新密码</text>
<input class="field-input" password placeholder="请输入新密码" placeholder-class="field-placeholder" value="{{resetForm.newPassword}}" data-field="newPassword" bindinput="handleResetInputChange"></input>
</view>
<view class="primary-btn submit-btn" bindtap="handleResetPassword">确认重置密码</view>
</view>
</view>
</scroll-view>
<view wx:if="{{showMiniShopSelector}}" class="mini-shop-mask" catchtouchmove="noop">
<view class="page-card mini-shop-modal">
+27 -1
View File
@@ -1,3 +1,19 @@
.auth-page-root {
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
.auth-scroll {
flex: 1;
min-height: 0;
}
.auth-page-shell {
min-height: 100%;
}
.auth-page {
background:
radial-gradient(circle at top right, rgba(255, 204, 219, 0.32), transparent 30%),
@@ -5,7 +21,7 @@
}
.auth-content {
min-height: calc(100vh - 120rpx);
min-height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
@@ -13,6 +29,11 @@
padding: 24rpx 24rpx 40rpx;
}
.auth-content.keyboard-active {
justify-content: flex-start;
align-items: stretch;
}
.auth-panel,
.reset-card {
width: 100%;
@@ -282,3 +303,8 @@
.mini-shop-action {
flex: 1;
}
.keyboard-spacer {
width: 100%;
flex-shrink: 0;
}