import moment from "moment"; import { getContentShareImg } from '../../api/index' Page({ /** * 页面的初始数据 */ data: { bottomLift: 0, checkAll: false, enjoyList: [], leftIcon: '<<', rightIcon: '>>', }, /** * 生命周期函数--监听页面加载 */ onShow(options) { var app = getApp().globalData this.setData({ bottomLift: app.bottomLift }) let enjoyList = wx.getStorageSync('enjoyList') || [] enjoyList.forEach(item => { item.checked = false }) wx.setStorageSync('enjoyList', enjoyList) console.log(this.data.enjoyList) this.initData() }, initData() { let temp = [] let enjoyList = wx.getStorageSync('enjoyList') || [] enjoyList = enjoyList.sort((a,b)=>{ return b.timestamp - a.timestamp }) let count = 0 enjoyList.forEach(item => { if(item.checked){ count++ } item.joinDate = moment(item.timestamp).format('YYYY年MM月DD日') item.joinTime = moment(item.timestamp).format('HH:mm') const index = temp.findIndex( i => i.joinDate === item.joinDate ) if(index === -1){ temp.push({ joinDate: item.joinDate, list: [item] }) }else{ temp[index].list.push(item) } }) this.setData({ checkAll: count === enjoyList.length }) this.setData({ enjoyList: temp }) console.log('enjoyList', temp); }, itemCheck(e){ const enjoyList = wx.getStorageSync('enjoyList') || [] const { id } = e.currentTarget.dataset console.log(id); enjoyList.forEach(item => { if(item.id === id){ item.checked = !item.checked } }) wx.setStorageSync('enjoyList', enjoyList) this.initData() }, itemClick(e) { let map = [ '/pages/detailDis/product', '/pages/detailDis/case', '/pages/detailDis/realpic', '/pages/consult/detail', ] const { id, type } = e.currentTarget.dataset wx.navigateTo({ url: map[type] + '?id=' + id, }) }, bind() { const enjoyList = wx.getStorageSync('enjoyList') || [] const temp = enjoyList.filter(i => i.checked) if(temp.length == 0) return wx.showToast({ title: '请先选择要关联的商品',icon:'none' }) wx.navigateTo({ url: '/pages/kefu/select', }) }, handleShare() { const { enjoyList } = this.data let info = {} enjoyList.forEach(item => { item.list.forEach(ite => { if(ite.checked){ info = ite } }) }) let map = [ '/pages/detailDis/product', '/pages/detailDis/case', '/pages/detailDis/realpic' ] console.log(info.id) wx.navigateToMiniProgram({ appId: 'wx208dd1edc0be24ee', path: `${map[info.joinType]}?origin=b&id=${info.id}&userId=${wx.getStorageSync('member').id}`, envVersion: 'release' }) }, handleDown() { const { enjoyList } = this.data let obj = {} enjoyList.forEach(item => { item.list.forEach(ite => { if(ite.checked){ obj = ite } }) }) console.log('obj', obj); let that = this getContentShareImg({ articleId: obj.id + '_' + obj.joinType, type: '0', // pageUrl: path, imgurl: obj.coverImage, }).then(res => { this.setData({ isShowPoster: true, showShare: false, posterUrl: res.data }) }) }, saveCard() { let that = this const posterUrl = this.data.posterUrl wx.downloadFile({ url: posterUrl, success: function (res) { console.log('下载成功', res); wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success(result) { console.log(result) that.setData({ isShowPoster: false }) wx.showToast({ title: '保存成功', icon: 'success', duration: 2000 }) } }) } }) }, closeCard() { this.setData({ isShowPoster: false }) }, itemDel() { let that = this let ids = [] let { enjoyList } = this.data enjoyList.forEach(item => { item.list.forEach(ite => { if(ite.checked){ ids.push(ite.id) } }) }) if(ids.length === 0) return wx.showToast({ title: '请先选择要删除的产品',icon: 'none' }) wx.showModal({ title: '提示', content: '你确认要删除该产品吗', success (res) { if (res.confirm) { let enjoyListTemp = wx.getStorageSync('enjoyList') || [] ids.forEach(item => { const index = enjoyListTemp.findIndex(i => i.id == item) console.log('index', index); if(index > -1){ enjoyListTemp.splice(index, 1) } }) wx.setStorageSync('enjoyList', enjoyListTemp) that.initData() } } }) }, checkAll() { this.setData({checkAll:!this.data.checkAll}) let enjoyList = wx.getStorageSync('enjoyList') || [] enjoyList.forEach(item => { item.checked = this.data.checkAll }) wx.setStorageSync('enjoyList', enjoyList) this.initData() }, openShare() { let count = 0 const { enjoyList } = this.data enjoyList.forEach(item => { item.list.forEach(ite => { if(ite.checked){ count ++ } }) }) if(count != 1) return wx.showToast({ title: '有且只能选择一个产品',icon: 'none' }) this.setData({ showShare: true }) }, onClose() { console.log('点击关闭'); this.setData({ showShare: false }) }, })