224 lines
5.1 KiB
JavaScript
224 lines
5.1 KiB
JavaScript
const auth = require('../../utils/auth')
|
|
const navigation = require('../../utils/navigation')
|
|
const { buildProductCards } = require('../../utils/product')
|
|
const { resolveImageList } = require('../../utils/file')
|
|
const { DEFAULT_SHOP_IMAGE } = require('../../utils/config')
|
|
const productApi = require('../../api/product')
|
|
const shopApi = require('../../api/shop')
|
|
const userApi = require('../../api/user')
|
|
|
|
Page({
|
|
data: {
|
|
initialized: false,
|
|
loading: false,
|
|
isLoggedIn: false,
|
|
userInfo: null,
|
|
greeting: '欢迎来到 Yumi Nail',
|
|
displayName: '先看看门店和作品',
|
|
searchKeyword: '',
|
|
bannerImages: [],
|
|
featuredItems: [],
|
|
popularSource: [],
|
|
popularItems: [],
|
|
popularPage: 1,
|
|
pageSize: 6,
|
|
hasMore: false
|
|
},
|
|
|
|
onLoad() {
|
|
this.loadHomeData()
|
|
},
|
|
|
|
onShow() {
|
|
this.syncUserState()
|
|
if (this.data.initialized && this.data.isLoggedIn) {
|
|
this.loadUserProfile()
|
|
}
|
|
},
|
|
|
|
async onPullDownRefresh() {
|
|
await this.loadHomeData()
|
|
wx.stopPullDownRefresh()
|
|
},
|
|
|
|
onReachBottom() {
|
|
if (!this.data.hasMore) {
|
|
return
|
|
}
|
|
|
|
const nextPage = this.data.popularPage + 1
|
|
const nextItems = this.data.popularSource.slice(0, nextPage * this.data.pageSize)
|
|
this.setData({
|
|
popularPage: nextPage,
|
|
popularItems: nextItems,
|
|
hasMore: nextItems.length < this.data.popularSource.length
|
|
})
|
|
},
|
|
|
|
syncUserState() {
|
|
const app = getApp()
|
|
const userInfo = auth.getStoredUserInfo()
|
|
const isLoggedIn = auth.isLoggedIn()
|
|
if (app) {
|
|
app.globalData.userInfo = userInfo
|
|
app.globalData.isLoggedIn = isLoggedIn
|
|
}
|
|
|
|
this.setData({
|
|
isLoggedIn,
|
|
userInfo,
|
|
greeting: isLoggedIn ? '欢迎回来' : '游客模式',
|
|
displayName: isLoggedIn
|
|
? ((userInfo && userInfo.nickname) || 'Yumi 用户')
|
|
: '先逛逛喜欢的款式和门店'
|
|
})
|
|
},
|
|
|
|
async loadUserProfile() {
|
|
if (!auth.isLoggedIn()) {
|
|
this.syncUserState()
|
|
return
|
|
}
|
|
|
|
try {
|
|
const userInfo = await userApi.getUserInfo()
|
|
getApp().refreshUserInfo(userInfo)
|
|
this.syncUserState()
|
|
} catch (error) {}
|
|
},
|
|
|
|
async loadHomeData() {
|
|
this.setData({ loading: true })
|
|
this.syncUserState()
|
|
|
|
try {
|
|
await Promise.all([
|
|
this.loadUserProfile(),
|
|
this.loadBannerImages(),
|
|
this.loadFeaturedProducts(),
|
|
this.loadPopularProducts()
|
|
])
|
|
|
|
this.setData({
|
|
initialized: true
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '首页加载失败',
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
async loadBannerImages() {
|
|
const shops = await shopApi.getPublicShops()
|
|
const list = Array.isArray(shops) ? shops : []
|
|
const banners = []
|
|
|
|
for (let index = 0; index < list.length; index += 1) {
|
|
const shop = list[index]
|
|
const imageIds = shop.coverImageFileId || shop.imageFileIds
|
|
const urls = await resolveImageList(imageIds, DEFAULT_SHOP_IMAGE)
|
|
if (urls[0]) {
|
|
banners.push({
|
|
id: `${shop.id}-${index}`,
|
|
shopId: shop.id,
|
|
shopName: shop.shopName,
|
|
businessHours: shop.businessHours || '欢迎到店体验',
|
|
imageUrl: urls[0]
|
|
})
|
|
}
|
|
}
|
|
|
|
this.setData({
|
|
bannerImages: banners.slice(0, 6)
|
|
})
|
|
},
|
|
|
|
async loadFeaturedProducts() {
|
|
let products = await productApi.getProductsByTag('精选')
|
|
if (!Array.isArray(products) || !products.length) {
|
|
products = await productApi.getAllProducts()
|
|
}
|
|
|
|
const cards = await buildProductCards((products || []).slice(0, 6))
|
|
this.setData({
|
|
featuredItems: cards
|
|
})
|
|
},
|
|
|
|
async loadPopularProducts() {
|
|
let products = await productApi.getProductsByTag('热门')
|
|
if (!Array.isArray(products) || !products.length) {
|
|
products = await productApi.getAllProducts()
|
|
}
|
|
|
|
const cards = await buildProductCards(products || [])
|
|
const initialItems = cards.slice(0, this.data.pageSize)
|
|
this.setData({
|
|
popularSource: cards,
|
|
popularItems: initialItems,
|
|
popularPage: 1,
|
|
hasMore: initialItems.length < cards.length
|
|
})
|
|
},
|
|
|
|
handleSearchInput(event) {
|
|
this.setData({
|
|
searchKeyword: event.detail.value
|
|
})
|
|
},
|
|
|
|
handleSearchConfirm() {
|
|
navigation.openProducts({
|
|
keyword: this.data.searchKeyword.trim()
|
|
})
|
|
},
|
|
|
|
handleBannerTap(event) {
|
|
const shopId = event.currentTarget.dataset.shopId
|
|
if (!shopId) {
|
|
return
|
|
}
|
|
|
|
navigation.navigate('/pages/shop-detail/index', {
|
|
id: shopId,
|
|
returnTo: navigation.getCurrentPageUrl()
|
|
})
|
|
},
|
|
|
|
handleProfileTap() {
|
|
if (this.data.isLoggedIn) {
|
|
navigation.switchTab('/pages/profile/index')
|
|
return
|
|
}
|
|
|
|
navigation.openAuth()
|
|
},
|
|
|
|
handleProductSelect(event) {
|
|
const product = event.detail || {}
|
|
if (!product.id) {
|
|
return
|
|
}
|
|
|
|
navigation.navigate('/pages/product-detail/index', {
|
|
id: product.id
|
|
})
|
|
},
|
|
|
|
handleSeeAllFeatured() {
|
|
navigation.openProducts({
|
|
tag: '精选'
|
|
})
|
|
},
|
|
|
|
handleSeeAllPopular() {
|
|
navigation.openProducts({
|
|
tag: '热门'
|
|
})
|
|
}
|
|
})
|