apply.js 4.67 KB
// pages/apply/apply.js
const navStyle = 'background-color: transparent;'
import { IMG_URL } from '~/utils/constantData.js'
import applyApi from '~/api/applyApi/index'
import { addCoProductApi } from '~/api/applyProd.js'
// `${IMG_URL}index/car2.png`,
//本地图片路径
let IMG_URLLocal = '../../images/'
import utils from '~/utils/util.js'
import { behavior as computedBehavior } from '~/utils/lib/miniprogram-computed'

Page({
  /**
   * 页面的初始数据
   */
  behaviors: [computedBehavior],
  data: {
    // navStyle,
    IMG_URL,
    tabActive: 1,
    tabList: [{
      title: '申请业务',
      name: 1
    }, {
      title: '申请产品',
      name: 2
    }],
    isLoading: false
  },
  // computed: {
  //   hasProdAct(data) {
  //     return data.selectProdArr.
  //   }
  // },
  // navbar高度设置
  navbarReady(e) {
    let { height, statusBarHeight } = e.detail
    this.setData({
      tabBarStyle: height / 2 + statusBarHeight
      // headerHeight: height / 2 + statusBarHeight +  88*2
    })
  },
  onTabChange(e) {
    const { name } = e.detail
    this.setData({
      tabActive: name
    })
  },
  // 申请业务
  applyBusiness() {
    if (this.data.isLoading) {
      return
    }
    const subCompData = this.selectComponent('#apply-business').data
    let { name } = subCompData.carObj
    // console.log('name: ', name)
    if (!name) {
      wx.pro.showToast({
        title: '请输入开店名称',
        icon: 'error'
      })
      return
    }
    this.submitBusinessForm(subCompData)
  },
  submitBusinessForm(subCompData) {
    let { carObj, selectProdArr } = subCompData
    let { clueId } = this.data
    let selectProdArrStr = ''
    selectProdArr.forEach((item, index) => { selectProdArrStr += `${item.productName}${selectProdArr.length - 1 == index ? '' : ';'}` })

    let { userInfo } = wx.$storage.get()
    let MerchantInfo = userInfo.subLoginUser
    let params = {
      serviceMerchantId: MerchantInfo.merchantId,
      serviceMerchantName: MerchantInfo.merchantName,
      serviceMerchantPhone: MerchantInfo.phone,
      merchantContact: carObj.contact,
      phone: carObj.phone,
      applyMerchantName: carObj.name,
      carType: carObj.model,
      baseCustomer: carObj.selNum,
      clueId,
      intentProducts: selectProdArrStr
    }
    this.setData({
      isLoading: true
    })
    // return wx.$api.showToast('申请成功')
    applyApi
      .merchantApply(params)
      .then(result => {
        // console.log(result)
        if (result) {
          wx.$api.showToast('申请成功')
          setTimeout(() => {
            wx.$router.back(page => {
              page && page.getClueInit && page.getClueInit()
            })
          }, 1000)
        }
      })
      .catch(err => {
        this.setData({
          isLoading: false
        })
      })
  },
  // 申请产品
  applyProduct() {
    if (this.data.isLoading) {
      return
    }
    const subCompData = this.selectComponent('#apply-product').data
    let { prodName, selectStoreItem } = subCompData
    if (!prodName) {
      wx.pro.showToast({
        title: '请输入产品名称',
        icon: 'error'
      })
      return
    }
    if (utils.isEmptyObj(selectStoreItem)) {
      wx.pro.showToast({
        title: '请选择合作门店',
        icon: 'error'
      })
      return
    }

    this.submitProductForm(subCompData)
  },
  async submitProductForm(subCompData) {
    let { prodName, selectStoreItem } = subCompData
    let userInfo = wx.$storage.get('userInfo')
    let params = {
      ...selectStoreItem,
      serviceMerchantId: userInfo.merchantId,
      serviceMerchantName: userInfo.merchantName,
      coProduct: prodName
    }

    this.setData({
      isLoading: true
    })
    let { code } = await addCoProductApi(params)
    if (code == 0) {
      wx.$api.showToast('申请成功')
      setTimeout(() => {
        wx.$router.back()
      }, 1000)
    } else {
      this.setData({
        isLoading: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { },
  onRoute(params) {
    // 线索id
    this.setData({
      clueId: params
    })
    // console.log('params: ', params)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { },

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

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

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

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

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