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

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
+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;
}