jiangping
2024-09-27 04b4bddaac0a222760113899568d20b45af701f4
wechat_staff/pages/kefu/fond.js
@@ -1,69 +1,239 @@
// pages/kefu/fond.js
import moment from "moment";
import { getContentShareImg } from '../../api/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    bottomLift: 0
    bottomLift: 0,
    checkAll: false,
    enjoyList: [],
    leftIcon: '<<',
    rightIcon: '>>',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  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()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  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);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  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()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  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,
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  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',
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  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'
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  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
            })
          }
        })
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  }
      }
    })
  },
  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
    })
  },
})