CooperationApply.js 3.95 KB
// components/CooperationApply/CooperationApply.js
import WxValidate from "../../utils/WxValidate";
import util from "../../utils/util"
import {
  componentsMixin
} from "../../mixins/index";
Component({
  behaviors: [componentsMixin],
  options: {
    // 基础库2.2.3开始支持
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    tabData: [{
        name: "增值转化",
        value: 0,
      },
      {
        name: "系统订阅",
        value: 1,
      },
    ],
    tabIndex: 0,
    applyForm: {
      applyType: '1',
      merchantName: "",
      carModel: "",
      contactName: "",
      contactPhone: "",
    },
    foldFlag: false,
    selectApply: 1
  },

  /**
   * 组件的方法列表
   */
  methods: {
    selectTab(e) {
      console.log(e.currentTarget.dataset.index);
      this.setData({
        tabIndex: e.currentTarget.dataset.index,
      });
      this.selectComponent("#sceneBox").resetScene()
    },
    // 合作方式切换
    applyChange(e) {
      console.log(e.detail.value);
      this.setData({
        selectApply: e.detail.value,
      });
      // this.selectComponent("#sceneBox").resetScene()
    },
    // 展开收起
    handleFold() {
      this.setData({
        foldFlag: !this.data.foldFlag
      })
    },
    // 表单验证规则
    initValidate() {
      console.log(123123);
      const rules = {
        applyType: {
          required: true,
        },
        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, this.data.applyForm.applyType];
        let selectScene = this.selectComponent("#sceneBox").getSceneItems();
        if (selectScene && !selectScene[2]) {
          wx.hideLoading();
          wx.showToast({
            title: "请至少选择一个产品",
            icon: "none",
          });
          return false
        }
        if (selectScene) {
          params.businessScenario = selectScene[0].title;
          params.productItems = selectScene[1].join(",");
        }
        let applyType = this.data.tabIndex == 0 ? '增值转化' : '系统订阅';
        let ccoperationType = this.data.selectApply == 1 ? '门店' : '代理商';
        params.merchantName = params.merchantName + '(' + applyType + '-' + ccoperationType + ')'
        return params
      }
    },

    // 设置手机号
    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)
      }
    }
  },
});