Files
JayuCloud/WeChatProgram/pages/home/index.js
T

442 lines
14 KiB
JavaScript
Raw Normal View History

2026-04-01 15:18:35 +08:00
const auth = require('../../utils/auth')
const navigation = require('../../utils/navigation')
const cache = require('../../utils/cache')
2026-04-01 15:18:35 +08:00
const { buildProductCards } = require('../../utils/product')
const { resolveImageList } = require('../../utils/file')
const { DEFAULT_SHOP_IMAGE, CACHE_TTL } = require('../../utils/config')
2026-04-22 16:53:32 +08:00
const {
createKeyboardSafeData,
attachKeyboardSafe,
detachKeyboardSafe,
handleKeyboardFocus,
handleKeyboardBlur,
resetKeyboardSafe
} = require('../../utils/keyboard-safe')
2026-04-01 15:18:35 +08:00
const productApi = require('../../api/product')
const shopApi = require('../../api/shop')
const userApi = require('../../api/user')
const theme = require('../../utils/theme')
2026-04-01 15:18:35 +08:00
/**
* home page logic.
* Call guide: page data drives the WXML template, and each method is called by user events or lifecycle callbacks.
*/
2026-04-01 15:18:35 +08:00
Page({
2026-04-22 16:53:32 +08:00
data: Object.assign(createKeyboardSafeData({
cursorSpacing: 96
}), theme.pageThemeData(getApp().globalData.shopTheme), {
2026-04-01 15:18:35 +08:00
initialized: false,
loading: false,
loadingMore: false,
2026-04-01 15:18:35 +08:00
isLoggedIn: false,
userInfo: null,
2026-04-22 16:53:32 +08:00
navTitle: '游客模式',
2026-04-01 15:18:35 +08:00
greeting: '欢迎来到 Yumi Nail',
displayName: '先看看门店和作品',
searchKeyword: '',
bannerImages: [],
featuredItems: [],
popularItems: [],
announcements: [],
2026-04-01 15:18:35 +08:00
popularPage: 1,
pageSize: 6,
hasMore: false,
popularTotal: 0
2026-04-22 16:53:32 +08:00
}),
2026-04-01 15:18:35 +08:00
/**
* `onLoad` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
onLoad() {
this.homeRequestId = 0
2026-04-22 16:53:32 +08:00
this.ensureKeyboardSafe()
2026-04-01 15:18:35 +08:00
this.loadHomeData()
},
/**
* `onShow` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
onShow() {
2026-04-22 16:53:32 +08:00
this.ensureKeyboardSafe()
2026-04-01 15:18:35 +08:00
this.syncUserState()
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
if (this.data.initialized && this.data.isLoggedIn) {
this.loadUserProfile()
}
},
/**
* `onHide` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-22 16:53:32 +08:00
onHide() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
},
/**
* `onUnload` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-22 16:53:32 +08:00
onUnload() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
},
2026-04-01 15:18:35 +08:00
async onPullDownRefresh() {
cache.removeByPrefix('home:')
2026-04-01 15:18:35 +08:00
await this.loadHomeData()
wx.stopPullDownRefresh()
},
/**
* `onReachBottom` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
onReachBottom() {
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (!this.data.hasMore || this.data.loadingMore) {
2026-04-01 15:18:35 +08:00
return
}
this.loadPopularProducts(false)
2026-04-01 15:18:35 +08:00
},
/**
* `ensureKeyboardSafe` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-22 16:53:32 +08:00
ensureKeyboardSafe() {
attachKeyboardSafe(this, {
cursorSpacing: 96,
spacerExtraRpx: 24
})
},
/**
* `syncUserState` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
syncUserState() {
const app = getApp()
const userInfo = auth.getStoredUserInfo()
const isLoggedIn = auth.isLoggedIn()
2026-04-22 16:53:32 +08:00
const navTitle = userInfo && String(userInfo.shopName || '').trim()
? String(userInfo.shopName).trim()
: '游客模式'
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
if (app) {
app.globalData.userInfo = userInfo
app.globalData.isLoggedIn = isLoggedIn
}
this.setData({
isLoggedIn,
userInfo,
2026-04-22 16:53:32 +08:00
navTitle,
2026-04-01 15:18:35 +08:00
greeting: isLoggedIn ? '欢迎回来' : '游客模式',
displayName: isLoggedIn
? ((userInfo && userInfo.nickname) || 'Yumi 用户')
: '先逛逛喜欢的款式和门店',
bannerImages: isLoggedIn ? [] : this.data.bannerImages
2026-04-01 15:18:35 +08:00
})
},
async loadUserProfile() {
if (!auth.isLoggedIn()) {
this.syncUserState()
return
}
try {
const userInfo = await userApi.getUserInfo()
getApp().refreshUserInfo(userInfo)
this.syncUserState()
} catch (error) {}
},
async loadAnnouncements() {
if (!auth.isLoggedIn()) {
this.setData({ announcements: [] })
return
}
try {
const list = await userApi.listMyAnnouncements()
this.setData({
announcements: Array.isArray(list) ? list.map(item => ({
...item,
displayText: `${item.publisherName || '平台'}:${item.content || ''}`
})) : []
})
} catch (error) {
this.setData({ announcements: [] })
}
},
2026-04-01 15:18:35 +08:00
async loadHomeData() {
const requestId = ++this.homeRequestId
2026-04-01 15:18:35 +08:00
this.setData({ loading: true })
this.syncUserState()
try {
const loggedIn = auth.isLoggedIn()
2026-04-01 15:18:35 +08:00
await Promise.all([
this.loadUserProfile(),
this.loadAnnouncements(),
loggedIn ? Promise.resolve() : this.loadBannerImages(requestId),
this.loadFeaturedProducts(requestId),
this.loadPopularProducts(true, requestId)
2026-04-01 15:18:35 +08:00
])
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (requestId === this.homeRequestId) {
this.setData({
initialized: true
})
}
2026-04-01 15:18:35 +08:00
} catch (error) {
wx.showToast({
title: error.message || '首页加载失败',
icon: 'none'
})
} finally {
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (requestId === this.homeRequestId) {
this.setData({ loading: false })
}
2026-04-01 15:18:35 +08:00
}
},
async loadBannerImages(requestId) {
const banners = await cache.remember('home:banners', CACHE_TTL.banner, async () => {
const shops = await shopApi.getPublicShops()
const list = Array.isArray(shops) ? shops : []
const resolved = await Promise.all(list.slice(0, 6).map(async (shop, index) => {
const imageIds = shop.coverImageFileId || shop.imageFileIds
const urls = await resolveImageList(imageIds, DEFAULT_SHOP_IMAGE)
return {
2026-04-01 15:18:35 +08:00
id: `${shop.id}-${index}`,
shopId: shop.id,
shopName: shop.shopName,
businessHours: shop.businessHours || '欢迎到店体验',
imageUrl: urls[0]
}
}))
return resolved.filter(item => item.imageUrl)
})
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (requestId === this.homeRequestId) {
this.setData({
bannerImages: Array.isArray(banners) ? banners : []
})
}
},
async loadFeaturedProducts(requestId) {
const items = await cache.remember('home:featured', CACHE_TTL.homeList, async () => {
const result = await productApi.getProductPage({
page: 1,
size: 6,
tag: '精选'
})
return buildProductCards(result && result.list ? result.list : [])
})
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (requestId === this.homeRequestId) {
this.setData({
featuredItems: Array.isArray(items) ? items : []
})
}
},
async loadPopularProducts(reset, requestId) {
const currentRequestId = requestId || this.homeRequestId
const nextPage = reset ? 1 : this.data.popularPage + 1
const loadingState = reset ? { loading: true } : { loadingMore: true }
this.setData(loadingState)
try {
const result = await productApi.getProductPage({
page: nextPage,
size: this.data.pageSize,
tag: '热门'
})
const cards = await buildProductCards(result && result.list ? result.list : [])
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (currentRequestId !== this.homeRequestId) {
return
}
const mergedItems = reset ? cards : this.data.popularItems.concat(cards)
const total = Number(result && result.total) || 0
this.setData({
popularItems: mergedItems,
popularPage: nextPage,
popularTotal: total,
hasMore: mergedItems.length < total
})
} catch (error) {
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (currentRequestId !== this.homeRequestId) {
return
}
wx.showToast({
title: error.message || '热门作品加载失败',
icon: 'none'
})
} finally {
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (currentRequestId === this.homeRequestId) {
this.setData({
loadingMore: false
2026-04-01 15:18:35 +08:00
})
}
}
},
/**
* `handleSearchInput` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleSearchInput(event) {
this.setData({
searchKeyword: event.detail.value
})
},
/**
* `handleSearchFocus` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-22 16:53:32 +08:00
handleSearchFocus(event) {
handleKeyboardFocus(this, event)
},
/**
* `handleSearchBlur` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-22 16:53:32 +08:00
handleSearchBlur() {
handleKeyboardBlur(this)
},
/**
* `handleSearchConfirm` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleSearchConfirm() {
2026-04-22 16:53:32 +08:00
resetKeyboardSafe(this)
2026-04-01 15:18:35 +08:00
navigation.openProducts({
keyword: this.data.searchKeyword.trim()
})
},
/**
* `handleBannerTap` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleBannerTap(event) {
const shopId = event.currentTarget.dataset.shopId
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
if (!shopId) {
return
}
navigation.navigate('/pages/shop-detail/index', {
id: shopId,
returnTo: navigation.getCurrentPageUrl()
})
},
/**
* `handleProfileTap` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleProfileTap() {
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
if (this.data.isLoggedIn) {
navigation.switchTab('/pages/profile/index')
return
}
navigation.openAuth()
},
/**
* `handleProductSelect` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleProductSelect(event) {
const product = event.detail || {}
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
if (!product.id) {
return
}
navigation.navigate('/pages/product-detail/index', {
id: product.id
})
},
/**
* `handleSeeAllFeatured` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleSeeAllFeatured() {
navigation.openProducts({
tag: '精选'
})
},
/**
* `handleSeeAllPopular` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleSeeAllPopular() {
navigation.openProducts({
tag: '热门'
})
}
})