form.js 6.4 KB

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