FirstStep.js 10.8 KB
// packageEquity/pages/carEquity/components/FirstStep/FirstStep.js
import data from '../constant/stepData'
import fn from '../constant/ruleFn'
import carLifeApi from '~/packageEquity/carEquityApi'
import commonApi from '~/api/api'
const defaultLabelWidth = 'var(--fontSize140)'
import Util from '~/utils/util'
import { validateVin, formatVinInput } from '~/utils/vinVaildate'
// import api from '~/api/api'
Component({
  options: {
    styleIsolation: 'apply-shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    serviceInfo: {
      type: Object,
      observer: function (n) {
        this.setData({
          formData: n
        })
      }
    },
    orderInfo: {
      type: Object,
      observer: function (n) {
        if (n?.modifyEquityQuotation) {
          this.setData({
            // activeNames: ['3'],
            disable: true
          })
        } else {
          this.setData({
            disable: false
          })
        }
      }
    },
    // 页面存放表单对象的对象名
    dataAttrName: {
      type: String,
      value: null
    },
    subBtnShow: {
      type: Boolean,
      value: true
    },
    mode: {
      type: Number,
      value: 1
    }
  },
  observers: {
    'mode': function (value, old) {
      console.log(value, old, 'mode');
      if (value != 1) {
        this.setData({
          first: data.firstObj,
          formRule: data.formRule
        })
      }
    }

    // 'orderInfo.outTradeNo': function (value) {
    //   console.log(value, 'outTradeNo')
    //   value && this.setData({
    //     textNameObj: {
    //       confirmName: '下一步(1/4)'
    //       // cancelName: '存草稿'
    //     }
    //   })
    // }
  },
  /**
   * 组件的初始数据
   */
  data: {
    defaultLabelWidth,
    formData: {},
    employeeList: [],
    disable: true,
    first: '',
    formRule: '',
    // first: data.firstObjSimple,
    // formRule: data.formRuleSimple,
    referrerPicker: false,
    deliveryPicker:false,
    textNameObj: {
      confirmName: '下一步(1/4)',
      cancelName: '存草稿'
    }
  },

  lifetimes: {
    attached() {
      this.getEmployee()
      this.getShopMerchantPointConfig()
      // 没有才走获取档案接口逻辑修改配置,有是必填优先及更高
      // 在createOrderCarQuotationSimple 页面做逻辑判断
      // this.getYqStoreConfig()

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getShopMerchantPointConfig(){
      console.log(1111111111)
      let {  userInfo } = this.data;
      let oParams = {};
      oParams.merchantId = userInfo.shopMerchantId;
      commonApi.getShopMerchantPointConfig(oParams).then(res=>{
        // if(res.data.pointsMultiRole=='Y'){
        //   toastList.userId='销售顾问/录单员不能为空'
        // }
        this.setData({
          pointsMultiRole:res.data.pointsMultiRole
        })
      })
    },
    // 推荐人
    // handleSalesChange(e) {
    //   let idx = e.detail.value || 0
    //   this.setData({
    //     'formData.referrerId': this.data.employeeList[idx].uuid,
    //     'formData.referrer': this.data.employeeList[idx].staffName
    //   })
    // },
    inputEvent(e) {
      console.log(e, 'inputEvent');
      let {
        attr
      } = e.detail
      
      // 车架号输入处理:实时转大写 + 实时校验
      if (attr == 'carVin') {
        const thisPage = wx.$router.getPageInstance()
        const { dataAttrName } = this.data
        // 从页面数据中获取刚刚更新的车架号值
        const value = thisPage.data[dataAttrName]?.carVin || ''
        const upperVin = formatVinInput(value)
        // 更新为大写
        thisPage.setData({
          [dataAttrName + '.carVin']: upperVin
        })
        // 当输入满17位时,进行实时校验(防抖500ms)
        if (upperVin.length === 17) {
            validateVin(upperVin).then(res => {
              let { valid } = res.data.data;
              console.log("upperVinRes",valid);
              // 保存 valid 值到组件 data 中
              this.setData({
                vinValid: valid
              });
            }).catch(() => {
              console.log("upperVinErr",upperVin);
            })
        }
      }
      
      // 是车价就重新筛选商品列表
      if (attr == 'price') {
        let thisPage = wx.$router.getPageInstance()
        thisPage.resetGoods && thisPage.resetGoods()
      }
    },
    handleReferrerPicker() {
      this.setData({
        referrerPicker: true
      })
    },
    handleDeliveryPicker() {
      this.setData({
        deliveryPicker: true
      })
    },
    
    handleSalesChange(e) {
      // let value = e.detail
      console.log(e)
      let idx = e.detail.value || 0
      let {
        employeeList,
        disable
      } = this.data
      if (disable) return false
      let thisPage = wx.$router.getPageInstance()

      thisPage.setData({
        ['orderInfo.referrerId']: employeeList[idx].uuid,
        ['orderInfo.referrer']: employeeList[idx].staffName
      })
    },
    handleDeliveryChange(e) {
      // let value = e.detail
      let idx = e.detail.value || 0
      let {
        employeeList,
        disable
      } = this.data
      if (disable) return false
      let thisPage = wx.$router.getPageInstance()

      thisPage.setData({
        ['orderInfo.deliveryId']: employeeList[idx].uuid,
        ['orderInfo.deliveryName']: employeeList[idx].staffName
      })
    },
    // 获取推荐人列表
    getEmployee() {
      let userInfo = wx.$util.getStorageSync('dj_boutique_user_info')
      this.setData({
        userInfo
      })
      let params = {
        merchantId: userInfo.default4SStoreInfo.merchantId
      }
      carLifeApi.getEmployeeList(params).then(res => {
        if (res.code == 0) {
          this.setData({
            employeeList: res.data
          })
        } else {
          wx.$util.showToast(res.message || '接口异常')
        }
      })
    },
    navOcr(e) {
      // idCard:1,
      // vehicleCertificate:5
      let {
        type
      } = e.currentTarget.dataset
      console.log(type)
      // wx.$router.go('ocrEquityModules', { type })
      wx.$router.go(`ocrEquityModules?type=${type}`)
    },

    btnTap(e) {
      // console.log(e)
      let {
        name
      } = e.detail
      if (name == 'confirm') {
        // this.triggerEvent('confirm')
        // 需要校验
        this.handleConfirm()
      } else {
        this.triggerEvent('editSave')
      }
    },
    handleConfirm() {
      var {
        dataAttrName,
        formRule,
        pointsMultiRole,
        vinValid
      } = this.data

      // let thisPage = this.$router.getCurrentPage()
      let thisPage = wx.$router.getPageInstance()
      // console.log(thisPage.data['orderInfo']['actualSalesPersonId'])
      if (!thisPage.data['orderInfo']['referrerId']) {
        wx.$util.showToast(pointsMultiRole=='Y'?'请选择销售顾问/录单员':'请选择推荐人')
        return false
      }
      if(pointsMultiRole=='Y'){
        if (!thisPage.data['orderInfo']['deliveryId']) {
          wx.$util.showToast('请选择交付专员')
          return false
        }
        if(thisPage.data['orderInfo']['deliveryId']==thisPage.data['orderInfo']['referrerId']){
          wx.$util.showToast('销售顾问/录单员与交付专员不能选取同一人')
          return false
        }
      }
      // 车架号校验
      const carVin = thisPage.data[dataAttrName]?.carVin
      if (carVin) {
        const isCarVinRequired = formRule?.carVin?.some(rule => rule.required === true) // 再次判断一下车架号是否必填项
        // 前置判断
        if (carVin.length !== 17 && isCarVinRequired) {
          wx.$util.showToast('请输入 17 位有效字符(支持大写 A-H/J-N/P-Z、0-9)')
          return false
        }
        // 根据接口校验结果判断
        if (carVin.length === 17 && !vinValid) {
          wx.$util.showToast('车架号校验不匹配,请核对后重新输入')
          return false
        }
      }
      let validObj = fn.checkRequired(thisPage.data[dataAttrName], formRule)
      if (validObj) {
        wx.$util.showToast(validObj.message)
        return false
      }
      
      // debugger
      this.triggerEvent('confirm')
      return true
    },
    showCustomPop() {
      this.setData({
        customerInfoBox: true
      })
    },
    chooseCustomerInfo(e) {
      console.log(e)
      let {
        customer
      } = e.detail
      const thisPage = wx.$router.getPageInstance()
      thisPage.setCustomerInfo(customer)
    },
    changeGender(e) {
      let {
        gender
      } = e.currentTarget.dataset
      wx.vibrateShort()
      const thisPage = wx.$router.getPageInstance()
      thisPage.setData({
        ['owner.ownerGender']: gender
      })
    },
    bindRegionChange(e) {
      const thisPage = wx.$router.getPageInstance(),
        {
          values
        } = e.detail.detail
      let city = Array.from(new Set([...values.map(e => e.name)]))
      thisPage.setData({
        ['owner.city']: city.join('-')
      })
    },
    // 品牌车型
    handleJumpBrand(e) {
      // wx.$router.go('packageEquity.carBrands')
      this.setData({
        showCarBrands: true, // 选择时再加载车型组件
        carBrandBox: true
      })
    },

    chooseCar(e) {
      let { dataAttrName } = this.data
      let { carBrand, carBrandId, carChildsBrandId, carChildsBrandName, carSeriesId, carSeriesName } = e.detail
      let thisPage = wx.$router.getPageInstance()
      thisPage.setData({
        [dataAttrName + '.' + 'carBrand']: carBrand,
        [dataAttrName + '.' + 'carBrandId']: carBrandId,
        [dataAttrName + '.' + 'carChildsBrandId']: carChildsBrandId,
        [dataAttrName + '.' + 'carChildsBrandName']: carChildsBrandName,
        [dataAttrName + '.' + 'carSeriesId']: carSeriesId,
        [dataAttrName + '.' + 'carSeriesName']: carSeriesName
      })
        // 是车价就重新筛选商品列表
        if (carSeriesId) {
          thisPage.resetGoods && thisPage.resetGoods()
        }
    },
    /**获取园区配置 */
    async getYqStoreConfig() {
      const {
        merchantId: yqMerchantId
      } = wx.$util.getUserInfo(), {
        first
      } = this.data, {
        data
      } = await commonApi.getYqStoreConfig({
        yqMerchantId
      })
      let {
        customerFormList,
        carFormList
      } = first
      //是否配置园区店面档案互通 Y / N
      if (data) {
        if (data.customerExchangeFlag == 'N') {
          customerFormList = customerFormList?.filter(item => !item.configItem)
          carFormList = carFormList?.filter(item => !item.configItem)
        }
      }
      this.setData({
        ['first.customerFormList']: customerFormList,
        ['first.carFormList']: carFormList,
        showCustomeBtn: data.customerExchangeFlag == 'Y'
      })

    },
  }
})