FirstStep.js 3.99 KB
// packageEquity/pages/carEquity/components/FirstStep/FirstStep.js
import constantData from '../constant/firstStepData'
import fn from '../constant/ruleFn'
const defaultLabelWidth = 'var(--fontSize140)'
import api from '~/api/api'
// import carLifeApi from '../../../../carEquityApi'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    serviceInfo: {
      type: Object,
      value: () => { },
      observer: function (n) {
        this.setData({
          formData: n
        })
        this.dealFormRule()
      }
    },
    // 页面存放表单对象的对象名,用于直接变更page的值
    dataAttrName: {
      type: String,
      value: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    defaultLabelWidth,
    // formData: {},
    employeeList: [],
    secondObj: constantData.secondObj,
    formRule: constantData.formRule
  },

  // lifetimes: {
  //   attached() {
  //     this.getEmployee()
  //   }
  // },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取推荐人列表
    // getEmployee() {
    //   let userInfo = wx.$util.getStorageSync('dj_boutique_user_info')
    //   let params = {
    //     merchantId: userInfo.merchantId
    //   }
    //   carLifeApi.getEmployeeList(params).then(res => {
    //     if (res.code == 0) {
    //       this.setData({
    //         employeeList: res.data
    //       })
    //     } else {
    //       wx.$util.showToast(res.message || '接口异常')
    //     }
    //   })
    // },

    dealFormRule() {
      var { formData, formRule } = this.data
      // const list = ['tireBrand', 'frontTireSpecification', 'rearTireSpecification']
      const list = ['tireBrand']
      if(formData && formData.tireFlag == 0) {
        list.forEach(item => {
          formRule[item][0].required = false
        })
      }
      this.setData({
        formRule
      })
    },

    // 删除图片
    handleDelete(e) {
      let { attr } = e.currentTarget.dataset
      let { dataAttrName } = this.data
      let thisPage = wx.$router.getPageInstance()
      thisPage.setData({
        [dataAttrName + '.' + attr]: ''
      })
    },

    // 放大图片
    handlePreviewImg(e) {
      let { attr } = e.currentTarget.dataset
      let { dataAttrName } = this.data
      let thisPage = wx.$router.getPageInstance()
      wx.previewImage({
        urls: [thisPage.data[dataAttrName][attr]],
      })
    },

    handleUpload(e) {
      let { attr } = e.currentTarget.dataset
      let { dataAttrName } = this.data
      let thisPage = wx.$router.getPageInstance()
      wx.chooseImage({
        count: 1,
        success(res) {
          let [tempFiles] = res.tempFilePaths
          let tempFilesList = tempFiles.split('.')
          let imgType = tempFilesList[tempFilesList.length - 1]
          let type = imgType == 'unknown' ? 'jpg' : imgType
          wx.getFileSystemManager().readFile({
            filePath: tempFiles, //选择图片返回的相对路径
            encoding: 'base64', //编码格式
            success(_res) {
              wx.$util.showLoading('上传中...')
              api
                .uploadImg({
                  file: `${type}@${_res.data}`
                })
                .then(img => {
                  wx.$util.hideLoading()
                  let { code, data, message } = img
                  if (code == 0) {
                    thisPage.setData({
                      [dataAttrName + '.' + attr]: data
                    })
                  } else {
                    wx.$util.showToast(message)
                  }
                })
            }
          })
        }
      })
    },

    handleDraft() {
      this.triggerEvent('draft', { type: 'draft' })
    },

    handleConfirm() {
      var { formData, formRule } = this.data
      let validObj = fn.checkRequired(formData, formRule)
      if (validObj) {
        wx.$util.showToast(validObj.message)
        // wx.pageScrollTo({
        //   selector: `.${key}`
        // })
        return
      }
      this.triggerEvent('confirm')
    }
  }
})