2023-04-17 16:14:18 +08:00

159 lines
3.5 KiB
JavaScript

// pages/img_show/img_show.js
var app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
src: '',
screen_data: {},
videoLength: 0,
imgLength: 0,
windowWidth: 0,
swiperHeight: 320,
attachment: [],
indicatorDots: true,
autoplay: false,
interval: 5000,
duration: 500,
circular: true,
skip: true,
videoautoplay: true,
controls: true,
current: 0,
imgIndex: 0,
videoIndex: 0,
tab: 'video',
currentTime: 0,
loop: false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let that = this;
this.setData({
src: options.src || '',
frompage: options.frompage || '',
screen_data: app.globalData.screen_data,
current: options.current || 0,
currentTime: options.currentTime || 0,
loop: options.loop || false
})
wx.getSystemInfo({
success: function (res) {
that.setData({
windowWidth: res.windowWidth
})
}
})
if (options.frompage && options.frompage == 'apartment') {
let attachment = wx.getStorageSync('attachment')
this.setData({
attachment
})
wx.removeStorageSync('attachment')
this.calculateimgLength()
}
if (!this.data.frompage) {
this.videoContext = wx.createVideoContext('videobox', this) //这里对应的视频id
setTimeout(() => {
// this.videoContext.play()
}, 2000)
}
},
/**
* 用户点击关闭
*/
closePage() {
wx.navigateBack()
},
// 计算视频个数
calculateimgLength() {
let that = this;
this.data.attachment.forEach(item => {
if (item.isvideo) {
that.data.videoLength++;
} else {
that.data.imgLength++;
}
})
this.setData({
videoLength: this.data.videoLength,
imgLength: this.data.imgLength
})
if (this.data.current >= this.data.videoLength) {
this.setData({
tab: 'img',
imgIndex: this.data.current - this.data.videoLength,
})
} else {
this.setData({
videoIndex: this.data.current
})
}
},
// 切换图片或视频
changeTab(e) {
let tab = e.currentTarget.dataset.tab;
if (this.data.tab == tab) {
return false
}
// this.data.tab = tab;
this.setData({
current: tab == 'img' ? this.data.videoLength : 0,
imgIndex: 0,
videoIndex: 0,
tab
})
console.log('changetab,this.tab=', this.tab)
},
currentIndex: 0,
bindchangeSwiper(e) {
// console.log(e.detail.current, e.detail.source)
if (this.data.tab == 'video') {
let videoID = wx.createVideoContext('video' + this.currentIndex, this)
videoID.pause()
}
let current = e.detail.current;
this.currentIndex = current;
if (e.detail.current >= this.data.videoLength) {
// 显示视频
if (this.data.tab == 'video') {
this.setData({
tab: 'img',
imgIndex: 0,
})
} else {
this.setData({
imgIndex: current - this.data.videoLength
})
}
} else {
// 显示图片
if (this.data.tab == 'img') {
this.setData({
tab: 'video',
videoIndex: 0,
})
} else {
this.setData({
videoIndex: current
})
}
}
},
load(e) {
// if (this.data.swiperHeight > 0) { return false }
this.setData({
swiperHeight: Math.max(this.data.swiperHeight, this.data.windowWidth * e.detail.height / e.detail.width)
})
},
})