103 lines
3.1 KiB
JavaScript
103 lines
3.1 KiB
JavaScript
Page({
|
|
onShow() {
|
|
// 原有代码...
|
|
|
|
// 🆕 注册通知组件
|
|
this.registerNotificationComponent();
|
|
},
|
|
|
|
// 🆕 新增:注册通知组件
|
|
registerNotificationComponent() {
|
|
const app = getApp();
|
|
const notificationComp = this.selectComponent('#global-notification');
|
|
|
|
if (notificationComp && notificationComp.showNotification) {
|
|
console.log('🏪 商城页面注册通知组件');
|
|
app.globalData.globalNotification = {
|
|
show: (data) => notificationComp.showNotification(data),
|
|
hide: () => notificationComp.hideNotification()
|
|
};
|
|
}
|
|
},
|
|
data: {
|
|
// 图片URL(带时间戳避免缓存)
|
|
imageUrl: '',
|
|
// 容器高度(初始为100vh,加载后调整为图片高度)
|
|
containerHeight: 0
|
|
},
|
|
|
|
onLoad() {
|
|
// 初始设置容器高度为屏幕高度
|
|
this.getSystemInfo()
|
|
// 构建图片URL(带时间戳)
|
|
this.buildImageUrl()
|
|
},
|
|
|
|
// 获取系统信息
|
|
getSystemInfo() {
|
|
const res = wx.getSystemInfoSync()
|
|
this.setData({
|
|
containerHeight: res.windowHeight
|
|
})
|
|
},
|
|
|
|
// 构建图片URL(关键:加时间戳避免缓存)
|
|
buildImageUrl() {
|
|
const app = getApp()
|
|
const ossImageUrl = app.globalData.ossImageUrl || ''
|
|
const dashouguize = app.globalData.dashouguize || ''
|
|
|
|
// 加上时间戳参数,强制每次重新加载
|
|
const timestamp = new Date().getTime()
|
|
const fullImageUrl = ossImageUrl + dashouguize + '?t=' + timestamp
|
|
|
|
//console.log('规则图片URL(带时间戳):', fullImageUrl)
|
|
|
|
this.setData({
|
|
imageUrl: fullImageUrl
|
|
})
|
|
},
|
|
|
|
// 图片加载成功
|
|
onImageLoad(e) {
|
|
//console.log('图片加载成功,原始尺寸:', e.detail.width, 'x', e.detail.height)
|
|
|
|
// 根据图片实际高度设置容器高度
|
|
// 图片宽度为屏幕宽度,高度按比例计算
|
|
const systemInfo = wx.getSystemInfoSync()
|
|
const screenWidth = systemInfo.screenWidth // 单位:px
|
|
|
|
// 图片的实际像素尺寸
|
|
const imageWidth = e.detail.width
|
|
const imageHeight = e.detail.height
|
|
|
|
// 计算在小程序中显示的高度(保持比例)
|
|
// 图片在小程序中宽度为100%屏幕宽度,高度按比例缩放
|
|
const displayHeight = (imageHeight / imageWidth) * screenWidth
|
|
|
|
//console.log('计算后显示高度:', displayHeight, 'px')
|
|
|
|
// 设置容器高度为图片高度
|
|
this.setData({
|
|
containerHeight: displayHeight
|
|
})
|
|
},
|
|
|
|
// 图片加载失败
|
|
onImageError() {
|
|
console.error('图片加载失败')
|
|
wx.showToast({
|
|
title: '图片加载失败',
|
|
icon: 'none',
|
|
duration: 2000
|
|
})
|
|
},
|
|
|
|
// 下拉刷新
|
|
onPullDownRefresh() {
|
|
// 重新构建图片URL(带新时间戳)
|
|
this.buildImageUrl()
|
|
// 停止下拉刷新
|
|
wx.stopPullDownRefresh()
|
|
}
|
|
}) |