CooperationApply.js 2.57 KB
// components/CooperationApply/CooperationApply.js
import WxValidate from "../../utils/WxValidate";
import util from "../../utils/util"
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];
        wx.hideLoading();
        this.showModal(error);
        return false;
      } else {
        return [params, this.data.tabIndex];
      }
    },

    // 设置手机号
    setPhone(phone) {
      this.setData({
        ['applyForm.contactPhone']: phone,
      });
    },
  },

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