form.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // console.log(e)
  132. if (!e.force) return e
  133. // console.log(e)
  134. if (e.label === '关联订单') {
  135. console.log(e.force && e.inputValue !== '')
  136. }
  137. if (e.force && e.inputValue !== '') {
  138. if (e.role.formatter) {
  139. let regStatus = e.role.formatter(e.inputValue)
  140. this.setErrorMsg(regStatus, index)
  141. } else {
  142. this.setErrorMsg('', index)
  143. }
  144. } else {
  145. console.log(e.label)
  146. this.setErrorMsg(e.label + '不能为空', index)
  147. }
  148. return e
  149. })
  150. console.log(arr)
  151. let isValidate = !arr.some(item => item.errorMsg && item.errorMsg !== '')
  152. console.log(isValidate,'--')
  153. fn(isValidate)
  154. },
  155. setErrorMsg(msg, index) {
  156. this.data.formLayoutData.formInfo[index].errorMsg = msg
  157. this.setData({
  158. formLayoutData: this.data.formLayoutData
  159. })
  160. },
  161. async getarealist() {
  162. const res = await api.arealist()
  163. this.setData({
  164. options: util.createMenu(res.data)
  165. })
  166. },
  167. setObjectData(data) {
  168. let that = this,
  169. obj = {}
  170. that.setData({
  171. disabledStatus: false
  172. })
  173. that.compData = that.selectComponent("#upload")
  174. that.compData.toSetFileData().then(e => {
  175. that.setData({
  176. disabledStatus: false
  177. })
  178. data.forEach(e => {
  179. if (e.type === 'upload') {
  180. e.inputValue = that.compData.data.attachmentids
  181. }
  182. obj[e.id] = e.inputValue
  183. })
  184. if (obj.provinceArr) {
  185. let arr = obj.provinceArr.split(",")
  186. obj.province = arr[0]
  187. obj.city = arr[1]
  188. obj.county = arr[2]
  189. }
  190. that.triggerEvent("click", obj);
  191. })
  192. },
  193. formSubmit() {
  194. var that = this
  195. that.validate((valid) => {
  196. console.log(valid)
  197. if (valid) {
  198. that.setObjectData(that.data.formLayoutData.formInfo)
  199. } else {
  200. return false;
  201. }
  202. });
  203. }
  204. }
  205. })