form.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 = verify.queryStr(detail.detail.value);
  52. this.setData({
  53. formLayoutData: this.data.formLayoutData
  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 index = detail.currentTarget.dataset.index
  62. this.data.formLayoutData.formInfo[index].inputValue = detail.detail.value
  63. this.setData({
  64. formLayoutData: this.data.formLayoutData
  65. })
  66. },
  67. onPicker(detail) {
  68. let data = detail.currentTarget.dataset.item
  69. this.setData({
  70. pickerIndex: detail.currentTarget.dataset.index,
  71. pickerVisible: true,
  72. actPicker: {
  73. label: data.label,
  74. data: data.data
  75. }
  76. })
  77. console.log(this.data.formLayoutData.formInfo)
  78. },
  79. onPickerConfirm(detail) {
  80. this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.value[0]
  81. this.data.actPicker.data.forEach(e => {
  82. if (e.value === detail.detail.value[0]) {
  83. this.data.formLayoutData.formInfo.filter(v => {
  84. if (v.id === e.hide) {
  85. v.force = false
  86. v.errorMsg = null,
  87. v.inputValue = 0
  88. }
  89. })
  90. }
  91. })
  92. this.setData({
  93. formLayoutData: this.data.formLayoutData
  94. })
  95. },
  96. showDatePicker(detail) {
  97. let data = detail.currentTarget.dataset.item
  98. this.setData({
  99. pickerIndex: detail.currentTarget.dataset.index,
  100. dateVisible: true,
  101. timePickerValue: data.inputValue
  102. })
  103. },
  104. onTimePickerConfirm(detail) {
  105. this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.value
  106. this.setData({
  107. formLayoutData: this.data.formLayoutData
  108. })
  109. },
  110. showCascader(detail) {
  111. let data = detail.currentTarget.dataset.item
  112. this.setData({
  113. pickerIndex: detail.currentTarget.dataset.index,
  114. cascaderVisible: true,
  115. })
  116. },
  117. // onChange (detail) {
  118. // let data = detail.currentTarget.dataset.item
  119. // let index = detail.currentTarget.dataset.index
  120. // this.data.formLayoutData.formInfo[index].inputValue = detail.detail.value
  121. // if (data.role.formatter) {
  122. // let regStatus = data.role.formatter(detail.detail.value)
  123. // this.setErrorMsg(regStatus,index)
  124. // }
  125. // },
  126. onChange(detail) {
  127. this.data.formLayoutData.formInfo[this.data.pickerIndex].inputValue = detail.detail.selectedOptions.map(e => {
  128. return e.value
  129. }).join(',')
  130. this.setData({
  131. formLayoutData: this.data.formLayoutData
  132. })
  133. },
  134. validate(fn) {
  135. let arr = this.data.formLayoutData.formInfo.map((e, index) => {
  136. if (!e.force) return e
  137. if (e.label === '关联订单') console.log(e.force && e.inputValue !== '')
  138. if (e.force && e.inputValue !== '') {
  139. if (e.role.formatter) {
  140. let regStatus = e.role.formatter(e.inputValue)
  141. this.setErrorMsg(regStatus, index)
  142. } else {
  143. this.setErrorMsg('', index)
  144. }
  145. } else {
  146. this.setErrorMsg(e.label + '不能为空', index)
  147. }
  148. return e
  149. })
  150. let isValidate = !arr.some(item => (item.errorMsg && item.errorMsg !== '') && item.label !== '关联订单')
  151. fn(isValidate)
  152. },
  153. setErrorMsg(msg, index) {
  154. this.data.formLayoutData.formInfo[index].errorMsg = msg
  155. this.setData({
  156. formLayoutData: this.data.formLayoutData
  157. })
  158. },
  159. async getarealist() {
  160. const res = await api.arealist()
  161. this.setData({
  162. options: util.createMenu(res.data)
  163. })
  164. },
  165. setObjectData(data) {
  166. console.log(data)
  167. let that = this,
  168. obj = {}
  169. that.setData({
  170. disabledStatus: false
  171. })
  172. data.forEach(e => {
  173. if (e.type === 'upload') {
  174. obj[e.id] = that.selectComponent("#upload").handleBind();
  175. } else if (e.id == "provinceArr") {
  176. let arr = e.inputValue.split(",");
  177. obj.province = arr[0] || ""
  178. obj.city = arr[1] || ""
  179. obj.county = arr[2] || ""
  180. } else {
  181. obj[e.id] = e.inputValue
  182. }
  183. })
  184. that.triggerEvent("click", obj);
  185. },
  186. formSubmit() {
  187. var that = this
  188. that.validate((valid) => {
  189. console.log(valid)
  190. if (valid) {
  191. that.setObjectData(that.data.formLayoutData.formInfo)
  192. } else {
  193. return false;
  194. }
  195. });
  196. }
  197. }
  198. })