methods.js 5.82 KB
import {
  getSceneMarketMaterialMenu,
  saveSceneMarketLead,
  findSceneMarketLeadsById,
  getSceneMarketConfig
} from '../request'
import dayjs from 'dayjs'
export default {
  async getSceneMarketConfig(callBack) {
    const {
      sceneForm,
      fixedForm
    } = await getSceneMarketConfig()
    this.setData({
      sceneForm,
      fixedForm
    })
    callBack()
  },
  changeCarBrand(e) {
    const {
      id,
      name
    } = e
    this.setData({
      ['formData.storeBrandId']: id,
      ['formData.storeBrand']: name
    })
  },
  async getSceneMarketMaterialMenu() {
    try {
      const {
        code,
        message,
        data
      } = await getSceneMarketMaterialMenu()
      if (code == 0) {
        let optionList = [
            [],
            []
          ],
          secondOption = []
        for (let key in data) {
          optionList[0].push(data[key])
          secondOption.push(data[key].sceneMarketLevelTwoList)
        }
        optionList[1] = secondOption[0]
        this.setData({
          [`fixedForm[2].optionList`]: optionList,
          secondOption
        })
      } else throw message
    } catch (error) {
      wx.$util.showToast(error)
    }
  },
  bindMultiPickerColumnChange({
    detail: e
  }) {
    let {
      column,
      value
    } = e.detail, {
      secondOption
    } = this.data
    if (column == 0) {
      this.setData({
        [`fixedForm[2].optionList[1]`]: secondOption[value]
      })
    }
  },
  bindMultiPickerChange({
    detail: e
  }) {
    const {
      value
    } = e.detail, {
        fixedForm
      } = this.data,
      [first, second] = value
    let businessCodeName = `${fixedForm[2].optionList[0][first].directoryName}-${fixedForm[2].optionList[1][second].directoryName}`,
      businessCode = fixedForm[2].optionList[1][second].code
    this.setData({
      ['formData.businessCodeName']: businessCodeName,
      ['formData.businessCode']: businessCode,
      nextBtn: true,
      dynamicFormData: {},
      dynamicForm: []
    })
  },
  bindKeyInput({
    detail: e
  }) {
    const {
      key,
      formName
    } = e.currentTarget.dataset
    this.setData({
      [`${formName}.${key}`]: e.detail.value
    })
  },
  bindPickerChange({
    detail: e
  }) {
    const {
      key,
      option,
      formName
    } = e.currentTarget.dataset
    this.setData({
      [`${formName}.${key}`]: option[e.detail.value]
    })
  },
  next() {
    const {
      formData,
      sceneForm
    } = this.data
    if (formData.storeName && formData.storeBrandId && formData.businessCode) {
      if (sceneForm[formData.businessCode]) this.setData({
        dynamicForm: sceneForm[formData.businessCode],
        nextBtn: false
      })
      else wx.$util.showToast('当前场景尚未配置对应表单')
    }
  },
  submit() {
    if(this.submitStatus) return
    try {
      wx.$util.showLoading('提交中...')
      this.submitStatus = true
      const {
        formData,
        dynamicForm,
        dynamicFormData,
        fixedForm
      } = this.data
      let requiredList = []
      fixedForm.map(e => {
        if (!formData[e.key]) throw e.placeholder
      })
      dynamicForm.map(e => {
        e.form.map(i => {
          if (i.required) requiredList.push({
            placeholder: i.placeholder,
            key: i.key
          })
        })
      })
      requiredList.map(e => {
        if (!dynamicFormData[e.key]) throw e.placeholder
      })
      this.saveSceneMarketLead()
    } catch (error) {
      wx.hideLoading()
      wx.$util.showToast(error)
      this.submitStatus = false
    }
  },
  async saveSceneMarketLead() {
    try {
      let {
        formData,
        dynamicForm,
        dynamicFormData
      } = this.data,
        extendedAttributes = {}

      dynamicForm.map(e => {
        e.form.map(i => {
          if (dynamicFormData[i.key]) extendedAttributes[i.key] = dynamicFormData[i.key]
        })
      })

      const params = {
        storeName: formData.storeName,
        storeBrandId: formData.storeBrandId,
        storeBrand: formData.storeBrand,
        businessCode: formData.businessCode,
        extendedAttributes: JSON.stringify(extendedAttributes)
      }
      const {
        code,
        message,
        data
      } = await saveSceneMarketLead(params)
      if (code == 0) {
        if (data) {
          this.submitStatus = false
          wx.hideLoading()
          wx.showToast({
            title: '数据保存成功',
            mask: true
          })
          setTimeout(() => {
            wx.$router.back(page => {
              page.search()
            })
          }, 1e3)
        } else throw message
      } else throw message

    } catch (error) {
      this.submitStatus = false
      wx.hideLoading()
      console.log(error)
      wx.$util.showToast(error)
    }
  },
  async findSceneMarketLeadsById(id) {
    try {
      const {
        code,
        message,
        data
      } = await findSceneMarketLeadsById({
        id
      })
      if (code == 0) {
        let {
          sceneForm,
          fixedForm
        } = this.data,
          dynamicForm = sceneForm[data.businessCode],
          dynamicFormData = JSON.parse(data.extendedAttributes)
        data.time = dayjs(data.createdDate).format('YYYY年MM月DD日')
        let lastIndex = fixedForm.length - 1,
          lastItem = JSON.parse(JSON.stringify(fixedForm.at(-1)))
        fixedForm.splice(lastIndex, 1)
        fixedForm = [
          ...fixedForm,
          {
            label: '员工姓名',
            key: 'createdBy',
          },
          {
            label: '提报日期',
            key: 'time',
          },
          lastItem
        ]
        this.setData({
          formData: data,
          dynamicForm,
          dynamicFormData,
          readonly: true,
          fixedForm
        })
      } else throw message
    } catch (error) {
      wx.$util.showToast(error)
    }
  },
}