123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- // components/form/form.js
- import api from '../../api/api'
- import util from '../../../utils/util'
- const app = getApp()
- Component({
- /**
- * 组件的属性列表
- */
- options: {
- multipleSlots: true // 复数插槽: 是
- },
- properties: {
- formLayoutData:{
- value:{},
- type:Object
- }
- },
- /**
- * 组件的初始数据
- */
- data: {
- pickerVisible:false,
- dateVisible:false,
- cascaderVisible:false,
- disabledStatus:false,
- pickerIndex:0,
- timePickerValue:'',
- actPicker:{},
- options:[]
- },
-
- /**
- * 组件的方法列表
- */
- lifetimes:{
- created(){
- },
- attached () {
- this.getarealist()
- getApp().globalData.formData = this
- }
- },
- methods: {
-
- onBlur (detail) {
- let data = detail.currentTarget.dataset.item
- let index = detail.currentTarget.dataset.index
- this.data.formLayoutData.formInfo[index].inputValue = detail.detail.value
- if (data.role.formatter) {
- let regStatus = data.role.formatter(detail.detail.value)
- this.setErrorMsg(regStatus,index)
- }
- },
- onClear (detail) {
- let index = detail.currentTarget.dataset.index
- this.data.formLayoutData.formInfo[index].inputValue = detail.detail.value
- this.setData({
- formLayoutData:this.data.formLayoutData
- })
- },
- onPicker (detail) {
- let data = detail.currentTarget.dataset.item
- this.setData({
- pickerIndex:detail.currentTarget.dataset.index,
- pickerVisible:true,
- actPicker:{
- label:data.label,
- data:data.data
- }
- })
- console.log(this.data.formLayoutData.formInfo)
- },
- onPickerConfirm (detail) {
- this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.value[0]
- this.data.actPicker.data.forEach(e=>{
- if (e.value === detail.detail.value[0]) {
- this.data.formLayoutData.formInfo.filter(v=>{
- if (v.id === e.hide) {
- v.force = false
- v.errorMsg = null,
- v.inputValue = 0
- }
- })
- }
- })
- this.setData({
- formLayoutData:this.data.formLayoutData
- })
- },
- showDatePicker (detail) {
- let data = detail.currentTarget.dataset.item
- this.setData({
- pickerIndex:detail.currentTarget.dataset.index,
- dateVisible:true,
- timePickerValue:data.inputValue
- })
- },
- onTimePickerConfirm (detail) {
- this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.value
- this.setData({
- formLayoutData:this.data.formLayoutData
- })
- },
- showCascader (detail) {
- let data = detail.currentTarget.dataset.item
- this.setData({
- pickerIndex:detail.currentTarget.dataset.index,
- cascaderVisible:true,
- })
- },
- // onChange (detail) {
- // let data = detail.currentTarget.dataset.item
- // let index = detail.currentTarget.dataset.index
- // this.data.formLayoutData.formInfo[index].inputValue = detail.detail.value
- // if (data.role.formatter) {
- // let regStatus = data.role.formatter(detail.detail.value)
- // this.setErrorMsg(regStatus,index)
- // }
- // },
- onChange (detail) {
- this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.selectedOptions.map(e=>{
- return e.value
- }).join(',')
- this.setData({
- formLayoutData:this.data.formLayoutData
- })
- },
- validate (fn) {
- let arr = this.data.formLayoutData.formInfo.map((e,index)=>{
- // console.log(e)
- if (!e.force) return e
- console.log(e)
- if (e.label === '关联订单') {
- console.log(e.force && e.inputValue !== '')
- }
- if (e.force && e.inputValue !== '') {
-
- if (e.role.formatter) {
- let regStatus = e.role.formatter(e.inputValue)
-
- this.setErrorMsg(regStatus,index)
-
- } else {
- this.setErrorMsg('',index)
- }
- } else {
- console.log(e.label)
- this.setErrorMsg(e.label + '不能为空',index)
- }
- return e
- })
- let isValidate = !arr.some(item=>item.errorMsg && item.errorMsg !== '')
- fn(isValidate)
-
- },
- setErrorMsg (msg,index) {
- this.data.formLayoutData.formInfo[index].errorMsg = msg
- this.setData({
- formLayoutData:this.data.formLayoutData
- })
- },
- async getarealist () {
- const res = await api.arealist()
-
- this.setData({
- options:util.createMenu(res.data)
- })
- },
-
- setObjectData (data) {
- let that = this, obj = {}
- that.setData({
- disabledStatus:false
- })
-
- that.compData = that.selectComponent("#upload")
- that.compData.toSetFileData().then(e=>{
- that.setData({
- disabledStatus:false
- })
- data.forEach(e=>{
- if (e.type === 'upload') {
- e.inputValue = that.compData.data.attachmentids
- }
-
- obj[e.id] = e.inputValue
-
- })
-
- if (obj.provinceArr) {
- let arr = obj.provinceArr.split(",")
- obj.province = arr[0]
- obj.city = arr[1]
- obj.county = arr[2]
- }
- that.triggerEvent("click", obj);
- })
- },
- formSubmit () {
- var that = this
- console.log(this.data.formLayoutData.formInfo)
- that.validate((valid) => {
- console.log(valid)
- if (valid) {
- that.setObjectData(that.data.formLayoutData.formInfo)
- } else {
- return false;
- }
- });
- }
- }
- })
|