index.js 4.59 KB
// pages/crowdfunding/components/service/index.js
import {
  shopmallApi
} from '../../api/index'
let backgroundAudioManager
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    detail: {
      type: Object,
      value: {},
      observer: function (n) {
        this.init(n)
      }
    },
    groupInfo: {
      type: Object,
      value: () => {}
    },
    isFestival: {
      type: Boolean,
      value: false,
    },
    musicStatus: {
      type: Boolean,
      value: false,
      observer: function (n) {
        // true:播放状态,false:暂停状态
        this.changeMusicStatus(n)
      }
    },
    showTopButton: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    musicState: false,
    showOnlineService: false,
    showOrderBtn: false,
    activityBigImg: 'https://img02.glsx.com.cn/weapp/resource/miniprogram/expandedGold/festival_38.png',
  },
  /**
   * 组件的方法列表
   */
  methods: {

    // 初始化
    init(n) {
      let _this = this
      if (n.activityMusic) {
        backgroundAudioManager = wx.createInnerAudioContext()
        wx.setInnerAudioOption && wx.setInnerAudioOption({
          obeyMuteSwitch: false
        })
        backgroundAudioManager.src = n.activityMusic
        backgroundAudioManager.onCanplay(_=>{
          backgroundAudioManager.play()
          const thisPage = wx.$router.getPageInstance()
          if(thisPage.isLiveFull) backgroundAudioManager.pause()
        })
        // 监听背景音乐暂停播放
        backgroundAudioManager.onPause(function () {
          _this.setData({
            musicState: true
          })
        })
        // 监听背景音乐播完
        backgroundAudioManager.onPlay(function () {
          _this.setData({
            musicState: false,
            showMusic: true
          })
        })
        // 监听背景音乐播完完毕,重新播放
        backgroundAudioManager.onEnded(function () {
          backgroundAudioManager.play()
        })
      }
    },

    // 打开客服
    goService() {
      const appEnter = wx.getEnterOptionsSync()
      let o = this.data.detail
      if (!o.customerServerUrl) {
        this.setData({
          showOnlineService: true
        })
        return false;
      }
      if (appEnter.scene === 1154) {
        wx.$eventbus.emit('showTimeLineToast')
        return
      }
      wx.openCustomerServiceChat({
        extInfo: {
          url: o.customerServerUrl
        },
        corpId: 'wx51bda2e574d24a91',
        fail: function (e) {
          wx.showModal({
            title: '提示',
            content: JSON.stringify(e),
          })
        }
      })
    },

    // 背景音乐暂停、播放
    musicPause() {
      let n = this.data.detail,
        {
          musicState
        } = this.data
      if (!backgroundAudioManager) {
        backgroundAudioManager = wx.createInnerAudioContext()
        backgroundAudioManager.src = n.activityMusic
      }
      if (musicState) {
        backgroundAudioManager.play()
        this.triggerEvent('change', true)
      } else {
        backgroundAudioManager.pause()
        this.triggerEvent('change', false)
      }
      wx.vibrateShort()
    },

    // 停止播放背景音乐
    changeMusicStatus(musicStatus) {
      let n = this.data.detail
      if (!n.activityMusic) return
      if (!backgroundAudioManager) {
        backgroundAudioManager = wx.createInnerAudioContext()
        backgroundAudioManager.src = n.activityMusic
      }
      if (!musicStatus) {
        backgroundAudioManager.pause()
      } else {
        backgroundAudioManager.play()
      }
    },


    // 滚动到顶部
    goTop() {
      wx.pageScrollTo({
        scrollTop: 0
      })
    },

    // 拨打电话
    makePhone() {
      let o = this.data.detail;
      if (o.customerServicePhone) {
        return false;
      }
      wx.makePhoneCall({
        phoneNumber: o.customerServicePhone
      })
    },

    // 关闭客服弹框
    closeService() {
      this.setData({
        showOnlineService: false
      })
    },

    // 获取订单列表
    getOrderList() {
      shopmallApi.getOrderList(this.data.detail.id).then(res => {
        let data = res.data || []
        if (data && data.length) {
          this.setData({
            showOrderBtn: true
          })
        }
      })
    },

    // 查看订单
    lookOrderList() {
      let n = this.data.detail;
      wx.$router.go(`/packageShopmall/packageCrowdfunding/pages/order/list?id=${n.id}`)
    }

  },
  lifetimes: {
    ready() {
      wx.$eventbus.on('musicPause', _ => {
        this.musicPause()
      })
    }
  }
})