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

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
+37 -1
View File
@@ -5,6 +5,14 @@ const navigation = require('../../utils/navigation')
const { DEFAULT_AVATAR } = require('../../utils/config')
const { getDraftValue, clearDraft, scheduleDraftSave, clearDraftTimer } = require('../../utils/form-draft')
const { runWithLoading } = require('../../utils/loading')
const {
createKeyboardSafeData,
attachKeyboardSafe,
detachKeyboardSafe,
handleKeyboardFocus,
handleKeyboardBlur,
resetKeyboardSafe
} = require('../../utils/keyboard-safe')
const phoneRegex = /^1[3-9]\d{9}$/
const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*#?&]{8,}$/
@@ -41,7 +49,9 @@ const placeholderEntries = [
]
Page({
data: {
data: Object.assign(createKeyboardSafeData({
cursorSpacing: 108
}), {
isLoggedIn: false,
loading: false,
savingProfile: false,
@@ -72,22 +82,46 @@ Page({
password: '',
confirmPassword: ''
}
}),
onLoad() {
this.ensureKeyboardSafe()
},
onShow() {
this.ensureKeyboardSafe()
this.loadSettings()
},
onHide() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
this.clearCountdown()
clearDraftTimer(this)
},
onUnload() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
this.clearCountdown()
clearDraftTimer(this)
},
ensureKeyboardSafe() {
attachKeyboardSafe(this, {
cursorSpacing: 108,
spacerExtraRpx: 48
})
},
handleKeyboardFocus(event) {
handleKeyboardFocus(this, event)
},
handleKeyboardBlur() {
handleKeyboardBlur(this)
},
clearCountdown() {
if (this.countdownTimer) {
clearInterval(this.countdownTimer)
@@ -252,6 +286,7 @@ Page({
handleToggleSecurityPanel(event) {
const panel = event.currentTarget.dataset.panel
const nextPanel = this.data.activeSecurityPanel === panel ? '' : panel
resetKeyboardSafe(this)
this.clearCountdown()
this.resetSecurityForms()
this.setData({
@@ -262,6 +297,7 @@ Page({
},
handleCloseSecurityPanel() {
resetKeyboardSafe(this)
this.resetSecurityForms()
this.clearCountdown()
this.setData({
+145 -20
View File
@@ -1,8 +1,16 @@
<view class="settings-page-root">
<navigation-bar title="设置中心" color="#2d2430" background="#fff7f8" bindback="handleBack"></navigation-bar>
<scroll-view class="settings-scroll" scroll-y enable-flex enhanced show-scrollbar="false">
<view class="page-shell settings-page-shell">
<scroll-view
class="settings-scroll"
scroll-y
enable-flex
enhanced
show-scrollbar="false"
scroll-with-animation="{{true}}"
scroll-into-view="{{keyboardAnchorId}}"
>
<view class="page-shell settings-page-shell {{keyboardVisible ? 'keyboard-active' : ''}}">
<view class="page-content">
<view wx:if="{{!isLoggedIn}}" class="page-card empty-card">
<text class="empty-title">登录后进入设置中心</text>
@@ -37,14 +45,38 @@
当前账号未设置登录密码。
</view>
<view class="field-block">
<view id="settings-profile-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" bindinput="handleFormInput"></input>
<input
class="field-input"
placeholder="请输入昵称"
placeholder-class="field-placeholder"
value="{{form.nickname}}"
data-field="nickname"
data-keyboard-anchor="settings-profile-nickname-anchor"
bindinput="handleFormInput"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
<view class="field-block">
<view id="settings-profile-signature-anchor" class="field-block">
<text class="field-label">个性签名</text>
<textarea class="field-textarea" placeholder="请输入个性签名" placeholder-class="field-placeholder" value="{{form.signature}}" data-field="signature" bindinput="handleFormInput"></textarea>
<textarea
class="field-textarea"
placeholder="请输入个性签名"
placeholder-class="field-placeholder"
value="{{form.signature}}"
data-field="signature"
data-keyboard-anchor="settings-profile-signature-anchor"
bindinput="handleFormInput"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></textarea>
</view>
<view class="primary-btn save-btn" bindtap="handleSaveProfile">{{savingProfile ? '保存中...' : '保存资料'}}</view>
@@ -87,14 +119,40 @@
<view wx:if="{{activeSecurityPanel === 'bindPhone'}}" class="security-panel">
<view class="section-subtitle">输入后完成绑定</view>
<view class="field-block">
<view id="settings-bind-phone-anchor" class="field-block">
<text class="field-label">手机号</text>
<input class="field-input" type="number" placeholder="请输入手机号" placeholder-class="field-placeholder" value="{{bindPhoneForm.phone}}" data-field="phone" bindinput="handleBindPhoneInput"></input>
<input
class="field-input"
type="number"
placeholder="请输入手机号"
placeholder-class="field-placeholder"
value="{{bindPhoneForm.phone}}"
data-field="phone"
data-keyboard-anchor="settings-bind-phone-anchor"
bindinput="handleBindPhoneInput"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
<view class="field-block">
<view id="settings-bind-phone-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="{{bindPhoneForm.verificationCode}}" data-field="verificationCode" bindinput="handleBindPhoneInput"></input>
<input
class="field-input code-input"
type="number"
placeholder="请输入验证码"
placeholder-class="field-placeholder"
value="{{bindPhoneForm.verificationCode}}"
data-field="verificationCode"
data-keyboard-anchor="settings-bind-phone-code-anchor"
bindinput="handleBindPhoneInput"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
<view class="code-btn {{countdown > 0 ? 'disabled' : ''}}" bindtap="handleSendBindPhoneCode">{{countdown > 0 ? countdown + 's' : '发送验证码'}}</view>
</view>
</view>
@@ -110,14 +168,40 @@
<text class="field-label">当前手机号</text>
<view class="field-static">{{userInfo.phone}}</view>
</view>
<view class="field-block">
<view id="settings-change-phone-anchor" class="field-block">
<text class="field-label">新手机号</text>
<input class="field-input" type="number" placeholder="请输入新的手机号" placeholder-class="field-placeholder" value="{{changePhoneForm.newPhone}}" data-field="newPhone" bindinput="handleChangePhoneInput"></input>
<input
class="field-input"
type="number"
placeholder="请输入新的手机号"
placeholder-class="field-placeholder"
value="{{changePhoneForm.newPhone}}"
data-field="newPhone"
data-keyboard-anchor="settings-change-phone-anchor"
bindinput="handleChangePhoneInput"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
<view class="field-block">
<view id="settings-change-phone-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="{{changePhoneForm.verificationCode}}" data-field="verificationCode" bindinput="handleChangePhoneInput"></input>
<input
class="field-input code-input"
type="number"
placeholder="请输入验证码"
placeholder-class="field-placeholder"
value="{{changePhoneForm.verificationCode}}"
data-field="verificationCode"
data-keyboard-anchor="settings-change-phone-code-anchor"
bindinput="handleChangePhoneInput"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
<view class="code-btn {{countdown > 0 ? 'disabled' : ''}}" bindtap="handleSendChangePhoneCode">{{countdown > 0 ? countdown + 's' : '发送验证码'}}</view>
</view>
</view>
@@ -131,20 +215,59 @@
<view wx:if="{{!userInfo.phone}}" class="hint-card password-hint">请先绑定手机号,再通过验证码设置或修改登录密码。</view>
<block wx:else>
<view class="section-subtitle">密码需包含字母和数字</view>
<view class="field-block">
<view id="settings-password-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="{{passwordForm.verificationCode}}" data-field="verificationCode" bindinput="handlePasswordInput"></input>
<input
class="field-input code-input"
type="number"
placeholder="请输入验证码"
placeholder-class="field-placeholder"
value="{{passwordForm.verificationCode}}"
data-field="verificationCode"
data-keyboard-anchor="settings-password-code-anchor"
bindinput="handlePasswordInput"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
<view class="code-btn {{countdown > 0 ? 'disabled' : ''}}" bindtap="handleSendPasswordCode">{{countdown > 0 ? countdown + 's' : '发送验证码'}}</view>
</view>
</view>
<view class="field-block">
<view id="settings-password-anchor" class="field-block">
<text class="field-label">新密码</text>
<input class="field-input" password placeholder="请输入新密码" placeholder-class="field-placeholder" value="{{passwordForm.password}}" data-field="password" bindinput="handlePasswordInput"></input>
<input
class="field-input"
password
placeholder="请输入新密码"
placeholder-class="field-placeholder"
value="{{passwordForm.password}}"
data-field="password"
data-keyboard-anchor="settings-password-anchor"
bindinput="handlePasswordInput"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
<view class="field-block">
<view id="settings-password-confirm-anchor" class="field-block">
<text class="field-label">确认密码</text>
<input class="field-input" password placeholder="请再次输入新密码" placeholder-class="field-placeholder" value="{{passwordForm.confirmPassword}}" data-field="confirmPassword" bindinput="handlePasswordInput"></input>
<input
class="field-input"
password
placeholder="请再次输入新密码"
placeholder-class="field-placeholder"
value="{{passwordForm.confirmPassword}}"
data-field="confirmPassword"
data-keyboard-anchor="settings-password-confirm-anchor"
bindinput="handlePasswordInput"
bindfocus="handleKeyboardFocus"
bindblur="handleKeyboardBlur"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
></input>
</view>
<view class="panel-actions">
<view class="secondary-btn panel-btn" bindtap="handleCloseSecurityPanel">取消</view>
@@ -176,6 +299,8 @@
<view class="logout-btn" bindtap="handleLogout">退出登录</view>
</block>
</view>
<view wx:if="{{keyboardVisible}}" class="keyboard-spacer" style="height: {{keyboardInsetRpx}}rpx;"></view>
</view>
</scroll-view>
</view>
+9
View File
@@ -14,6 +14,10 @@
min-height: 100%;
}
.settings-page-shell.keyboard-active {
padding-bottom: 0;
}
.settings-hero {
margin-bottom: 20rpx;
}
@@ -225,3 +229,8 @@
font-size: 28rpx;
font-weight: 700;
}
.keyboard-spacer {
width: 100%;
flex-shrink: 0;
}