index.js 3.87 KB
/*
 * @Description: 表单框架
 * @Author: huyue
 * @Date: 2023-04-28 16:19:53
 * @LastEditors: huyue
 * @LastEditTime: 2023-08-10 16:54:00
 */
import WxValidate from '../WxValidate';

const utils = require('../utils');

Component({
  externalClasses: [
    'switch-active-class',
    'switch-class',
    'switch-item-class',
    'lable-class',
  ],
  options: {
    multipleSlots: true, // 复数插槽: 是
  },
  data: {
    formDataObj: {},
    formDataList: [],
  },
  properties: {
    /**
     * formObj:表单数据绑定的对象
     */
    formObj: {
      type: Object,
      value: {},
      observer: function (n) {
        this.setData({
          formDataObj: n,
        });
      },
    },
    /**
     * formData:表单配置数据
     */
    formData: {
      type: Array,
      value: [],
      observer: function (n) {
        console.log(n);
        this.initValidate(n);
        this.setData({
          formDataList: n,
        });
      },
    },
    /**
     * inline:表单配置是否一行展示
     */
    inline: {
      type: Boolean,
      value: true,
    },
    /**
     * themeStyle: 主题样式
     */
    themeStyle: {
      type: String,
      value: '#3582EE'
    },
    textPostion: {
      type: String,
      value: 'right'
    }
  },
  methods: {
    // 初始化选项值
    getOptionsList(optionList, refreshSigle) {
      console.log(optionList, refreshSigle);
      if (refreshSigle) {
        // 深拷贝传值
        const cloneList = utils.deepClone(optionList[refreshSigle]);
        const index = this.data.formDataList.findIndex(
          (k) => k.name === refreshSigle
        );

        this.setData({
          [`formDataList[${index}].valueList`]: cloneList,
        });
      } else {
        this.data.formDataList.forEach((item, index) => {
          if (optionList[item.name] && optionList[item.name].length) {
            // 深拷贝传值
            const cloneList = utils.deepClone(optionList[item.name]);
            this.setData({
              [`formDataList[${index}].valueList`]: cloneList,
            });
          }
        });
        console.log(this.data.formDataList, '===');
      }
    },
    // 表单验证规则
    initValidate(obj) {
      const rules = {};
      // const messages = {}
      obj.forEach((item) => {
        if (item.rules) {
          rules[item.name] = item.rules;
          // messages[item.name] = {
          //   required: item.rulesMessage || '请输入' + item.lable
          // }
        }
      });
      console.log(rules);
      // console.log(messages);
      this.WxValidate = new WxValidate(rules);
    },
    // 获取GlsxFormItem的值
    getFormItemValue(e) {
      // const value = this.selectComponent('#GlsxFormItem').formDataObj
      console.log(e);
      const {name, value, type} = e.detail;
      if (type === 'muiltiImg') {
        this.data.formDataObj[name].splice(value, 1)
        this.setData({
          [`formDataObj.${name}`]: this.data.formDataObj[name],
        })
        return
      }
      this.setData({
        [`formDataObj.${name}`]: value,
      })
      // 加change事件,告知表单项变化
      this.triggerEvent('changeFormItem', e.detail)
    },
    // 验证表单数据
    // eslint-disable-next-line consistent-return
    validateFormData() {
      const params = this.data.formDataObj;
      if (!this.WxValidate.checkForm(params)) {
        const error = this.WxValidate.errorList;
        return error;
      }
    },
    // 返回formObj的值
    returnFormObj() {
      return this.data.formDataObj;
    },
    // select事件
    customClick(e) {
      const clickName = e.detail + 'Click';
      console.log(clickName);
      this.triggerEvent(clickName, e.detail);
    }
  },
  // 组件页面生周期
  lifetimes: {
    created: function () {
      // 在组件实例刚刚被创建时执行
      // 初始化验证规则函数
      this.initValidate();
    },
  },
});