applyActivity.js 3.16 KB
import ActivityApi from "~/api/activityApi/index"
import UsersApi from "~/api/usersApi/index"
import {
  reportData
} from '~/api/api.js'
Page({

  data: {
    currentIndex: 0,
    currentSceneCode: '',
    currentSceneName: '',
    product: [],
    USERINFO: {}
  },
  onLoad() {
    UsersApi.getWXUserInfo()
    const {
      USERINFO
    } = this.getCommonData()
    // 已开通场景
    ActivityApi.getOpenSceneList({merchantId: USERINFO.merchantId}).then(res => {
      let arr = []
      res && res.forEach(val => {
        if (val.status == 1 && val.sceneCode != 'SC005') arr.push(val)
      })
      if (arr[0] && arr[0].sceneCode) this.getProducts(arr[0].sceneCode)
      this.setData({
        list: arr,
        currentSceneCode: (arr[0] && arr[0].sceneCode) || 'SC001',
        currentSceneName: (arr[0] && arr[0].name)
      })
    })
  },
  getProducts(code) {
    let arr = []
    const {
      USERINFO
    } = this.getCommonData()
    this.setData({
      product: arr
    })
    ActivityApi.getNotOpenProductsList({merchantId: USERINFO.merchantId, sceneCode: code}).then(res => { // 默认选中第一个业务场景 获取可用产品/活动
      res.forEach(item => {
        item.isSelect = false
        if (item.sceneCode == this.data.currentSceneCode) arr.push(item)
      })
      this.setData({
        product: arr
      })
    })
  },
  select(e) {
    const { index, item } = e.currentTarget.dataset
    this.setData({
      currentIndex: index,
      currentSceneCode: item.sceneCode,
      currentSceneName: item.name
    })
    this.getProducts(item.sceneCode)
  },
  selectProduct(e) {
    const { item, index } = e.currentTarget.dataset
    let name = `product[${index}].isSelect`
    this.setData({
      [name]: !item.isSelect
    })
  },
  // 申请开通
  applyOpened() {
    // 获取已选的产品code
    let { product, currentSceneCode, currentSceneName } = this.data
    let [arr1, arr2] = [[], []]
    product.forEach(val => {
      if (val.isSelect) {
        arr1.push(val.productCode)
        arr2.push(val.productName)
      }
    })
    if (!arr1.length) return wx.showToast({
      title: '请选择产品/活动',
      icon: "none"
    })
    const {
      USERINFO,
      WXUSERINFO
    } = this.getCommonData()
    wx.showToast({
      title: '提交中',
      icon: 'loading'
    })
    reportData({
      openId: WXUSERINFO.openId || '',
      unionId: USERINFO.uuid || '',
      nickName: USERINFO.name || '',
      avatar: USERINFO.sta_avatarUrl || USERINFO.weChatAvatarUrl || '',
      phone: USERINFO.phone || USERINFO.weChatPhone || '',
      merchantId: USERINFO.merchantId || '',
      shopName: USERINFO.merchantName || '',
      sceneCode: currentSceneCode || '',
      sceneName: currentSceneName || '',
      productCode: arr1.join(','),
      productDesc: arr2.join(','),
      shopId: USERINFO.merchantId || ''
    }, 2).then(res => {
      wx.hideToast()
      wx.$router.redirectTo('packageActivity.applySuccess')
    })
    
  },
  getCommonData() {
    const USERINFO = wx.$storage.get('userInfo')
    const WXUSERINFO = wx.$storage.get('wx_user_info')
    this.setData({
      USERINFO
    })
    return {
      USERINFO,
      WXUSERINFO
    }
  }

})