CooperationApply.js 2.22 KB
// components/CooperationApply/CooperationApply.js
import WxValidate from "../../utils/WxValidate"
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    tabData: [{
        name: '门店合作申请',
        value: 0
      },
      {
        name: '代理商合作申请',
        value: 1
      }
    ],
    tabIndex: 0,
    applyForm: {
      merchantName: '',
      carModel: '',
      contactName: '',
      contactPhone: ''
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    selectTab(e) {
      console.log(e.currentTarget.dataset.index);
      this.setData({
        tabIndex: e.currentTarget.dataset.index
      })
    },
    // 表单验证规则
    initValidate() {
      console.log(123123);
      const rules = {
        merchantName: {
          required: true
        },
        contactName: {
          required: true
        },
        contactPhone: {
          required: true,
          tel: true
        }
      }
      const messages = {
        merchantName: {
          required: '请填写门店名称',
        },
        contactName: {
          required: '请填写联系人'
        },
        contactPhone: {
          required: '请填写手机号'
        }
      }
      this.WxValidate = new WxValidate(rules, messages)
    },

    //报错
    showModal(error) {
      wx.showToast({
        title: error.msg,
        icon: 'none',
      })
    },

    // 表单输入
    handleInput(e) {
      let name = e.currentTarget.dataset.name
      let val = e.detail.value
      const form = `applyForm.${name}`
      this.setData({
        [form]: val
      })
    },

    // 校验表单
    validateForm() {
      let params = this.data.applyForm
      // 校验表单
      if (!this.WxValidate.checkForm(params)) {
        const error = this.WxValidate.errorList[0]
        this.showModal(error)
        wx.hideLoading()
        return false
      } else {
        return [params, this.data.tabIndex]
      }
    }
  },

  // 组件页面生周期
  lifetimes: {
    created: function () {
      // 在组件实例刚刚被创建时执行
      // 初始化验证规则函数
      console.log('created');
      this.initValidate()
    },
  }
})