openBusiness.js 4.73 KB
// packageOutput/pages/openBusiness/openBusiness.js
import outputApi from '~/api/outputApi/index'
import { IMG_URL } from '~/utils/constantData'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    IMGURL: IMG_URL,
    merchantInfo: {},
    sceneCodeList: [],
    // 场景业务
    sceneInfo: {
      sceneList: [{
        sceneCode: 'SC001',
        flag: true
      }, {
        sceneCode: 'SC002',
        flag: true
      }, {
        sceneCode: 'SC003',
        flag: true
      }, {
        sceneCode: 'SC004',
        flag: true
      }]
    },
    sceneActive: 1,
    sceneCodeActive: 'SC002',
    isLoading: false,
    // 活动内容
    activityList: [],
    sceneActivityObj: {
      scene0: [],
      scene1: [],
      scene2: [],
      scene3: []
    }
  },

  // 查找当前高亮场景的idx
  findSceneIdx(sceneCode = this.data.sceneCodeActive) {
    return this.data.sceneCodeList.findIndex(item => item == sceneCode)
  },

  getSceneProStatus(sceneCode) {
    var params = {
      serviceMerchantId: this.data.merchantInfo.pMerchantId,
      merchant4sId: this.data.merchantInfo.merchantId,
      sceneCode: sceneCode
    }
    outputApi.getSceneProStatus(params).then(res => {
      var proList = res.find(item => item.sceneCode == sceneCode)
      this.setData({
        ['sceneActivityObj.scene' + this.findSceneIdx()]: proList.productList
      })
      this.setCurActivity()
    })
  },

  // 场景切换
  handleSceneChange(e) {
    // var idx = e.detail.sceneIdx
    var sceneCode = e.detail.sceneCode
    this.setData({
      sceneCodeActive: sceneCode,
    })
    if (!this.data.sceneActivityObj['scene' + this.findSceneIdx()].length) {
      this.getSceneProStatus(this.data.sceneCodeList[this.findSceneIdx()])
      return true
    }
    this.setCurActivity()
    // console.log('换内容---')
  },

  setCurActivity() {
    this.setData({
      activityList: JSON.parse(JSON.stringify(this.data.sceneActivityObj['scene' + this.findSceneIdx()]))
    })
  },

  // 活动切换
  handleActClick(e) {
    var idx = e.currentTarget.dataset.idx
    this.setData({
      ['activityList[' + idx + '].checked']: !this.data.activityList[idx].checked
    })
  },

  handleJump() {
    // clearTimeout(this.data.time)
    this.data.time = new setTimeout(() => {
      wx.$router.go('pages.output', { page: 'client', refresh: true })
    }, 2000)
    // wx.$api.showToast('申请成功')
    // setTimeout(() => {
    //   wx.$router.back(page => {
    //     let node = page.selectComponent('#client')
    //     // console.log(node)
    //     node.getMerchantRange && node.getMerchantRange()
    //     // page.getClueInit && page.getClueInit()
    //   })
    // }, 1000)
  },

  handleConfirm() {
    if(this.data.isLoading) {
      return
    }
    var params = {
      serviceMerchantId: this.data.merchantInfo.pMerchantId,
      serviceMerchantName: this.data.merchantInfo.pMerchantName,
      merchantId: this.data.merchantInfo.merchantId,
      merchantName: this.data.merchantInfo.merchantName,
      sceneCode: this.data.sceneCodeList[this.findSceneIdx()],
      productCodes: this.data.activityList.filter(item => item.checked == true).map(item => item.productCode),
      clueId: this.data.clueId
    }
    if (!params.productCodes.length) {
      wx.$api.showToast('请选产品/活动')
      return true
    }
    this.setData({
      isLoading: true
    })
    outputApi.applyActivity(params).then(res => {
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000,
        mask: true,
        success: () => {
          this.handleJump()
        }
      })
    }).catch(() => {
      this.setData({
        isLoading: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.$router.getData(obj => {
      this.setData({
        merchantInfo: obj.merchantInfo,
        // sceneActive: obj.sceneActive,
        sceneCodeList: obj.sceneCodeList || ['SC002', 'SC003', 'SC004', 'SC001'],
        sceneCodeActive: obj.sceneCodeActive || 'SC002',
        clueId: obj.clueId
      })
      this.getSceneProStatus(this.data.sceneCodeList[this.findSceneIdx()])
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    wx.setNavigationBarTitle({
      title: '开通店面业务',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})