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

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;
}
+45 -2
View File
@@ -4,17 +4,28 @@ const cache = require('../../utils/cache')
const { buildProductCards } = require('../../utils/product')
const { resolveImageList } = require('../../utils/file')
const { DEFAULT_SHOP_IMAGE, CACHE_TTL } = require('../../utils/config')
const {
createKeyboardSafeData,
attachKeyboardSafe,
detachKeyboardSafe,
handleKeyboardFocus,
handleKeyboardBlur,
resetKeyboardSafe
} = require('../../utils/keyboard-safe')
const productApi = require('../../api/product')
const shopApi = require('../../api/shop')
const userApi = require('../../api/user')
Page({
data: {
data: Object.assign(createKeyboardSafeData({
cursorSpacing: 96
}), {
initialized: false,
loading: false,
loadingMore: false,
isLoggedIn: false,
userInfo: null,
navTitle: '游客模式',
greeting: '欢迎来到 Yumi Nail',
displayName: '先看看门店和作品',
searchKeyword: '',
@@ -25,20 +36,32 @@ Page({
pageSize: 6,
hasMore: false,
popularTotal: 0
},
}),
onLoad() {
this.homeRequestId = 0
this.ensureKeyboardSafe()
this.loadHomeData()
},
onShow() {
this.ensureKeyboardSafe()
this.syncUserState()
if (this.data.initialized && this.data.isLoggedIn) {
this.loadUserProfile()
}
},
onHide() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
},
onUnload() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
},
async onPullDownRefresh() {
cache.removeByPrefix('home:')
await this.loadHomeData()
@@ -52,10 +75,20 @@ Page({
this.loadPopularProducts(false)
},
ensureKeyboardSafe() {
attachKeyboardSafe(this, {
cursorSpacing: 96,
spacerExtraRpx: 24
})
},
syncUserState() {
const app = getApp()
const userInfo = auth.getStoredUserInfo()
const isLoggedIn = auth.isLoggedIn()
const navTitle = userInfo && String(userInfo.shopName || '').trim()
? String(userInfo.shopName).trim()
: '游客模式'
if (app) {
app.globalData.userInfo = userInfo
app.globalData.isLoggedIn = isLoggedIn
@@ -64,6 +97,7 @@ Page({
this.setData({
isLoggedIn,
userInfo,
navTitle,
greeting: isLoggedIn ? '欢迎回来' : '游客模式',
displayName: isLoggedIn
? ((userInfo && userInfo.nickname) || 'Yumi 用户')
@@ -203,7 +237,16 @@ Page({
})
},
handleSearchFocus(event) {
handleKeyboardFocus(this, event)
},
handleSearchBlur() {
handleKeyboardBlur(this)
},
handleSearchConfirm() {
resetKeyboardSafe(this)
navigation.openProducts({
keyword: this.data.searchKeyword.trim()
})
+10 -4
View File
@@ -1,8 +1,8 @@
<view class="home-page-root">
<navigation-bar title="Yumi Nail" back="{{false}}" color="#2d2430" background="#fff7f8"></navigation-bar>
<navigation-bar title="{{navTitle}}" back="{{false}}" color="#2d2430" background="#fff7f8"></navigation-bar>
<scroll-view class="home-scroll" scroll-y enable-flex enhanced show-scrollbar="false">
<view class="page-shell home-page">
<scroll-view class="home-scroll" scroll-y enable-flex enhanced show-scrollbar="false" scroll-with-animation="{{true}}" scroll-into-view="{{keyboardAnchorId}}">
<view class="page-shell home-page {{keyboardVisible ? 'keyboard-active' : ''}}">
<view class="page-content">
<view class="page-hero home-hero">
<text class="hero-kicker">{{isLoggedIn ? greeting : 'YUMI NAIL 小程序'}}</text>
@@ -14,7 +14,7 @@
<view class="hero-action secondary" bindtap="handleSeeAllPopular">看看热门作品</view>
</view>
<view class="search-box">
<view id="home-search-anchor" class="search-box">
<input
class="search-input"
placeholder="搜索作品、风格或门店"
@@ -22,6 +22,11 @@
value="{{searchKeyword}}"
bindinput="handleSearchInput"
bindconfirm="handleSearchConfirm"
bindfocus="handleSearchFocus"
bindblur="handleSearchBlur"
data-keyboard-anchor="home-search-anchor"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
confirm-type="search"
/>
<view class="search-btn" bindtap="handleSearchConfirm">搜索</view>
@@ -135,6 +140,7 @@
</view>
</view>
</view>
<view wx:if="{{keyboardVisible}}" class="keyboard-light-spacer" style="height: {{keyboardInsetRpx}}rpx;"></view>
</view>
</scroll-view>
</view>
+9
View File
@@ -17,6 +17,10 @@
linear-gradient(180deg, #fff7f8 0%, #fffdfb 100%);
}
.home-page.keyboard-active {
padding-bottom: 0;
}
.home-hero {
background:
radial-gradient(circle at top right, rgba(255, 186, 207, 0.5), transparent 36%),
@@ -159,3 +163,8 @@
font-size: 22rpx;
color: #8d7a84;
}
.keyboard-light-spacer {
width: 100%;
flex-shrink: 0;
}
+42 -2
View File
@@ -8,13 +8,23 @@ const { buildProductCards } = require('../../utils/product')
const navigation = require('../../utils/navigation')
const { runWithLoading } = require('../../utils/loading')
const { CACHE_TTL } = require('../../utils/config')
const {
createKeyboardSafeData,
attachKeyboardSafe,
detachKeyboardSafe,
handleKeyboardFocus,
handleKeyboardBlur,
resetKeyboardSafe
} = require('../../utils/keyboard-safe')
const ALL_CATEGORY = '\u5168\u90e8'
const FILTER_BUSY_MESSAGE = '\u7b5b\u9009\u5904\u7406\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u64cd\u4f5c'
const CART_BUSY_MESSAGE = '\u8d2d\u7269\u8f66\u5904\u7406\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u64cd\u4f5c'
Page({
data: {
data: Object.assign(createKeyboardSafeData({
cursorSpacing: 96
}), {
initialized: false,
loading: false,
loadingMore: false,
@@ -27,14 +37,16 @@ Page({
pageSize: 10,
total: 0,
hasMore: false
},
}),
onLoad(options) {
this.filterRequestId = 0
this.ensureKeyboardSafe()
this.bootstrapPage(options || {})
},
async onShow() {
this.ensureKeyboardSafe()
const app = getApp()
const pendingFilter = app && app.globalData ? app.globalData.pendingProductFilter : null
@@ -51,6 +63,16 @@ Page({
}
},
onHide() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
},
onUnload() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
},
async onPullDownRefresh() {
await this.loadProducts(true)
wx.stopPullDownRefresh()
@@ -63,6 +85,13 @@ Page({
this.loadProducts(false)
},
ensureKeyboardSafe() {
attachKeyboardSafe(this, {
cursorSpacing: 96,
spacerExtraRpx: 16
})
},
async bootstrapPage(options) {
this.setData({
loading: true
@@ -263,7 +292,16 @@ Page({
})
},
handleSearchFocus(event) {
handleKeyboardFocus(this, event)
},
handleSearchBlur() {
handleKeyboardBlur(this)
},
async handleSearchConfirm() {
resetKeyboardSafe(this)
try {
await actionGuard.run(this, 'products:search', async () => {
this.setData({
@@ -287,6 +325,7 @@ Page({
async handleSelectCategory(event) {
const category = event.currentTarget.dataset.category
resetKeyboardSafe(this)
try {
await actionGuard.run(this, `products:category:${category}`, async () => {
this.setData({
@@ -310,6 +349,7 @@ Page({
},
async handleResetFilters() {
resetKeyboardSafe(this)
try {
await actionGuard.run(this, 'products:reset', async () => {
this.setData({
+7 -2
View File
@@ -1,7 +1,7 @@
<navigation-bar title="作品库" back="{{false}}" color="#2d2430" background="#fff7f8"></navigation-bar>
<view class="page-shell product-page">
<view class="page-content product-page-content">
<view class="page-shell product-page {{keyboardVisible ? 'keyboard-active' : ''}}">
<view class="page-content product-page-content" style="{{keyboardVisible ? 'padding-bottom: ' + keyboardInsetRpx + 'rpx;' : ''}}">
<view class="sticky-filter-bar">
<view class="filter-surface">
<view class="search-row">
@@ -12,6 +12,11 @@
value="{{searchKeyword}}"
bindinput="handleSearchInput"
bindconfirm="handleSearchConfirm"
bindfocus="handleSearchFocus"
bindblur="handleSearchBlur"
data-keyboard-anchor="products-search-anchor"
adjust-position="false"
cursor-spacing="{{keyboardCursorSpacing}}"
confirm-type="search"
/>
<view class="search-btn" bindtap="handleSearchConfirm">搜索</view>
+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;
}
+243
View File
@@ -0,0 +1,243 @@
const DEFAULT_CURSOR_SPACING = 96
const DEFAULT_SPACER_EXTRA_RPX = 48
const DEFAULT_SCROLL_DELAY = 32
const MAX_CURSOR_SPACING = 180
function getWindowWidth() {
try {
if (typeof wx.getWindowInfo === 'function') {
const info = wx.getWindowInfo()
if (info && info.windowWidth) {
return info.windowWidth
}
}
const systemInfo = wx.getSystemInfoSync()
return (systemInfo && systemInfo.windowWidth) || 375
} catch (error) {
return 375
}
}
function pxToRpx(px) {
return Math.round((Number(px) || 0) * 750 / getWindowWidth())
}
function createKeyboardSafeData(options = {}) {
const cursorSpacing = Number(options.cursorSpacing) > 0
? Math.round(Number(options.cursorSpacing))
: DEFAULT_CURSOR_SPACING
return {
keyboardVisible: false,
keyboardHeightPx: 0,
keyboardInsetRpx: 0,
keyboardAnchorId: '',
keyboardCursorSpacing: cursorSpacing
}
}
function clearKeyboardTimers(state) {
if (!state) {
return
}
if (state.anchorTimer) {
clearTimeout(state.anchorTimer)
state.anchorTimer = null
}
if (state.blurTimer) {
clearTimeout(state.blurTimer)
state.blurTimer = null
}
}
function queueAnchorScroll(page, anchorId) {
const state = page && page.__keyboardSafe
if (!page || !state || !anchorId) {
return
}
if (state.anchorTimer) {
clearTimeout(state.anchorTimer)
}
const nextDelay = Number(state.options.scrollDelay) || DEFAULT_SCROLL_DELAY
const updateAnchor = () => {
if (!page.__keyboardSafe) {
return
}
page.setData({
keyboardAnchorId: anchorId
})
}
if (page.data && page.data.keyboardAnchorId === anchorId) {
page.setData({
keyboardAnchorId: ''
})
state.anchorTimer = setTimeout(updateAnchor, nextDelay)
return
}
state.anchorTimer = setTimeout(updateAnchor, 0)
}
function attachKeyboardSafe(page, options = {}) {
if (!page) {
return
}
detachKeyboardSafe(page)
const state = {
activeAnchorId: '',
anchorTimer: null,
blurTimer: null,
options: {
cursorSpacing: Number(options.cursorSpacing) > 0
? Math.round(Number(options.cursorSpacing))
: DEFAULT_CURSOR_SPACING,
spacerExtraRpx: Number(options.spacerExtraRpx) >= 0
? Number(options.spacerExtraRpx)
: DEFAULT_SPACER_EXTRA_RPX,
scrollDelay: Number(options.scrollDelay) > 0
? Number(options.scrollDelay)
: DEFAULT_SCROLL_DELAY
},
listener: null
}
state.listener = result => {
const nextHeight = Math.max(0, Number(result && result.height) || 0)
const keyboardVisible = nextHeight > 0
const keyboardInsetRpx = keyboardVisible
? pxToRpx(nextHeight) + state.options.spacerExtraRpx
: 0
const keyboardCursorSpacing = keyboardVisible
? Math.max(
state.options.cursorSpacing,
Math.min(MAX_CURSOR_SPACING, Math.round(nextHeight * 0.38))
)
: state.options.cursorSpacing
page.setData({
keyboardVisible,
keyboardHeightPx: nextHeight,
keyboardInsetRpx,
keyboardCursorSpacing,
keyboardAnchorId: keyboardVisible ? page.data.keyboardAnchorId : (state.activeAnchorId ? page.data.keyboardAnchorId : '')
})
if (keyboardVisible && state.activeAnchorId) {
queueAnchorScroll(page, state.activeAnchorId)
}
}
page.__keyboardSafe = state
if (typeof wx.onKeyboardHeightChange === 'function') {
wx.onKeyboardHeightChange(state.listener)
}
}
function detachKeyboardSafe(page) {
if (!page || !page.__keyboardSafe) {
return
}
const state = page.__keyboardSafe
clearKeyboardTimers(state)
if (typeof wx.offKeyboardHeightChange === 'function' && state.listener) {
try {
wx.offKeyboardHeightChange(state.listener)
} catch (error) {}
}
delete page.__keyboardSafe
}
function handleKeyboardFocus(page, event) {
const state = page && page.__keyboardSafe
if (!page || !state) {
return
}
if (state.blurTimer) {
clearTimeout(state.blurTimer)
state.blurTimer = null
}
const anchorId = String(
event
&& event.currentTarget
&& event.currentTarget.dataset
&& event.currentTarget.dataset.keyboardAnchor
? event.currentTarget.dataset.keyboardAnchor
: ''
)
state.activeAnchorId = anchorId
if (anchorId) {
queueAnchorScroll(page, anchorId)
}
if (!page.data.keyboardCursorSpacing || page.data.keyboardCursorSpacing < state.options.cursorSpacing) {
page.setData({
keyboardCursorSpacing: state.options.cursorSpacing
})
}
}
function resetKeyboardSafe(page, options = {}) {
if (!page) {
return
}
const state = page.__keyboardSafe
if (state) {
clearKeyboardTimers(state)
if (options.clearActiveAnchor !== false) {
state.activeAnchorId = ''
}
}
const fallbackCursorSpacing = state ? state.options.cursorSpacing : DEFAULT_CURSOR_SPACING
page.setData({
keyboardVisible: false,
keyboardHeightPx: 0,
keyboardInsetRpx: 0,
keyboardAnchorId: options.keepAnchor ? (page.data.keyboardAnchorId || '') : '',
keyboardCursorSpacing: fallbackCursorSpacing
})
}
function handleKeyboardBlur(page) {
const state = page && page.__keyboardSafe
if (!page || !state) {
return
}
if (state.blurTimer) {
clearTimeout(state.blurTimer)
}
state.blurTimer = setTimeout(() => {
if (!page.__keyboardSafe) {
return
}
resetKeyboardSafe(page)
}, 120)
}
module.exports = {
createKeyboardSafeData,
attachKeyboardSafe,
detachKeyboardSafe,
handleKeyboardFocus,
handleKeyboardBlur,
resetKeyboardSafe
}