form.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // components/form/form.js
  2. import api from '../../api/api'
  3. import util from '../../../utils/util'
  4. const verify = require('../../../utils/Check');
  5. const app = getApp()
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. options: {
  11. multipleSlots: true // 复数插槽: 是
  12. },
  13. properties: {
  14. hideBtn: {
  15. value: false,
  16. type: Boolean
  17. },
  18. formLayoutData: {
  19. value: {},
  20. type: Object
  21. }
  22. },
  23. /**
  24. * 组件的初始数据
  25. */
  26. data: {
  27. pickerVisible: false,
  28. dateVisible: false,
  29. cascaderVisible: false,
  30. disabledStatus: false,
  31. pickerIndex: 0,
  32. timePickerValue: '',
  33. actPicker: {},
  34. options: []
  35. },
  36. /**
  37. * 组件的方法列表
  38. */
  39. lifetimes: {
  40. created() {},
  41. attached() {
  42. this.getarealist()
  43. getApp().globalData.formData = this
  44. }
  45. },
  46. methods: {
  47. onBlur(detail) {
  48. let data = detail.currentTarget.dataset.item
  49. let index = detail.currentTarget.dataset.index
  50. //添加排除特殊字符
  51. this.data.formLayoutData.formInfo[index].inputValue = detail.detail.value;
  52. /* this.setData({
  53. [`formLayoutData.formInfo[${index}].inputValue`]: verify.queryStr(detail.detail.value)
  54. }) */
  55. if (data.role.formatter) {
  56. let regStatus = data.role.formatter(detail.detail.value)
  57. this.setErrorMsg(regStatus, index)
  58. }
  59. },
  60. onClear(detail) {
  61. let data = detail.currentTarget.dataset.item
  62. let index = detail.currentTarget.dataset.index
  63. this.data.formLayoutData.formInfo[index].inputValue = '';
  64. this.setData({
  65. formLayoutData: this.data.formLayoutData
  66. })
  67. },
  68. onPicker(detail) {
  69. let data = detail.currentTarget.dataset.item
  70. this.setData({
  71. pickerIndex: detail.currentTarget.dataset.index,
  72. pickerVisible: true,
  73. actPicker: {
  74. label: data.label,
  75. data: data.data
  76. }
  77. })
  78. console.log(this.data.formLayoutData.formInfo)
  79. },
  80. onPickerConfirm(detail) {
  81. this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.value[0]
  82. this.data.actPicker.data.forEach(e => {
  83. if (e.value === detail.detail.value[0]) {
  84. this.data.formLayoutData.formInfo.filter(v => {
  85. if (v.id === e.hide) {
  86. v.force = false
  87. v.errorMsg = null,
  88. v.inputValue = 0
  89. }
  90. })
  91. }
  92. })
  93. this.setData({
  94. formLayoutData: this.data.formLayoutData
  95. })
  96. },
  97. showDatePicker(detail) {
  98. let data = detail.currentTarget.dataset.item
  99. this.setData({
  100. pickerIndex: detail.currentTarget.dataset.index,
  101. dateVisible: true,
  102. timePickerValue: data.inputValue
  103. })
  104. },
  105. onTimePickerConfirm(detail) {
  106. this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.value
  107. this.setData({
  108. formLayoutData: this.data.formLayoutData
  109. })
  110. },
  111. showCascader(detail) {
  112. let data = detail.currentTarget.dataset.item
  113. this.setData({
  114. pickerIndex: detail.currentTarget.dataset.index,
  115. cascaderVisible: true,
  116. })
  117. },
  118. // onChange (detail) {
  119. // let data = detail.currentTarget.dataset.item
  120. // let index = detail.currentTarget.dataset.index
  121. // this.data.formLayoutData.formInfo[index].inputValue = detail.detail.value
  122. // if (data.role.formatter) {
  123. // let regStatus = data.role.formatter(detail.detail.value)
  124. // this.setErrorMsg(regStatus,index)
  125. // }
  126. // },
  127. onChange(detail) {
  128. this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.selectedOptions.map(e => {
  129. return e.value
  130. }).join(',')
  131. this.setData({
  132. formLayoutData: this.data.formLayoutData
  133. })
  134. },
  135. validate(fn) {
  136. let arr = this.data.formLayoutData.formInfo.map((e, index) => {
  137. if (!e.force) return e
  138. if (e.label === '关联订单') console.log(e.force && e.inputValue !== '')
  139. if (e.force && e.inputValue !== '' && e.inputValue != 'undefined') {
  140. if (e.role.formatter) {
  141. let regStatus = e.role.formatter(e.inputValue)
  142. this.setErrorMsg(regStatus, index)
  143. } else {
  144. this.setErrorMsg('', index)
  145. }
  146. } else {
  147. this.setErrorMsg(e.label + '不能为空', index)
  148. }
  149. return e
  150. })
  151. let isValidate = !arr.some(item => (item.errorMsg && item.errorMsg !== '') && item.label !== '关联订单')
  152. fn(isValidate)
  153. },
  154. setErrorMsg(msg, index) {
  155. this.data.formLayoutData.formInfo[index].errorMsg = msg
  156. this.setData({
  157. formLayoutData: this.data.formLayoutData
  158. })
  159. },
  160. async getarealist() {
  161. const res = await api.arealist()
  162. this.setData({
  163. options: util.createMenu(res.data)
  164. })
  165. },
  166. setObjectData(data) {
  167. console.log(data)
  168. let that = this,
  169. obj = {}
  170. that.setData({
  171. disabledStatus: false
  172. })
  173. data.forEach(e => {
  174. if (e.type === 'upload') {
  175. obj[e.id] = that.selectComponent("#upload").handleBind();
  176. } else if (e.id == "provinceArr") {
  177. let arr = e.inputValue.split(",");
  178. obj.province = arr[0] || ""
  179. obj.city = arr[1] || ""
  180. obj.county = arr[2] || ""
  181. } else {
  182. obj[e.id] = e.inputValue
  183. }
  184. })
  185. that.triggerEvent("click", obj);
  186. },
  187. formSubmit() {
  188. var that = this
  189. that.validate((valid) => {
  190. console.log(valid)
  191. if (valid) {
  192. that.setObjectData(that.data.formLayoutData.formInfo)
  193. } else {
  194. return false;
  195. }
  196. });
  197. }
  198. }
  199. })